diff --git a/src/main/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionService.java b/src/main/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionService.java index 46137274..3b27f1fd 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionService.java @@ -28,6 +28,9 @@ @Service public class HomeFacilityPermissionService { + + static final String WS_TYPE_CODE = "WS"; + @Autowired private AuthenticationHelper authenticationHelper; @@ -56,7 +59,7 @@ public void checkProgramSupported(UUID programId) { /** * Returns true if facility is within the same geographic zone as the home facility. - * Returns false otherwise. + * Returns false otherwise or in case facility id is equal to home facility id. * * @param facilityId UUID of facility * @return boolean flag indicating linkage between facility and home facility @@ -67,7 +70,7 @@ public boolean checkFacilityAndHomeFacilityLinkage(UUID facilityId) { return false; } FacilityDto facility = facilityService.findOne(facilityId); - if (facility.getType().getCode().equals("WS")) { + if (facility.getType().getCode().equals(WS_TYPE_CODE)) { FacilityDto homeFacility = facilityService.findOne(homeFacilityId); return homeFacility.getGeographicZone().getId().equals(facility.getGeographicZone().getId()); } else { @@ -78,4 +81,4 @@ public boolean checkFacilityAndHomeFacilityLinkage(UUID facilityId) { private void throwException(String errorKey, String... params) { throw new PermissionMessageException(new Message(errorKey, (Object)params)); } -} \ No newline at end of file +} diff --git a/src/test/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionServiceTest.java b/src/test/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionServiceTest.java index 2426ff60..656453db 100644 --- a/src/test/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionServiceTest.java +++ b/src/test/java/org/openlmis/stockmanagement/service/HomeFacilityPermissionServiceTest.java @@ -16,8 +16,11 @@ package org.openlmis.stockmanagement.service; import static java.util.UUID.randomUUID; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.openlmis.stockmanagement.service.HomeFacilityPermissionService.WS_TYPE_CODE; import java.util.Collections; import java.util.UUID; @@ -28,6 +31,7 @@ import org.mockito.Mock; import org.openlmis.stockmanagement.dto.referencedata.FacilityDto; import org.openlmis.stockmanagement.dto.referencedata.FacilityTypeDto; +import org.openlmis.stockmanagement.dto.referencedata.GeographicZoneDto; import org.openlmis.stockmanagement.dto.referencedata.SupportedProgramDto; import org.openlmis.stockmanagement.dto.referencedata.UserDto; import org.openlmis.stockmanagement.exception.PermissionMessageException; @@ -57,10 +61,10 @@ public class HomeFacilityPermissionServiceTest { @Mock private OAuth2Authentication authentication; - + @Mock private FacilityReferenceDataService facilityService; - + private UUID homeFacilityId; private UUID programId; private UUID facilityTypeId; @@ -91,13 +95,42 @@ public void shouldPassValidationIfProgramIsSupported() throws Exception { homeFacilityPermissionService.checkProgramSupported(programId); } - + + @Test + public void shouldPassCheckIfFacilityIsWithinTheSameGeographicZoneAsHomeFacility() { + //given + FacilityDto facilityDto = mock(FacilityDto.class); + FacilityTypeDto facilityTypeDto = new FacilityTypeDto(); + facilityTypeDto.setCode(WS_TYPE_CODE); + when(facilityDto.getType()).thenReturn(facilityTypeDto); + UUID geographicZoneId = randomUUID(); + + mockGeographicZoneId(facilityDto, geographicZoneId); + mockGeographicZoneId(homeFacilityDto, geographicZoneId); + UUID facilityId = UUID.randomUUID(); + when(facilityService.findOne(facilityId)).thenReturn(facilityDto); + + //when + boolean result = homeFacilityPermissionService.checkFacilityAndHomeFacilityLinkage(facilityId); + + //then + assertTrue(result); + verify(facilityService).findOne(facilityId); + verify(facilityService).findOne(homeFacilityId); + } + + private void mockGeographicZoneId(FacilityDto facilityDto, UUID geographicZoneId) { + GeographicZoneDto geographicZoneDto = new GeographicZoneDto(); + geographicZoneDto.setId(geographicZoneId); + when(facilityDto.getGeographicZone()).thenReturn(geographicZoneDto); + } + private void mockUserDto() { userDto = mock(UserDto.class); when(userDto.getHomeFacilityId()).thenReturn(homeFacilityId); when(authenticationHelper.getCurrentUser()).thenReturn(userDto); } - + private void mockFacilityDto() { homeFacilityDto = mock(FacilityDto.class); @@ -112,4 +145,4 @@ private void mockFacilityDto() { when(facilityService.findOne(homeFacilityId)).thenReturn(homeFacilityDto); } -} \ No newline at end of file +}