rewrote unit tests

This commit is contained in:
jvi
2020-09-07 23:41:04 +02:00
parent 8894711d8e
commit d208f12546
14 changed files with 302 additions and 342 deletions

21
pom.xml
View File

@@ -271,13 +271,26 @@
</dependency> </dependency>
<!-- TODO: remove this and migrate to JU5 -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>4.12</version> <version>5.6.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

View File

@@ -4,7 +4,6 @@ import ca.uhn.fhir.empi.api.IEmpiSettings;
import ca.uhn.fhir.empi.rules.config.EmpiRuleValidator; import ca.uhn.fhir.empi.rules.config.EmpiRuleValidator;
import ca.uhn.fhir.empi.rules.config.EmpiSettings; import ca.uhn.fhir.empi.rules.config.EmpiSettings;
import ca.uhn.fhir.jpa.starter.AppProperties; import ca.uhn.fhir.jpa.starter.AppProperties;
import ca.uhn.fhir.jpa.starter.HapiProperties;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;

View File

@@ -26,11 +26,14 @@ public class FhirServerConfigDstu2 extends BaseJavaConfigDstu2 {
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired
AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider(); DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider();
pagingProvider.setDefaultPageSize(HapiProperties.getDefaultPageSize()); pagingProvider.setDefaultPageSize(appProperties.getDefault_page_size());
pagingProvider.setMaximumPageSize(HapiProperties.getMaximumPageSize()); pagingProvider.setMaximumPageSize(appProperties.getMax_page_size());
return pagingProvider; return pagingProvider;
} }
@@ -46,7 +49,7 @@ public class FhirServerConfigDstu2 extends BaseJavaConfigDstu2 {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e); throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
retVal.setJpaProperties(HapiProperties.getJpaProperties());
return retVal; return retVal;
} }

View File

@@ -26,11 +26,14 @@ public class FhirServerConfigDstu3 extends BaseJavaConfigDstu3 {
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired
AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider(); DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider();
pagingProvider.setDefaultPageSize(HapiProperties.getDefaultPageSize()); pagingProvider.setDefaultPageSize(appProperties.getDefault_page_size());
pagingProvider.setMaximumPageSize(HapiProperties.getMaximumPageSize()); pagingProvider.setMaximumPageSize(appProperties.getMax_page_size());
return pagingProvider; return pagingProvider;
} }
@@ -46,7 +49,7 @@ public class FhirServerConfigDstu3 extends BaseJavaConfigDstu3 {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e); throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
retVal.setJpaProperties(HapiProperties.getJpaProperties());
return retVal; return retVal;
} }

View File

@@ -26,11 +26,14 @@ public class FhirServerConfigR4 extends BaseJavaConfigR4 {
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired
AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider(); DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider();
pagingProvider.setDefaultPageSize(HapiProperties.getDefaultPageSize()); pagingProvider.setDefaultPageSize(appProperties.getDefault_page_size());
pagingProvider.setMaximumPageSize(HapiProperties.getMaximumPageSize()); pagingProvider.setMaximumPageSize(appProperties.getMax_page_size());
return pagingProvider; return pagingProvider;
} }
@@ -46,7 +49,7 @@ public class FhirServerConfigR4 extends BaseJavaConfigR4 {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e); throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
retVal.setJpaProperties(HapiProperties.getJpaProperties());
return retVal; return retVal;
} }

View File

@@ -26,14 +26,16 @@ public class FhirServerConfigR5 extends BaseJavaConfigR5 {
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired
AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider(); DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider();
pagingProvider.setDefaultPageSize(HapiProperties.getDefaultPageSize()); pagingProvider.setDefaultPageSize(appProperties.getDefault_page_size());
pagingProvider.setMaximumPageSize(HapiProperties.getMaximumPageSize()); pagingProvider.setMaximumPageSize(appProperties.getMax_page_size());
return pagingProvider; return pagingProvider;
} }
@Override @Override
@Bean() @Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

View File

