From da608df6d7bc89158014810be4b2cb32673d327b Mon Sep 17 00:00:00 2001 From: Szymon Radziszewski Date: Thu, 18 Apr 2024 15:53:20 +0200 Subject: [PATCH] OAM-46: Added PUT /api/wards/saveAll endpoint --- .../web/WardControllerIntegrationTest.java | 47 ++++++++--- .../referencedata/web/WardController.java | 82 +++++++++++++------ src/main/resources/api-definition.yaml | 48 +++++++++++ 3 files changed, 140 insertions(+), 37 deletions(-) diff --git a/src/integration-test/java/org/openlmis/referencedata/web/WardControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/referencedata/web/WardControllerIntegrationTest.java index 6a4eec13..37823ec3 100644 --- a/src/integration-test/java/org/openlmis/referencedata/web/WardControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/referencedata/web/WardControllerIntegrationTest.java @@ -15,6 +15,7 @@ package org.openlmis.referencedata.web; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.nullable; @@ -47,6 +48,7 @@ public class WardControllerIntegrationTest extends BaseWebIntegrationTest { private static final String RESOURCE_URL = WardController.RESOURCE_PATH; private static final String ID_URL = RESOURCE_URL + "/{id}"; + private static final String SAVE_ALL_URL = RESOURCE_URL + "/saveAll"; private static final String NAME = "name"; @@ -75,8 +77,8 @@ public void shouldReturnPageOfWards() { .then() .statusCode(HttpStatus.SC_OK) .body("content", Matchers.hasSize(1)) - .body("content[0].id", Matchers.is(ward.getId().toString())) - .body("content[0].name", Matchers.is(ward.getName())); + .body("content[0].id", is(ward.getId().toString())) + .body("content[0].name", is(ward.getName())); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } @@ -106,8 +108,8 @@ public void shouldCreateWard() { .post(RESOURCE_URL) .then() .statusCode(HttpStatus.SC_CREATED) - .body(ID, Matchers.is(Matchers.notNullValue())) - .body(NAME, Matchers.is(wardDto.getName())); + .body(ID, is(Matchers.notNullValue())) + .body(NAME, is(wardDto.getName())); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } @@ -139,8 +141,8 @@ public void shouldReturnGivenWard() { .get(ID_URL) .then() .statusCode(HttpStatus.SC_OK) - .body(ID, Matchers.is(wardDto.getId().toString())) - .body(NAME, Matchers.is(wardDto.getName())); + .body(ID, is(wardDto.getId().toString())) + .body(NAME, is(wardDto.getName())); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } @@ -157,7 +159,7 @@ public void shouldReturnNotFoundMessageIfWardDoesNotExistForGivenWardEndpoint() .get(ID_URL) .then() .statusCode(HttpStatus.SC_NOT_FOUND) - .body(MESSAGE_KEY, Matchers.is(WardMessageKeys.ERROR_WARD_NOT_FOUND)); + .body(MESSAGE_KEY, is(WardMessageKeys.ERROR_WARD_NOT_FOUND)); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } @@ -193,8 +195,31 @@ public void shouldUpdateWard() { .put(ID_URL) .then() .statusCode(HttpStatus.SC_OK) - .body(ID, Matchers.is(wardDto.getId().toString())) - .body(NAME, Matchers.is(wardDto.getName())); + .body(ID, is(wardDto.getId().toString())) + .body(NAME, is(wardDto.getName())); + + assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); + } + + @Test + public void shouldUpdateListOfWards() { + mockUserHasRight(WARDS_MANAGE); + given(wardRepository.findById(wardDto.getId())) + .willReturn(Optional.of(ward)); + given(facilityRepository.findById(facility.getId())) + .willReturn(Optional.of(facility)); + + restAssured + .given() + .header(HttpHeaders.AUTHORIZATION, getTokenHeader()) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .body(Collections.singletonList(wardDto)) + .when() + .put(SAVE_ALL_URL) + .then() + .statusCode(HttpStatus.SC_OK) + .assertThat() + .body("size()", is(1)); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } @@ -213,7 +238,7 @@ public void shouldReturnBadRequestMessageIfWardCannotBeUpdated() { .put(ID_URL) .then() .statusCode(HttpStatus.SC_BAD_REQUEST) - .body(MESSAGE_KEY, Matchers.is(WardMessageKeys.ERROR_WARD_ID_MISMATCH)); + .body(MESSAGE_KEY, is(WardMessageKeys.ERROR_WARD_ID_MISMATCH)); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } @@ -263,7 +288,7 @@ public void shouldReturnNotFoundMessageIfWardDoesNotExistForDeleteWardEndpoint() .delete(ID_URL) .then() .statusCode(HttpStatus.SC_NOT_FOUND) - .body(MESSAGE_KEY, Matchers.is(WardMessageKeys.ERROR_WARD_NOT_FOUND)); + .body(MESSAGE_KEY, is(WardMessageKeys.ERROR_WARD_NOT_FOUND)); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } diff --git a/src/main/java/org/openlmis/referencedata/web/WardController.java b/src/main/java/org/openlmis/referencedata/web/WardController.java index a86e19ff..a331ca3c 100644 --- a/src/main/java/org/openlmis/referencedata/web/WardController.java +++ b/src/main/java/org/openlmis/referencedata/web/WardController.java @@ -17,6 +17,7 @@ import static org.openlmis.referencedata.domain.RightName.WARDS_MANAGE; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -76,17 +77,28 @@ public class WardController extends BaseController { @ResponseBody public WardDto createWard(@RequestBody WardDto ward) { rightService.checkAdminRight(WARDS_MANAGE); - LOGGER.debug("Creating new ward"); - Ward newWard = Ward.newWard(ward); + return createOne(ward); + } - LOGGER.debug("Find facility"); - Facility facility = findFacility(ward.getFacility().getId()); - newWard.setFacility(facility); + /** + * Updates the list of wards. + */ + @PutMapping(value = "/saveAll") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public List saveWards(@RequestBody List wards) { + rightService.checkAdminRight(WARDS_MANAGE); - newWard.setId(null); - newWard = wardRepository.saveAndFlush(newWard); + List savedWards = new ArrayList<>(); + for (WardDto ward : wards) { + if (ward.getId() != null) { + savedWards.add(saveOne(ward.getId(), ward)); + } else { + savedWards.add(createOne(ward)); + } + } - return WardDto.newInstance(newWard); + return savedWards; } /** @@ -102,24 +114,7 @@ public WardDto saveWard(@PathVariable("id") UUID id, throw new ValidationMessageException(WardMessageKeys.ERROR_WARD_ID_MISMATCH); } - LOGGER.debug("Updating ward"); - Ward db; - Optional wardOptional = wardRepository.findById(id); - if (wardOptional.isPresent()) { - db = wardOptional.get(); - db.updateFrom(ward); - } else { - db = Ward.newWard(ward); - db.setId(id); - } - - LOGGER.debug("Find facility"); - Facility facility = findFacility(ward.getFacility().getId()); - db.setFacility(facility); - - wardRepository.saveAndFlush(db); - - return WardDto.newInstance(db); + return saveOne(id, ward); } /** @@ -203,6 +198,41 @@ public ResponseEntity getWardAuditLog( returnJson); } + private WardDto saveOne(UUID id, WardDto ward) { + LOGGER.debug("Updating ward"); + Ward db; + Optional wardOptional = wardRepository.findById(id); + if (wardOptional.isPresent()) { + db = wardOptional.get(); + db.updateFrom(ward); + } else { + db = Ward.newWard(ward); + db.setId(id); + } + + LOGGER.debug("Find facility"); + Facility facility = findFacility(ward.getFacility().getId()); + db.setFacility(facility); + + wardRepository.saveAndFlush(db); + + return WardDto.newInstance(db); + } + + private WardDto createOne(WardDto ward) { + LOGGER.debug("Creating new ward"); + Ward newWard = Ward.newWard(ward); + + LOGGER.debug("Find facility"); + Facility facility = findFacility(ward.getFacility().getId()); + newWard.setFacility(facility); + + newWard.setId(null); + newWard = wardRepository.saveAndFlush(newWard); + + return WardDto.newInstance(newWard); + } + private Facility findFacility(UUID facilityId) { return facilityRepository.findById(facilityId) .orElseThrow(() -> new ValidationMessageException( diff --git a/src/main/resources/api-definition.yaml b/src/main/resources/api-definition.yaml index ed64926c..bbdf208b 100644 --- a/src/main/resources/api-definition.yaml +++ b/src/main/resources/api-definition.yaml @@ -120,6 +120,12 @@ schemas: - wardPage: !include schemas/wardPage.json + - wardArray: | + { + "type": "array", + "items": { "type": "object", "$ref": "schemas/ward.json" } + } + - orderable: !include schemas/orderable.json - orderableChildDto: !include schemas/orderableChildDto.json @@ -942,6 +948,17 @@ resourceTypes: get: is: [ secured, paginated, sorted ] description: Get all wards that match the given parameters. + queryParameters: + facilityId: + displayName: facilityId + type: string + required: false + repeat: false + disabled: + displayName: disabled + type: boolean + required: false + repeat: false responses: 200: headers: @@ -984,6 +1001,37 @@ resourceTypes: body: application/json: schema: localizedErrorResponse + /saveAll: + put: + is: [ secured ] + description: Update the list of wards. + body: + application/json: + schema: wardArray + responses: + 200: + headers: + Keep-Alive: + body: + application/json: + schema: wardArray + 400: + body: + application/json: + schema: localizedErrorResponse + 401: + headers: + Keep-Alive: + body: + application/json: + 403: + body: + application/json: + schema: localizedErrorResponse + 404: + body: + application/json: + schema: localizedErrorResponse /{id}: uriParameters: id: