-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from OpenLMIS/OAM-184
OAM-184: Added validation for Wards/Services in requisition functions
- Loading branch information
Showing
11 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/openlmis/requisition/web/FacilityTypeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* This program is part of the OpenLMIS logistics management information system platform software. | ||
* Copyright © 2017 VillageReach | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Affero General Public License as published by the Free Software Foundation, either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero General Public License for more details. You should have received a copy of | ||
* the GNU Affero General Public License along with this program. If not, see | ||
* http://www.gnu.org/licenses. For additional information contact [email protected]. | ||
*/ | ||
|
||
package org.openlmis.requisition.web; | ||
|
||
import static org.openlmis.requisition.i18n.MessageKeys.ERROR_FACILITY_CANNOT_BE_WARD_SERVICE_TYPE; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import org.openlmis.requisition.dto.FacilityDto; | ||
import org.openlmis.requisition.dto.FacilityTypeDto; | ||
import org.openlmis.requisition.exception.ValidationMessageException; | ||
import org.openlmis.requisition.service.referencedata.FacilityReferenceDataService; | ||
import org.openlmis.requisition.utils.Message; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class FacilityTypeHelper { | ||
|
||
public static final String WARD_SERVICE_TYPE_CODE = "WS"; | ||
|
||
@Autowired | ||
private FacilityReferenceDataService facilityReferenceDataService; | ||
|
||
/** | ||
* Method check if facility has type ward/service. | ||
* | ||
* @param facility Facility. | ||
* @param facilityFunction the function of the facility being checked (e.g. supplying facility, | ||
* requesting facility) | ||
*/ | ||
public void checkIfFacilityHasSupportedType(FacilityDto facility, String facilityFunction) { | ||
FacilityTypeDto type = facility.getType(); | ||
if (type != null && type.getCode().equals(WARD_SERVICE_TYPE_CODE)) { | ||
throw new ValidationMessageException( | ||
new Message(ERROR_FACILITY_CANNOT_BE_WARD_SERVICE_TYPE, facilityFunction) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Method check if facilities has type ward/service. | ||
* | ||
* @param facilitiesUuids UUIDs of facilities. | ||
* @param facilityFunction the function of the facility being checked (e.g. supplying facility, | ||
* requesting facility) | ||
*/ | ||
public void checkIfFacilityHasSupportedType(Set<UUID> facilitiesUuids, | ||
String facilityFunction) { | ||
List<FacilityDto> facilities = facilityReferenceDataService.search(facilitiesUuids); | ||
facilities.forEach(facility -> checkIfFacilityHasSupportedType(facility, facilityFunction)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/test/java/org/openlmis/requisition/web/FacilityTypeHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* This program is part of the OpenLMIS logistics management information system platform software. | ||
* Copyright © 2017 VillageReach | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Affero General Public License as published by the Free Software Foundation, either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero General Public License for more details. You should have received a copy of | ||
* the GNU Affero General Public License along with this program. If not, see | ||
* http://www.gnu.org/licenses. For additional information contact [email protected]. | ||
*/ | ||
|
||
package org.openlmis.requisition.web; | ||
|
||
import static org.mockito.ArgumentMatchers.anySet; | ||
import static org.mockito.Mockito.when; | ||
import static org.openlmis.requisition.web.FacilityTypeHelper.WARD_SERVICE_TYPE_CODE; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import org.javers.common.collections.Lists; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.openlmis.requisition.dto.FacilityDto; | ||
import org.openlmis.requisition.dto.FacilityTypeDto; | ||
import org.openlmis.requisition.exception.ValidationMessageException; | ||
import org.openlmis.requisition.service.referencedata.FacilityReferenceDataService; | ||
import org.openlmis.requisition.testutils.FacilityDtoDataBuilder; | ||
import org.openlmis.requisition.testutils.FacilityTypeDtoDataBuilder; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class FacilityTypeHelperTest { | ||
|
||
public static final String TEST_FACILITY = "Test facility"; | ||
public static final String TEST_CODE_TYPE = "TEST"; | ||
|
||
@Mock | ||
FacilityReferenceDataService facilityReferenceDataService; | ||
|
||
@InjectMocks | ||
FacilityTypeHelper facilityTypeHelper; | ||
|
||
private FacilityDto facility; | ||
|
||
@Before | ||
public void setUp() { | ||
facility = generateInstance(TEST_CODE_TYPE); | ||
} | ||
|
||
@Test(expected = ValidationMessageException.class) | ||
public void shouldThrowExceptionWhenFacilityIsWardServiceType() { | ||
FacilityDto ward = generateInstance(WARD_SERVICE_TYPE_CODE); | ||
|
||
facilityTypeHelper.checkIfFacilityHasSupportedType(ward, TEST_FACILITY); | ||
} | ||
|
||
@Test | ||
public void shouldPassWhenFacilityIsOtherTypeThanWardService() { | ||
facilityTypeHelper.checkIfFacilityHasSupportedType(facility, TEST_FACILITY); | ||
} | ||
|
||
@Test(expected = ValidationMessageException.class) | ||
public void shouldThrowExceptionWhenOneOfTheFacilitiesIsWardServiceType() { | ||
FacilityDto ward = generateInstance(WARD_SERVICE_TYPE_CODE); | ||
List<FacilityDto> facilities = Lists.asList(ward, facility); | ||
|
||
when(facilityReferenceDataService.search(anySet())).thenReturn(facilities); | ||
|
||
facilityTypeHelper.checkIfFacilityHasSupportedType(new HashSet<>(), TEST_FACILITY); | ||
} | ||
|
||
@Test | ||
public void shouldPassWhenNoneOfTheFacilitiesAreWardServiceType() { | ||
FacilityDto anotherFacility = generateInstance("Code2"); | ||
List<FacilityDto> facilities = Lists.asList(anotherFacility, facility); | ||
|
||
when(facilityReferenceDataService.search(anySet())).thenReturn(facilities); | ||
|
||
facilityTypeHelper.checkIfFacilityHasSupportedType(new HashSet<>(), TEST_FACILITY); | ||
} | ||
|
||
private FacilityDto generateInstance(String typeCode) { | ||
FacilityTypeDto facilityType = new FacilityTypeDtoDataBuilder() | ||
.withCode(typeCode) | ||
.buildAsDto(); | ||
|
||
return new FacilityDtoDataBuilder() | ||
.withType(facilityType) | ||
.buildAsDto(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters