Skip to content

Commit

Permalink
OLMIS-2027: add unit tests for GeographicZoneController and FacilityR…
Browse files Browse the repository at this point in the history
…epository, allow search geographicZones only by parentId, readability improvments, fix RAML
  • Loading branch information
Paweł Albecki committed Mar 1, 2017
1 parent 61fa0c8 commit ee778ab
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Facility> 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<Facility> foundFacilties = repository.search(code, name, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<GeographicZone> 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<GeographicZone> 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)
Expand All @@ -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<GeographicZone> 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<GeographicZone> 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()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import java.util.List;
import java.util.UUID;

public interface GeographicZoneRepository extends
PagingAndSortingRepository<GeographicZone, UUID> {
public interface GeographicZoneRepository extends PagingAndSortingRepository<GeographicZone, UUID> {

@Override
<S extends GeographicZone> S save(S entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,25 +164,30 @@ public List<GeographicZone> 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);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/api-definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,9 @@ resourceTypes:
body:
application/json:
schema: geographicZoneArray
"400":
body:
application/json:
"403":
body:
application/json:
Expand Down

0 comments on commit ee778ab

Please sign in to comment.