Skip to content

Commit

Permalink
Generate CapabilityStatement for DSTU3 conformance, Enable @Validate
Browse files Browse the repository at this point in the history
…for Jax-rs server
  • Loading branch information
SRiviere committed Jan 19, 2017
1 parent 1364616 commit 6cc7eeb
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
Expand Up @@ -41,8 +41,10 @@
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.dstu3.hapi.rest.server.ServerConformanceProvider;
import org.hl7.fhir.dstu3.model.Conformance;
import org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider;
import ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider;
import org.hl7.fhir.dstu3.model.CapabilityStatement;
import ca.uhn.fhir.model.dstu2.resource.Conformance;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -83,8 +85,8 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv
private RestulfulServerConfiguration serverConfiguration = new RestulfulServerConfiguration();

/** the conformance. It is created once during startup */
private Conformance myDstu3Conformance;
private ca.uhn.fhir.model.dstu2.resource.Conformance myDstu2Conformance;
private CapabilityStatement myDstu3Conformance;
private Conformance myDstu2Conformance;

/**
* Constructor allowing the description, servername and server to be set
Expand Down Expand Up @@ -142,11 +144,12 @@ protected void setUpPostConstruct() {
hardcodedServerAddressStrategy.setValue(getBaseForServer());
serverConfiguration.setServerAddressStrategy(hardcodedServerAddressStrategy);
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
ServerConformanceProvider serverConformanceProvider = new ServerConformanceProvider(serverConfiguration);
// ServerConformanceProvider serverConformanceProvider = new ServerConformanceProvider(serverConfiguration);
final ServerCapabilityStatementProvider serverConformanceProvider = new ServerCapabilityStatementProvider((serverConfiguration));
serverConformanceProvider.initializeOperations();
myDstu3Conformance = serverConformanceProvider.getServerConformance(null);
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider serverConformanceProvider = new ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider(serverConfiguration);
ServerConformanceProvider serverConformanceProvider = new ServerConformanceProvider(serverConfiguration);
serverConformanceProvider.initializeOperations();
myDstu2Conformance = serverConformanceProvider.getServerConformance(null);
}
Expand Down Expand Up @@ -275,9 +278,9 @@ public int addProvider(IResourceProvider theProvider, Class<? extends IResourceP
@Override
public Class<IBaseResource> getResourceType() {
if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
return Class.class.cast(Conformance.class);
return Class.class.cast(CapabilityStatement.class);
} else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) {
return Class.class.cast(ca.uhn.fhir.model.dstu2.resource.Conformance.class);
return Class.class.cast(Conformance.class);
}
return null;
}
Expand Down
Expand Up @@ -32,6 +32,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;

Expand Down Expand Up @@ -125,7 +126,8 @@ public IServerAddressStrategy getServerAddressStrategy() {
* @return the ascii string for the server base
*/
public String getBaseForServer() {
return getUriInfo().getBaseUri().toASCIIString();
final String url = getUriInfo().getBaseUri().toASCIIString();
return StringUtils.isNotBlank(url) && url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
}

/**
Expand Down
Expand Up @@ -286,6 +286,12 @@ public Response findCompartment(@PathParam("id") final String id, @PathParam("co
return execute(theRequest, compartment);
}

@POST
@Path("/$validate")
public Response validate(final String resource) throws IOException {
return customOperation(resource, RequestTypeEnum.POST, null, "$validate", RestOperationTypeEnum.EXTENDED_OPERATION_TYPE);
}

/**
* Execute the method described by the requestBuilder and methodKey
*
Expand Down
@@ -1,7 +1,9 @@
package ca.uhn.fhir.jaxrs.server;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -24,10 +26,11 @@
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.Conformance;
import org.hl7.fhir.dstu3.model.CapabilityStatement;
import org.hl7.fhir.dstu3.model.DateType;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.OperationOutcome;
import org.hl7.fhir.dstu3.model.Parameters;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Resource;
Expand Down Expand Up @@ -154,7 +157,7 @@ public void testConditionalCreate() throws Exception {
toCreate.getIdentifier().add(new Identifier().setValue("myIdentifier"));
outcome.setResource(toCreate);

when(mock.create(patientCaptor.capture(), eq("Patient?_format=json&identifier=2"))).thenReturn(outcome);
when(mock.create(patientCaptor.capture(), eq("/Patient?_format=json&identifier=2"))).thenReturn(outcome);
client.setEncoding(EncodingEnum.JSON);

MethodOutcome response = client.create().resource(toCreate).conditional()
Expand All @@ -168,7 +171,7 @@ public void testConditionalCreate() throws Exception {
/** Conformance - Server */
@Test
public void testConformance() {
final Conformance conf = client.fetchConformance().ofType(Conformance.class).execute();
final CapabilityStatement conf = client.fetchConformance().ofType(CapabilityStatement.class).execute();
assertEquals(conf.getRest().get(0).getResource().get(0).getType().toString(), "Patient");
}

Expand Down Expand Up @@ -426,6 +429,21 @@ public void testXFindUnknownPatient() {
assertTrue(e.getMessage().contains("999955541264"));
}
}

@Test
public void testValidate() {
// prepare mock
final OperationOutcome oo = new OperationOutcome();
final Patient patient = new Patient();
patient.addIdentifier((new Identifier().setValue("1")));
//invoke
final Parameters inParams = new Parameters();
inParams.addParameter().setResource(patient);

final MethodOutcome mO = client.validate().resource(patient).execute();
//verify
assertNotNull(mO.getOperationOutcome());
}

@BeforeClass
public static void setUpClass() throws Exception {
Expand Down
@@ -1,6 +1,7 @@
package ca.uhn.fhir.jaxrs.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -44,9 +45,11 @@
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome;
import ca.uhn.fhir.model.dstu2.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Conformance;
import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
import ca.uhn.fhir.model.dstu2.resource.Parameters;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.BoundCodeDt;
Expand Down Expand Up @@ -152,7 +155,7 @@ public void testConditionalCreate() throws Exception {
toCreate.getIdentifierFirstRep().setValue("myIdentifier");
outcome.setResource(toCreate);

when(mock.create(patientCaptor.capture(), eq("Patient?_format=json&identifier=2"))).thenReturn(outcome);
when(mock.create(patientCaptor.capture(), eq("/Patient?_format=json&identifier=2"))).thenReturn(outcome);
client.setEncoding(EncodingEnum.JSON);

MethodOutcome response = client.create().resource(toCreate).conditional()
Expand Down Expand Up @@ -411,6 +414,21 @@ public void testXFindUnknownPatient() {
}
}

@Test
public void testValidate() {
// prepare mock
final OperationOutcome oo = new OperationOutcome();
final Patient patient = new Patient();
patient.addIdentifier((new IdentifierDt().setValue("1")));
//invoke
final Parameters inParams = new Parameters();
inParams.addParameter().setResource(patient);

final MethodOutcome mO = client.validate().resource(patient).execute();
//verify
assertNotNull(mO.getOperationOutcome());
}

private <T> T withId(final T id) {
return argThat(new BaseMatcher<T>() {
@Override
Expand Down
Expand Up @@ -17,6 +17,7 @@
import ca.uhn.fhir.jaxrs.server.AbstractJaxRsResourceProvider;
import ca.uhn.fhir.jaxrs.server.interceptor.JaxRsExceptionInterceptor;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
import ca.uhn.fhir.model.dstu2.resource.Parameters;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
Expand All @@ -32,6 +33,7 @@
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Update;
import ca.uhn.fhir.rest.annotation.Validate;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
Expand Down Expand Up @@ -125,6 +127,13 @@ public Response someCustomOperationUsingPost(@PathParam("id") String id, String
public Parameters someCustomOperation(@IdParam IdDt myId, @OperationParam(name = "dummy") StringDt dummyInput) {
return mock.someCustomOperation(myId, dummyInput);
}

@Validate()
public MethodOutcome validate(@ResourceParam final Patient resource) {
final MethodOutcome mO = new MethodOutcome();
mO.setOperationOutcome(new OperationOutcome());
return mO;
}

@Override
public Class<Patient> getResourceType() {
Expand Down
Expand Up @@ -13,6 +13,7 @@
import javax.ws.rs.core.Response;

import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.OperationOutcome;
import org.hl7.fhir.dstu3.model.Parameters;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.StringType;
Expand All @@ -34,6 +35,7 @@
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Update;
import ca.uhn.fhir.rest.annotation.Validate;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
Expand Down Expand Up @@ -128,6 +130,13 @@ public Parameters someCustomOperation(@IdParam IdType myId, @OperationParam(name
return mock.someCustomOperation(myId, dummyInput);
}

@Validate()
public MethodOutcome validate(@ResourceParam final Patient resource) {
MethodOutcome mO = new MethodOutcome();
mO.setOperationOutcome(new OperationOutcome());
return mO;
}

@Override
public Class<Patient> getResourceType() {
return Patient.class;
Expand Down

0 comments on commit 6cc7eeb

Please sign in to comment.