public void TestImportBundle() { MockObjectRepository tdb = new MockObjectRepository(); tdb.InitializeFHIRRepository(); tdb.FindOrAddImplementationGuide(MockObjectRepository.DEFAULT_FHIR_DSTU1_IG_TYPE_NAME, "Unowned FHIR DSTU1 Profiles"); string bundleXml = Helper.GetSampleContents("Trifolia.Test.DocSamples.FHIR.DSTU1.bundle.xml"); FHIRImporter importer = new FHIRImporter(tdb, false); importer.Import(bundleXml); try { importer.Import(bundleXml); Assert.Fail("Expected an exception to be thrown when importing FHIR profiles with 'create' option, when profile already exists"); } catch { } importer = new FHIRImporter(tdb, true); importer.Import(bundleXml); }
private HttpResponseMessage ImportProfile(Bundle profiles, bool shouldUpdate) { using (StringWriter sw = new StringWriter()) { XmlWriter writer = null; try { writer = XmlWriter.Create(sw); FhirSerializer.SerializeBundle(profiles, writer); } catch (Exception) { var response = new HttpResponseMessage(HttpStatusCode.ExpectationFailed); response.Content = new StringContent("Failed to serialize request content"); return(response); } writer.Flush(); string bundleXml = sw.ToString(); try { string defaultImplementationGuideName = DEFAULT_IG_NAME; ImplementationGuideType igType = this.tdb.ImplementationGuideTypes.Single(y => y.Name == FHIR_IG_TYPE_NAME); ImplementationGuide defaultImplementationGuide = this.tdb.ImplementationGuides.SingleOrDefault(y => y.Name.ToLower() == defaultImplementationGuideName.ToLower()); TDBOrganization defaultOrganization = this.tdb.Organizations.First(); User defaultAuthorUser = this.tdb.Users.Single(y => y.UserName == TEMPLATE_AUTHOR_USERNAME); // Create a default implementation guide for imports if one doesn't already exist if (defaultImplementationGuide == null) { defaultImplementationGuide = new ImplementationGuide() { Name = defaultImplementationGuideName, Organization = defaultOrganization, ImplementationGuideType = igType }; this.tdb.ImplementationGuides.AddObject(defaultImplementationGuide); } FHIRImporter importer = new FHIRImporter(this.tdb, shouldUpdate); importer.DefaultImplementationGuide = defaultImplementationGuide; importer.DefaultAuthorUser = defaultAuthorUser; importer.Import(bundleXml); this.tdb.SaveChanges(); } catch (AuthorizationException ex) { throw ex; } catch (Exception ex) { var response = new HttpResponseMessage(HttpStatusCode.InternalServerError); response.Content = new StringContent(ex.Message); throw new HttpResponseException(response); } } return(new HttpResponseMessage(HttpStatusCode.OK)); }