@@ -7,13 +7,13 @@ import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan(basePackageClasses = {JpaRestfulServer.class}) @ServletComponentScan(basePackageClasses = {JpaRestfulServer.class})
@SpringBootApplication(exclude = ElasticsearchRestClientAutoConfiguration.class) @SpringBootApplication(exclude = ElasticsearchRestClientAutoConfiguration.class)
public class Application { public class Demo {
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("spring.profiles.active", "r4"); System.setProperty("spring.profiles.active", "r4");
System.setProperty("spring.batch.job.enabled", "false"); System.setProperty("spring.batch.job.enabled", "false");
SpringApplication.run(Application.class, args); SpringApplication.run(Demo.class, args);
//Server is now accessible at eg. http://localhost:8080/metadata //Server is now accessible at eg. http://localhost:8080/metadata
} }

View File

@@ -5,80 +5,54 @@ import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.test.utilities.JettyUtil;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.nio.file.Paths; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties =
{
"spring.batch.job.enabled=false",
"spring.profiles.active=dstu2",
"spring.datasource.url=jdbc:h2:mem:dbr2"
})
public class ExampleServerDstu2IT { public class ExampleServerDstu2IT {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private static IGenericClient ourClient; private IGenericClient ourClient;
private static FhirContext ourCtx; private FhirContext ourCtx;
private static int ourPort;
private static Server ourServer;
static { @LocalServerPort
ourCtx = FhirContext.forDstu2(); private int port;
System.setProperty("spring.profiles.active", "dstu2");
System.setProperty("spring.batch.job.enabled", "false");
System.setProperty("spring.datasource.url", "jdbc:h2:mem:dbr2");
}
@Test @Test
public void testCreateAndRead() { void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional"; String methodName = "testCreateResourceConditional";
Patient pt = new Patient(); Patient pt = new Patient();
pt.addName().addFamily(methodName); pt.addName().addFamily(methodName);
IIdType id = ourClient.create().resource(pt).execute().getId(); IIdType id = ourClient.create().resource(pt).execute().getId();
Patient pt2 = ourClient.read().resource(Patient.class).withId(id).execute(); Patient pt2 = ourClient.read().resource(Patient.class).withId(id).execute();
assertEquals(methodName, pt2.getName().get(0).getFamily().get(0).getValue()); assertEquals(methodName, pt2.getName().get(0).getFamily().get(0).getValue());
} }
@AfterAll
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeAll @BeforeEach
public static void beforeClass() throws Exception { void beforeEach() {
String path = Paths.get("").toAbsolutePath().toString();
ourLog.info("Project base path is: {}", path);
ourServer = new Server(0);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
ourServer.setHandler(webAppContext);
ourServer.start();
ourPort = JettyUtil.getPortForStartedServer(ourServer);
ourCtx = FhirContext.forDstu2();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = "http://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/"; String ourServerBase = "http://localhost:" + port + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase); ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true)); ourClient.registerInterceptor(new LoggingInterceptor(true));
} }
public static void main(String[] theArgs) throws Exception {
ourPort = 8080;
beforeClass();
}
} }

View File

