public void UpdateAndCheckHistory() { _store.EraseData(); var importer = new ResourceImporter(new Uri("http://localhost")); importer.QueueNewEntry(_stockPatient); importer.QueueNewEntry(_stockOrg); var dd = (ResourceEntry)clone(_stockPatient); ((Patient)dd.Resource).Name[0].Text = "Donald Duck"; dd.Links.SelfLink = null; importer.QueueNewEntry(dd); importer.QueueNewDeletedEntry(_stockPatient.Id); importer.QueueNewDeletedEntry(_stockOrg.Id); var imported = importer.ImportQueued(); var origId = imported.First().Id; _store.AddEntries(imported); var history = _store.ListVersionsById(origId) .OrderBy(be => new ResourceLocation(be.Links.SelfLink).VersionId); Assert.IsNotNull(history); Assert.AreEqual(3, history.Count()); Assert.IsTrue(history.All(be => be.Id == origId)); Assert.IsTrue(history.Last() is DeletedEntry); var ver1 = new ResourceLocation(history.First().Links.SelfLink).VersionId; var ver2 = new ResourceLocation(history.ElementAt(1).Links.SelfLink).VersionId; var ver3 = new ResourceLocation(history.ElementAt(2).Links.SelfLink).VersionId; Assert.AreNotEqual(Int32.Parse(ver1), Int32.Parse(ver2)); Assert.AreNotEqual(Int32.Parse(ver2), Int32.Parse(ver3)); Assert.AreNotEqual(Int32.Parse(ver1), Int32.Parse(ver3)); var firstVersionAsEntry = _store.FindVersionByVersionId(history.First().Links.SelfLink); //TODO: There's a bug here...the cr+lf in the _stockPatient gets translated to \r\n in the 'firstVersion'. //Cannot see this in the stored version though. Assert.AreEqual(FhirSerializer.SerializeResourceToJson(_stockPatient.Resource), FhirSerializer.SerializeResourceToJson(((ResourceEntry)firstVersionAsEntry).Resource)); var secondVersionAsEntry = _store.FindVersionByVersionId(history.ElementAt(1).Links.SelfLink); Assert.AreEqual("Donald Duck", ((Patient)((ResourceEntry)secondVersionAsEntry).Resource).Name[0].Text); var allHistory = _store.ListVersions(); Assert.AreEqual(5, allHistory.Count()); Assert.AreEqual(3, allHistory.Count(be => be.Id.ToString().Contains("patient"))); Assert.AreEqual(2, allHistory.Count(be => be.Id.ToString().Contains("organization"))); Assert.AreEqual(2, allHistory.Count(be => be is DeletedEntry)); }
public void TestStoreBinaryEntry() { byte[] data = UTF8Encoding.UTF8.GetBytes("Hello world!"); var e = new ResourceEntry <Binary>(); e.AuthorName = "Ewout"; e.Id = new Uri("binary/@3141", UriKind.Relative); e.Links.SelfLink = new Uri("binary/@3141/history/@1", UriKind.Relative); e.Resource = new Binary() { Content = data, ContentType = "text/plain" }; // Test store var result = (ResourceEntry <Binary>)_store.AddEntry(e); Assert.IsNotNull(result.Resource); CollectionAssert.AreEqual(data, result.Resource.Content); Assert.AreEqual("text/plain", result.Resource.ContentType); // Test retrieve ResourceEntry <Binary> result2 = (ResourceEntry <Binary>)_store.FindEntryById(e.Id); Assert.IsNotNull(result2); CollectionAssert.AreEqual(e.Resource.Content, result2.Resource.Content); Assert.AreEqual(e.Resource.ContentType, result2.Resource.ContentType); // Test update byte[] data2 = UTF8Encoding.UTF8.GetBytes("Hello world!?"); e.Resource = new Binary() { Content = data2, ContentType = "text/plain" }; e.SelfLink = new Uri("binary/@3141/history/@2", UriKind.Relative); ResourceEntry <Binary> result3 = (ResourceEntry <Binary>)_store.AddEntry(e); Assert.IsNotNull(result3); Assert.AreEqual(e.Resource.ContentType, result3.Resource.ContentType); CollectionAssert.AreEqual(e.Resource.Content, result3.Resource.Content); // Test fetch latest ResourceEntry <Binary> result4 = (ResourceEntry <Binary>)_store.FindEntryById(e.Id); Assert.AreEqual(result4.Id, result3.Id); var allVersions = _store.ListVersionsById(e.Id); Assert.AreEqual(2, allVersions.Count()); Assert.AreEqual(allVersions.First().Id, e.Id); Assert.AreEqual(allVersions.Skip(1).First().Id, result.Id); }