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,34 +9,33 @@ 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 {
@ -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";
@ -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,9 +2371,13 @@ 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))
reference = resolve(uri, reference);
ValueSet fr = context.fetchResource(ValueSet.class, reference); 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); txTime = txTime + (System.nanoTime() - t);
return fr; return fr;
} }