@@ -7,9 +7,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.test.utilities.JettyUtil;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
@@ -18,39 +15,44 @@ import org.hl7.fhir.dstu3.model.Observation;
import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Subscription; import org.hl7.fhir.dstu3.model.Subscription;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.net.URI; import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static ca.uhn.fhir.util.TestUtil.waitForSize; import static ca.uhn.fhir.util.TestUtil.waitForSize;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties =
{
"spring.batch.job.enabled=false",
"spring.profiles.active=dstu3",
"spring.datasource.url=jdbc:h2:mem:dbr3",
"hapi.fhir.subscription.websocket_enabled=true",
"hapi.fhir.allow_external_references=true",
"hapi.fhir.allow_placeholder_references=true",
})
public class ExampleServerDstu3IT { public class ExampleServerDstu3IT {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu3IT.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private static IGenericClient ourClient; private IGenericClient ourClient;
private static FhirContext ourCtx; private FhirContext ourCtx;
private static int ourPort;
private static Server ourServer;
static { @LocalServerPort
HapiProperties.forceReload(); private int port;
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "DSTU3");
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:h2:mem:dbr3");
HapiProperties.setProperty(HapiProperties.SUBSCRIPTION_WEBSOCKET_ENABLED, "true");
HapiProperties.setProperty(HapiProperties.ALLOW_EXTERNAL_REFERENCES, "true");
HapiProperties.setProperty(HapiProperties.ALLOW_PLACEHOLDER_REFERENCES, "true");
ourCtx = FhirContext.forDstu3();
}
@Test @Test
public void testCreateAndRead() { public void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional"; String methodName = "testCreateResourceConditional";
Patient pt = new Patient(); Patient pt = new Patient();
@@ -90,7 +92,7 @@ public class ExampleServerDstu3IT {
SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON); SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON);
myWebSocketClient.start(); myWebSocketClient.start();
URI echoUri = new URI("ws://localhost:" + ourPort + "/hapi-fhir-jpaserver/websocket"); URI echoUri = new URI("ws://localhost:" + port + "/hapi-fhir-jpaserver/websocket");
ClientUpgradeRequest request = new ClientUpgradeRequest(); ClientUpgradeRequest request = new ClientUpgradeRequest();
ourLog.info("Connecting to : {}", echoUri); ourLog.info("Connecting to : {}", echoUri);
Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request); Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request);
@@ -119,39 +121,15 @@ public class ExampleServerDstu3IT {
ourClient.delete().resourceById(mySubscriptionId).execute(); ourClient.delete().resourceById(mySubscriptionId).execute();
} }
@AfterAll @BeforeEach
public static void afterClass() throws Exception { void beforeEach() {
ourServer.stop();
}
@BeforeAll
public static void beforeClass() throws Exception {
String path = Paths.get("").toAbsolutePath().toString();
ourLog.info("Project base path is: {}", path);
ourServer = new Server(0);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
ourServer.setHandler(webAppContext);
ourServer.start();
ourPort = JettyUtil.getPortForStartedServer(ourServer);
ourCtx = FhirContext.forDstu3();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = "http://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/"; String ourServerBase = "http://localhost:" + port + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase); ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true)); ourClient.registerInterceptor(new LoggingInterceptor(true));
} }
public static void main(String[] theArgs) throws Exception {
ourPort = 8080;
beforeClass();
}
} }

View File

@@ -7,26 +7,20 @@ import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.test.utilities.JettyUtil;
import ca.uhn.fhir.util.BundleUtil; import ca.uhn.fhir.util.BundleUtil;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.Observation; import org.junit.jupiter.api.BeforeEach;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Person;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.Subscription;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.net.URI; import java.net.URI;
import java.nio.file.Paths;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -38,26 +32,28 @@ import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties =
{
"spring.batch.job.enabled=false",
"spring.profiles.active=r5",
"spring.datasource.url=jdbc:h2:mem:dbr5",
"hapi.fhir.subscription.websocket_enabled=true",
"hapi.fhir.empi_enabled=true"
})
public class ExampleServerR4IT { public class ExampleServerR4IT {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerR4IT.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private static IGenericClient ourClient; private IGenericClient ourClient;
private static FhirContext ourCtx; private FhirContext ourCtx;
private static int ourPort;
private static Server ourServer; @LocalServerPort
private int port;
static {
HapiProperties.forceReload();
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:h2:mem:dbr4");
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "R4");
HapiProperties.setProperty(HapiProperties.SUBSCRIPTION_WEBSOCKET_ENABLED, "true");
HapiProperties.setProperty(HapiProperties.EMPI_ENABLED, "true");
ourCtx = FhirContext.forR4();
}
@Test @Test
public void testCreateAndRead() { void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional"; String methodName = "testCreateResourceConditional";
Patient pt = new Patient(); Patient pt = new Patient();
@@ -121,7 +117,7 @@ public class ExampleServerR4IT {
SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON); SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON);
myWebSocketClient.start(); myWebSocketClient.start();
URI echoUri = new URI("ws://localhost:" + ourPort + "/hapi-fhir-jpaserver/websocket"); URI echoUri = new URI("ws://localhost:" + port + "/hapi-fhir-jpaserver/websocket");
ClientUpgradeRequest request = new ClientUpgradeRequest(); ClientUpgradeRequest request = new ClientUpgradeRequest();
ourLog.info("Connecting to : {}", echoUri); ourLog.info("Connecting to : {}", echoUri);
Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request); Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request);
@@ -154,42 +150,16 @@ public class ExampleServerR4IT {
return ourClient.search().forResource(Subscription.class).where(Subscription.STATUS.exactly().code("active")).cacheControl(new CacheControlDirective().setNoCache(true)).returnBundle(Bundle.class).execute().getEntry().size(); return ourClient.search().forResource(Subscription.class).where(Subscription.STATUS.exactly().code("active")).cacheControl(new CacheControlDirective().setNoCache(true)).returnBundle(Bundle.class).execute().getEntry().size();
} }
@AfterAll
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeAll
public static void beforeClass() throws Exception {
String path = Paths.get("").toAbsolutePath().toString();
ourLog.info("Project base path is: {}", path); @BeforeEach
void beforeEach() {
ourServer = new Server(0);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDisplayName("HAPI FHIR");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
ourServer.setHandler(webAppContext);
ourServer.start();
ourPort = JettyUtil.getPortForStartedServer(ourServer);
ourCtx = FhirContext.forR4();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = HapiProperties.getServerAddress(); String ourServerBase = "http://localhost:" + port + "/hapi-fhir-jpaserver/fhir/";
ourServerBase = "http://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase); ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true)); ourClient.registerInterceptor(new LoggingInterceptor(true));
} }
public static void main(String[] theArgs) throws Exception {
ourPort = 8080;
beforeClass();
}
} }

