Skip to content

Commit

Permalink
Test Location Hierarchy Flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
lincmba committed Apr 5, 2024
1 parent d28722a commit e3c8fa8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.hl7.fhir.r4.model.Resource;

public abstract class BaseEndpoint extends HttpServlet {
public Bundle createBundle(List<Resource> resourceList) {
public static Bundle createBundle(List<Resource> resourceList) {
Bundle responseBundle = new Bundle();
List<Bundle.BundleEntryComponent> bundleEntryComponentList = new ArrayList<>();

Expand All @@ -24,7 +24,7 @@ public Bundle createBundle(List<Resource> resourceList) {
return responseBundle;
}

public Bundle createEmptyBundle(String requestURL) {
public static Bundle createEmptyBundle(String requestURL) {
Bundle responseBundle = new Bundle();
responseBundle.setId(UUID.randomUUID().toString());
Bundle.BundleLinkComponent linkComponent = new Bundle.BundleLinkComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import static org.mockito.Mockito.mock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Location;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -55,6 +60,50 @@ public void testGetLocationHierarchyFound() {
assertEquals("Location Resource : 12345", locationHierarchy.getId());
}

@Test
public void testGetPaginatedLocationsPaginatesLocations() {
HttpServletRequest request = mock(HttpServletRequest.class);
Mockito.doReturn("12345").when(request).getParameter(Constants.IDENTIFIER);
Mockito.doReturn("2").when(request).getParameter(Constants.PAGINATION_PAGE_SIZE);
Mockito.doReturn("2").when(request).getParameter(Constants.PAGINATION_PAGE_NUMBER);
Mockito.doReturn(new StringBuffer("http://test:8080/LocationHierarchy"))
.when(request)
.getRequestURL();

Map<String, String[]> parameters = new HashMap<>();
// Populate the HashMap with the specified parameters
parameters.put(Constants.IDENTIFIER, new String[] {"12345"});
parameters.put(Constants.PAGINATION_PAGE_SIZE, new String[] {"2"});
parameters.put(Constants.PAGINATION_PAGE_NUMBER, new String[] {"2"});

Mockito.doReturn(parameters).when(request).getParameterMap();

LocationHierarchyEndpointHelper mockLocationHierarchyEndpointHelper =
mock(LocationHierarchyEndpointHelper.class);
List<Location> locations = createLocationList(5);

Mockito.doCallRealMethod()
.when(mockLocationHierarchyEndpointHelper)
.getPaginatedLocations(request);
Mockito.doReturn(locations)
.when(mockLocationHierarchyEndpointHelper)
.getDescendants("12345", null);

Bundle resultBundle = mockLocationHierarchyEndpointHelper.getPaginatedLocations(request);

Assert.assertTrue(resultBundle.hasEntry());
Assert.assertTrue(resultBundle.hasLink());
Assert.assertTrue(resultBundle.hasTotal());
Assert.assertEquals(2, resultBundle.getEntry().size());
Assert.assertEquals(2, resultBundle.getLink().size());
Assert.assertEquals(
"http://test:8080/LocationHierarchy?identifier=12345&_page=1&_count=2",
resultBundle.getLink("previous").getUrl());
Assert.assertEquals(
"http://test:8080/LocationHierarchy?identifier=12345&_page=2&_count=2",
resultBundle.getLink("self").getUrl());
}

private Bundle getLocationBundle() {
Bundle bundleLocation = new Bundle();
bundleLocation.setId("Location/1234");
Expand All @@ -70,4 +119,15 @@ private Bundle getLocationBundle() {
bundleLocation.addEntry(bundleEntryComponent);
return bundleLocation;
}

private List<Location> createLocationList(int numLocations) {
// Create a list of locations
List<Location> locations = new ArrayList<>();
for (int i = 0; i < numLocations; i++) {
Location location = new Location();
location.setId(Integer.toString(i));
locations.add(location);
}
return locations;
}
}

0 comments on commit e3c8fa8

Please sign in to comment.