package ca.uhn.fhir.rest.server; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpOptions; 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.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; /** * Created by dsotnikov on 2/25/2014. */ public class ServerFeaturesDstu2Test { private static CloseableHttpClient ourClient; private static FhirContext ourCtx = FhirContext.forDstu2(); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServerFeaturesDstu2Test.class); private static int ourPort; private static Server ourServer; private static RestfulServer ourServlet; @Test public void testOptions() throws Exception { HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + ""); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); assertEquals(200, status.getStatusLine().getStatusCode()); assertThat(responseContent, containsString("<Conformance")); /* * Now with a leading / */ httpGet = new HttpOptions("http://localhost:" + ourPort + "/"); status = ourClient.execute(httpGet); responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); assertEquals(200, status.getStatusLine().getStatusCode()); assertThat(responseContent, containsString("<Conformance")); } /** * See #313 */ @Test public void testOptionsForNonBasePath1() throws Exception { HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + "/Foo"); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); ourLog.info(responseContent); assertEquals(404, status.getStatusLine().getStatusCode()); } /** * See #313 */ @Test public void testOptionsForNonBasePath2() throws Exception { HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + "/Patient/1"); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); ourLog.info(responseContent); assertEquals(400, status.getStatusLine().getStatusCode()); } /** * See #313 */ @Test public void testOptionsForNonBasePath3() throws Exception { HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + "/metadata"); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); ourLog.info(responseContent); assertEquals(405, status.getStatusLine().getStatusCode()); } @Test public void testOptionsJson() throws Exception { HttpOptions httpGet = new HttpOptions("http://localhost:" + ourPort + "?_format=json"); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); assertEquals(200, status.getStatusLine().getStatusCode()); assertThat(responseContent, containsString("resourceType\":\"Conformance")); } @Test public void testHeadJson() throws Exception { HttpHead httpGet = new HttpHead("http://localhost:" + ourPort + "/Patient/123"); HttpResponse status = ourClient.execute(httpGet); assertEquals(null, status.getEntity()); ourLog.info(status.toString()); assertEquals(400, status.getStatusLine().getStatusCode()); assertThat(status.getFirstHeader("x-powered-by").getValue(), containsString("HAPI")); } @Test public void testRegisterAndUnregisterResourceProviders() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1"); HttpResponse status = ourClient.execute(httpGet); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); assertEquals(200, status.getStatusLine().getStatusCode()); assertThat(responseContent, containsString("PRP1")); Collection<IResourceProvider> providers = new ArrayList<IResourceProvider>(ourServlet.getResourceProviders()); for (IResourceProvider provider : providers) { ourServlet.unregisterProvider(provider); } ourServlet.registerProvider(new DummyPatientResourceProvider2()); httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1"); status = ourClient.execute(httpGet); responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8); IOUtils.closeQuietly(status.getEntity().getContent()); assertEquals(200, status.getStatusLine().getStatusCode()); assertThat(responseContent, containsString("PRP2")); } @AfterClass public static void afterClassClearContext() throws Exception { ourServer.stop(); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourPort = PortUtil.findFreePort(); ourServer = new Server(ourPort); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); ServletHandler proxyHandler = new ServletHandler(); ourServlet = new RestfulServer(ourCtx); ourServlet.setFhirContext(ourCtx); ourServlet.setResourceProviders(patientProvider); ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); ourServer.start(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setConnectionManager(connectionManager); ourClient = builder.build(); } public static class DummyPatientResourceProvider implements IResourceProvider { @Override public Class<? extends IResource> getResourceType() { return Patient.class; } @Read public Patient read(@IdParam IdDt theId) { Patient p1 = new Patient(); p1.setId("p1ReadId"); p1.addIdentifier().setValue("PRP1"); return p1; } } public static class DummyPatientResourceProvider2 implements IResourceProvider { @Override public Class<? extends IResource> getResourceType() { return Patient.class; } @Read public Patient read(@IdParam IdDt theId) { Patient p1 = new Patient(); p1.setId("p1ReadId"); p1.addIdentifier().setValue("PRP2"); return p1; } } }