From 963116094234c1eb98e1a14d97dee40a385867d5 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Sat, 23 Apr 2016 18:43:11 -0400 Subject: [PATCH] Add unit test --- ...rchReturningProfiledResourceDstu2Test.java | 141 ++++++++++++++++++ .../uhn/fhir/parser/XmlParserDstu3Test.java | 1 - 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java new file mode 100644 index 00000000000..f3dcafe72a9 --- /dev/null +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java @@ -0,0 +1,141 @@ +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.util.ArrayList; +import java.util.Collections; +import java.util.List; +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.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.hl7.fhir.instance.model.api.IBaseResource; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; +import ca.uhn.fhir.model.api.annotation.Description; +import ca.uhn.fhir.model.dstu2.resource.Patient; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.annotation.OptionalParam; +import ca.uhn.fhir.rest.annotation.Search; +import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.rest.param.StringParam; +import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.TestUtil; +import ca.uhn.fhir.validation.PatientProfileDstu2; + +public class SearchReturningProfiledResourceDstu2Test { + + private static CloseableHttpClient ourClient; + private static FhirContext ourCtx = FhirContext.forDstu2(); + private static String ourLastMethod; + private static StringParam ourLastRef; + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SearchReturningProfiledResourceDstu2Test.class); + private static int ourPort; + private static Server ourServer; + + @Before + public void before() { + ourLastMethod = null; + ourLastRef = null; + } + + @Test + public void testProfilesGetAdded() throws Exception { + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_pretty=true"); + HttpResponse status = ourClient.execute(httpGet); + String responseContent = IOUtils.toString(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + ourLog.info(responseContent); + + assertThat(responseContent, containsString("")); + assertThat(responseContent, containsString("")); + + } + + + @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); + + ServletHandler proxyHandler = new ServletHandler(); + RestfulServer servlet = new RestfulServer(ourCtx); + + servlet.setResourceProviders(new DummyPatientResourceProvider()); + ServletHolder servletHolder = new ServletHolder(servlet); + 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 { + + /** + * Basic search, available for every resource. Allows search per id (use of read is better, though), fulltext + * content + * and lastUpdated timestamp. + * + * @param id + * @param content + * @param lastUpdated + * @return + */ + //@formatter:off + @Search + public List search( + @Description(shortDefinition = "The resource id") + @OptionalParam(name = "_id") StringParam id, + @Description(shortDefinition = "Search the contents of the resource's data using a fulltext search") + @OptionalParam(name = Constants.PARAM_CONTENT) StringParam content, + @Description(shortDefinition = "Search for resources considering the time of their last update") + @OptionalParam(name = Constants.PARAM_LASTUPDATED) DateRangeParam lastUpdated) { + //@formatter:on + + List result = new ArrayList(); + + PatientProfileDstu2 pp = new PatientProfileDstu2(); + + ResourceMetadataKeyEnum.PROFILES.put(pp, Collections.singletonList(new IdDt("http://foo"))); + + pp.setId("123"); + pp.getOwningOrganization().setReference("Organization/456"); + result.add(pp); + + ourLog.info("Search: Everything ok. Going to return results!"); + return result; + } + + @Override + public Class getResourceType() { + return Patient.class; + } + + } + +} diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index 01dc7ec742a..9ab68cfbbd0 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; -import java.util.Locale; import java.util.UUID; import org.apache.commons.io.IOUtils;