public void AutoGeneratesDifferential()
        {
            var identifierBsn = _source.FindStructureDefinition("http://validationtest.org/fhir/StructureDefinition/IdentifierWithBSN");

            Assert.IsNotNull(identifierBsn);

            var instance = new Identifier("http://clearly.incorrect.nl/definition", "1234");

            var validationContext = new ValidationSettings {
                ResourceResolver = _source, GenerateSnapshot = false
            };
            var automatedValidator = new Validator(validationContext);

            var report = automatedValidator.Validate(instance, identifierBsn);

            Assert.IsTrue(report.ToString().Contains("does not include a snapshot"));

            validationContext.GenerateSnapshot = true;
            report = automatedValidator.Validate(instance, identifierBsn);
            Assert.IsFalse(report.ToString().Contains("does not include a snapshot"));

            bool snapshotNeedCalled = false;

            // I disabled cloning of SDs in the validator, so the last call to Validate() will have added a snapshot
            // to our local identifierBSN
            identifierBsn.Snapshot = null;

            automatedValidator.OnSnapshotNeeded += (object s, OnSnapshotNeededEventArgs a) => { snapshotNeedCalled = true; /* change nothing, warning should return */ };

            report = automatedValidator.Validate(instance, identifierBsn);
            Assert.IsTrue(snapshotNeedCalled);
            Assert.IsTrue(report.ToString().Contains("does not include a snapshot"));
        }
Esempio n. 2
0
 /// <summary>Clone constructor. Generates a new <see cref="ValidationSettings"/> instance initialized from the state of the specified instance.</summary>
 /// <exception cref="ArgumentNullException">The specified argument is <c>null</c>.</exception>
 public ValidationSettings(ValidationSettings other)
 {
     if (other == null)
     {
         throw Error.ArgumentNull(nameof(other));
     }
     other.CopyTo(this);
 }
        public void SetupSource()
        {
            source = new CachedResolver(
                new MultiResolver(
                    new TestProfileArtifactSource(),
                    new ZipSource("specification.zip")));

            var ctx = new ValidationSettings()
            {
                ResourceResolver = source, GenerateSnapshot = true, Trace = false
            };

            validator = new Validator(ctx);
        }
Esempio n. 4
0
        /// <summary>Copy all configuration settings to another instance.</summary>
        /// <param name="other">Another <see cref="ValidationSettings"/> instance.</param>
        /// <exception cref="ArgumentNullException">The specified argument is <c>null</c>.</exception>
        public void CopyTo(ValidationSettings other)
        {
            if (other == null)
            {
                throw Error.ArgumentNull(nameof(other));
            }

            other.EnableXsdValidation      = EnableXsdValidation;
            other.GenerateSnapshot         = GenerateSnapshot;
            other.GenerateSnapshotSettings = GenerateSnapshotSettings?.Clone();
            other.ResolveExteralReferences = ResolveExteralReferences;
            other.ResourceResolver         = ResourceResolver;
            other.SkipConstraintValidation = SkipConstraintValidation;
            other.TerminologyService       = TerminologyService;
            other.Trace = Trace;
        }
        public void ValidateBundle()
        {
            var bundleXml = File.ReadAllText("TestData\\validation\\bundle-contained-references.xml");

            var bundle = (new FhirXmlParser()).Parse <Bundle>(bundleXml);

            Assert.IsNotNull(bundle);

            var ctx = new ValidationSettings()
            {
                ResourceResolver = _source, GenerateSnapshot = true, ResolveExteralReferences = true, Trace = false
            };
            bool hitResolution = false;

            _validator = new Validator(ctx);
            _validator.OnExternalResolutionNeeded += (s, a) => hitResolution = true;

            var report = _validator.Validate(bundle);

            Assert.IsTrue(report.Success);
            Assert.AreEqual(1, report.Warnings);            // 1 unresolvable reference
            Assert.IsTrue(hitResolution);

            report = _validator.Validate(bundle, "http://validationtest.org/fhir/StructureDefinition/BundleWithContainedEntries");
            Assert.IsFalse(report.Success);
            Assert.AreEqual(1, report.Warnings);          // 1 unresolvable reference
            Assert.AreEqual(4, report.Errors);            // 4 non-contained references

            report = _validator.Validate(bundle, "http://validationtest.org/fhir/StructureDefinition/BundleWithContainedBundledEntries");
            Assert.IsFalse(report.Success);
            Assert.AreEqual(1, report.Warnings);          // 1 unresolvable reference
            Assert.AreEqual(1, report.Errors);            // 1 external reference

            report = _validator.Validate(bundle, "http://validationtest.org/fhir/StructureDefinition/BundleWithBundledEntries");
            Assert.IsFalse(report.Success);
            Assert.AreEqual(1, report.Warnings);          // 1 unresolvable reference
            Assert.AreEqual(2, report.Errors);            // 1 external reference, 1 contained reference

            report = _validator.Validate(bundle, "http://validationtest.org/fhir/StructureDefinition/BundleWithReferencedEntries");
            Assert.IsFalse(report.Success);
            Assert.AreEqual(1, report.Warnings);          // 1 unresolvable reference
            Assert.AreEqual(4, report.Errors);            // 3 bundled reference, 1 contained reference
        }
        public void SetupSource()
        {
            _source = new CachedResolver(
                new MultiResolver(
                    new BundleExampleResolver(@"TestData\validation"),
                    new DirectorySource(@"TestData\validation"),
                    new TestProfileArtifactSource(),
                    new ZipSource("specification.zip")));

            var ctx = new ValidationSettings()
            {
                ResourceResolver    = _source,
                GenerateSnapshot    = true,
                EnableXsdValidation = true,
                Trace = false,
                ResolveExteralReferences = true
            };

            _validator = new Validator(ctx);
        }
