diff --git a/openapi/oh.yaml b/openapi/oh.yaml index cac0d7d3..6bd27d76 100644 --- a/openapi/oh.yaml +++ b/openapi/oh.yaml @@ -1904,7 +1904,9 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/AgeTypeDTO" + type: array + items: + $ref: "#/components/schemas/AgeTypeDTO" required: true responses: "200": @@ -1912,7 +1914,9 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/AgeTypeDTO" + type: array + items: + $ref: "#/components/schemas/AgeTypeDTO" security: - bearerAuth: [] /admissiontypes: @@ -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: @@ -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: diff --git a/src/main/java/org/isf/agetype/rest/AgeTypeController.java b/src/main/java/org/isf/agetype/rest/AgeTypeController.java index c5fc70da..33ed4f0d 100644 --- a/src/main/java/org/isf/agetype/rest/AgeTypeController.java +++ b/src/main/java/org/isf/agetype/rest/AgeTypeController.java @@ -82,27 +82,37 @@ public List 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 ageTypes = new ArrayList<>(); - ageTypes.add(ageType); + public List updateAgeType(@Valid @RequestBody List 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 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 + ); } } diff --git a/src/test/java/org/isf/agetype/rest/AgeTypeControllerTest.java b/src/test/java/org/isf/agetype/rest/AgeTypeControllerTest.java index d2c42893..46478d13 100644 --- a/src/test/java/org/isf/agetype/rest/AgeTypeControllerTest.java +++ b/src/test/java/org/isf/agetype/rest/AgeTypeControllerTest.java @@ -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; @@ -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); @@ -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; @@ -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 ageTypes = AgeTypeHelper.genList(5); + List body = ageTypeMapper.map2DTOList(ageTypes); - List 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);