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);
        }
        public void TestElementSnapshot()
        {
            // Request core Element snapshot; verify recursion handling

            var orgSource    = ZipSource.CreateValidationSource();
            var cachedSource = new CachedResolver(orgSource);

            // Assumption: source provides Element structure
            var sdCached = cachedSource.FindStructureDefinitionForCoreType(FHIRDefinedType.Element);

            Assert.IsNotNull(sdCached);
            Assert.IsNotNull(sdCached.Differential);
            var elemCnt = sdCached.Differential.Element.Count;

            Assert.AreEqual(3, elemCnt); // Element | Element.id | Element.extension

            // Generate snapshot by calling SnapshotSource
            // Important! Specify flag to force re-generation (don't trust existing core snapshots...)
            var snapSource = new SnapshotSource(cachedSource, true);

            var sd = snapSource.FindStructureDefinitionForCoreType(FHIRDefinedType.Element);

            Assert.IsNotNull(sd);
            Assert.AreEqual(sdCached, sd);   // Expecting same (cached) object reference, with updated Snapshot component
            Assert.IsTrue(sd.HasSnapshot);
            Assert.IsTrue(sd.Snapshot.IsCreatedBySnapshotGenerator());

            var elems = sd.Snapshot.Element;

            Assert.AreEqual(elemCnt, elems.Count);

            void assert_ele1(ElementDefinition eld)
            {
                Assert.AreEqual("ele-1", eld.Constraint.FirstOrDefault()?.Key);
                Assert.AreEqual("ele-1", eld.Condition.FirstOrDefault());
            }

            // Assumption: differential introduces constraint "ele-1" on root element (only)
            var diffElems = sd.Differential.Element;

            assert_ele1(diffElems[0]);
            for (int i = 1; i < elemCnt; i++)
            {
                var elem = diffElems[i];
                Assert.IsFalse(elem.Constraint.Any());
                Assert.IsFalse(elem.Condition.Any());
            }

            // Verify explicit inheritance of recursive constraints in snapshot
            //
            // [0] Element
            // [1] Element.id        : id < string < Element
            // [2] Element.extension : Extension < Element
            //
            // Verify that the "ele-1" constraint & condition, introduced by diff root element [0],
            // is correctly propagated to snapshot child elements [1] and [2]
            foreach (var elem in elems)
            {
                assert_ele1(elem);
            }
        }