/** See issue #50 */
  @Test
  public void testOutOfBoundsDate() {
    Patient p = new Patient();
    p.setBirthDate(new DateDt("2000-15-31"));

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

    assertThat(encoded, StringContains.containsString("2000-15-31"));

    p = ourCtx.newXmlParser().parseResource(Patient.class, encoded);
    assertEquals("2000-15-31", p.getBirthDateElement().getValueAsString());
    assertEquals("2001-03-31", new SimpleDateFormat("yyyy-MM-dd").format(p.getBirthDate()));

    ValidationResult result = ourCtx.newValidator().validateWithResult(p);
    String resultString =
        ourCtx
            .newXmlParser()
            .setPrettyPrint(true)
            .encodeResourceToString(result.toOperationOutcome());
    ourLog.info(resultString);

    assertEquals(2, ((OperationOutcome) result.toOperationOutcome()).getIssue().size());
    assertThat(resultString, StringContains.containsString("2000-15-31"));
  }
  @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);
    }
  }