@Test public void testSchematronResourceValidator() throws IOException { String res = IOUtils.toString( getClass().getClassLoader().getResourceAsStream("patient-example-dicom.json")); Patient p = ourCtx.newJsonParser().parseResource(Patient.class, res); FhirValidator val = ourCtx.newValidator(); val.setValidateAgainstStandardSchema(false); val.setValidateAgainstStandardSchematron(true); ValidationResult validationResult = val.validateWithResult(p); assertTrue(validationResult.isSuccessful()); p.getTelecomFirstRep().setValue("123-4567"); validationResult = val.validateWithResult(p); assertFalse(validationResult.isSuccessful()); OperationOutcome operationOutcome = (OperationOutcome) validationResult.toOperationOutcome(); ourLog.info( ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome)); assertEquals(1, operationOutcome.getIssue().size()); assertThat(operationOutcome.getIssueFirstRep().getDiagnostics(), containsString("cpt-2:")); p.getTelecomFirstRep().setSystem(ContactPointSystemEnum.EMAIL); validationResult = val.validateWithResult(p); assertTrue(validationResult.isSuccessful()); }
@Test public void testSchemaBundleValidatorFails() throws IOException { String res = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("bundle-example.json")); Bundle b = ourCtx.newJsonParser().parseBundle(res); FhirValidator val = createFhirValidator(); ValidationResult validationResult = val.validateWithResult(b); assertTrue(validationResult.isSuccessful()); MedicationOrder p = (MedicationOrder) b.getEntries().get(0).getResource(); TimingDt timing = new TimingDt(); timing.getRepeat().setDuration(123); timing.getRepeat().setDurationUnits((UnitsOfTimeEnum) null); p.getDosageInstructionFirstRep().setTiming(timing); validationResult = val.validateWithResult(b); assertFalse(validationResult.isSuccessful()); OperationOutcome operationOutcome = (OperationOutcome) validationResult.toOperationOutcome(); String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); ourLog.info(encoded); assertThat(encoded, containsString("tim-1:")); }
@Test public void testSchemaBundleValidatorIsSuccessful() throws IOException { String res = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("bundle-example.json")); Bundle b = ourCtx.newJsonParser().parseBundle(res); ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(b)); FhirValidator val = createFhirValidator(); ValidationResult result = val.validateWithResult(b); OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); assertTrue(result.toString(), result.isSuccessful()); assertNotNull(operationOutcome); assertEquals(1, operationOutcome.getIssue().size()); }
@Override protected void validateResourceForStorage( QuestionnaireResponse theResource, ResourceTable theEntityToSave) { super.validateResourceForStorage(theResource, theEntityToSave); if (!myValidateResponses) { return; } QuestionnaireResponse qa = (QuestionnaireResponse) theResource; if (qa == null || qa.getQuestionnaire() == null || qa.getQuestionnaire().getReference() == null || qa.getQuestionnaire().getReference().isEmpty()) { return; } FhirValidator val = myRefImplCtx.newValidator(); val.setValidateAgainstStandardSchema(false); val.setValidateAgainstStandardSchematron(false); FhirQuestionnaireResponseValidator module = new FhirQuestionnaireResponseValidator(); module.setResourceLoader(new JpaResourceLoader()); val.registerValidatorModule(module); ValidationResult result = val.validateWithResult( myRefImplCtx .newJsonParser() .parseResource(getContext().newJsonParser().encodeResourceToString(qa))); if (!result.isSuccessful()) { IBaseOperationOutcome oo = getContext() .newJsonParser() .parseResource( OperationOutcome.class, myRefImplCtx.newJsonParser().encodeResourceToString(result.toOperationOutcome())); throw new UnprocessableEntityException(getContext(), oo); } }