@SuppressWarnings("deprecation")
  @Test
  public void testSchemaBundleValidator() throws IOException {
    String res =
        IOUtils.toString(getClass().getClassLoader().getResourceAsStream("bundle-example.json"));
    Bundle b = ourCtx.newJsonParser().parseBundle(res);

    FhirValidator val = createFhirValidator();

    val.validate(b);

    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);

    try {
      val.validate(b);
      fail();
    } catch (ValidationFailureException e) {
      String encoded =
          ourCtx
              .newXmlParser()
              .setPrettyPrint(true)
              .encodeResourceToString(e.getOperationOutcome());
      ourLog.info(encoded);
      assertThat(encoded, containsString("tim-1:"));
    }
  }
  @SuppressWarnings("deprecation")
  @Test
  public void testSchemaResourceValidator() throws IOException {
    String res =
        IOUtils.toString(
            getClass().getClassLoader().getResourceAsStream("patient-example-dicom.json"));
    Patient p = ourCtx.newJsonParser().parseResource(Patient.class, res);

    ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p));

    FhirValidator val = ourCtx.newValidator();
    val.setValidateAgainstStandardSchema(true);
    val.setValidateAgainstStandardSchematron(false);

    val.validate(p);

    p.getAnimal().getBreed().setText("The Breed");
    try {
      val.validate(p);
      fail();
    } catch (ValidationFailureException e) {
      OperationOutcome operationOutcome = (OperationOutcome) e.getOperationOutcome();
      ourLog.info(
          ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome));
      assertEquals(1, operationOutcome.getIssue().size());
      assertThat(
          operationOutcome.getIssueFirstRep().getDetailsElement().getValue(),
          containsString("cvc-complex-type"));
    }
  }
  @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:"));
  }
  private FhirValidator createFhirValidator() {

    AllergyIntolerance allergy = new AllergyIntolerance();
    allergy.getSubstance().addCoding().setCode("some substance");

    FhirValidator val = ourCtx.newValidator();
    val.setValidateAgainstStandardSchema(true);
    val.setValidateAgainstStandardSchematron(true);
    return val;
  }
  @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());
  }
  @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());
  }