fixing tests

This commit is contained in:
leif stawnyczy 2023-08-18 09:44:57 -04:00
parent 071138f4b8
commit de7a00ef69
4 changed files with 53 additions and 37 deletions

View File

@ -64,15 +64,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Nonnull;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class PersistedJpaBundleProvider implements IBundleProvider {

View File

@ -26,12 +26,12 @@ import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public interface IBundleProvider {
@ -167,7 +167,7 @@ public interface IBundleProvider {
* @return A list of resources. The size of this list must be at least <code>theToIndex - theFromIndex</code>.
*/
default List<IBaseResource> getResources(
int theFromIndex, int theToIndex, @Nonnull ResponsePage.ResponsePageBuilder theResponsePageBuilder) {
int theFromIndex, int theToIndex, @Nonnull ResponsePage.ResponsePageBuilder theResponsePageBuilder) {
return getResources(theFromIndex, theToIndex);
}
@ -184,8 +184,8 @@ public interface IBundleProvider {
Integer size = size();
if (size == null) {
throw new ConfigurationException(
Msg.code(464)
+ "Attempt to request all resources from an asynchronous search result. The SearchParameterMap for this search probably should have been synchronous.");
Msg.code(464)
+ "Attempt to request all resources from an asynchronous search result. The SearchParameterMap for this search probably should have been synchronous.");
}
if (size > 0) {
retval.addAll(getResources(0, size));
@ -258,7 +258,7 @@ public interface IBundleProvider {
*/
default List<String> getAllResourceIds() {
return getAllResources().stream()
.map(resource -> resource.getIdElement().getIdPart())
.collect(Collectors.toList());
.map(resource -> resource.getIdElement().getIdPart())
.collect(Collectors.toList());
}
}

View File

@ -36,7 +36,6 @@ import java.util.List;
public class ResponsePage {
private static final Logger ourLog = LoggerFactory.getLogger(ResponsePage.class);
/**
* The id of the search used to page through search results
*/
@ -127,15 +126,27 @@ public class ResponsePage {
return myResourceList;
}
private boolean isBundleProviderOffsetPaging() {
if (myBundleProvider != null) {
if (myBundleProvider.getCurrentPageOffset() != null) {
// it's not enough that currentpageoffset is not null
// (sometimes it's 0, even if it's not a currentpageoffset search)
// so we have to make sure either next or prev links are not null
return (StringUtils.isNotBlank(myBundleProvider.getNextPageId())
|| StringUtils.isNotBlank(myBundleProvider.getPreviousPageId()));
}
}
return false;
}
private void determinePagingStyle() {
if (myPagingStyle != null) {
// already assigned
return;
}
if (myBundleProvider != null && (myBundleProvider.getCurrentPageOffset() != null
|| (StringUtils.isNotBlank(myBundleProvider.getNextPageId()) || StringUtils.isNotBlank(myBundleProvider.getPreviousPageId())))
) {
if (isBundleProviderOffsetPaging()) {
myPagingStyle = PagingStyle.BUNDLE_PROVIDER_OFFSETS;
} else if (myIsUsingOffsetPages) {
myPagingStyle = PagingStyle.NONCACHED_OFFSET;
@ -146,8 +157,8 @@ public class ResponsePage {
} else {
myPagingStyle = PagingStyle.UNKNOWN;
// only our unit tests end up here
ourLog.warn("Response page requires more information to determine paging style. " +
"Did you remember to mock out all proper values?");
ourLog.warn("Response page requires more information to determine paging style. "
+ "Did you remember to mock out all proper values?");
}
}
@ -390,7 +401,7 @@ public class ResponsePage {
myNumTotalResults, // total results
myIncludedResourceCount, // included count
myOmittedResourceCount // omitted resources
);
);
}
}

View File

@ -4,6 +4,7 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.server.method.ResponsePage;
import ca.uhn.fhir.test.utilities.JettyUtil;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Lists;
@ -60,28 +61,32 @@ public class SearchBundleProviderWithNoSizeR4Test {
@Test
public void testBundleProviderReturnsNoSize() throws Exception {
Bundle respBundle;
ourLastBundleProvider = mock(IBundleProvider.class);
when(ourLastBundleProvider.getCurrentPageOffset()).thenReturn(null);
when(ourLastBundleProvider.size()).thenReturn(null);
when(ourLastBundleProvider.getResources(any(int.class), any(int.class))).then(new Answer<List<IBaseResource>>() {
@Override
public List<IBaseResource> answer(InvocationOnMock theInvocation) {
int from =(Integer)theInvocation.getArguments()[0];
int to =(Integer)theInvocation.getArguments()[1];
ArrayList<IBaseResource> retVal = Lists.newArrayList();
for (int i = from; i < to; i++) {
Patient p = new Patient();
p.setId(Integer.toString(i));
retVal.add(p);
when(ourLastBundleProvider.getResponsePageBuilder())
.thenReturn(new ResponsePage.ResponsePageBuilder());
when(ourLastBundleProvider.getResources(any(int.class), any(int.class), any(ResponsePage.ResponsePageBuilder.class)))
.then(new Answer<List<IBaseResource>>() {
@Override
public List<IBaseResource> answer(InvocationOnMock theInvocation) {
int from = (Integer) theInvocation.getArguments()[0];
int to = (Integer) theInvocation.getArguments()[1];
ArrayList<IBaseResource> retVal = Lists.newArrayList();
for (int i = from; i < to; i++) {
Patient p = new Patient();
p.setId(Integer.toString(i));
retVal.add(p);
}
return retVal;
}
return retVal;
}});
});
HttpGet httpGet;
CloseableHttpResponse status = null;
BundleLinkComponent linkNext;
try {
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_format=json");
status = ourClient.execute(httpGet);
@ -90,17 +95,17 @@ public class SearchBundleProviderWithNoSizeR4Test {
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("searchAll", ourLastMethod);
respBundle = ourCtx.newJsonParser().parseResource(Bundle.class, responseContent);
assertEquals(10, respBundle.getEntry().size());
assertEquals("Patient/0", respBundle.getEntry().get(0).getResource().getIdElement().toUnqualifiedVersionless().getValue());
linkNext = respBundle.getLink("next");
assertNotNull(linkNext);
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
when(ourLastBundleProvider.size()).thenReturn(25);
try {
@ -111,7 +116,7 @@ public class SearchBundleProviderWithNoSizeR4Test {
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("searchAll", ourLastMethod);
respBundle = ourCtx.newJsonParser().parseResource(Bundle.class, responseContent);
assertEquals(10, respBundle.getEntry().size());
assertEquals("Patient/10", respBundle.getEntry().get(0).getResource().getIdElement().toUnqualifiedVersionless().getValue());
linkNext = respBundle.getLink("next");
@ -129,7 +134,7 @@ public class SearchBundleProviderWithNoSizeR4Test {
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("searchAll", ourLastMethod);
respBundle = ourCtx.newJsonParser().parseResource(Bundle.class, responseContent);
assertEquals(5, respBundle.getEntry().size());
assertEquals("Patient/20", respBundle.getEntry().get(0).getResource().getIdElement().toUnqualifiedVersionless().getValue());
linkNext = respBundle.getLink("next");