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

OP-1130 improve JPA methods - admtype package #339

Merged
merged 1 commit into from
Oct 5, 2023
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
29 changes: 9 additions & 20 deletions src/main/java/org/isf/admtype/rest/AdmissionTypeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,11 @@ public AdmissionTypeController(AdmissionTypeBrowserManager admtManager, Admissio
ResponseEntity<AdmissionTypeDTO> 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<AdmissionType> 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));
}

/**
Expand All @@ -103,11 +97,8 @@ ResponseEntity<AdmissionTypeDTO> 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));
}

/**
Expand Down Expand Up @@ -136,19 +127,17 @@ public ResponseEntity<List<AdmissionTypeDTO>> getAdmissionTypes() throws OHServi
@DeleteMapping(value = "/admissiontypes/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> deleteAdmissionType(@PathVariable("code") String code) throws OHServiceException {
LOGGER.info("Delete Admission Type code: {}", code);
boolean isDeleted = false;
if (admtManager.isCodePresent(code)) {
List<AdmissionType> admts = admtManager.getAdmissionType();
List<AdmissionType> admtFounds = admts.stream().filter(ad -> ad.getCode().equals(code))
List<AdmissionType> admissionTypes = admtManager.getAdmissionType();
List<AdmissionType> 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);
}

}
25 changes: 11 additions & 14 deletions src/test/java/org/isf/admtype/rest/AdmissionTypeControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AdmissionType> 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)
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
Loading