Esempio n. 7
0
        public ValidationFixture()
        {
            var zip = ZipSource.CreateValidationSource();

            Resolver = new CachedResolver(
                new MultiResolver(
                    new TestProfileArtifactSource(),
                    new DirectorySource(@"TestData\validation"),
                    zip
                    ));

            var ctx = new ValidationSettings()
            {
                ResourceResolver    = Resolver,
                GenerateSnapshot    = true,
                EnableXsdValidation = true,
                Trace = false,
                ResolveExteralReferences = true
            };

            Validator = new Validator(ctx);
        }
        public void SetupSource()
        {
            // Ensure the FHIR extensions are registered
            Hl7.Fhir.FhirPath.PocoNavigatorExtensions.PrepareFhirSymbolTableFunctions();

            _source = new CachedResolver(
                new MultiResolver(
                    new BundleExampleResolver(@"TestData\validation"),
                    new DirectorySource(@"TestData\validation"),
                    new TestProfileArtifactSource(),
                    new ZipSource("specification.zip")));

            var ctx = new ValidationSettings()
            {
                ResourceResolver    = _source,
                GenerateSnapshot    = true,
                EnableXsdValidation = true,
                Trace = false,
                ResolveExteralReferences = true
            };

            _validator = new Validator(ctx);
        }
        public void TestPatientWithOrganization()
        {
            // DirectorySource (and ResourceStreamScanner) does not support json...
            // var source = new DirectorySource(@"TestData\validation");
            // var res = source.ResolveByUri("Patient/pat1"); // cf. "Patient/Levin"

            var jsonPatient = File.ReadAllText(@"TestData\validation\patient-ck.json");
            var parser      = new FhirJsonParser();
            var patient     = parser.Parse <Patient>(jsonPatient);

            Assert.IsNotNull(patient);

            var jsonOrganization = File.ReadAllText(@"TestData\validation\organization-ck.json");
            var organization     = parser.Parse <Organization>(jsonOrganization);

            Assert.IsNotNull(organization);

            var resources   = new Resource[] { patient, organization };
            var memResolver = new InMemoryResourceResolver(resources);

            // [WMR 20161220] Validator always uses existing snapshots if present
            // ProfilePreprocessor.GenerateSnapshots:
            // if (!sd.HasSnapshot) { ... snapshotGenerator(sd) ... }

            // Create custom source to properly force snapshot expansion
            // Run validator on instance
            // Afterwards, verify that instance profile has been expanded

            var source = new CachedResolver(
                // Clear snapshots after initial load
                // This will force the validator to regenerate all snapshots
                new ClearSnapshotResolver(
                    new MultiResolver(
                        // new BundleExampleResolver(@"TestData\validation"),
                        // new DirectorySource(@"TestData\validation"),
                        // new TestProfileArtifactSource(),
                        memResolver,
                        new ZipSource("specification.zip"))));

            var ctx = new ValidationSettings()
            {
                ResourceResolver    = source,
                GenerateSnapshot    = true,
                EnableXsdValidation = true,
                Trace = false,
                ResolveExteralReferences = true
            };

            var validator = new Validator(ctx);

            var report = validator.Validate(patient);

            Assert.IsTrue(report.Success);

            // Assert.AreEqual(4, report.Warnings);

            // To check for ele-1 constraints on expanded Patient snapshot:
            // source.FindStructureDefinitionForCoreType(FHIRDefinedType.Patient).Snapshot.Element.Select(e=>e.Path + " : " + e.Constraint.FirstOrDefault()?.Key ?? "").ToArray()
            var patientStructDef = source.FindStructureDefinitionForCoreType(FHIRDefinedType.Patient);

            Assert.IsNotNull(patientStructDef);
            Assert.IsTrue(patientStructDef.HasSnapshot);
            assertElementConstraints(patientStructDef.Snapshot.Element);
        }
Esempio n. 10
0
 public Validator(ValidationSettings settings)
 {
     Settings = settings;
 }
Esempio n. 11
0
 public Validator() : this(ValidationSettings.CreateDefault())
 {
 }
Esempio n. 12
0
 public Validator(ValidationSettings settings)
 {
     Settings = settings.Clone();
 }