Skip to content

Commit

Permalink
Cleanup tests for java config in JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Oct 20, 2015
1 parent 9c0f6f9 commit 938a251
Show file tree
Hide file tree
Showing 22 changed files with 2,880 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,24 @@ public void postConstruct() {

@Override
public ValueSet expand(IIdType theId, String theFilter) {
ValueSet source = loadValueSetForExpansion(theId);
return expand(source, theFilter);

}

private ValueSet loadValueSetForExpansion(IIdType theId) {
if (theId.getValue().startsWith("http://hl7.org/fhir/")) {
org.hl7.fhir.instance.model.ValueSet valueSet = myValidationSupport.fetchResource(myRiCtx, org.hl7.fhir.instance.model.ValueSet.class, theId.getValue());
if (valueSet != null) {
return getContext().newJsonParser().parseResource(ValueSet.class, myRiCtx.newJsonParser().encodeResourceToString(valueSet));
}
}
BaseHasResource sourceEntity = readEntity(theId);
if (sourceEntity == null) {
throw new ResourceNotFoundException(theId);
}
ValueSet source = (ValueSet) toResource(sourceEntity, false);

return expand(source, theFilter);

return source;
}

@Override
Expand Down Expand Up @@ -182,7 +192,7 @@ public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateC
if (!haveCodeableConcept && !haveCoding && !haveCode) {
throw new InvalidRequestException("No code, coding, or codeableConcept provided to validate");
}
if (!(haveCodeableConcept ^ haveCoding ^ haveCode)) {
if (!multiXor(haveCodeableConcept, haveCoding, haveCode)) {
throw new InvalidRequestException("$validate-code can only validate (system AND code) OR (coding) OR (codeableConcept)");
}

Expand All @@ -199,11 +209,9 @@ public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateC
if (theCode == null || theCode.isEmpty()) {
throw new InvalidRequestException("Either ValueSet ID or ValueSet identifier or system and code must be provided. Unable to validate.");
}
Set<Long> ids = searchForIds(ValueSet.SP_CODE, new TokenParam(toStringOrNull(theSystem), theCode.getValue()));
valueSetIds = new ArrayList<IIdType>();
for (Long next : ids) {
valueSetIds.add(new IdDt("ValueSet", next));
}
String code = theCode.getValue();
String system = toStringOrNull(theSystem);
valueSetIds = findValueSetIdsContainingSystemAndCode(code, system);
}

for (IIdType nextId : valueSetIds) {
Expand All @@ -223,6 +231,30 @@ public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validateC
return new ValidateCodeResult(false, "Code not found", null);
}

private List<IIdType> findValueSetIdsContainingSystemAndCode(String theCode, String theSystem) {
if (theSystem != null && theSystem.startsWith("http://hl7.org/fhir/")) {
return Collections.singletonList((IIdType)new IdDt(theSystem));
}

List<IIdType> valueSetIds;
Set<Long> ids = searchForIds(ValueSet.SP_CODE, new TokenParam(theSystem, theCode));
valueSetIds = new ArrayList<IIdType>();
for (Long next : ids) {
valueSetIds.add(new IdDt("ValueSet", next));
}
return valueSetIds;
}

private static boolean multiXor(boolean... theValues) {
int count = 0;
for (int i = 0; i < theValues.length; i++) {
if (theValues[i]) {
count++;
}
}
return count == 1;
}

private String toStringOrNull(IPrimitiveType<String> thePrimitive) {
return thePrimitive != null ? thePrimitive.getValue() : null;
}
Expand Down Expand Up @@ -259,4 +291,68 @@ private ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult validate
return null;
}

