Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lincmba committed Nov 21, 2024
1 parent d833ad0 commit 274e0c1
Showing 1 changed file with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -29,6 +30,7 @@
import org.mockito.Mockito;
import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs;
import org.smartregister.model.location.LocationHierarchy;
import org.smartregister.model.location.LocationHierarchyTree;

import com.auth0.jwt.interfaces.DecodedJWT;

Expand Down Expand Up @@ -457,6 +459,114 @@ public void testGetPaginatedLocationsSummaryReturnsSummary() {
Assert.assertEquals(5, resultBundle.getTotal());
}

@Test
public void
testHandleNonIdentifierRequestNonListModeWithSelectedLocationsReturnsLocationHierarchies() {
HttpServletRequest request = mock(HttpServletRequest.class);
Mockito.doReturn("1,2,3,4")
.when(request)
.getParameter(Constants.SYNC_LOCATIONS_SEARCH_PARAM);
Mockito.doReturn(new StringBuffer("http://test:8080/LocationHierarchy"))
.when(request)
.getRequestURL();
Map<String, String[]> parameters = new HashMap<>();
parameters.put(Constants.SYNC_LOCATIONS_SEARCH_PARAM, new String[] {"1,2,3,4"});
LocationHierarchyEndpointHelper mockLocationHierarchyEndpointHelper =
mock(LocationHierarchyEndpointHelper.class);
PractitionerDetailsEndpointHelper mockPractitionerDetailsEndpointHelper =
mock(PractitionerDetailsEndpointHelper.class);
DecodedJWT mockDecodedJWT = mock(DecodedJWT.class);
MockedStatic<JwtUtils> mockJwtUtils = Mockito.mockStatic(JwtUtils.class);

List<Location> locations = createTestLocationList(4, false, false);
LocationHierarchy locationHierarchy = createLocationHierarchy(locations);
List<LocationHierarchy> locationHierarchies = new ArrayList<>();
locationHierarchies.add(locationHierarchy);

List<String> userRoles = Collections.singletonList(Constants.ROLE_ALL_LOCATIONS);

Mockito.doReturn(parameters).when(request).getParameterMap();
Mockito.doCallRealMethod()
.when(mockLocationHierarchyEndpointHelper)
.extractSyncLocations("1,2,3,4");
Mockito.doCallRealMethod()
.when(mockLocationHierarchyEndpointHelper)
.handleNonIdentifierRequest(
request, mockPractitionerDetailsEndpointHelper, mockDecodedJWT);
Mockito.doReturn(locationHierarchies)
.when(mockLocationHierarchyEndpointHelper)
.getLocationHierarchies(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
Mockito.doReturn(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.when(mockLocationHierarchyEndpointHelper)
.getSyncStrategyByAppId(Mockito.any());
mockJwtUtils
.when(() -> JwtUtils.getUserRolesFromJWT(any(DecodedJWT.class)))
.thenReturn(userRoles);
Bundle resultBundle =
mockLocationHierarchyEndpointHelper.handleNonIdentifierRequest(
request, mockPractitionerDetailsEndpointHelper, mockDecodedJWT);
Assert.assertEquals(1, resultBundle.getTotal());
Assert.assertEquals(1, resultBundle.getEntry().size());
mockJwtUtils.close();
}

@Test
public void
testHandleNonIdentifierRequestNonListModeWithoutSelectedLocationsReturnsLocationHierarchies() {
HttpServletRequest request = mock(HttpServletRequest.class);
Mockito.doReturn("1,2,3,4")
.when(request)
.getParameter(Constants.SYNC_LOCATIONS_SEARCH_PARAM);
Mockito.doReturn(new StringBuffer("http://test:8080/LocationHierarchy"))
.when(request)
.getRequestURL();
Map<String, String[]> parameters = new HashMap<>();
parameters.put(Constants.SYNC_LOCATIONS_SEARCH_PARAM, new String[] {"1,2,3,4"});
LocationHierarchyEndpointHelper mockLocationHierarchyEndpointHelper =
mock(LocationHierarchyEndpointHelper.class);
PractitionerDetailsEndpointHelper mockPractitionerDetailsEndpointHelper =
mock(PractitionerDetailsEndpointHelper.class);
DecodedJWT mockDecodedJWT = mock(DecodedJWT.class);
MockedStatic<JwtUtils> mockJwtUtils = Mockito.mockStatic(JwtUtils.class);
List<Location> locations = createTestLocationList(4, false, false);
LocationHierarchy locationHierarchy = createLocationHierarchy(locations);
List<LocationHierarchy> locationHierarchies = new ArrayList<>();
locationHierarchies.add(locationHierarchy);
List<String> userRoles = Collections.singletonList(Constants.ROLE_ALL_LOCATIONS);
Mockito.doReturn(parameters).when(request).getParameterMap();
Mockito.doReturn(Collections.emptyList())
.when(mockLocationHierarchyEndpointHelper)
.extractSyncLocations(Mockito.any());
Mockito.doCallRealMethod()
.when(mockLocationHierarchyEndpointHelper)
.handleNonIdentifierRequest(
request, mockPractitionerDetailsEndpointHelper, mockDecodedJWT);
Mockito.doReturn(locationHierarchies)
.when(mockLocationHierarchyEndpointHelper)
.getLocationHierarchies(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
Mockito.doReturn(Arrays.asList("1", "2", "3", "4"))
.when(mockPractitionerDetailsEndpointHelper)
.getPractitionerLocationIdsByByKeycloakId(Mockito.any());

Mockito.doReturn(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.when(mockLocationHierarchyEndpointHelper)
.getSyncStrategyByAppId(Mockito.any());

mockJwtUtils
.when(() -> JwtUtils.getUserRolesFromJWT(any(DecodedJWT.class)))
.thenReturn(userRoles);

Bundle resultBundle =
mockLocationHierarchyEndpointHelper.handleNonIdentifierRequest(
request, mockPractitionerDetailsEndpointHelper, mockDecodedJWT);

Assert.assertEquals(1, resultBundle.getTotal());
Assert.assertEquals(1, resultBundle.getEntry().size());
mockJwtUtils.close();
}

@Test
public void testFilterLocationsByLastUpdatedBasic() {
List<Location> locations = createTestLocationList(5, true, true);
Expand Down Expand Up @@ -534,4 +644,12 @@ public static List<Location> createTestLocationList(
}
return locations;
}

public static LocationHierarchy createLocationHierarchy(List<Location> locations) {
LocationHierarchy locationHierarchy = new LocationHierarchy();
LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree();
locationHierarchyTree.buildTreeFromList(locations);
locationHierarchy.setLocationHierarchyTree(locationHierarchyTree);
return locationHierarchy;
}
}

0 comments on commit 274e0c1

Please sign in to comment.