Tweak to validator

This commit is contained in:
James Agnew 2019-05-06 10:18:55 -04:00
parent 64f57b9736
commit 61902da527
4 changed files with 211 additions and 57 deletions

View File

@ -1,9 +1,5 @@
package ca.uhn.fhir.jpa.dao.dstu3; package ca.uhn.fhir.jpa.dao.dstu3;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import ca.uhn.fhir.jpa.util.TestUtil; import ca.uhn.fhir.jpa.util.TestUtil;
import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.MethodOutcome;
@ -12,21 +8,33 @@ import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException; import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import ca.uhn.fhir.util.StopWatch; import ca.uhn.fhir.util.StopWatch;
import ca.uhn.fhir.validation.IValidatorModule;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.hl7.fhir.dstu3.hapi.validation.FhirInstanceValidator;
import org.hl7.fhir.dstu3.model.*; import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.Observation.ObservationStatus; import org.hl7.fhir.dstu3.model.Observation.ObservationStatus;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.utils.IResourceValidator;
import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.AopTestUtils;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class FhirResourceDaoDstu3ValidateTest extends BaseJpaDstu3Test { public class FhirResourceDaoDstu3ValidateTest extends BaseJpaDstu3Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoDstu3ValidateTest.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoDstu3ValidateTest.class);
@Autowired
private IValidatorModule myValidatorModule;
@Test @Test
public void testValidateChangedQuestionnaire() { public void testValidateChangedQuestionnaire() {
@ -73,6 +81,77 @@ public class FhirResourceDaoDstu3ValidateTest extends BaseJpaDstu3Test {
} }
@After
public void after() {
FhirInstanceValidator val = AopTestUtils.getTargetObject(myValidatorModule);
val.setBestPracticeWarningLevel(IResourceValidator.BestPracticeWarningLevel.Warning);
}
@Test
public void testValidateWithCanonicalReference() {
FhirInstanceValidator val = AopTestUtils.getTargetObject(myValidatorModule);
IResourceValidator.BestPracticeWarningLevel a = IResourceValidator.BestPracticeWarningLevel.Ignore;
val.setBestPracticeWarningLevel(a);
ValueSet vs = new ValueSet();
vs.setId("MYVS");
vs.setUrl("http://myvs");
vs.getCompose()
.addInclude()
.setSystem("http://hl7.org/fhir/administrative-gender")
.addConcept(new ValueSet.ConceptReferenceComponent().setCode("male"))
.addConcept(new ValueSet.ConceptReferenceComponent().setCode("female"));
myValueSetDao.update(vs);
Questionnaire q = new Questionnaire();
q.setId("MYQ");
q.setUrl("http://hl7.org/fhir/Questionnaire/myq");
q.addItem()
.setLinkId("LINKID")
.setType(Questionnaire.QuestionnaireItemType.CHOICE)
.setOptions(new Reference().setReference("ValueSet/MYVS"));
myQuestionnaireDao.update(q);
// Validate with matching code
QuestionnaireResponse qr = new QuestionnaireResponse();
qr.setStatus(QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED);
qr.setQuestionnaire(new Reference("Questionnaire/MYQ"));
qr.addItem()
.setLinkId("LINKID")
.addAnswer()
.setValue(new Coding().setSystem("http://hl7.org/fhir/administrative-gender").setCode("aaa").setDisplay("AAAA"));
// Validate as resource
try {
MethodOutcome outcome = myQuestionnaireResponseDao.validate(qr, null, null, null, ValidationModeEnum.CREATE, null, mySrd);
OperationOutcome oo = (OperationOutcome) outcome.getOperationOutcome();
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
fail();
} catch (PreconditionFailedException e) {
OperationOutcome oo = (OperationOutcome) e.getOperationOutcome();
String encoded = myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo);
ourLog.info(encoded);
assertThat(encoded, containsString("is not in the options value set"));
}
// Validate as string
try {
String raw = myFhirCtx.newJsonParser().encodeResourceToString(qr);
MethodOutcome outcome = myQuestionnaireResponseDao.validate(qr, null, raw, EncodingEnum.JSON, ValidationModeEnum.CREATE, null, mySrd);
OperationOutcome oo = (OperationOutcome) outcome.getOperationOutcome();
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
fail();
} catch (PreconditionFailedException e) {
OperationOutcome oo = (OperationOutcome) e.getOperationOutcome();
String encoded = myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo);
ourLog.info(encoded);
assertThat(encoded, containsString("is not in the options value set"));
}
}
@Test @Test
public void testValidateStructureDefinition() throws Exception { public void testValidateStructureDefinition() throws Exception {
String input = IOUtils.toString(getClass().getResourceAsStream("/sd-david-dhtest7.json"), StandardCharsets.UTF_8); String input = IOUtils.toString(getClass().getResourceAsStream("/sd-david-dhtest7.json"), StandardCharsets.UTF_8);

View File

@ -1,9 +1,5 @@
package ca.uhn.fhir.jpa.dao.r4; package ca.uhn.fhir.jpa.dao.r4;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.ValidationModeEnum; import ca.uhn.fhir.rest.api.ValidationModeEnum;
@ -13,41 +9,40 @@ import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import ca.uhn.fhir.util.StopWatch; import ca.uhn.fhir.util.StopWatch;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.IValidatorModule;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r4.model.CanonicalType;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Observation;
import org.hl7.fhir.r4.model.Observation.ObservationStatus; import org.hl7.fhir.r4.model.Observation.ObservationStatus;
import org.hl7.fhir.r4.model.OperationOutcome; import org.hl7.fhir.r4.utils.IResourceValidator;
import org.hl7.fhir.r4.model.Organization; import org.junit.After;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.StructureDefinition;
import org.hl7.fhir.r4.model.ValueSet;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.AopTestUtils;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test { public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoR4ValidateTest.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoR4ValidateTest.class);
@Autowired
@AfterClass private IValidatorModule myValidatorModule;
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@Test @Test
public void testValidateStructureDefinition() throws Exception { public void testValidateStructureDefinition() throws Exception {
String input = IOUtils.toString(getClass().getResourceAsStream("/r4/sd-david-dhtest7.json"), StandardCharsets.UTF_8); String input = IOUtils.toString(getClass().getResourceAsStream("/r4/sd-david-dhtest7.json"), StandardCharsets.UTF_8);
StructureDefinition sd = myFhirCtx.newJsonParser().parseResource(StructureDefinition.class, input); StructureDefinition sd = myFhirCtx.newJsonParser().parseResource(StructureDefinition.class, input);
ourLog.info("Starting validation"); ourLog.info("Starting validation");
try { try {
myStructureDefinitionDao.validate(sd, null, null, null, ValidationModeEnum.UPDATE, null, mySrd); myStructureDefinitionDao.validate(sd, null, null, null, ValidationModeEnum.UPDATE, null, mySrd);
@ -55,7 +50,7 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome())); ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome()));
} }
ourLog.info("Done validation"); ourLog.info("Done validation");
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
ourLog.info("Starting validation"); ourLog.info("Starting validation");
try { try {
@ -66,13 +61,13 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
ourLog.info("Done validation in {}ms", sw.getMillis()); ourLog.info("Done validation in {}ms", sw.getMillis());
} }
@Test @Test
public void testValidateDocument() throws Exception { public void testValidateDocument() throws Exception {
String input = IOUtils.toString(getClass().getResourceAsStream("/r4/document-bundle.json"), StandardCharsets.UTF_8); String input = IOUtils.toString(getClass().getResourceAsStream("/r4/document-bundle.json"), StandardCharsets.UTF_8);
Bundle document = myFhirCtx.newJsonParser().parseResource(Bundle.class, input); Bundle document = myFhirCtx.newJsonParser().parseResource(Bundle.class, input);
ourLog.info("Starting validation"); ourLog.info("Starting validation");
try { try {
MethodOutcome outcome = myBundleDao.validate(document, null, null, null, ValidationModeEnum.CREATE, null, mySrd); MethodOutcome outcome = myBundleDao.validate(document, null, null, null, ValidationModeEnum.CREATE, null, mySrd);
@ -80,7 +75,7 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome())); ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome()));
} }
ourLog.info("Done validation"); ourLog.info("Done validation");
// ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome.getOperationOutcome())); // ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome.getOperationOutcome()));
} }
@ -139,24 +134,24 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
MethodOutcome outcome = null; MethodOutcome outcome = null;
ValidationModeEnum mode = ValidationModeEnum.CREATE; ValidationModeEnum mode = ValidationModeEnum.CREATE;
switch (enc) { switch (enc) {
case JSON: case JSON:
encoded = myFhirCtx.newJsonParser().encodeResourceToString(input); encoded = myFhirCtx.newJsonParser().encodeResourceToString(input);
try { try {
myObservationDao.validate(input, null, encoded, EncodingEnum.JSON, mode, null, mySrd); myObservationDao.validate(input, null, encoded, EncodingEnum.JSON, mode, null, mySrd);
fail(); fail();
} catch (PreconditionFailedException e) { } catch (PreconditionFailedException e) {
return (OperationOutcome) e.getOperationOutcome(); return (OperationOutcome) e.getOperationOutcome();
} }
break; break;
case XML: case XML:
encoded = myFhirCtx.newXmlParser().encodeResourceToString(input); encoded = myFhirCtx.newXmlParser().encodeResourceToString(input);
try { try {
myObservationDao.validate(input, null, encoded, EncodingEnum.XML, mode, null, mySrd); myObservationDao.validate(input, null, encoded, EncodingEnum.XML, mode, null, mySrd);
fail(); fail();
} catch (PreconditionFailedException e) { } catch (PreconditionFailedException e) {
return (OperationOutcome) e.getOperationOutcome(); return (OperationOutcome) e.getOperationOutcome();
} }
break; break;
} }
throw new IllegalStateException(); // shouldn't get here throw new IllegalStateException(); // shouldn't get here
@ -185,6 +180,57 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
} }
@Test
public void testValidateWithCanonicalReference() {
FhirInstanceValidator val = AopTestUtils.getTargetObject(myValidatorModule);
val.setBestPracticeWarningLevel(IResourceValidator.BestPracticeWarningLevel.Ignore);
ValueSet vs = new ValueSet();
vs.setId("MYVS");
vs.setUrl("http://myvs");
vs.getCompose()
.addInclude()
.setSystem("http://hl7.org/fhir/administrative-gender")
.addConcept(new ValueSet.ConceptReferenceComponent().setCode("male"))
.addConcept(new ValueSet.ConceptReferenceComponent().setCode("female"));
myValueSetDao.update(vs);
Questionnaire q = new Questionnaire();
q.setId("MYQ");
q.setUrl("http://myquestionnaire");
q.addItem()
.setLinkId("LINKID")
.setType(Questionnaire.QuestionnaireItemType.CHOICE)
.setAnswerValueSet("ValueSet/MYVS");
myQuestionnaireDao.update(q);
// Validate with matching code
QuestionnaireResponse qr = new QuestionnaireResponse();
qr.setStatus(QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED);
qr.setQuestionnaire("Questionnaire/MYQ");
qr.addItem()
.setLinkId("LINKID")
.addAnswer()
.setValue(new Coding().setSystem("http://hl7.org/fhir/administrative-gender").setCode("aaa"));
try {
MethodOutcome outcome = myQuestionnaireResponseDao.validate(qr, null, null, null, ValidationModeEnum.CREATE, null, mySrd);
OperationOutcome oo = (OperationOutcome) outcome.getOperationOutcome();
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
fail();
} catch (PreconditionFailedException e) {
OperationOutcome oo = (OperationOutcome) e.getOperationOutcome();
String encoded = myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo);
ourLog.info(encoded);
assertThat(encoded, containsString("is not in the options value set"));
}
}
@After
public void after() {
FhirInstanceValidator val = AopTestUtils.getTargetObject(myValidatorModule);
val.setBestPracticeWarningLevel(IResourceValidator.BestPracticeWarningLevel.Warning);
}
@Test @Test
public void testValidateForCreate() { public void testValidateForCreate() {
String methodName = "testValidateForCreate"; String methodName = "testValidateForCreate";
@ -337,18 +383,18 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
} }
return retVal; return retVal;
} }
/** /**
* Format has changed, this is out of date * Format has changed, this is out of date
*/ */
@Test @Test
@Ignore @Ignore
public void testValidateNewQuestionnaireFormat() throws Exception { public void testValidateNewQuestionnaireFormat() throws Exception {
String input =IOUtils.toString(FhirResourceDaoR4ValidateTest.class.getResourceAsStream("/questionnaire_r4.xml")); String input = IOUtils.toString(FhirResourceDaoR4ValidateTest.class.getResourceAsStream("/questionnaire_r4.xml"));
try { try {
MethodOutcome results = myQuestionnaireDao.validate(null, null, input, EncodingEnum.XML, ValidationModeEnum.UPDATE, null, mySrd); MethodOutcome results = myQuestionnaireDao.validate(null, null, input, EncodingEnum.XML, ValidationModeEnum.UPDATE, null, mySrd);
OperationOutcome oo = (OperationOutcome) results.getOperationOutcome(); OperationOutcome oo = (OperationOutcome) results.getOperationOutcome();
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo)); ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
} catch (PreconditionFailedException e) { } catch (PreconditionFailedException e) {
// this is a failure of the test // this is a failure of the test
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome())); ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome()));
@ -356,4 +402,9 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
} }
} }
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
} }