View File

@@ -22,7 +22,12 @@ import org.hl7.fhir.r5.model.Subscription;
import org.hl7.fhir.r5.model.SubscriptionTopic; import org.hl7.fhir.r5.model.SubscriptionTopic;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.net.URI; import java.net.URI;
import java.nio.file.Paths; import java.nio.file.Paths;
@@ -33,25 +38,27 @@ import static ca.uhn.fhir.util.TestUtil.waitForSize;
import static org.awaitility.Awaitility.await; import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties =
{
"spring.batch.job.enabled=false",
"spring.profiles.active=r5",
"spring.datasource.url=jdbc:h2:mem:dbr5",
"hapi.fhir.subscription.websocket_enabled=true"
})
public class ExampleServerR5IT { public class ExampleServerR5IT {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerR5IT.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private static IGenericClient ourClient; private IGenericClient ourClient;
private static FhirContext ourCtx; private FhirContext ourCtx;
private static int ourPort;
private static Server ourServer; @LocalServerPort
private int port;
static {
HapiProperties.forceReload();
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:h2:mem:dbr5");
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "R5");
HapiProperties.setProperty(HapiProperties.SUBSCRIPTION_WEBSOCKET_ENABLED, "true");
ourCtx = FhirContext.forR5();
}
@Test @Test
public void testCreateAndRead() { public void testCreateAndRead() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
String methodName = "testCreateResourceConditional"; String methodName = "testCreateResourceConditional";
Patient pt = new Patient(); Patient pt = new Patient();
@@ -97,7 +104,7 @@ public class ExampleServerR5IT {
SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON); SocketImplementation mySocketImplementation = new SocketImplementation(mySubscriptionId.getIdPart(), EncodingEnum.JSON);
myWebSocketClient.start(); myWebSocketClient.start();
URI echoUri = new URI("ws://localhost:" + ourPort + "/hapi-fhir-jpaserver/websocket"); URI echoUri = new URI("ws://localhost:" + port + "/hapi-fhir-jpaserver/websocket");
ClientUpgradeRequest request = new ClientUpgradeRequest(); ClientUpgradeRequest request = new ClientUpgradeRequest();
ourLog.info("Connecting to : {}", echoUri); ourLog.info("Connecting to : {}", echoUri);
Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request); Future<Session> connection = myWebSocketClient.connect(mySocketImplementation, echoUri, request);
@@ -123,41 +130,14 @@ public class ExampleServerR5IT {
ourClient.delete().resourceById(mySubscriptionId).execute(); ourClient.delete().resourceById(mySubscriptionId).execute();
} }
@AfterAll @BeforeEach
public static void afterClass() throws Exception { void beforeEach() {
ourServer.stop();
}
@BeforeAll
public static void beforeClass() throws Exception {
String path = Paths.get("").toAbsolutePath().toString();
ourLog.info("Project base path is: {}", path);
ourServer = new Server(0);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDisplayName("HAPI FHIR");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
ourServer.setHandler(webAppContext);
ourServer.start();
ourPort = JettyUtil.getPortForStartedServer(ourServer);
ourCtx = FhirContext.forR5();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = "http://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/"; String ourServerBase = "http://localhost:" + port + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase); ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true)); ourClient.registerInterceptor(new LoggingInterceptor(true));
} }
public static void main(String[] theArgs) throws Exception {
ourPort = 8080;
beforeClass();
}
} }

