Esempio n. 1
0
        public void SaveStructureDefinition(StructureDefinition sd)
        {
            // Ensure the folder exists
            Uri    t = new Uri(sd.Url);
            string profileOutputDirectory = Path.Combine(_outputProfilePath, t.Host);

            if (!Directory.Exists(profileOutputDirectory))
            {
                Directory.CreateDirectory(profileOutputDirectory);
            }

            // Non extensions will just go in the root of the output
            if (sd.BaseDefinition != "http://hl7.org/fhir/StructureDefinition/Extension")
            {
                profileOutputDirectory = _outputProfilePath;
            }

            // Now output the file
            System.IO.File.WriteAllText($"{profileOutputDirectory}/StructureDefinition-{sd.Id}.xml", new FhirXmlSerializer(new SerializerSettings()
            {
                AppendNewLine = true, Pretty = true
            }).SerializeToString(sd));

            // And add it to our resolver
            sourceSD.InvalidateByCanonicalUri(sd.Url);
            ds.Refresh();

            // And check that it comes back...
            var instSD = sourceSD.ResolveByUri(sd.Url) as StructureDefinition;

            if (instSD == null)
            {
                Console.WriteLine($"Was not able to resolve the newly created extension");
            }
        }
Esempio n. 2
0
        public void TestCacheLoadingStrategy()
        {
            const string resourceUri = "http://hl7.org/fhir/ValueSet/v2-0292";

            // Create empty in-memory resolver
            var mem   = new InMemoryProfileResolver();
            var cache = new CachedResolver(mem);

            // Load on demand should return null
            var resource = cache.ResolveByCanonicalUri(resourceUri);

            Assert.IsNull(resource);

            // Resolve core resource from ZIP and refresh in-memory resolver
            var zipSource = ZipSource.CreateValidationSource();
            var original  = zipSource.ResolveByUri(resourceUri) as ValueSet;

            Assert.IsNotNull(original);
            mem.Reload(original);

            // Load on demand should still return null
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNull(resource);

            // Invalidate the cache, delete existing cache entry
            cache.InvalidateByCanonicalUri(resourceUri);

            // Load from cache should still return null
            resource = cache.ResolveByCanonicalUri(resourceUri, CachedResolverLoadingStrategy.LoadFromCache);
            Assert.IsNull(resource);

            // Load on demand should now resolve instance and update cache
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNotNull(resource);
            Assert.AreEqual(original, resource);
            Assert.IsTrue(cache.IsCachedCanonicalUri(resourceUri));

            // Update in-memory resolver with new, modified instance (same url)
            var modified = (ValueSet)original.DeepCopy();

            modified.Name = "MODIFIED";
            mem.Reload(modified);

            // Load on demand should still return the original, unmodified instance from cache
            // As the cache is unaware that the internal source has changed
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNotNull(resource);
            Assert.AreEqual(original, resource);

            // Forced load should update cache and return new, modified instance
            resource = cache.ResolveByCanonicalUri(resourceUri, CachedResolverLoadingStrategy.LoadFromSource);
            Assert.IsNotNull(resource);
            Assert.AreEqual(modified, resource);

            // Clear in-memory resolver; i.e. simulate delete file from disk
            mem.Clear();

            // Load on demand should still return the modified instance from cache
            // As the cache is unaware that the internal source has changed
            resource = cache.ResolveByCanonicalUri(resourceUri);
            Assert.IsNotNull(resource);
            Assert.AreEqual(modified, resource);
            Assert.IsTrue(cache.IsCachedCanonicalUri(resourceUri));

            // Forced load should update cache and now return null
            resource = cache.ResolveByCanonicalUri(resourceUri, CachedResolverLoadingStrategy.LoadFromSource);
            Assert.IsNull(resource);
            Assert.IsFalse(cache.IsCachedCanonicalUri(resourceUri));
        }