Fix #206 - Don't require hl7org structures to start JPA server

This commit is contained in:
jamesagnew 2015-08-19 07:47:45 -04:00
parent 6941f92090
commit 4d04b9cc6a
8 changed files with 235 additions and 117 deletions

View File

@ -67,7 +67,6 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
<scope>test</scope>
</dependency>
<dependency>
@ -269,25 +268,21 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty_version}</version>
<scope>test</scope>
</dependency>

View File

@ -1,30 +1,10 @@
package ca.uhn.fhir.jpa.dao;
/*
* #%L
* HAPI FHIR JPA Server
* %%
* Copyright (C) 2014 - 2015 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import javax.annotation.PostConstruct;
import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
@ -41,12 +21,30 @@ import ca.uhn.fhir.validation.ValidationResult;
public class FhirResourceDaoQuestionnaireResponseDstu2 extends FhirResourceDaoDstu2<QuestionnaireResponse> {
private FhirContext myRefImplCtx = FhirContext.forDstu2Hl7Org();
private FhirContext myRefImplCtx;
private Boolean myValidateResponses;
/**
* Initialize the bean
*/
@PostConstruct
public void initialize() {
try {
Class.forName("org.hl7.fhir.instance.model.QuestionnaireResponse");
myValidateResponses = true;
myRefImplCtx = FhirContext.forDstu2Hl7Org();
} catch (ClassNotFoundException e) {
myValidateResponses = Boolean.FALSE;
}
}
@Override
protected void validateResourceForStorage(IResource theResource) {
super.validateResourceForStorage(theResource);
if (!myValidateResponses) {
return;
}
QuestionnaireResponse qa = (QuestionnaireResponse) theResource;
if (qa == null || qa.getQuestionnaire() == null || qa.getQuestionnaire().getReference() == null || qa.getQuestionnaire().getReference().isEmpty()) {
return;
@ -73,7 +71,8 @@ public class FhirResourceDaoQuestionnaireResponseDstu2 extends FhirResourceDaoDs
public <T extends IBaseResource> T load(Class<T> theType, IIdType theId) throws ResourceNotFoundException {
/*
* The QuestionnaireResponse validator uses RI structures, so for now we need to convert between that and HAPI structures. This is a bit hackish, but hopefully it will go away at some point.
* The QuestionnaireResponse validator uses RI structures, so for now we need to convert between that and HAPI
* structures. This is a bit hackish, but hopefully it will go away at some point.
*/
if ("ValueSet".equals(theType.getSimpleName())) {
IFhirResourceDao<ValueSet> dao = getDao(ValueSet.class);

View File

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>

View File

@ -1,21 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--
Note: HAPI projects use the "hapi-fhir" POM as their base to provide
easy management.
You do not need to use this in your own projects, so the
"parent" tag and it's contents below may be removed if you
are using this file as a basis for your own project.
-->
<!-- Note: HAPI projects use the "hapi-fhir" POM as their base to provide
easy management. You do not need to use this in your own projects, so the
"parent" tag and it's contents below may be removed if you are using this
file as a basis for your own project. -->
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>ca.uhn.hapi.example</groupId>
<artifactId>hapi-fhir-jpaserver-example</artifactId>
<packaging>war</packaging>
@ -40,7 +37,7 @@
<artifactId>hapi-fhir-base</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<!-- At least one "structures" JAR must also be included -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
@ -53,10 +50,8 @@
<version>1.2-SNAPSHOT</version>
</dependency>
<!--
This dependency includes the JPA server itself, which is packaged
separately from the rest of HAPI FHIR
-->
<!-- This dependency includes the JPA server itself, which is packaged
separately from the rest of HAPI FHIR -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-jpaserver-base</artifactId>
@ -69,19 +64,16 @@
<artifactId>hapi-fhir-testpage-overlay</artifactId>
<version>1.2-SNAPSHOT</version>
<type>war</type>
<scope>provided</scope>
<scope>provided</scope>
</dependency>
<!--
HAPI-FHIR uses Logback for logging support. The logback library is included
automatically by Maven as a part of the hapi-fhir-base dependency, but you
also need to include a logging library. Logback is used here, but log4j
would also be fine.
-->
<!-- HAPI-FHIR uses Logback for logging support. The logback library is
included automatically by Maven as a part of the hapi-fhir-base dependency,
but you also need to include a logging library. Logback is used here, but
log4j would also be fine. -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
</dependency>
<!-- Needed for JEE/Servlet support -->
@ -91,10 +83,8 @@
<scope>provided</scope>
</dependency>
<!--
If you are using HAPI narrative generation, you will need to include Thymeleaf
as well. Otherwise the following can be omitted.
-->
<!-- If you are using HAPI narrative generation, you will need to include
Thymeleaf as well. Otherwise the following can be omitted. -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
@ -113,11 +103,8 @@
</exclusion>
</exclusions>
</dependency>
<!--
Spring Web is used to deploy the server to a
web container.
-->
<!-- Spring Web is used to deploy the server to a web container. -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
@ -125,21 +112,17 @@
<scope>provided</scope>
</dependency>
<!--
You may not need this if you are deploying to an application server which
provides database connection pools itself.
-->
<!-- You may not need this if you are deploying to an application server
which provides database connection pools itself. -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<!--
This example uses Derby embedded database. If you are using another
database such as Mysql or Oracle, you may omit the following
dependencies and replace them with an appropriate database client
dependency for your database platform.
-->
<!-- This example uses Derby embedded database. If you are using another
database such as Mysql or Oracle, you may omit the following dependencies
and replace them with an appropriate database client dependency for your
database platform. -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
@ -153,31 +136,50 @@
<artifactId>derbyclient</artifactId>
</dependency>
<!--
Arquillian is just used for automated tests, you don't neccesarily need it
to use this example.
<!--
The following dependencies are only needed for automated unit tests,
you do not neccesarily need them to run the example.
-->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--
Tells Maven to name the generated WAR file as
hapi-fhir-jpaserver-example.war
-->
<!-- Tells Maven to name the generated WAR file as hapi-fhir-jpaserver-example.war -->
<finalName>hapi-fhir-jpaserver-example</finalName>
<!--
The following is not required for the application to build, but
allows you to test it by issuing "mvn jetty:run" from the command
line.
-->
<!-- The following is not required for the application to build, but allows
you to test it by issuing "mvn jetty:run" from the command line. -->
<pluginManagement>
<plugins>
<plugin>
@ -192,11 +194,9 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!--
Tell Maven which Java source version you want to use
-->
<!-- Tell Maven which Java source version you want to use -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -205,11 +205,9 @@
<target>1.6</target>
</configuration>
</plugin>
<!--
The configuration here tells the WAR plugin to include the FHIR Tester
overlay. You can omit it if you are not using that feature.
-->
<!-- The configuration here tells the WAR plugin to include the FHIR Tester
overlay. You can omit it if you are not using that feature. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
@ -222,11 +220,9 @@
</overlays>
</configuration>
</plugin>
<!--
This plugin is just a part of the HAPI internal build process, you do not
need to incude it in your own projects
-->
<!-- This plugin is just a part of the HAPI internal build process, you
do not need to incude it in your own projects -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
@ -234,7 +230,7 @@
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -42,7 +42,7 @@
</init-param>
<init-param>
<param-name>FhirVersion</param-name>
<param-value>DSTU1</param-value>
<param-value>DSTU2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

View File

@ -0,0 +1,36 @@
package ca.uhn.fhir.jpa.dao;
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,66 @@
package ca.uhn.fhir.jpa.dao;
import static org.junit.Assert.*;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
public class ResourceProviderDstu2Test {
private static IGenericClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu2();
private static int ourPort;
private static Server ourServer;
@Test
public void testCreateAndRead() throws IOException {
String methodName = "testCreateResourceConditional";
Patient pt = new Patient();
pt.addName().addFamily(methodName);
IIdType id = ourClient.create().resource(pt).execute().getId();
Patient pt2 = ourClient.read().resource(Patient.class).withId(id).execute();
assertEquals(methodName, pt2.getName().get(0).getFamily().get(0).getValue());
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = RandomServerPortProvider.findFreePort();
ourServer = new Server(ourPort);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setDescriptor("src/main/webapp/WEB-INF/web.xml");
webAppContext.setResourceBase("target/hapi-fhir-jpaserver-example");
webAppContext.setParentLoaderPriority(true);
ourServer.setHandler(webAppContext);
ourServer.start();
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
ourClient = ourCtx.newRestfulGenericClient("http://localhost:" + ourPort + "/base");
ourClient.registerInterceptor(new LoggingInterceptor(true));
}
}

49
pom.xml
View File

@ -45,12 +45,6 @@
</description>
<dependencies>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<prerequisites>
@ -231,6 +225,16 @@
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
@ -252,10 +256,30 @@
<version>${derby_version}</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.8.Final</version>
</dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.2.6.v20141205</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.6.v20141205</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.6.v20141205</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.2.6.v20141205</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.2.6.v20141205</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
@ -580,7 +604,7 @@
<fileset dir="hapi-fhir-cobertura/target/site/cobertura" />
</copy>
<copy todir="target/site">
<fileset dir="hapi-fhir-base/target/site" includes="checkstyle.*"/>
<fileset dir="hapi-fhir-base/target/site" includes="checkstyle.*" />
</copy>
<echo>Fixing Checkstyle Report</echo>
<replace dir="target/site" summary="true">
@ -609,7 +633,8 @@
</div>
</div>]]></replacevalue>
</replace>
<!--<replaceregexp file="target/site/checkstyle.html" byline="false" match="&lt;ul class=&quot;breadcrumb.*?&lt;/ul&gt;" replace="" flags="s"/>-->
<!--<replaceregexp file="target/site/checkstyle.html" byline="false"
match="&lt;ul class=&quot;breadcrumb.*?&lt;/ul&gt;" replace="" flags="s"/> -->
</target>
</configuration>
</execution>