View File

@@ -7,44 +7,43 @@ import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.client.interceptor.UrlTenantSelectionInterceptor; import ca.uhn.fhir.rest.client.interceptor.UrlTenantSelectionInterceptor;
import ca.uhn.fhir.rest.server.provider.ProviderConstants; import ca.uhn.fhir.rest.server.provider.ProviderConstants;
import ca.uhn.fhir.test.utilities.JettyUtil; import org.hl7.fhir.r4.model.*;
import org.eclipse.jetty.server.Server; import org.junit.jupiter.api.BeforeEach;
import org.eclipse.jetty.webapp.WebAppContext;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeType;
import org.hl7.fhir.r4.model.IntegerType;
import org.hl7.fhir.r4.model.Parameters;
import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.nio.file.Paths; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties =
{
"spring.batch.job.enabled=false",
"spring.profiles.active=r4",
"spring.datasource.url=jdbc:h2:mem:dbr4-mt",
"hapi.fhir.subscription.websocket_enabled=true",
"hapi.fhir.subscription.websocket_enabled=true",
"hapi.fhir.partitioning_multitenancy_enabled"
})
public class MultitenantServerR4IT { public class MultitenantServerR4IT {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MultitenantServerR4IT.class);
private static IGenericClient ourClient; private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExampleServerDstu2IT.class);
private static FhirContext ourCtx; private IGenericClient ourClient;
private static int ourPort; private FhirContext ourCtx;
private static Server ourServer;
@LocalServerPort
private int port;
private static UrlTenantSelectionInterceptor ourClientTenantInterceptor; private static UrlTenantSelectionInterceptor ourClientTenantInterceptor;
static {
HapiProperties.forceReload();
HapiProperties.setProperty(HapiProperties.DATASOURCE_URL, "jdbc:h2:mem:dbr4-mt");
HapiProperties.setProperty(HapiProperties.FHIR_VERSION, "R4");
HapiProperties.setProperty(HapiProperties.SUBSCRIPTION_WEBSOCKET_ENABLED, "true");
HapiProperties.setProperty(HapiProperties.PARTITIONING_ENABLED, "true");
HapiProperties.setProperty(HapiProperties.PARTITIONING_MULTITENANCY_ENABLED, "true");
ourCtx = FhirContext.forR4();
}
@Test @Test
public void testCreateAndReadInTenantA() { public void testCreateAndReadInTenantA() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
// Create tenant A // Create tenant A
ourClientTenantInterceptor.setTenantId("DEFAULT"); ourClientTenantInterceptor.setTenantId("DEFAULT");
@@ -70,7 +69,7 @@ public class MultitenantServerR4IT {
@Test @Test
public void testCreateAndReadInTenantB() { public void testCreateAndReadInTenantB() {
ourLog.info("Base URL is: " + HapiProperties.getServerAddress());
// Create tenant A // Create tenant A
ourClientTenantInterceptor.setTenantId("DEFAULT"); ourClientTenantInterceptor.setTenantId("DEFAULT");
@@ -94,45 +93,14 @@ public class MultitenantServerR4IT {
assertEquals("Family B", pt2.getName().get(0).getFamily()); assertEquals("Family B", pt2.getName().get(0).getFamily());
} }
@AfterAll @BeforeEach
public static void afterClass() throws Exception { void beforeEach() {
ourServer.stop();
}
@BeforeAll
public static void beforeClass() throws Exception {
String path = Paths.get("").toAbsolutePath().toString();
ourLog.info("Project base path is: {}", path);
ourServer = new Server(0);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/hapi-fhir-jpaserver");
webAppContext.setDisplayName("HAPI FHIR");
webAppContext.setDescriptor(path + "/src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path + "/target/hapi-fhir-jpaserver-starter");
webAppContext.setParentLoaderPriority(true);
ourServer.setHandler(webAppContext);
ourServer.start();
ourPort = JettyUtil.getPortForStartedServer(ourServer);
ourCtx = FhirContext.forR4();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
String ourServerBase = HapiProperties.getServerAddress(); String ourServerBase = "http://localhost:" + port + "/hapi-fhir-jpaserver/fhir/";
ourServerBase = "http://localhost:" + ourPort + "/hapi-fhir-jpaserver/fhir/";
ourClient = ourCtx.newRestfulGenericClient(ourServerBase); ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
ourClient.registerInterceptor(new LoggingInterceptor(true)); ourClient.registerInterceptor(new LoggingInterceptor(true));
ourClientTenantInterceptor = new UrlTenantSelectionInterceptor();
ourClient.registerInterceptor(ourClientTenantInterceptor);
}
public static void main(String[] theArgs) throws Exception {
ourPort = 8080;
beforeClass();
} }
} }

View File

@@ -1,36 +0,0 @@
package ca.uhn.fhir.jpa.starter;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
/**
* Provides server ports
*/
public class RandomServerPortProvider {
private static List<Integer> ourPorts = new ArrayList<Integer>();
public static int findFreePort() {
ServerSocket server;
try {
server = new ServerSocket(0);
int port = server.getLocalPort();
ourPorts.add(port);
server.close();
Thread.sleep(500);
return port;
} catch (IOException e) {
throw new Error(e);
} catch (InterruptedException e) {
throw new Error(e);
}
}
public static List<Integer> list() {
return ourPorts;
}
}

View File

@@ -0,0 +1,103 @@
spring:
datasource:
url: 'jdbc:h2:file:./target/database/h2'
username: sa
password: null
driverClassName: org.h2.Driver
max-active: 15
profiles:
### This is the FHIR version. Choose between, dstu2, dstu3, r4 or r5
active: r4
hapi:
fhir:
#supported_resource_types:
# - Patient
# - Observation
# allow_cascading_deletes: true
# allow_contains_searches: true
# allow_external_references: true
# allow_multiple_delete: true
# allow_override_default_search_params: true
# allow_placeholder_references: true
# auto_create_placeholder_reference_targets: false
# default_encoding: JSON
# default_pretty_print: true
# default_page_size: 20
# enable_index_missing_fields: false
# enforce_referential_integrity_on_delete: false
# enforce_referential_integrity_on_write: false
# etag_support_enabled: true
# expunge_enabled: true
# daoconfig_client_id_strategy: null
# fhirpath_interceptor_enabled: false
# filter_search_enabled: true
# graphql_enabled: true
#partitioning:
# cross_partition_reference_mode: true
# multitenancy_enabled: true
# partitioning_include_in_search_hashes: true
#cors:
# allow_Credentials: true
# Supports multiple, comma separated allowed origin entries
# cors.allowed_origin=http://localhost:8080,https://localhost:8080,https://fhirtest.uhn.ca
# allowed_origin:
# - '*'
# logger:
# error_format: 'ERROR - ${requestVerb} ${requestUrl}'
# format: >-
# Path[${servletPath}] Source[${requestHeader.x-forwarded-for}]
# Operation[${operationType} ${operationName} ${idOrResourceName}]
# UA[${requestHeader.user-agent}] Params[${requestParameters}]
# ResponseEncoding[${responseEncodingNoDefault}]
# log_exceptions: true
# name: fhirtest.access
# max_binary_size: 104857600
# max_page_size: 200
# retain_cached_searches_mins: 60
# reuse_cached_search_results_millis: 60000
tester:
-
id: home
name: Local Tester
server_address: 'http://localhost:8080/hapi-fhir-jpaserver/fhir'
refuse_to_fetch_third_party_urls: false
fhir_version: R4
-
id: global
name: Global Tester
server_address: "http://hapi.fhir.org/baseR4"
refuse_to_fetch_third_party_urls: false
fhir_version: R4
# validation:
# requests_enabled: true
# responses_enabled: true
# binary_storage_enabled: true
# bulk_export_enabled: true
# partitioning_multitenancy_enabled:
# subscription:
# resthook_enabled: false
# websocket_enabled: false
# email:
# from: some@test.com
# host: google.com
# port:
# username:
# password:
# auth:
# startTlsEnable:
# startTlsRequired:
# quitWait:
#
#elasticsearch:
# debug:
# pretty_print_json_log: false
# refresh_after_write: false
# enabled: false
# password: SomePassword
# required_index_status: YELLOW
# rest_url: 'http://localhost:9200'
# schema_management_strategy: CREATE
# username: SomeUsername