Add some new tests for JPA DSTU2 operations

This commit is contained in:
jamesagnew 2015-02-27 08:45:34 -05:00
parent 0a8c7c4960
commit 6580abe5f2
5 changed files with 401 additions and 159 deletions

View File

@ -73,6 +73,10 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding implem
myIdIndex = idIndex;
myVersionIdIndex = versionIdIndex;
if (myIdIndex== null) {
throw new ConfigurationException("@" + Read.class.getSimpleName() + " method " + theMethod.getName() + " on type \"" + theMethod.getDeclaringClass().getName() + "\" does not have a parameter annotated with @" + IdParam.class.getSimpleName());
}
Class<?>[] parameterTypes = theMethod.getParameterTypes();
if (!IdDt.class.equals(parameterTypes[myIdIndex])) {
throw new ConfigurationException("ID parameter must be of type: " + IdDt.class.getCanonicalName() + " - Found: " + parameterTypes[myIdIndex]);

View File

@ -5,10 +5,7 @@ import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
@ -18,7 +15,10 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
@ -130,7 +130,117 @@ public class ResourceProviderDstu2Test {
response.close();
}
}
@Test
public void testCreateResourceConditional() throws IOException {
String methodName = "testCreateResourceConditional";
Patient pt = new Patient();
pt.addName().addFamily(methodName);
String resource = ourFhirCtx.newXmlParser().encodeResourceToString(pt);
HttpPost post = new HttpPost(ourServerBase + "/Patient");
post.setEntity(new StringEntity(resource, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
CloseableHttpResponse response = ourHttpClient.execute(post);
IdDt id;
try {
assertEquals(201, response.getStatusLine().getStatusCode());
String newIdString = response.getFirstHeader(Constants.HEADER_LOCATION_LC).getValue();
assertThat(newIdString, startsWith(ourServerBase + "/Patient/"));
id = new IdDt(newIdString);
} finally {
response.close();
}
post = new HttpPost(ourServerBase + "/Patient");
post.setEntity(new StringEntity(resource, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
post.addHeader(Constants.HEADER_IF_NONE_EXIST, "Patient?name=" + methodName);
response = ourHttpClient.execute(post);
try {
assertEquals(200, response.getStatusLine().getStatusCode());
String newIdString = response.getFirstHeader(Constants.HEADER_LOCATION_LC).getValue();
assertEquals(id.getValue(), newIdString);
} finally {
response.close();
}
}
@Test
public void testUpdateResourceConditional() throws IOException {
String methodName = "testUpdateResourceConditional";
Patient pt = new Patient();
pt.addName().addFamily(methodName);
String resource = ourFhirCtx.newXmlParser().encodeResourceToString(pt);
HttpPost post = new HttpPost(ourServerBase + "/Patient");
post.setEntity(new StringEntity(resource, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
CloseableHttpResponse response = ourHttpClient.execute(post);
IdDt id;
try {
assertEquals(201, response.getStatusLine().getStatusCode());
String newIdString = response.getFirstHeader(Constants.HEADER_LOCATION_LC).getValue();
assertThat(newIdString, startsWith(ourServerBase + "/Patient/"));
id = new IdDt(newIdString);
} finally {
response.close();
}
HttpPut put = new HttpPut(ourServerBase + "/Patient?name=" + methodName);
put.setEntity(new StringEntity(resource, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
response = ourHttpClient.execute(put);
try {
assertEquals(200, response.getStatusLine().getStatusCode());
IdDt newId = new IdDt(response.getFirstHeader(Constants.HEADER_LOCATION_LC).getValue());
assertEquals(id.toVersionless(), newId.toVersionless());
assertNotEquals(id, newId);
} finally {
response.close();
}
}
@Test
public void testDeleteResourceConditional() throws IOException {
String methodName = "testDeleteResourceConditional";
Patient pt = new Patient();
pt.addName().addFamily(methodName);
String resource = ourFhirCtx.newXmlParser().encodeResourceToString(pt);
HttpPost post = new HttpPost(ourServerBase + "/Patient");
post.setEntity(new StringEntity(resource, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
CloseableHttpResponse response = ourHttpClient.execute(post);
IdDt id;
try {
assertEquals(201, response.getStatusLine().getStatusCode());
String newIdString = response.getFirstHeader(Constants.HEADER_LOCATION_LC).getValue();
assertThat(newIdString, startsWith(ourServerBase + "/Patient/"));
id = new IdDt(newIdString);
} finally {
response.close();
}
HttpDelete delete = new HttpDelete(ourServerBase + "/Patient?name=" + methodName);
response = ourHttpClient.execute(delete);
try {
assertEquals(204, response.getStatusLine().getStatusCode());
} finally {
response.close();
}
HttpGet read = new HttpGet(ourServerBase + "/Patient/" + id.getIdPart());
response = ourHttpClient.execute(read);
try {
ourLog.info(response.toString());
assertEquals(Constants.STATUS_HTTP_410_GONE, response.getStatusLine().getStatusCode());
} finally {
response.close();
}
}
/**
* Test for issue #60
*/

View File

@ -3,21 +3,19 @@ package ca.uhn.fhir.rest.server;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import org.hamcrest.core.StringContains;
import org.hl7.fhir.instance.model.IBaseResource;
import org.junit.Test;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.base.resource.ResourceMetadataMap;
import ca.uhn.fhir.model.dstu.composite.ContainedDt;
import ca.uhn.fhir.model.dstu.composite.NarrativeDt;
import ca.uhn.fhir.model.dstu.resource.BaseResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.CodeDt;
import ca.uhn.fhir.model.primitive.IdDt;
@ -29,18 +27,14 @@ import ca.uhn.fhir.rest.param.StringParam;
public class ServerInvalidDefinitionTest {
@Test
public void testPrivateResourceProvider() {
/**
* Normal, should initialize properly
*/
@Test()
public void testBaseline() throws ServletException {
RestfulServer srv = new RestfulServer();
srv.setResourceProviders(new PrivateResourceProvider());
try {
srv.init();
fail();
} catch (ServletException e) {
assertThat(e.getCause().toString(), StringContains.containsString("ConfigurationException"));
assertThat(e.getCause().toString(), StringContains.containsString("public"));
}
srv.setResourceProviders(new InstantiableTypeForResourceProvider());
srv.init();
}
@Test
@ -57,19 +51,6 @@ public class ServerInvalidDefinitionTest {
}
}
@Test
public void testReadMethodWithSearchParameters() {
RestfulServer srv = new RestfulServer();
srv.setResourceProviders(new ReadMethodWithSearchParamProvider());
try {
srv.init();
fail();
} catch (ServletException e) {
assertThat(e.getCause().toString(), StringContains.containsString("ConfigurationException"));
}
}
@Test
public void testMultipleResourceProviderForSameType() {
RestfulServer srv = new RestfulServer();
@ -86,16 +67,16 @@ public class ServerInvalidDefinitionTest {
}
@Test
public void testSearchWithId() {
public void testPrivateResourceProvider() {
RestfulServer srv = new RestfulServer();
srv.setResourceProviders(new SearchWithIdParamProvider());
srv.setResourceProviders(new PrivateResourceProvider());
try {
srv.init();
fail();
} catch (ServletException e) {
assertThat(e.getCause().toString(), StringContains.containsString("ConfigurationException"));
assertThat(e.getCause().toString(), StringContains.containsString("compartment"));
assertThat(e.getCause().toString(), StringContains.containsString("public"));
}
}
@ -112,20 +93,51 @@ public class ServerInvalidDefinitionTest {
assertThat(e.getCause().toString(), StringContains.containsString("does not contain any valid HAPI-FHIR annotations"));
}
}
/**
* Normal, should initialize properly
*/
@Test()
public void testBaseline() throws ServletException {
@Test
public void testReadMethodWithoutIdParamProvider() {
RestfulServer srv = new RestfulServer();
srv.setResourceProviders(new InstantiableTypeForResourceProvider());
srv.init();
srv.setResourceProviders(new ReadMethodWithoutIdParamProvider());
try {
srv.init();
fail();
} catch (ServletException e) {
assertThat(e.getCause().toString(), StringContains.containsString("does not have a parameter"));
}
}
private static class PrivateResourceProvider implements IResourceProvider {
@Test
public void testReadMethodWithSearchParameters() {
RestfulServer srv = new RestfulServer();
srv.setResourceProviders(new ReadMethodWithSearchParamProvider());
try {
srv.init();
fail();
} catch (ServletException e) {
assertThat(e.getCause().toString(), StringContains.containsString("ConfigurationException"));
}
}
@Test
public void testSearchWithId() {
RestfulServer srv = new RestfulServer();
srv.setResourceProviders(new SearchWithIdParamProvider());
try {
srv.init();
fail();
} catch (ServletException e) {
assertThat(e.getCause().toString(), StringContains.containsString("ConfigurationException"));
assertThat(e.getCause().toString(), StringContains.containsString("compartment"));
}
}
public static class InstantiableTypeForResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
public Class<Patient> getResourceType() {
return Patient.class;
}
@ -136,107 +148,6 @@ public class ServerInvalidDefinitionTest {
}
public static class ReadMethodWithSearchParamProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Read
public Patient read(@IdParam IdDt theId, @RequiredParam(name = "aaa") StringParam theParam) {
return null;
}
}
public static class SearchWithIdParamProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Search
public List<Patient> read(@IdParam IdDt theId, @RequiredParam(name = "aaa") StringParam theParam) {
return null;
}
}
public static class ProviderWithNonResourceType implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return new IResource() {
@Override
public boolean isEmpty() {
return false;
}
@Override
public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
return null;
}
@Override
public void setResourceMetadata(ResourceMetadataMap theMap) {
}
@Override
public void setLanguage(CodeDt theLanguage) {
}
@Override
public void setId(IdDt theId) {
}
@Override
public NarrativeDt getText() {
return null;
}
@Override
public String getResourceName() {
return null;
}
@Override
public ResourceMetadataMap getResourceMetadata() {
return null;
}
@Override
public CodeDt getLanguage() {
return null;
}
@Override
public IdDt getId() {
return null;
}
@Override
public ContainedDt getContained() {
return null;
}
@Override
public FhirVersionEnum getStructureFhirVersionEnum() {
return FhirVersionEnum.DSTU1;
}
}.getClass();
}
@Search
public List<Patient> read(@IdParam IdDt theId, @RequiredParam(name = "aaa") StringParam theParam) {
return null;
}
}
public static class InvalidSpecialParameterNameResourceProvider implements IResourceProvider {
@Override
@ -251,20 +162,6 @@ public class ServerInvalidDefinitionTest {
}
public static class InstantiableTypeForResourceProvider implements IResourceProvider {
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
@Read
public Patient read(@IdParam IdDt theId) {
return null;
}
}
public static class PatientResourceProvider1 implements IResourceProvider {
@Override
@ -293,4 +190,132 @@ public class ServerInvalidDefinitionTest {
}
private static class PrivateResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Read
public Patient read(@IdParam IdDt theId) {
return null;
}
}
public static class ProviderWithNonResourceType implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return new IResource() {
@Override
public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
return null;
}
@Override
public ContainedDt getContained() {
return null;
}
@Override
public IdDt getId() {
return null;
}
@Override
public CodeDt getLanguage() {
return null;
}
@Override
public ResourceMetadataMap getResourceMetadata() {
return null;
}
@Override
public String getResourceName() {
return null;
}
@Override
public FhirVersionEnum getStructureFhirVersionEnum() {
return FhirVersionEnum.DSTU1;
}
@Override
public NarrativeDt getText() {
return null;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void setId(IdDt theId) {
}
@Override
public void setLanguage(CodeDt theLanguage) {
}
@Override
public void setResourceMetadata(ResourceMetadataMap theMap) {
}
}.getClass();
}
@Search
public List<Patient> read(@IdParam IdDt theId, @RequiredParam(name = "aaa") StringParam theParam) {
return null;
}
}
public static class ReadMethodWithoutIdParamProvider implements IResourceProvider {
@Override
public Class<? extends IBaseResource> getResourceType() {
return Patient.class;
}
@Read
public Patient read() {
return null;
}
}
public static class ReadMethodWithSearchParamProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Read
public Patient read(@IdParam IdDt theId, @RequiredParam(name = "aaa") StringParam theParam) {
return null;
}
}
public static class SearchWithIdParamProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Search
public List<Patient> read(@IdParam IdDt theId, @RequiredParam(name = "aaa") StringParam theParam) {
return null;
}
}
}

View File

@ -0,0 +1,103 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.*;
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.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.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.util.PortUtil;
public class SearchWithDstu2BundleTest {
private static CloseableHttpClient ourClient;
private static int ourPort;
private static Server ourServer;
private static FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SearchWithDstu2BundleTest.class);
@Test
public void testSearch() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_format=xml&_pretty=true");
// HttpResponse status = ourClient.execute(httpGet);
// String responseContent = IOUtils.toString(status.getEntity().getContent());
// IOUtils.closeQuietly(status.getEntity().getContent());
//
// assertEquals(200, status.getStatusLine().getStatusCode());
//
// ourLog.info(responseContent);
}
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourCtx = new FhirContext();
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer();
servlet.setFhirContext(ourCtx);
servlet.setResourceProviders(patientProvider);
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();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
@Search
public Bundle search() {
Bundle retVal = new Bundle();
Patient p1 = new Patient();
p1.setId("Patient/123/_history/456");
p1.addIdentifier().setLabel("p1ReadValue");
retVal.addEntry().setResource(p1);
return retVal;
}
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
}
}

View File

@ -717,7 +717,7 @@
<module>hapi-tinder-test</module>
<module>hapi-fhir-structures-dstu</module>
<module>hapi-fhir-structures-dstu2</module>
<module>hapi-fhir-structures-dev</module>
<!--<module>hapi-fhir-structures-dev</module>-->
<module>hapi-fhir-jpaserver-base</module>
<module>hapi-fhir-jpaserver-example</module>
<module>restful-server-example</module>