diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ValidateTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ValidateTest.java index 40895ee05b5..43dc7be6559 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ValidateTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ValidateTest.java @@ -1,6 +1,8 @@ package ca.uhn.fhir.jpa.dao.r4; import ca.uhn.fhir.jpa.api.config.DaoConfig; +import ca.uhn.fhir.jpa.api.dao.DaoRegistry; +import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion; import ca.uhn.fhir.jpa.term.api.ITermCodeSystemStorageSvc; import ca.uhn.fhir.jpa.term.api.ITermReadSvc; @@ -17,10 +19,10 @@ 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.common.hapi.validation.validator.FhirInstanceValidator; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator; import org.hl7.fhir.r4.model.*; import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r4.model.Observation.ObservationStatus; @@ -38,9 +40,11 @@ import java.util.Collections; import static org.awaitility.Awaitility.await; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.in; import static org.hamcrest.Matchers.not; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +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); @@ -50,6 +54,8 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test { private ITermReadSvc myTermReadSvc; @Autowired private ITermCodeSystemStorageSvc myTermCodeSystemStorageSvcc; + @Autowired + private DaoRegistry myDaoRegistry; /** * Create a loinc valueset that expands to more results than the expander is willing to do @@ -157,7 +163,7 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test { myValueSetDao.create(vs); myTermReadSvc.preExpandDeferredValueSetsToTerminologyTables(); - await().until(()->myTermReadSvc.isValueSetPreExpandedForCodeValidation(vs)); + await().until(() -> myTermReadSvc.isValueSetPreExpandedForCodeValidation(vs)); // Load the profile, which is just the Vital Signs profile modified to accept all loinc codes // and not just certain ones @@ -215,10 +221,29 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test { } + @Test + public void testValidateCodeableConceptWithNoSystem() { + AllergyIntolerance allergy = new AllergyIntolerance(); + allergy.getText().setStatus(Narrative.NarrativeStatus.GENERATED).getDiv().setValue("
hi!
"); + allergy.getClinicalStatus().addCoding().setSystem(null).setCode("active").setDisplay("Active"); + allergy.getVerificationStatus().addCoding().setSystem("http://terminology.hl7.org/CodeSystem/allergyintolerance-verification").setCode("confirmed").setDisplay("Confirmed"); + allergy.setPatient(new Reference("Patient/123")); - private OperationOutcome validateAndReturnOutcome(Observation theObs) { + allergy.addNote() + .setText("This is text") + .setAuthor(new Reference("Patient/123")); + + ourLog.info(myFhirCtx.newJsonParser().encodeResourceToString(allergy)); + + OperationOutcome oo = validateAndReturnOutcome(allergy); + assertThat(encode(oo), containsString("None of the codes provided are in the value set http://hl7.org/fhir/ValueSet/allergyintolerance-clinical")); + } + + @SuppressWarnings("unchecked") + private OperationOutcome validateAndReturnOutcome(T theObs) { + IFhirResourceDao dao = (IFhirResourceDao) myDaoRegistry.getResourceDao(theObs.getClass()); try { - MethodOutcome outcome = myObservationDao.validate(theObs, null, null, null, ValidationModeEnum.CREATE, null, mySrd); + MethodOutcome outcome = dao.validate(theObs, null, null, null, ValidationModeEnum.CREATE, null, mySrd); return (OperationOutcome) outcome.getOperationOutcome(); } catch (PreconditionFailedException e) { return (OperationOutcome) e.getOperationOutcome(); @@ -790,7 +815,6 @@ public class FhirResourceDaoR4ValidateTest extends BaseJpaR4Test { } - private IBaseResource findResourceByIdInBundle(Bundle vss, String name) { IBaseResource retVal = null; for (BundleEntryComponent next : vss.getEntry()) {