Tweak to validator
This commit is contained in:
parent
64f57b9736
commit
61902da527
|
@ -1,9 +1,5 @@
|
|||
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.rest.api.EncodingEnum;
|
||||
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.UnprocessableEntityException;
|
||||
import ca.uhn.fhir.util.StopWatch;
|
||||
import ca.uhn.fhir.validation.IValidatorModule;
|
||||
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.Bundle.BundleEntryComponent;
|
||||
import org.hl7.fhir.dstu3.model.Observation.ObservationStatus;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
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.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.util.AopTestUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
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 {
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoDstu3ValidateTest.class);
|
||||
@Autowired
|
||||
private IValidatorModule myValidatorModule;
|
||||
|
||||
@Test
|
||||
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
|
||||
public void testValidateStructureDefinition() throws Exception {
|
||||
String input = IOUtils.toString(getClass().getResourceAsStream("/sd-david-dhtest7.json"), StandardCharsets.UTF_8);
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
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.MethodOutcome;
|
||||
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.util.StopWatch;
|
||||
import ca.uhn.fhir.util.TestUtil;
|
||||
import ca.uhn.fhir.validation.IValidatorModule;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
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.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.OperationOutcome;
|
||||
import org.hl7.fhir.r4.model.Organization;
|
||||
import org.hl7.fhir.r4.model.Patient;
|
||||
import org.hl7.fhir.r4.model.StructureDefinition;
|
||||
import org.hl7.fhir.r4.model.ValueSet;
|
||||
import org.hl7.fhir.r4.utils.IResourceValidator;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.util.AopTestUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
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 {
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoR4ValidateTest.class);
|
||||
|
||||
@AfterClass
|
||||
public static void afterClassClearContext() {
|
||||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
}
|
||||
@Autowired
|
||||
private IValidatorModule myValidatorModule;
|
||||
|
||||
@Test
|
||||
public void testValidateStructureDefinition() throws Exception {
|
||||
String input = IOUtils.toString(getClass().getResourceAsStream("/r4/sd-david-dhtest7.json"), StandardCharsets.UTF_8);
|
||||
StructureDefinition sd = myFhirCtx.newJsonParser().parseResource(StructureDefinition.class, input);
|
||||
|
||||
|
||||
|
||||
|
||||
ourLog.info("Starting validation");
|
||||
try {
|
||||
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("Done validation");
|
||||
|
||||
|
||||
StopWatch sw = new StopWatch();
|
||||
ourLog.info("Starting validation");
|
||||
try {
|
||||
|
@ -66,13 +61,13 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
|
|||
ourLog.info("Done validation in {}ms", sw.getMillis());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testValidateDocument() throws Exception {
|
||||
String input = IOUtils.toString(getClass().getResourceAsStream("/r4/document-bundle.json"), StandardCharsets.UTF_8);
|
||||
Bundle document = myFhirCtx.newJsonParser().parseResource(Bundle.class, input);
|
||||
|
||||
|
||||
|
||||
|
||||
ourLog.info("Starting validation");
|
||||
try {
|
||||
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("Done validation");
|
||||
|
||||
|
||||
// ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome.getOperationOutcome()));
|
||||
}
|
||||
|
||||
|
@ -139,24 +134,24 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
|
|||
MethodOutcome outcome = null;
|
||||
ValidationModeEnum mode = ValidationModeEnum.CREATE;
|
||||
switch (enc) {
|
||||
case JSON:
|
||||
encoded = myFhirCtx.newJsonParser().encodeResourceToString(input);
|
||||
try {
|
||||
myObservationDao.validate(input, null, encoded, EncodingEnum.JSON, mode, null, mySrd);
|
||||
fail();
|
||||
} catch (PreconditionFailedException e) {
|
||||
return (OperationOutcome) e.getOperationOutcome();
|
||||
}
|
||||
break;
|
||||
case XML:
|
||||
encoded = myFhirCtx.newXmlParser().encodeResourceToString(input);
|
||||
try {
|
||||
myObservationDao.validate(input, null, encoded, EncodingEnum.XML, mode, null, mySrd);
|
||||
fail();
|
||||
} catch (PreconditionFailedException e) {
|
||||
return (OperationOutcome) e.getOperationOutcome();
|
||||
}
|
||||
break;
|
||||
case JSON:
|
||||
encoded = myFhirCtx.newJsonParser().encodeResourceToString(input);
|
||||
try {
|
||||
myObservationDao.validate(input, null, encoded, EncodingEnum.JSON, mode, null, mySrd);
|
||||
fail();
|
||||
} catch (PreconditionFailedException e) {
|
||||
return (OperationOutcome) e.getOperationOutcome();
|
||||
}
|
||||
break;
|
||||
case XML:
|
||||
encoded = myFhirCtx.newXmlParser().encodeResourceToString(input);
|
||||
try {
|
||||
myObservationDao.validate(input, null, encoded, EncodingEnum.XML, mode, null, mySrd);
|
||||
fail();
|
||||
} catch (PreconditionFailedException e) {
|
||||
return (OperationOutcome) e.getOperationOutcome();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
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
|
||||
public void testValidateForCreate() {
|
||||
String methodName = "testValidateForCreate";
|
||||
|
@ -337,18 +383,18 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test {
|
|||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Format has changed, this is out of date
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
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 {
|
||||
MethodOutcome results = myQuestionnaireDao.validate(null, null, input, EncodingEnum.XML, ValidationModeEnum.UPDATE, null, mySrd);
|
||||
OperationOutcome oo = (OperationOutcome) results.getOperationOutcome();
|
||||
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
|
||||
MethodOutcome results = myQuestionnaireDao.validate(null, null, input, EncodingEnum.XML, ValidationModeEnum.UPDATE, null, mySrd);
|
||||
OperationOutcome oo = (OperationOutcome) results.getOperationOutcome();
|
||||
ourLog.info(myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
|
||||
} catch (PreconditionFailedException e) {
|
||||
// this is a failure of the test
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -2371,13 +2371,17 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
|
|||
return null;
|
||||
} else {
|
||||
long t = System.nanoTime();
|
||||
if (!Utilities.isAbsoluteUrl(reference))
|
||||
reference = resolve(uri, reference);
|
||||
ValueSet fr = context.fetchResource(ValueSet.class, reference);
|
||||
txTime = txTime + (System.nanoTime() - t);
|
||||
ValueSet fr = context.fetchResource(ValueSet.class, reference);
|
||||
if (fr == null) {
|
||||
if (!Utilities.isAbsoluteUrl(reference)) {
|
||||
reference = resolve(uri, reference);
|
||||
fr = context.fetchResource(ValueSet.class, reference);
|
||||
}
|
||||
}
|
||||
txTime = txTime + (System.nanoTime() - t);
|
||||
return fr;
|
||||
}
|
||||
} else
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue