diff --git a/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java b/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java index 922c6b388..5e8345568 100644 --- a/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java +++ b/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java @@ -76,17 +76,11 @@ public AdmissionTypeController(AdmissionTypeBrowserManager admtManager, Admissio ResponseEntity newAdmissionType(@RequestBody AdmissionTypeDTO admissionTypeDTO) throws OHServiceException { String code = admissionTypeDTO.getCode(); LOGGER.info("Create Admission Type {}", code); - boolean isCreated = admtManager.newAdmissionType(mapper.map2Model(admissionTypeDTO)); - AdmissionType admtCreated = null; - List admtFounds = admtManager.getAdmissionType().stream().filter(ad -> ad.getCode().equals(code)) - .collect(Collectors.toList()); - if (!admtFounds.isEmpty()) { - admtCreated = admtFounds.get(0); - } - if (!isCreated || admtCreated == null) { + AdmissionType newAdmissionType = admtManager.newAdmissionType(mapper.map2Model(admissionTypeDTO)); + if (!admtManager.isCodePresent(code)) { throw new OHAPIException(new OHExceptionMessage("Admission Type is not created."), HttpStatus.INTERNAL_SERVER_ERROR); } - return ResponseEntity.status(HttpStatus.CREATED).body(mapper.map2DTO(admtCreated)); + return ResponseEntity.status(HttpStatus.CREATED).body(mapper.map2DTO(newAdmissionType)); } /** @@ -103,11 +97,8 @@ ResponseEntity updateAdmissionTypes(@RequestBody AdmissionType if (!admtManager.isCodePresent(admt.getCode())) { throw new OHAPIException(new OHExceptionMessage("Admission Type not found.")); } - boolean isUpdated = admtManager.updateAdmissionType(admt); - if (!isUpdated) { - throw new OHAPIException(new OHExceptionMessage("Admission Type is not updated."), HttpStatus.INTERNAL_SERVER_ERROR); - } - return ResponseEntity.ok(mapper.map2DTO(admt)); + AdmissionType updatedAdmissionType = admtManager.updateAdmissionType(admt); + return ResponseEntity.ok(mapper.map2DTO(updatedAdmissionType)); } /** @@ -136,19 +127,17 @@ public ResponseEntity> getAdmissionTypes() throws OHServi @DeleteMapping(value = "/admissiontypes/{code}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity deleteAdmissionType(@PathVariable("code") String code) throws OHServiceException { LOGGER.info("Delete Admission Type code: {}", code); - boolean isDeleted = false; if (admtManager.isCodePresent(code)) { - List admts = admtManager.getAdmissionType(); - List admtFounds = admts.stream().filter(ad -> ad.getCode().equals(code)) + List admissionTypes = admtManager.getAdmissionType(); + List admtFounds = admissionTypes.stream().filter(ad -> ad.getCode().equals(code)) .collect(Collectors.toList()); if (!admtFounds.isEmpty()) { - isDeleted = admtManager.deleteAdmissionType(admtFounds.get(0)); + admtManager.deleteAdmissionType(admtFounds.get(0)); } } else { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); } - - return ResponseEntity.ok(isDeleted); + return ResponseEntity.ok(true); } } diff --git a/src/test/java/org/isf/admtype/rest/AdmissionTypeControllerTest.java b/src/test/java/org/isf/admtype/rest/AdmissionTypeControllerTest.java index 70916c363..ecff577df 100644 --- a/src/test/java/org/isf/admtype/rest/AdmissionTypeControllerTest.java +++ b/src/test/java/org/isf/admtype/rest/AdmissionTypeControllerTest.java @@ -91,14 +91,14 @@ public void testNewAdmissionType_201() throws Exception { AdmissionTypeDTO body = AdmissionTypeDTOHelper.setup(admissionTypemapper); boolean isCreated = true; - when(admtManagerMock.newAdmissionType(admissionTypemapper.map2Model(body))) - .thenReturn(isCreated); AdmissionType admissionType = new AdmissionType("ZZ", "aDescription"); - List admtFounds = new ArrayList<>(); - admtFounds.add(admissionType); - when(admtManagerMock.getAdmissionType()) - .thenReturn(admtFounds); + + when(admtManagerMock.newAdmissionType(admissionType)) + .thenReturn(admissionType); + + when(admtManagerMock.isCodePresent(body.getCode())) + .thenReturn(true); MvcResult result = this.mockMvc .perform(post(request) @@ -114,16 +114,16 @@ public void testNewAdmissionType_201() throws Exception { } @Test - public void testUpdateAdmissionTypet_200() throws Exception { + public void testUpdateAdmissionType_200() throws Exception { String request = "/admissiontypes"; AdmissionTypeDTO body = AdmissionTypeDTOHelper.setup(admissionTypemapper); + AdmissionType admissionType = new AdmissionType("ZZ", "aDescription"); - when(admtManagerMock.isCodePresent(body.getCode())) + when(admtManagerMock.isCodePresent(admissionType.getCode())) .thenReturn(true); - boolean isUpdated = true; - when(admtManagerMock.updateAdmissionType(admissionTypemapper.map2Model(body))) - .thenReturn(isUpdated); + when(admtManagerMock.updateAdmissionType(admissionType)) + .thenReturn(admissionType); MvcResult result = this.mockMvc .perform(put(request) @@ -173,9 +173,6 @@ public void testDeleteAdmissionType_200() throws Exception { when(admtManagerMock.getAdmissionType()) .thenReturn(admtFounds); - when(admtManagerMock.deleteAdmissionType(admtFounds.get(0))) - .thenReturn(true); - MvcResult result = this.mockMvc .perform(delete(request, code)) .andDo(log())