Skip to content

Commit

Permalink
OLMIS-7952: Added test coverage and corrected method description.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsznaj committed Jul 9, 2024
1 parent 09b3e72 commit d62eb5b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

@Service
public class HomeFacilityPermissionService {

static final String WS_TYPE_CODE = "WS";

@Autowired
private AuthenticationHelper authenticationHelper;

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -78,4 +81,4 @@ public boolean checkFacilityAndHomeFacilityLinkage(UUID facilityId) {
private void throwException(String errorKey, String... params) {
throw new PermissionMessageException(new Message(errorKey, (Object)params));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -57,10 +61,10 @@ public class HomeFacilityPermissionServiceTest {

@Mock
private OAuth2Authentication authentication;

@Mock
private FacilityReferenceDataService facilityService;

private UUID homeFacilityId;
private UUID programId;
private UUID facilityTypeId;
Expand Down Expand Up @@ -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);

Expand All @@ -112,4 +145,4 @@ private void mockFacilityDto() {

when(facilityService.findOne(homeFacilityId)).thenReturn(homeFacilityDto);
}
}
}

0 comments on commit d62eb5b

Please sign in to comment.