@Override
public ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.LookupCodeResult lookupCode(CodeDt theCode, UriDt theSystem, CodingDt theCoding) {
boolean haveCoding = theCoding != null && isNotBlank(theCoding.getSystem()) && isNotBlank(theCoding.getCode());
boolean haveCode = theCode != null && theCode.isEmpty() == false;
boolean haveSystem = theSystem != null && theSystem.isEmpty() == false;

if (!haveCoding && !(haveSystem && haveCode)) {
throw new InvalidRequestException("No code, coding, or codeableConcept provided to validate");
}
if (!multiXor(haveCoding, (haveSystem && haveCode)) || (haveSystem != haveCode)) {
throw new InvalidRequestException("$lookup can only validate (system AND code) OR (coding.system AND coding.code)");
}

String code;
String system;
if (haveCoding) {
code = theCoding.getCode();
system = theCoding.getSystem();
} else {
code = theCode.getValue();
system = theSystem.getValue();
}

List<IIdType> valueSetIds = findValueSetIdsContainingSystemAndCode(code, system);
for (IIdType nextId : valueSetIds) {
ValueSet expansion = expand(nextId, null);
List<ExpansionContains> contains = expansion.getExpansion().getContains();
ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.LookupCodeResult result = lookup(contains, system, code);
if (result != null) {
return result;
}
}

LookupCodeResult retVal = new LookupCodeResult();
retVal.setFound(false);
retVal.setSearchedForCode(code);
retVal.setSearchedForSystem(system);
return retVal;
}

private ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.LookupCodeResult lookup(List<ExpansionContains> theContains, String theSystem, String theCode) {
for (ExpansionContains nextCode : theContains) {

String system = nextCode.getSystem();
String code = nextCode.getCode();
if (theSystem.equals(system) && theCode.equals(code)) {
ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.LookupCodeResult retVal = new LookupCodeResult();
retVal.setSearchedForCode(code);
retVal.setSearchedForSystem(system);
retVal.setFound(true);
if (nextCode.getAbstract() != null) {
retVal.setCodeIsAbstract(nextCode.getAbstract().booleanValue());
}
retVal.setCodeDisplay(nextCode.getDisplay());
retVal.setCodeSystemVersion(nextCode.getVersion());
retVal.setCodeSystemDisplayName("Unknown"); // TODO: implement
return retVal;
}

}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,81 @@
public interface IFhirResourceDaoValueSet<T extends IBaseResource> extends IFhirResourceDao<T> {

ValueSet expand(IIdType theId, String theFilter);

ValueSet expand(ValueSet theSource, String theFilter);

ValueSet expandByIdentifier(String theUri, String theFilter);

LookupCodeResult lookupCode(CodeDt theCode, UriDt theSystem, CodingDt theCoding);

ValidateCodeResult validateCode(UriDt theValueSetIdentifier, IIdType theId, CodeDt theCode, UriDt theSystem, StringDt theDisplay, CodingDt theCoding, CodeableConceptDt theCodeableConcept);

public class LookupCodeResult {
private String myCodeDisplay;
private boolean myCodeIsAbstract;
private String myCodeSystemDisplayName;
private String myCodeSystemVersion;
private boolean myFound;
private String mySearchedForCode;
private String mySearchedForSystem;

public String getCodeDisplay() {
return myCodeDisplay;
}

public String getCodeSystemDisplayName() {
return myCodeSystemDisplayName;
}

public String getCodeSystemVersion() {
return myCodeSystemVersion;
}

public String getSearchedForCode() {
return mySearchedForCode;
}

public String getSearchedForSystem() {
return mySearchedForSystem;
}

public boolean isCodeIsAbstract() {
return myCodeIsAbstract;
}

public boolean isFound() {
return myFound;
}

public void setCodeDisplay(String theCodeDisplay) {
myCodeDisplay = theCodeDisplay;
}

public void setCodeIsAbstract(boolean theCodeIsAbstract) {
myCodeIsAbstract = theCodeIsAbstract;
}

public void setCodeSystemDisplayName(String theCodeSystemDisplayName) {
myCodeSystemDisplayName = theCodeSystemDisplayName;
}

public void setCodeSystemVersion(String theCodeSystemVersion) {
myCodeSystemVersion = theCodeSystemVersion;
}

public void setFound(boolean theFound) {
myFound = theFound;
}

public void setSearchedForCode(String theSearchedForCode) {
mySearchedForCode = theSearchedForCode;
}

public void setSearchedForSystem(String theSearchedForSystem) {
mySearchedForSystem = theSearchedForSystem;
}
}

public class ValidateCodeResult {
private String myDisplay;
private String myMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.lang3.BooleanUtils;

import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.LookupCodeResult;
import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult;
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
Expand All @@ -41,6 +42,7 @@
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.OperationParam;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;

public class BaseJpaResourceProviderValueSetDstu2 extends JpaResourceProviderDstu2<ValueSet> {

Expand Down Expand Up @@ -102,6 +104,42 @@ private String toFilterString(StringDt theFilter) {
return theFilter != null ? theFilter.getValue() : null;
}

//@formatter:off
@Operation(name = "$lookup", idempotent = true, returnParameters= {
@OperationParam(name="name", type=StringDt.class, min=1),
@OperationParam(name="version", type=StringDt.class, min=0),
@OperationParam(name="display", type=StringDt.class, min=1),
@OperationParam(name="abstract", type=BooleanDt.class, min=1),
})
public Parameters lookup(
HttpServletRequest theServletRequest,
@OperationParam(name="code", min=0, max=1) CodeDt theCode,
@OperationParam(name="system", min=0, max=1) UriDt theSystem,
@OperationParam(name="coding", min=0, max=1) CodingDt theCoding
) {
//@formatter:on

startRequest(theServletRequest);
try {
IFhirResourceDaoValueSet<ValueSet> dao = (IFhirResourceDaoValueSet<ValueSet>) getDao();
LookupCodeResult result = dao.lookupCode(theCode, theSystem, theCoding);
if (result.isFound()==false) {
throw new ResourceNotFoundException("Unable to find code[" + result.getSearchedForCode() + "] in system[" + result.getSearchedForSystem() + "]");
}
Parameters retVal = new Parameters();
retVal.addParameter().setName("name").setValue(new StringDt(result.getCodeSystemDisplayName()));
if (isNotBlank(result.getCodeSystemVersion())) {
retVal.addParameter().setName("version").setValue(new StringDt(result.getCodeSystemVersion()));
}
retVal.addParameter().setName("display").setValue(new StringDt(result.getCodeDisplay()));
retVal.addParameter().setName("abstract").setValue(new BooleanDt(result.isCodeIsAbstract()));
return retVal;
} finally {
endRequest(theServletRequest);
}
}


//@formatter:off
@Operation(name = "$validate-code", idempotent = true, returnParameters= {
@OperationParam(name="result", type=BooleanDt.class, min=1),
Expand Down Expand Up @@ -137,4 +175,6 @@ public Parameters validateCode(
endRequest(theServletRequest);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ca.uhn.fhir.jpa.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class DispatcherServletConfig {
//nothing
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import org.junit.Before;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;

import ca.uhn.fhir.jpa.config.DispatcherServletConfig;
import ca.uhn.fhir.jpa.config.TestDstu2Config;
import ca.uhn.fhir.jpa.dao.BaseJpaDstu2Test;
import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider;
import ca.uhn.fhir.model.api.Bundle;
Expand Down Expand Up @@ -126,7 +129,7 @@ public void before() throws Exception {

DispatcherServlet dispatcherServlet = new DispatcherServlet();
// dispatcherServlet.setApplicationContext(webApplicationContext);
dispatcherServlet.setContextConfigLocation("classpath:/fhir-spring-subscription-config-dstu2.xml");
dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
ServletHolder subsServletHolder = new ServletHolder();
subsServletHolder.setServlet(dispatcherServlet);
proxyHandler.addServlet(subsServletHolder, "/*");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ca.uhn.fhir.jpa.provider;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.stringContainsInOrder;
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 java.util.ArrayList;
import java.util.Date;
Expand All @@ -21,9 +25,10 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.config.TestDstu1Config;
import ca.uhn.fhir.jpa.dao.BaseJpaTest;
import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
Expand Down Expand Up @@ -60,7 +65,7 @@

public class ResourceProviderDstu1Test extends BaseJpaTest {

private static ClassPathXmlApplicationContext ourAppCtx;
private static AnnotationConfigApplicationContext ourAppCtx;
private static IGenericClient ourClient;
private static DaoConfig ourDaoConfig;
private static FhirContext ourCtx = FhirContext.forDstu1();
Expand Down Expand Up @@ -523,7 +528,7 @@ public static void beforeClass() throws Exception {

ourServerBase = "http://localhost:" + port + "/fhir/context";

ourAppCtx = new ClassPathXmlApplicationContext("hapi-fhir-server-resourceproviders-dstu1.xml", "fhir-jpabase-spring-test-config.xml");
ourAppCtx = new AnnotationConfigApplicationContext(TestDstu1Config.class);

ourDaoConfig = (DaoConfig) ourAppCtx.getBean(DaoConfig.class);

Expand Down

0 comments on commit 938a251

Please sign in to comment.