diff --git a/src/integration-test/java/org/openlmis/referencedata/repository/FacilityRepositoryIntegrationTest.java b/src/integration-test/java/org/openlmis/referencedata/repository/FacilityRepositoryIntegrationTest.java index f0562cc21..0c7b5780c 100644 --- a/src/integration-test/java/org/openlmis/referencedata/repository/FacilityRepositoryIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/referencedata/repository/FacilityRepositoryIntegrationTest.java @@ -64,18 +64,6 @@ public void setUp() { geographicZoneRepository.save(geographicZone); } - Facility generateInstance() { - int instanceNumber = this.getNextInstanceNumber(); - Facility facility = new Facility("F" + instanceNumber); - facility.setType(this.facilityType); - facility.setGeographicZone(this.geographicZone); - facility.setName("Facility #" + instanceNumber); - facility.setDescription("Test facility"); - facility.setActive(true); - facility.setEnabled(true); - return facility; - } - @Test public void shouldFindFacilitiesWithSimilarCode() { Facility facility = generateInstance(); @@ -174,6 +162,48 @@ public void shouldFindFacilitiesByGeographicZone() { assertEquals(validFacility.getId(), foundFacilties.get(0).getId()); } + @Test + public void shouldFindFacilitiesByCodeOrNameAndGeographicZone() { + // given + GeographicZone validZone = new GeographicZone("validZone", geographicLevel); + validZone = geographicZoneRepository.save(validZone); + + Facility facilityWithCodeAndName = generateInstance(); + facilityWithCodeAndName.setGeographicZone(validZone); + repository.save(facilityWithCodeAndName); + + Facility facilityWithCode = generateInstance(); + facilityWithCode.setGeographicZone(validZone); + repository.save(facilityWithCode); + + // when + List foundFacilties = repository + .search(facilityWithCodeAndName.getCode(), "Facility", validZone); + + // then + assertEquals(2, foundFacilties.size()); + assertEquals(facilityWithCodeAndName.getId(), foundFacilties.get(0).getId()); + assertEquals(facilityWithCode.getId(), foundFacilties.get(1).getId()); + } + + @Override + Facility generateInstance() { + int instanceNumber = this.getNextInstanceNumber(); + Facility facility = generateInstanceWithRequiredFields(geographicZone, "F" + instanceNumber); + facility.setName("Facility #" + instanceNumber); + facility.setDescription("Test facility"); + return facility; + } + + private Facility generateInstanceWithRequiredFields(GeographicZone zone, String code) { + Facility secondFacility = new Facility(code); + secondFacility.setGeographicZone(zone); + secondFacility.setActive(true); + secondFacility.setEnabled(true); + secondFacility.setType(this.facilityType); + return secondFacility; + } + private void searchFacilityAndCheckResults( String code, String name, Facility facility, int expectedSize) { List foundFacilties = repository.search(code, name, null); diff --git a/src/integration-test/java/org/openlmis/referencedata/web/GeographicZoneControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/referencedata/web/GeographicZoneControllerIntegrationTest.java index 42641b76a..f21facfa9 100644 --- a/src/integration-test/java/org/openlmis/referencedata/web/GeographicZoneControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/referencedata/web/GeographicZoneControllerIntegrationTest.java @@ -37,13 +37,13 @@ import org.springframework.data.domain.PageRequest; import org.springframework.http.MediaType; +import guru.nidi.ramltester.junit.RamlMatchers; + import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.UUID; -import guru.nidi.ramltester.junit.RamlMatchers; - @SuppressWarnings({"PMD.TooManyMethods"}) public class GeographicZoneControllerIntegrationTest extends BaseWebIntegrationTest { @@ -55,17 +55,18 @@ public class GeographicZoneControllerIntegrationTest extends BaseWebIntegrationT private static final Integer PAGE_NUMBER = 0; //Neither 0 nor Integer.MAX_VALUE work in this context private static final Integer PAGE_SIZE = 1000; + private static final String LEVEL_NUMBER = "levelNumber"; + private static final String PARENT = "parent"; @MockBean private GeographicZoneRepository geographicZoneRepository; @MockBean private GeographicLevelRepository geographicLevelRepository; - private GeographicLevel countryLevel; private GeographicLevel regionLevel; + private GeographicLevel districtLevel; - private GeographicZone countryZone; private GeographicZone regionZone; private GeographicZone districtZone; @@ -225,8 +226,8 @@ public void shouldFindGeographicZonesByParentAndLevel() { .queryParam(ACCESS_TOKEN, getToken()) .queryParam(PAGE, PAGE_NUMBER) .queryParam(SIZE, PAGE_SIZE) - .queryParam("levelNumber", districtLevel.getLevelNumber()) - .queryParam("parent", regionZone.getId()) + .queryParam(LEVEL_NUMBER, districtLevel.getLevelNumber()) + .queryParam(PARENT, regionZone.getId()) .contentType(MediaType.APPLICATION_JSON_VALUE) .when() .get(SEARCH_URL) @@ -259,7 +260,40 @@ public void shouldFindGeographicZonesByLevel() { .queryParam(ACCESS_TOKEN, getToken()) .queryParam(PAGE, PAGE_NUMBER) .queryParam(SIZE, PAGE_SIZE) - .queryParam("levelNumber", districtLevel.getLevelNumber()) + .queryParam(LEVEL_NUMBER, districtLevel.getLevelNumber()) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when() + .get(SEARCH_URL) + .then() + .statusCode(200) + .extract().as(GeographicZone[].class); + + // then + List result = Arrays.asList(response); + assertEquals(geographicZones.size(), result.size()); + assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); + } + + @Test + public void shouldFindGeographicZonesByParent() { + // given + doNothing() + .when(rightService) + .checkAdminRight(RightName.GEOGRAPHIC_ZONES_MANAGE_RIGHT); + + List geographicZones = Collections.singletonList(districtZone); + + given(geographicZoneRepository.findOne(regionZone.getId())).willReturn(regionZone); + given(geographicZoneRepository.findByParent(regionZone)) + .willReturn(geographicZones); + + // when + GeographicZone[] response = restAssured + .given() + .queryParam(ACCESS_TOKEN, getToken()) + .queryParam(PAGE, PAGE_NUMBER) + .queryParam(SIZE, PAGE_SIZE) + .queryParam(PARENT, regionZone.getId()) .contentType(MediaType.APPLICATION_JSON_VALUE) .when() .get(SEARCH_URL) @@ -273,6 +307,70 @@ public void shouldFindGeographicZonesByLevel() { assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } + @Test + public void shouldRespondWithBadRequestWhenGeographicZonesParentNotFound() { + // given + doNothing() + .when(rightService) + .checkAdminRight(RightName.GEOGRAPHIC_ZONES_MANAGE_RIGHT); + + List geographicZones = Collections.singletonList(districtZone); + + given(geographicZoneRepository.findOne(regionZone.getId())).willReturn(null); + given(geographicLevelRepository.findByLevelNumber(districtLevel.getLevelNumber())) + .willReturn(districtLevel); + given(geographicZoneRepository.findByParentAndLevel(regionZone, districtLevel)) + .willReturn(geographicZones); + + // when + restAssured.given() + .queryParam(ACCESS_TOKEN, getToken()) + .queryParam(PAGE, PAGE_NUMBER) + .queryParam(SIZE, PAGE_SIZE) + .queryParam(LEVEL_NUMBER, districtLevel.getLevelNumber()) + .queryParam(PARENT, regionZone.getId()) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when() + .get(SEARCH_URL) + .then() + .statusCode(400); + + // then + assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); + } + + @Test + public void shouldRespondWithBadRequestWhenGeographicLevelNotFound() { + // given + doNothing() + .when(rightService) + .checkAdminRight(RightName.GEOGRAPHIC_ZONES_MANAGE_RIGHT); + + List geographicZones = Collections.singletonList(districtZone); + + given(geographicZoneRepository.findOne(regionZone.getId())).willReturn(regionZone); + given(geographicLevelRepository.findByLevelNumber(districtLevel.getLevelNumber())) + .willReturn(null); + given(geographicZoneRepository.findByParentAndLevel(regionZone, districtLevel)) + .willReturn(geographicZones); + + // when + restAssured.given() + .queryParam(ACCESS_TOKEN, getToken()) + .queryParam(PAGE, PAGE_NUMBER) + .queryParam(SIZE, PAGE_SIZE) + .queryParam(LEVEL_NUMBER, districtLevel.getLevelNumber()) + .queryParam(PARENT, regionZone.getId()) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when() + .get(SEARCH_URL) + .then() + .statusCode(400); + + // then + assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); + } + @Test public void shouldGetGeographicZone() { doNothing() @@ -347,8 +445,8 @@ public void searchShouldReturnForbiddenOnUnauthorizedToken() { .queryParam(ACCESS_TOKEN, getToken()) .queryParam(PAGE, PAGE_NUMBER) .queryParam(SIZE, PAGE_SIZE) - .queryParam("levelNumber", districtLevel.getLevelNumber()) - .queryParam("parent", regionZone.getId()) + .queryParam(LEVEL_NUMBER, districtLevel.getLevelNumber()) + .queryParam(PARENT, regionZone.getId()) .contentType(MediaType.APPLICATION_JSON_VALUE) .when() .get(SEARCH_URL) diff --git a/src/main/java/org/openlmis/referencedata/repository/GeographicZoneRepository.java b/src/main/java/org/openlmis/referencedata/repository/GeographicZoneRepository.java index 10d468db9..25077c176 100644 --- a/src/main/java/org/openlmis/referencedata/repository/GeographicZoneRepository.java +++ b/src/main/java/org/openlmis/referencedata/repository/GeographicZoneRepository.java @@ -22,8 +22,7 @@ import java.util.List; import java.util.UUID; -public interface GeographicZoneRepository extends - PagingAndSortingRepository { +public interface GeographicZoneRepository extends PagingAndSortingRepository { @Override S save(S entity); diff --git a/src/main/java/org/openlmis/referencedata/web/GeographicZoneController.java b/src/main/java/org/openlmis/referencedata/web/GeographicZoneController.java index 934872e72..3fecc2410 100644 --- a/src/main/java/org/openlmis/referencedata/web/GeographicZoneController.java +++ b/src/main/java/org/openlmis/referencedata/web/GeographicZoneController.java @@ -15,6 +15,9 @@ package org.openlmis.referencedata.web; +import static java.util.Objects.isNull; +import static org.apache.commons.lang3.BooleanUtils.isNotTrue; + import org.openlmis.referencedata.domain.GeographicLevel; import org.openlmis.referencedata.domain.GeographicZone; import org.openlmis.referencedata.domain.RightName; @@ -161,25 +164,30 @@ public List searchGeographicZones( @RequestParam(value = "levelNumber", required = false) Integer levelNumber) { rightService.checkAdminRight(RightName.GEOGRAPHIC_ZONES_MANAGE_RIGHT); - GeographicZone parent = null != parentId - ? geographicZoneRepository.findOne(parentId) - : null; - if (parentId != null && parent == null) { - throw new ValidationMessageException( - new Message(GeographicZoneMessageKeys.ERROR_NOT_FOUND_WITH_ID, parentId)); + GeographicZone parent = null; + if (isNotTrue(isNull(parentId))) { + parent = geographicZoneRepository.findOne(parentId); + if (isNull(parent)) { + throw new ValidationMessageException( + new Message(GeographicZoneMessageKeys.ERROR_NOT_FOUND_WITH_ID, parentId)); + } } - GeographicLevel level = null != levelNumber - ? geographicLevelRepository.findByLevelNumber(levelNumber) - : null; - if (levelNumber != null && level == null) { - throw new ValidationMessageException( - new Message(GeographicLevelMessageKeys.ERROR_NOT_FOUND_WITH_NUMBER, levelNumber)); + GeographicLevel level = null; + if (isNotTrue(isNull(levelNumber))) { + level = geographicLevelRepository.findByLevelNumber(levelNumber); + if (isNull(level)) { + throw new ValidationMessageException( + new Message(GeographicLevelMessageKeys.ERROR_NOT_FOUND_WITH_NUMBER, levelNumber)); + } } - if (parent == null) { + if (isNull(parent)) { return geographicZoneRepository.findByLevel(level); } + if (isNull(level)) { + return geographicZoneRepository.findByParent(parent); + } return geographicZoneRepository.findByParentAndLevel(parent, level); } } diff --git a/src/main/resources/api-definition.yaml b/src/main/resources/api-definition.yaml index 722a5e6cf..e45a1e2c1 100644 --- a/src/main/resources/api-definition.yaml +++ b/src/main/resources/api-definition.yaml @@ -1939,6 +1939,9 @@ resourceTypes: body: application/json: schema: geographicZoneArray + "400": + body: + application/json: "403": body: application/json: