Skip to content

Commit

Permalink
Refactor ValueSet JPA RP to use optional ID params
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Oct 6, 2015
1 parent b62eb11 commit a337528
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.BooleanUtils;

import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult;
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
Expand All @@ -38,66 +40,64 @@
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.OperationParam;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;

public class BaseJpaResourceProviderValueSetDstu2 extends JpaResourceProviderDstu2<ValueSet> {

//@formatter:off
@Operation(name = "$expand", idempotent = true)
public ValueSet expant(
HttpServletRequest theServletRequest,

@IdParam IdDt theId,

@IdParam(optional=true) IdDt theId,
@OperationParam(name="valueSet", min=0, max=1) ValueSet theValueSet,
@OperationParam(name="identifier", min=0, max=1) UriDt theIdentifier,
@OperationParam(name = "filter", min=0, max=1) StringDt theFilter) {
//@formatter:on

startRequest(theServletRequest);
try {
IFhirResourceDaoValueSet<ValueSet> dao = (IFhirResourceDaoValueSet<ValueSet>) getDao();
return dao.expand(theId, toFilterString(theFilter));
} finally {
endRequest(theServletRequest);
boolean haveId = theId != null && theId.hasIdPart();
boolean haveIdentifier = theIdentifier != null && isNotBlank(theIdentifier.getValue());
boolean haveValueSet = theValueSet != null && theValueSet.isEmpty() == false;

if (!haveId && !haveIdentifier && !haveValueSet) {
throw new InvalidRequestException("$expand operation at the type level (no ID specified) requires an identifier or a valueSet as a part of the request");
}
}

//@formatter:off
@Operation(name = "$expand", idempotent = true)
public ValueSet everything(
HttpServletRequest theServletRequest,

@OperationParam(name="identifier", min=1, max=1) UriDt theIdentifier,

@OperationParam(name = "filter", min=0, max=1) StringDt theFilter) {
//@formatter:on
if (moreThanOneTrue(haveId, haveIdentifier, haveValueSet)) {
throw new InvalidRequestException("$expand must EITHER be invoked at the type level, or have an identifier specified, or have a ValueSet specified. Can not combine these options.");
}

startRequest(theServletRequest);
try {
IFhirResourceDaoValueSet<ValueSet> dao = (IFhirResourceDaoValueSet<ValueSet>) getDao();
return dao.expandByIdentifier(theIdentifier.getValue(), toFilterString(theFilter));
if (haveId) {
return dao.expand(theId, toFilterString(theFilter));
} else if (haveIdentifier) {
return dao.expandByIdentifier(theIdentifier.getValue(), toFilterString(theFilter));
} else {
return dao.expand(theValueSet, toFilterString(theFilter));
}

} finally {
endRequest(theServletRequest);
}
}

//@formatter:off
@Operation(name = "$expand", idempotent = true)
public ValueSet everything(
HttpServletRequest theServletRequest,

@OperationParam(name="valueSet", min=1, max=1) ValueSet theValueSet,

@OperationParam(name = "filter", min=0, max=1) StringDt theFilter) {
//@formatter:on

startRequest(theServletRequest);
try {
IFhirResourceDaoValueSet<ValueSet> dao = (IFhirResourceDaoValueSet<ValueSet>) getDao();
return dao.expand(theValueSet, toFilterString(theFilter));
} finally {
endRequest(theServletRequest);
private static boolean moreThanOneTrue(boolean... theBooleans) {
boolean haveOne = false;
for (boolean next : theBooleans) {
if (next) {
if (haveOne) {
return true;
} else {
haveOne = true;
}
}
}
return false;
}


private String toFilterString(StringDt theFilter) {
return theFilter != null ? theFilter.getValue() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.io.IOException;

Expand All @@ -16,6 +18,7 @@
import ca.uhn.fhir.model.dstu2.resource.ValueSet;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;

public class ResourceProviderDstu2ValueSetTest extends BaseResourceProviderDstu2Test {

Expand Down Expand Up @@ -157,5 +160,54 @@ public void testExpandByValueSet() throws IOException {
assertThat(resp, not(containsString("<code value=\"8450-9\"/>")));
}

@Test
public void testExpandInvalidParams() throws IOException {
//@formatter:off
try {
ourClient
.operation()
.onType(ValueSet.class)
.named("expand")
.withNoParameters(Parameters.class)
.execute();
fail();
} catch (InvalidRequestException e) {
assertEquals("HTTP 400 Bad Request: $expand operation at the type level (no ID specified) requires an identifier or a valueSet as a part of the request", e.getMessage());
}
//@formatter:on

//@formatter:off
try {
ValueSet toExpand = loadResourceFromClasspath(ValueSet.class, "/extensional-case-2.xml");
ourClient
.operation()
.onType(ValueSet.class)
.named("expand")
.withParameter(Parameters.class, "valueSet", toExpand)
.andParameter("identifier", new UriDt("http://www.healthintersections.com.au/fhir/ValueSet/extensional-case-2"))
.execute();
fail();
} catch (InvalidRequestException e) {
assertEquals("HTTP 400 Bad Request: $expand must EITHER be invoked at the type level, or have an identifier specified, or have a ValueSet specified. Can not combine these options.", e.getMessage());
}
//@formatter:on

//@formatter:off
try {
ValueSet toExpand = loadResourceFromClasspath(ValueSet.class, "/extensional-case-2.xml");
ourClient
.operation()
.onInstance(myExtensionalVsId)
.named("expand")
.withParameter(Parameters.class, "valueSet", toExpand)
.andParameter("identifier", new UriDt("http://www.healthintersections.com.au/fhir/ValueSet/extensional-case-2"))
.execute();
fail();
} catch (InvalidRequestException e) {
assertEquals("HTTP 400 Bad Request: $expand must EITHER be invoked at the type level, or have an identifier specified, or have a ValueSet specified. Can not combine these options.", e.getMessage());
}
//@formatter:on

}

}

0 comments on commit a337528

Please sign in to comment.