View File

@ -391,6 +391,26 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
} }
@Test
public void testCreateWithClientAssignedId() throws IOException {
Patient p = new Patient();
p.setActive(true);
p.setId("AAA");
String encoded = myFhirCtx.newJsonParser().encodeResourceToString(p);
HttpPut httpPut = new HttpPut(ourServerBase + "/Patient/AAA");
httpPut.setEntity(new StringEntity(encoded, ContentType.parse("application/json+fhir")));
try (CloseableHttpResponse status = ourHttpClient.execute(httpPut)) {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(status.getStatusLine().toString());
ourLog.info(responseContent);
assertEquals(201, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("true"));
}
}
@Before @Before

View File

@ -2371,13 +2371,17 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
return null; return null;
} else { } else {
long t = System.nanoTime(); long t = System.nanoTime();
if (!Utilities.isAbsoluteUrl(reference)) ValueSet fr = context.fetchResource(ValueSet.class, reference);
reference = resolve(uri, reference); if (fr == null) {
ValueSet fr = context.fetchResource(ValueSet.class, reference); if (!Utilities.isAbsoluteUrl(reference)) {
txTime = txTime + (System.nanoTime() - t); reference = resolve(uri, reference);
fr = context.fetchResource(ValueSet.class, reference);
}
}
txTime = txTime + (System.nanoTime() - t);
return fr; return fr;
} }
} else } else
return null; return null;
} }