Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OH2-415 | OH2-316 | Add endpoint to update age types #498

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions openapi/oh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1904,15 +1904,19 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/AgeTypeDTO"
type: array
items:
$ref: "#/components/schemas/AgeTypeDTO"
required: true
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/AgeTypeDTO"
type: array
items:
$ref: "#/components/schemas/AgeTypeDTO"
security:
- bearerAuth: []
/admissiontypes:
Expand Down Expand Up @@ -6231,13 +6235,13 @@ components:
description: lock
format: int32
example: 0
male:
type: boolean
female:
opd:
type: boolean
pharmacy:
type: boolean
opd:
male:
type: boolean
female:
type: boolean
PatientDTO:
required:
Expand Down Expand Up @@ -6949,11 +6953,11 @@ components:
type: string
description: "Flag record deleted, values are 'Y' OR 'N' "
example: "N"
fhu:
type: string
yprog:
type: integer
format: int32
fhu:
type: string
description: The admission
AdmissionTypeDTO:
required:
Expand Down
38 changes: 24 additions & 14 deletions src/main/java/org/isf/agetype/rest/AgeTypeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,37 @@ public List<AgeTypeDTO> getAllAgeTypes() throws OHServiceException {

/**
* Update an age type
* @param ageTypeDTO - the age type to be updated
* @param ageTypeDTOs - the list of age types to be updated
* @return {@link AgeTypeDTO} the updated age type
* @throws OHServiceException When failed to update age type
*/
@PutMapping(value = "/agetypes")
public AgeTypeDTO updateAgeType(@Valid @RequestBody AgeTypeDTO ageTypeDTO) throws OHServiceException {
if (ageTypeDTO.getCode() == null || ageTypeDTO.getCode().trim().isEmpty()) {
throw new OHAPIException(new OHExceptionMessage("The age type is not valid."));
}

LOGGER.info("Update age type");

AgeType ageType = mapper.map2Model(ageTypeDTO);
List<AgeType> ageTypes = new ArrayList<>();
ageTypes.add(ageType);
public List<AgeTypeDTO> updateAgeType(@Valid @RequestBody List<AgeTypeDTO> ageTypeDTOs) throws OHServiceException {

ageTypeDTOs.forEach(ageTypeDTO -> {
try {
if (ageTypeDTO.getCode() == null || ageTypeDTO.getCode().trim().isEmpty() || ageTypeManager.getTypeByCode(ageTypeDTO.getCode()) == null) {
try {
throw new OHAPIException(new OHExceptionMessage("The age type with code "+ageTypeDTO.getCode()+" is not valid."));
} catch (OHAPIException e) {
throw new RuntimeException(e);
}
}
} catch (OHServiceException e) {
throw new RuntimeException(e);
}
});

LOGGER.info("Updating age types");
List<AgeType> ageTypes = mapper.map2ModelList(ageTypeDTOs);

try {
ageTypeManager.updateAgeType(ageTypes);
return ageTypeDTO;
return mapper.map2DTOList(ageTypeManager.updateAgeType(ageTypes));
} catch (OHServiceException ex) {
throw new OHAPIException(new OHExceptionMessage("The age type is not updated."), HttpStatus.INTERNAL_SERVER_ERROR);
throw new OHAPIException(
new OHExceptionMessage("Unable to update age types. Please check that you've correctly set values"),
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/test/java/org/isf/agetype/rest/AgeTypeControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.isf.agetype.rest;

import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
Expand Down Expand Up @@ -55,6 +57,8 @@
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.fasterxml.jackson.databind.ObjectMapper;

public class AgeTypeControllerTest {

private static final Logger LOGGER = LoggerFactory.getLogger(AgeTypeControllerTest.class);
Expand All @@ -64,6 +68,8 @@ public class AgeTypeControllerTest {

private final AgeTypeMapper ageTypeMapper = new AgeTypeMapper();

private final ObjectMapper objectMapper = new ObjectMapper();

private MockMvc mockMvc;

private AutoCloseable closeable;
Expand Down Expand Up @@ -110,22 +116,21 @@ public void testGetAllAgeTypes_200() throws Exception {
@Test
public void testUpdateAgeType_200() throws Exception {
String request = "/agetypes";
AgeTypeDTO body = ageTypeMapper.map2DTO(AgeTypeHelper.setup());
List<AgeType> ageTypes = AgeTypeHelper.genList(5);
List<AgeTypeDTO> body = ageTypeMapper.map2DTOList(ageTypes);

List<AgeType> ageTypes = new ArrayList<>();
ageTypes.add(AgeTypeHelper.setup());

when(ageTypeManagerMock.updateAgeType(ageTypes))
.thenReturn(ageTypes);
when(ageTypeManagerMock.getTypeByCode(anyString())).thenReturn(ageTypes.get(0));
when(ageTypeManagerMock.getTypeByCode(anyInt())).thenReturn(ageTypes.get(0));
when(ageTypeManagerMock.updateAgeType(ageTypes)).thenReturn(ageTypes);

MvcResult result = this.mockMvc
.perform(put(request)
.contentType(MediaType.APPLICATION_JSON)
.content(Objects.requireNonNull(AgeTypeHelper.asJsonString(body)))
.content(Objects.requireNonNull(objectMapper.writeValueAsString(body)))
)
.andDo(log())
.andExpect(status().is2xxSuccessful())
.andExpect(status().isOk())
.andExpect(content().string(containsString(objectMapper.writeValueAsString(body))))
.andReturn();

LOGGER.debug("result: {}", result);
Expand Down
Loading