Just add some unit tests

This commit is contained in:
James Agnew 2015-04-20 12:07:52 -04:00
parent addcad7749
commit 89519faa6f
4 changed files with 202 additions and 13 deletions

View File

@ -2,22 +2,25 @@
<modelVersion>4.0.0</modelVersion>
<!--
Note: HAPI projects use the Sonatype OSS parent project to
facilitate deployment to the global Maven repos.
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>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
-->
<groupId>ca.uhn.hapi.example</groupId>
<artifactId>hapi-fhir-jpaserver-example</artifactId>
<version>0.9-SNAPSHOT</version>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>HAPI FHIR JPA Server - Example</name>

View File

@ -3,15 +3,78 @@ package ca.uhn.fhir.rest.server;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.server.ServerBaseTest.DummyProvider;
import ca.uhn.fhir.util.RandomServerPortProvider;
public class IncomingRequestAddressStrategyTest {
private static CloseableHttpClient ourClient;
private static String ourLastBase;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(IncomingRequestAddressStrategyTest.class);
private static IncomingRequestAddressStrategy ourStrategy;
private Server myServer;
public void after() throws Exception {
if (myServer != null) {
myServer.stop();
}
}
@Before
public void before() {
ourLastBase = null;
ourStrategy = new IncomingRequestAddressStrategy();
}
private void httpGet(String url) throws IOException, ClientProtocolException {
ourLastBase = null;
HttpGet httpPost = new HttpGet(url);
HttpResponse status = ourClient.execute(httpPost);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
}
private void startServer(int port, String contextPath, String servletPath) throws Exception {
myServer = new Server(port);
org.eclipse.jetty.servlet.ServletContextHandler proxyHandler = new org.eclipse.jetty.servlet.ServletContextHandler();
proxyHandler.setContextPath(contextPath);
ServletHolder handler = new ServletHolder();
handler.setServlet(new MyServlet());
proxyHandler.addServlet(handler, servletPath);
myServer.setHandler(proxyHandler);
myServer.start();
}
/**
* This is an incoming request from an instance of Tomcat on AWS, provided by
* Simon Ling of Systems Made Simple
* This is an incoming request from an instance of Tomcat on AWS, provided by Simon Ling of Systems Made Simple
*/
@Test
public void testAwsUrl() {
@ -27,4 +90,90 @@ public class IncomingRequestAddressStrategyTest {
assertEquals("http://fhirstorm.dyndns.org:8080/FhirStorm/fhir", actual);
}
@Test
public void testUnderJettyWithContextPathServletRoot() throws Exception {
int port = RandomServerPortProvider.findFreePort();
String contextPath = "/ctx";
String servletPath = "/*";
startServer(port, contextPath, servletPath);
httpGet("http://localhost:" + port);
assertEquals(null, ourLastBase);
httpGet("http://localhost:" + port + "/ctx");
assertEquals("http://localhost:" + port + "/ctx/", ourLastBase);
httpGet("http://localhost:" + port + "/ctx/");
assertEquals("http://localhost:" + port + "/ctx/", ourLastBase);
httpGet("http://localhost:" + port + "/ctx/Patient?_pretty=true");
assertEquals("http://localhost:" + port + "/ctx/", ourLastBase);
httpGet("http://localhost:" + port + "/ctx/Patient/123/_history/222");
assertEquals("http://localhost:" + port + "/ctx/", ourLastBase);
}
@Test
public void testUnderJettyWithContextPathServletPath() throws Exception {
int port = RandomServerPortProvider.findFreePort();
String contextPath = "/ctx";
String servletPath = "/servlet/*";
startServer(port, contextPath, servletPath);
httpGet("http://localhost:" + port);
assertEquals(null, ourLastBase);
httpGet("http://localhost:" + port + "/ctx/servlet/");
assertEquals("http://localhost:" + port + "/ctx/servlet", ourLastBase);
httpGet("http://localhost:" + port + "/ctx/servlet/Patient?_pretty=true");
assertEquals("http://localhost:" + port + "/ctx/servlet", ourLastBase);
httpGet("http://localhost:" + port + "/ctx/servlet/Patient/123/_history/222");
assertEquals("http://localhost:" + port + "/ctx/servlet", ourLastBase);
}
@Test
public void testUnderJettyWithContextRootServletRoot() throws Exception {
int port = RandomServerPortProvider.findFreePort();
String contextPath = "/";
String servletPath = "/*";
startServer(port, contextPath, servletPath);
httpGet("http://localhost:" + port);
assertEquals("http://localhost:" + port + contextPath, ourLastBase);
httpGet("http://localhost:" + port + "/Patient?_pretty=true");
assertEquals("http://localhost:" + port + contextPath, ourLastBase);
httpGet("http://localhost:" + port + "/Patient/123/_history/222");
assertEquals("http://localhost:" + port + contextPath, ourLastBase);
}
@BeforeClass
public static void beforeClass() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
private static class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest theReq, HttpServletResponse theResp) throws ServletException, IOException {
ourLastBase = ourStrategy.determineServerBase(getServletContext(), theReq);
theResp.setContentType("text/plain");
theResp.getWriter().append("Success");
theResp.getWriter().close();
}
}
}

View File

@ -0,0 +1,36 @@
package ca.uhn.fhir.util;
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

@ -125,3 +125,4 @@ local.properties
# TeXlipse plugin
.texlipse
/build/