Skip to content

Commit 107d10f

Browse files
authored
Merge pull request #101 from OpenLMIS/OAM-184
OAM-184: Added validation for Wards/Services in requisition functions
2 parents 6d125a9 + 6ce3098 commit 107d10f

File tree

11 files changed

+220
-2
lines changed

11 files changed

+220
-2
lines changed

src/integration-test/java/org/openlmis/requisition/web/BaseWebIntegrationTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ public abstract class BaseWebIntegrationTest {
266266
@MockBean
267267
protected ConfigurationSettingService configurationSettingService;
268268

269+
@MockBean
270+
protected FacilityTypeHelper facilityTypeHelper;
271+
269272
/**
270273
* Method called to initialize basic resources after the object is created.
271274
*/

src/integration-test/java/org/openlmis/requisition/web/BatchRequisitionControllerIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import static org.mockito.Matchers.anySet;
3232
import static org.mockito.Matchers.anySetOf;
3333
import static org.mockito.Matchers.eq;
34+
import static org.mockito.Mockito.anyString;
35+
import static org.mockito.Mockito.doNothing;
3436
import static org.mockito.Mockito.doReturn;
3537
import static org.mockito.Mockito.doThrow;
3638
import static org.mockito.Mockito.never;
@@ -223,6 +225,9 @@ public void setUp() {
223225
.search(anySetOf(UUID.class));
224226

225227
mockSearchSupervisoryNodeByProgramAndFacility();
228+
229+
doNothing().when(facilityTypeHelper).checkIfFacilityHasSupportedType(
230+
anySet(), anyString());
226231
}
227232

228233
@Test

src/main/java/org/openlmis/requisition/i18n/MessageKeys.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ public abstract class MessageKeys {
276276
ERROR_PREFIX + ".duplication";
277277
public static final String ERROR_REQUISITION_DUPLICATION_WITH_NODE =
278278
ERROR_PREFIX + ".duplicationWithNode";
279+
public static final String ERROR_FACILITY_CANNOT_BE_WARD_SERVICE_TYPE =
280+
ERROR_PREFIX + ".facilityCannotBeWardServiceType";
279281

280282
public static final String REQUISITION_EMAIL_CONVERT_TO_ORDER_SUBJECT
281283
= "requisition.email.convertToOrder.subject";

src/main/java/org/openlmis/requisition/web/BaseRequisitionController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public abstract class BaseRequisitionController extends BaseController {
201201
@Autowired
202202
private ReasonsValidator reasonsValidator;
203203

204+
@Autowired
205+
FacilityTypeHelper facilityTypeHelper;
206+
204207
InitiateResult doInitiate(UUID programId, UUID facilityId, UUID suggestedPeriod,
205208
boolean emergency, HttpServletRequest request, Profiler profiler) {
206209
if (null == facilityId || null == programId) {
@@ -214,6 +217,9 @@ InitiateResult doInitiate(UUID programId, UUID facilityId, UUID suggestedPeriod,
214217

215218
FacilityDto facility = findFacility(facilityId, profiler);
216219

220+
profiler.start("CHECK_FACILITY_TYPE_SUPPORTED");
221+
facilityTypeHelper.checkIfFacilityHasSupportedType(facility, "Requisitioning facility");
222+
217223
profiler.start("CHECK_FACILITY_SUPPORTS_PROGRAM");
218224
facilitySupportsProgramHelper.checkIfFacilitySupportsProgram(facility, programId);
219225

src/main/java/org/openlmis/requisition/web/BatchRequisitionController.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.openlmis.requisition.dto.OrderableDto;
5656
import org.openlmis.requisition.dto.ProcessingPeriodDto;
5757
import org.openlmis.requisition.dto.ReleasableRequisitionBatchDto;
58+
import org.openlmis.requisition.dto.ReleasableRequisitionDto;
5859
import org.openlmis.requisition.dto.RequisitionDto;
5960
import org.openlmis.requisition.dto.RequisitionErrorMessage;
6061
import org.openlmis.requisition.dto.RequisitionsProcessingStatusDto;
@@ -86,7 +87,6 @@
8687
import org.springframework.web.bind.annotation.ResponseBody;
8788
import org.springframework.web.bind.annotation.ResponseStatus;
8889

89-
9090
@Controller
9191
@Transactional
9292
@SuppressWarnings("PMD.TooManyMethods")
@@ -322,6 +322,9 @@ public ResponseEntity batchReleaseRequisitions(
322322
.getRequisitionsToRelease());
323323
ResponseEntity response;
324324

325+
profiler.start("CHECK_SUPPLYING_DEPOTS_TYPE");
326+
checkRequisitionsSupplyingDepotsTypes(releaseDto.getRequisitionsToRelease());
327+
325328
if (addValidationErrors(processingStatus, result, null)) {
326329
response = ResponseEntity.status(HttpStatus.FORBIDDEN)
327330
.body(localizeMessage(result.getError().getMessage()));
@@ -591,4 +594,15 @@ private Map<VersionIdentityDto, ApprovedProductDto> getApprovedProducts(
591594
.stream()
592595
.collect(toMap(ApprovedProductDto::getIdentity, Function.identity()));
593596
}
597+
598+
private void checkRequisitionsSupplyingDepotsTypes(
599+
List<ReleasableRequisitionDto> releasableRequisitions) {
600+
Set<UUID> supplyingDepotUuids = releasableRequisitions
601+
.stream()
602+
.map(ReleasableRequisitionDto::getSupplyingDepotId)
603+
.collect(Collectors.toSet());
604+
605+
facilityTypeHelper.checkIfFacilityHasSupportedType(supplyingDepotUuids, "Supplying depot");
606+
}
607+
594608
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This program is part of the OpenLMIS logistics management information system platform software.
3+
* Copyright © 2017 VillageReach
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms
6+
* of the GNU Affero General Public License as published by the Free Software Foundation, either
7+
* version 3 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU Affero General Public License for more details. You should have received a copy of
12+
* the GNU Affero General Public License along with this program. If not, see
13+
* http://www.gnu.org/licenses.  For additional information contact [email protected].
14+
*/
15+
16+
package org.openlmis.requisition.web;
17+
18+
import static org.openlmis.requisition.i18n.MessageKeys.ERROR_FACILITY_CANNOT_BE_WARD_SERVICE_TYPE;
19+
20+
import java.util.List;
21+
import java.util.Set;
22+
import java.util.UUID;
23+
import org.openlmis.requisition.dto.FacilityDto;
24+
import org.openlmis.requisition.dto.FacilityTypeDto;
25+
import org.openlmis.requisition.exception.ValidationMessageException;
26+
import org.openlmis.requisition.service.referencedata.FacilityReferenceDataService;
27+
import org.openlmis.requisition.utils.Message;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.stereotype.Component;
30+
31+
@Component
32+
public class FacilityTypeHelper {
33+
34+
public static final String WARD_SERVICE_TYPE_CODE = "WS";
35+
36+
@Autowired
37+
private FacilityReferenceDataService facilityReferenceDataService;
38+
39+
/**
40+
* Method check if facility has type ward/service.
41+
*
42+
* @param facility Facility.
43+
* @param facilityFunction the function of the facility being checked (e.g. supplying facility,
44+
* requesting facility)
45+
*/
46+
public void checkIfFacilityHasSupportedType(FacilityDto facility, String facilityFunction) {
47+
FacilityTypeDto type = facility.getType();
48+
if (type != null && type.getCode().equals(WARD_SERVICE_TYPE_CODE)) {
49+
throw new ValidationMessageException(
50+
new Message(ERROR_FACILITY_CANNOT_BE_WARD_SERVICE_TYPE, facilityFunction)
51+
);
52+
}
53+
}
54+
55+
/**
56+
* Method check if facilities has type ward/service.
57+
*
58+
* @param facilitiesUuids UUIDs of facilities.
59+
* @param facilityFunction the function of the facility being checked (e.g. supplying facility,
60+
* requesting facility)
61+
*/
62+
public void checkIfFacilityHasSupportedType(Set<UUID> facilitiesUuids,
63+
String facilityFunction) {
64+
List<FacilityDto> facilities = facilityReferenceDataService.search(facilitiesUuids);
65+
facilities.forEach(facility -> checkIfFacilityHasSupportedType(facility, facilityFunction));
66+
}
67+
68+
}

src/main/resources/messages_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ requisition.lineItem.suppliedByOtherPartner=Supplied by other warehouse/partner.
33

44
requisition.error.authorize.mustBeSubmittedToBeAuthorize=Cannot authorize requisition: {0}, requisition must have status 'SUBMITTED' to be authorized.
55
requisition.error.facilityDoesNotSupportProgram=Facility with ID {0} does not support program with ID {1}.
6+
requisition.error.facilityCannotBeWardServiceType={0} cannot be type Ward/Service.
67
requisition.error.initiate.missingParameters=Facility and program must be specified when initiating a requisition.
78
requisition.error.initiate.finishPreviousRequisition=Please finish previous requisition.
89
requisition.error.submit.mustBeInitiatedToBeSubmitted=Cannot submit requisition: {0}, requisition must have status 'INITIATED' or 'REJECTED' to be submitted.

src/test/java/org/openlmis/requisition/web/BatchRequisitionControllerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
package org.openlmis.requisition.web;
1717

18+
import static org.mockito.ArgumentMatchers.anySet;
19+
import static org.mockito.ArgumentMatchers.anyString;
1820
import static org.mockito.Matchers.any;
1921
import static org.mockito.Matchers.anyList;
22+
import static org.mockito.Mockito.doNothing;
2023
import static org.mockito.Mockito.doReturn;
2124
import static org.mockito.Mockito.never;
2225
import static org.mockito.Mockito.verify;
@@ -29,6 +32,7 @@
2932
import org.mockito.InjectMocks;
3033
import org.mockito.Mock;
3134
import org.mockito.MockitoAnnotations;
35+
import org.openlmis.requisition.dto.FacilityDto;
3236
import org.openlmis.requisition.dto.ReleasableRequisitionBatchDto;
3337
import org.openlmis.requisition.dto.UserDto;
3438
import org.openlmis.requisition.errorhandling.ValidationResult;
@@ -49,6 +53,9 @@ public class BatchRequisitionControllerTest {
4953
@Mock
5054
PermissionService permissionService;
5155

56+
@Mock
57+
FacilityTypeHelper facilityTypeHelper;
58+
5259
@InjectMocks
5360
BatchRequisitionController batchRequisitionController;
5461

@@ -58,6 +65,10 @@ public void setUp() {
5865

5966
UserDto currentUser = DtoGenerator.of(UserDto.class);
6067
when(authenticationHelper.getCurrentUser()).thenReturn(currentUser);
68+
doNothing().when(facilityTypeHelper).checkIfFacilityHasSupportedType(
69+
any(FacilityDto.class), anyString());
70+
doNothing().when(facilityTypeHelper).checkIfFacilityHasSupportedType(
71+
anySet(), anyString());
6172
}
6273

6374
@Test

src/test/java/org/openlmis/requisition/web/FacilitySupportsProgramHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.junit.runner.RunWith;
2929
import org.mockito.InjectMocks;
3030
import org.mockito.Mock;
31-
import org.mockito.runners.MockitoJUnitRunner;
31+
import org.mockito.junit.MockitoJUnitRunner;
3232
import org.openlmis.requisition.dto.FacilityDto;
3333
import org.openlmis.requisition.dto.SupportedProgramDto;
3434
import org.openlmis.requisition.exception.ValidationMessageException;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This program is part of the OpenLMIS logistics management information system platform software.
3+
* Copyright © 2017 VillageReach
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms
6+
* of the GNU Affero General Public License as published by the Free Software Foundation, either
7+
* version 3 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU Affero General Public License for more details. You should have received a copy of
12+
* the GNU Affero General Public License along with this program. If not, see
13+
* http://www.gnu.org/licenses.  For additional information contact [email protected].
14+
*/
15+
16+
package org.openlmis.requisition.web;
17+
18+
import static org.mockito.ArgumentMatchers.anySet;
19+
import static org.mockito.Mockito.when;
20+
import static org.openlmis.requisition.web.FacilityTypeHelper.WARD_SERVICE_TYPE_CODE;
21+
22+
import java.util.HashSet;
23+
import java.util.List;
24+
import org.javers.common.collections.Lists;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.InjectMocks;
29+
import org.mockito.Mock;
30+
import org.mockito.junit.MockitoJUnitRunner;
31+
import org.openlmis.requisition.dto.FacilityDto;
32+
import org.openlmis.requisition.dto.FacilityTypeDto;
33+
import org.openlmis.requisition.exception.ValidationMessageException;
34+
import org.openlmis.requisition.service.referencedata.FacilityReferenceDataService;
35+
import org.openlmis.requisition.testutils.FacilityDtoDataBuilder;
36+
import org.openlmis.requisition.testutils.FacilityTypeDtoDataBuilder;
37+
38+
@RunWith(MockitoJUnitRunner.class)
39+
public class FacilityTypeHelperTest {
40+
41+
public static final String TEST_FACILITY = "Test facility";
42+
public static final String TEST_CODE_TYPE = "TEST";
43+
44+
@Mock
45+
FacilityReferenceDataService facilityReferenceDataService;
46+
47+
@InjectMocks
48+
FacilityTypeHelper facilityTypeHelper;
49+
50+
private FacilityDto facility;
51+
52+
@Before
53+
public void setUp() {
54+
facility = generateInstance(TEST_CODE_TYPE);
55+
}
56+
57+
@Test(expected = ValidationMessageException.class)
58+
public void shouldThrowExceptionWhenFacilityIsWardServiceType() {
59+
FacilityDto ward = generateInstance(WARD_SERVICE_TYPE_CODE);
60+
61+
facilityTypeHelper.checkIfFacilityHasSupportedType(ward, TEST_FACILITY);
62+
}
63+
64+
@Test
65+
public void shouldPassWhenFacilityIsOtherTypeThanWardService() {
66+
facilityTypeHelper.checkIfFacilityHasSupportedType(facility, TEST_FACILITY);
67+
}
68+
69+
@Test(expected = ValidationMessageException.class)
70+
public void shouldThrowExceptionWhenOneOfTheFacilitiesIsWardServiceType() {
71+
FacilityDto ward = generateInstance(WARD_SERVICE_TYPE_CODE);
72+
List<FacilityDto> facilities = Lists.asList(ward, facility);
73+
74+
when(facilityReferenceDataService.search(anySet())).thenReturn(facilities);
75+
76+
facilityTypeHelper.checkIfFacilityHasSupportedType(new HashSet<>(), TEST_FACILITY);
77+
}
78+
79+
@Test
80+
public void shouldPassWhenNoneOfTheFacilitiesAreWardServiceType() {
81+
FacilityDto anotherFacility = generateInstance("Code2");
82+
List<FacilityDto> facilities = Lists.asList(anotherFacility, facility);
83+
84+
when(facilityReferenceDataService.search(anySet())).thenReturn(facilities);
85+
86+
facilityTypeHelper.checkIfFacilityHasSupportedType(new HashSet<>(), TEST_FACILITY);
87+
}
88+
89+
private FacilityDto generateInstance(String typeCode) {
90+
FacilityTypeDto facilityType = new FacilityTypeDtoDataBuilder()
91+
.withCode(typeCode)
92+
.buildAsDto();
93+
94+
return new FacilityDtoDataBuilder()
95+
.withType(facilityType)
96+
.buildAsDto();
97+
}
98+
99+
}

src/test/java/org/openlmis/requisition/web/RequisitionControllerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import static org.junit.Assert.assertNotNull;
2626
import static org.junit.Assert.assertNull;
2727
import static org.junit.Assert.assertTrue;
28+
import static org.mockito.ArgumentMatchers.anySet;
29+
import static org.mockito.ArgumentMatchers.anyString;
2830
import static org.mockito.ArgumentMatchers.isNull;
2931
import static org.mockito.Matchers.any;
3032
import static org.mockito.Matchers.anyBoolean;
@@ -279,6 +281,9 @@ public class RequisitionControllerTest {
279281
@Mock
280282
private RejectionDto rejectionDto;
281283

284+
@Mock
285+
private FacilityTypeHelper facilityTypeHelper;
286+
282287
private UUID programUuid = UUID.randomUUID();
283288
private UUID facilityUuid = UUID.randomUUID();
284289
private UUID uuid1 = UUID.fromString("00000000-0000-0000-0000-000000000001");
@@ -362,6 +367,10 @@ public void setUp() {
362367

363368
when(approvedProductReferenceDataService.getApprovedProducts(any(), any()))
364369
.thenReturn(new ApproveProductsAggregator(emptyList(), UUID.randomUUID()));
370+
doNothing().when(facilityTypeHelper).checkIfFacilityHasSupportedType(
371+
any(FacilityDto.class), anyString());
372+
doNothing().when(facilityTypeHelper).checkIfFacilityHasSupportedType(
373+
anySet(), anyString());
365374
}
366375

367376
private void stubValidations(Requisition... requisitions) {

0 commit comments

Comments
 (0)