Base test classes enhancements for gateway test configuration flexibility (#5821)

* Allow tests to set paging provider by server

* Allow tests to set target server port range

---------

Co-authored-by: juan.marchionatto <juan.marchionatto@smilecdr.com>
This commit is contained in:
jmarchionatto 2024-04-16 13:51:50 -04:00 committed by GitHub
parent 80d382d8cd
commit a90ccde71c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 23 deletions

View File

@ -28,24 +28,25 @@ import ca.uhn.fhir.rest.server.HardcodedServerAddressStrategy;
import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.IServerAddressStrategy; import ca.uhn.fhir.rest.server.IServerAddressStrategy;
import ca.uhn.fhir.tls.KeyStoreType; import ca.uhn.fhir.tls.KeyStoreType;
import jakarta.servlet.Servlet;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.extension.RegisterExtension;
import jakarta.servlet.Servlet;
import java.security.KeyStore; import java.security.KeyStore;
import java.util.List; import java.util.List;
public abstract class BaseRestServerHelper { public abstract class BaseRestServerHelper {
private static int myFirstTargetPort = -1;
private final String SERVER_KEYSTORE_PATH = "/tls/server-keystore.p12"; private final String SERVER_KEYSTORE_PATH = "/tls/server-keystore.p12";
private final String SERVER_TRUSTSTORE_PATH = "/tls/server-truststore.p12"; private final String SERVER_TRUSTSTORE_PATH = "/tls/server-truststore.p12";
@ -75,9 +76,11 @@ public abstract class BaseRestServerHelper {
} }
protected void startServer(Servlet theServlet) throws Exception { protected void startServer(Servlet theServlet) throws Exception {
myListenerServer = new Server(0);
myFhirContext.getRestfulClientFactory().setSocketTimeout(120000); int port = myFirstTargetPort == -1 ? 0 : myFirstTargetPort++;
myListenerServer = new Server(port);
myFhirContext.getRestfulClientFactory().setSocketTimeout(120_000);
ServletContextHandler proxyHandler = new ServletContextHandler(); ServletContextHandler proxyHandler = new ServletContextHandler();
proxyHandler.setContextPath("/"); proxyHandler.setContextPath("/");
@ -119,7 +122,7 @@ public abstract class BaseRestServerHelper {
myHttpsListenerPort = ((ServerConnector)myListenerServer.getConnectors()[1]).getLocalPort(); myHttpsListenerPort = ((ServerConnector)myListenerServer.getConnectors()[1]).getLocalPort();
} }
private SslContextFactory.Server getSslContextFactory() throws Exception{ private SslContextFactory.Server getSslContextFactory() {
try { try {
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
@ -199,5 +202,9 @@ public abstract class BaseRestServerHelper {
setServerAddressStrategy(strategy); setServerAddressStrategy(strategy);
} }
public static void setMyFirstTargetPort(int theMyFirstTargetPort) {
myFirstTargetPort = theMyFirstTargetPort;
}
protected abstract void setServerAddressStrategy(IServerAddressStrategy theServerAddressStrategy); protected abstract void setServerAddressStrategy(IServerAddressStrategy theServerAddressStrategy);
} }

View File

@ -28,13 +28,16 @@ import ca.uhn.fhir.rest.api.PagingHttpMethodEnum;
import ca.uhn.fhir.rest.api.RequestTypeEnum; import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.server.FifoMemoryPagingProvider; import ca.uhn.fhir.rest.server.FifoMemoryPagingProvider;
import ca.uhn.fhir.rest.server.IPagingProvider;
import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.IServerAddressStrategy; import ca.uhn.fhir.rest.server.IServerAddressStrategy;
import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
import ca.uhn.fhir.rest.server.provider.HashMapResourceProvider; import ca.uhn.fhir.rest.server.provider.HashMapResourceProvider;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee10.servlet.ServletApiRequest; import org.eclipse.jetty.ee10.servlet.ServletApiRequest;
import org.eclipse.jetty.server.Request;
import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
@ -48,9 +51,6 @@ import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -61,7 +61,8 @@ import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEachCallback, AfterEachCallback { public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEachCallback, AfterEachCallback {
protected final MyRestfulServer myRestServer; private final MyRestfulServer myRestServer;
private static IPagingProvider myPagingProvider = new FifoMemoryPagingProvider(20);
public RestServerR4Helper() { public RestServerR4Helper() {
this(false, false); this(false, false);
@ -216,6 +217,10 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa
myRestServer.setConceptMapResourceProvider(theResourceProvider); myRestServer.setConceptMapResourceProvider(theResourceProvider);
} }
public void setPagingProvider(IPagingProvider thePagingProvider) {
myPagingProvider = thePagingProvider;
}
@Override @Override
public IIdType createPatientWithId(String theId) { public IIdType createPatientWithId(String theId) {
Patient patient = new Patient(); Patient patient = new Patient();
@ -331,8 +336,7 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa
public void clearCounts() { public void clearCounts() {
for (IResourceProvider next : getResourceProviders()) { for (IResourceProvider next : getResourceProviders()) {
if (next instanceof HashMapResourceProvider) { if (next instanceof HashMapResourceProvider<?> provider) {
HashMapResourceProvider provider = (HashMapResourceProvider) next;
provider.clearCounts(); provider.clearCounts();
} }
} }
@ -372,8 +376,7 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa
public void clearDataAndCounts() { public void clearDataAndCounts() {
for (IResourceProvider next : getResourceProviders()) { for (IResourceProvider next : getResourceProviders()) {
if (next instanceof HashMapResourceProvider) { if (next instanceof HashMapResourceProvider<?> provider) {
HashMapResourceProvider provider = (HashMapResourceProvider) next;
provider.clear(); provider.clear();
} }
} }
@ -385,7 +388,7 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa
} }
public void setObservationResourceProvider(HashMapResourceProvider<Observation> theResourceProvider) { public void setObservationResourceProvider(HashMapResourceProvider<Observation> theResourceProvider) {
myObservationResourceProvider.getStoredResources().forEach(o -> theResourceProvider.store(o)); myObservationResourceProvider.getStoredResources().forEach(theResourceProvider::store);
unregisterProvider(myObservationResourceProvider); unregisterProvider(myObservationResourceProvider);
registerProvider(theResourceProvider); registerProvider(theResourceProvider);
@ -401,7 +404,7 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa
} }
public void setConceptMapResourceProvider(HashMapResourceProvider<ConceptMap> theResourceProvider) { public void setConceptMapResourceProvider(HashMapResourceProvider<ConceptMap> theResourceProvider) {
myConceptMapResourceProvider.getStoredResources().forEach(c -> theResourceProvider.store(c)); myConceptMapResourceProvider.getStoredResources().forEach(theResourceProvider::store);
unregisterProvider(myConceptMapResourceProvider); unregisterProvider(myConceptMapResourceProvider);
registerProvider(theResourceProvider); registerProvider(theResourceProvider);
@ -417,23 +420,23 @@ public class RestServerR4Helper extends BaseRestServerHelper implements BeforeEa
super.initialize(); super.initialize();
FhirContext fhirContext = getFhirContext(); FhirContext fhirContext = getFhirContext();
myPatientResourceProvider = new MyHashMapResourceProvider(fhirContext, Patient.class); myPatientResourceProvider = new MyHashMapResourceProvider<>(fhirContext, Patient.class);
registerProvider(myPatientResourceProvider); registerProvider(myPatientResourceProvider);
myObservationResourceProvider = new MyHashMapResourceProvider(fhirContext, Observation.class); myObservationResourceProvider = new MyHashMapResourceProvider<>(fhirContext, Observation.class);
registerProvider(myObservationResourceProvider); registerProvider(myObservationResourceProvider);
myOrganizationResourceProvider = new MyHashMapResourceProvider(fhirContext, Organization.class); myOrganizationResourceProvider = new MyHashMapResourceProvider<>(fhirContext, Organization.class);
registerProvider(myOrganizationResourceProvider); registerProvider(myOrganizationResourceProvider);
myConceptMapResourceProvider = new MyHashMapResourceProvider(fhirContext, ConceptMap.class); myConceptMapResourceProvider = new MyHashMapResourceProvider<>(fhirContext, ConceptMap.class);
registerProvider(myConceptMapResourceProvider); registerProvider(myConceptMapResourceProvider);
myPlainProvider = new RestServerDstu3Helper.MyPlainProvider(myInitialTransactionLatchEnabled); myPlainProvider = new RestServerDstu3Helper.MyPlainProvider(myInitialTransactionLatchEnabled);
registerProvider(myPlainProvider); registerProvider(myPlainProvider);
setPagingProvider(new FifoMemoryPagingProvider(20)); setPagingProvider(myPagingProvider);
} }
public class MyHashMapResourceProvider<T extends IBaseResource> extends HashMapResourceProvider<T> { public class MyHashMapResourceProvider<T extends IBaseResource> extends HashMapResourceProvider<T> {
public MyHashMapResourceProvider(FhirContext theContext, Class theType) { public MyHashMapResourceProvider(FhirContext theContext, Class<T> theType) {
super(theContext, theType); super(theContext, theType);
} }