private static SDataCollection <SDataResource> ReadResourceCollection(IDictionary <string, object> obj, IEnumerable <IDictionary <string, object> > items) { var collection = new SDataCollection <SDataResource>(items.Select(ReadResource)) { Id = ReadProtocolValue <string>(obj, "id"), Title = ReadProtocolValue <string>(obj, "title"), Updated = ReadProtocolValue <DateTimeOffset?>(obj, "updated"), TotalResults = ReadProtocolValue <int?>(obj, "totalResults"), StartIndex = ReadProtocolValue <int?>(obj, "startIndex"), ItemsPerPage = ReadProtocolValue <int?>(obj, "itemsPerPage"), Url = ReadProtocolValue <Uri>(obj, "url"), DeleteMissing = ReadProtocolValue <bool?>(obj, "deleteMissing"), Links = ReadLinks(obj), SyncMode = ReadProtocolValue <SyncMode?>(obj, "syncMode") }; object diagnoses; if (obj.TryGetValue("$diagnoses", out diagnoses)) { collection.Diagnoses = ContentHelper.Deserialize <Diagnoses>(diagnoses); } object digest; if (obj.TryGetValue("$digest", out digest)) { collection.SyncDigest = ContentHelper.Deserialize <Digest>(digest); } return(collection); }
private static SDataCollection <SDataResource> ReadResourceCollection(XElement source, IEnumerable <XElement> items) { var collection = new SDataCollection <SDataResource> { Url = ReadAttributeValue <Uri>(source, _sDataNs + "uri"), DeleteMissing = ReadAttributeValue <bool?>(source, _sDataNs + "deleteMissing"), XmlIsFlat = items != null }; foreach (var item in items ?? source.Elements()) { if (string.IsNullOrEmpty(collection.XmlLocalName)) { collection.XmlLocalName = item.Name.LocalName; } var child = ReadResource(item, null); if (InferItemType(item) == ItemType.Property) { var nilAttr = item.Attribute(_xsiNs + "nil"); var value = nilAttr != null && XmlConvert.ToBoolean(nilAttr.Value) ? null : item.Value; child.Add(item.Name.LocalName, value); break; } collection.Add(child); } return(collection); }
public static IEnumerable <object> AsCollection(object obj) { if (obj != null && !(obj is string) && !IsDictionary(obj)) { var generic = obj as IEnumerable <object>; if (generic != null) { return(generic); } var nonGeneric = obj as IEnumerable; if (nonGeneric != null) { generic = nonGeneric.Cast <object>(); var prot = obj as ISDataProtocolObject; if (prot != null) { generic = new SDataCollection <object>(generic); ((ISDataProtocolObject)generic).Info = prot.Info; } else { generic = generic.ToList(); } return(generic); } } return(null); }
public void ChangeTracking_ChangeResource_Test() { var resource = new SDataResource { { "FirstName", "Joe" }, { "LastName", "Bloggs" } }; var collection = new SDataCollection <SDataResource> { resource }; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); resource["FirstName"] = "Jill"; Assert.That(collection.IsChanged, Is.True); var changes = (SDataCollection <object>)collection.GetChanges(); Assert.That(changes.DeleteMissing, Is.False); Assert.That(changes, Is.EqualTo(new List <object> { new Dictionary <string, object> { { "FirstName", "Jill" } } })); resource["Age"] = 33; Assert.That(collection.IsChanged, Is.True); Assert.That(collection.GetChanges(), Is.EqualTo(new List <object> { new Dictionary <string, object> { { "FirstName", "Jill" }, { "Age", 33 } } })); }
public void Write_SimpleArray_Test() { var resource = new { Items = SDataCollection.Create(true, new object[0]) }; var obj = Helpers.WriteJson(resource); Assert.That(obj["Items"], Is.TypeOf <JsonArray>()); }
public void ChangeTracking_RemoveResource_Test() { var resource = new SDataResource(new Dictionary <string, object> { { "FirstName", "Joe" } }) { Key = "abc123" }; var collection = new SDataCollection <SDataResource> { resource }; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection.RemoveAt(0); Assert.That(collection.IsChanged, Is.True); var changes = (SDataCollection <object>)collection.GetChanges(); Assert.That(changes.DeleteMissing, Is.False); Assert.That(changes, Is.EqualTo(new List <object> { new Dictionary <string, object>() })); resource = ((SDataResource)changes[0]); Assert.That(resource.Key, Is.EqualTo("abc123")); Assert.That(resource.IsDeleted, Is.True); }
public void Write_Collection_Id_Test() { var resources = new SDataCollection <SDataResource> { Id = "id" }; var obj = Helpers.WriteJson(resources); var id = obj["$id"]; Assert.That(id, Is.EqualTo("id")); }
public void Deserialize_SDataProtocolObject_Collection_Test() { var value = new SDataCollection <object> { XmlLocalName = "contact" }; var result = ContentHelper.Deserialize <SDataProtocolObject_Collection>(value); Assert.That(result, Is.Not.Null); Assert.That(result, Is.Not.SameAs(value)); Assert.That(result, Is.InstanceOf <ISDataProtocolObject>()); Assert.That(result.Info.XmlLocalName, Is.EqualTo("contact")); }
public void ChangeTracking_AddResource_Test() { var resource = new SDataResource {{"FirstName", "Joe"}, {"LastName", "Bloggs"}}; var collection = new SDataCollection<SDataResource> {resource}; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection.Add(new SDataResource {{"FirstName", "Jim"}}); Assert.That(collection.IsChanged, Is.True); var changes = (SDataCollection<object>) collection.GetChanges(); Assert.That(changes.DeleteMissing, Is.False); Assert.That(changes, Is.EqualTo(new List<object> {new Dictionary<string, object> {{"FirstName", "Jim"}}})); }
public static IEnumerable <IDictionary <string, object> > AsDictionaries(object obj) { if (obj == null) { return(null); } var generic = obj as IEnumerable <IDictionary <string, object> >; if (generic != null) { return(generic); } var nonGeneric = obj as IEnumerable; if (nonGeneric != null && obj.GetType() .GetInterfaces() .Any(item => item.GetTypeInfo().IsGenericType&& item.GetGenericTypeDefinition() == typeof(IEnumerable <>) && typeof(IDictionary <string, object>).IsAssignableFrom(item.GetGenericArguments()[0]))) { generic = nonGeneric.Cast <IDictionary <string, object> >(); } var items = AsCollection(obj); if (items != null) { var dictionaries = items.Select(AsDictionary).ToList(); if (dictionaries.Count > 0 && dictionaries.All(item => item != null)) { generic = dictionaries; } } if (generic != null) { var prot = obj as ISDataProtocolObject; if (prot != null) { generic = new SDataCollection <IDictionary <string, object> >(generic); ((ISDataProtocolObject)generic).Info = prot.Info; } return(generic); } return(null); }
public void ToString_Test() { var resources = new SDataCollection <SDataResource> { new SDataResource { Descriptor = "Widget" } }; var converter = TypeDescriptor.GetConverter(resources); Assert.That(converter.CanConvertTo(typeof(string)), Is.True); var value = converter.ConvertTo(resources, typeof(string)); Assert.That(value, Is.EqualTo("(1 item)")); }
public void Write_Feed_Id_Test() { var resources = new SDataCollection <SDataResource> { Id = "id" }; var nav = Helpers.WriteAtom(resources); var mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); var node = nav.SelectSingleNode("atom:feed/atom:id", mgr); Assert.That(node, Is.Not.Null); Assert.That(node.Value, Is.EqualTo("id")); }
public void Write_Collection_SyncDigest_Test() { var resources = new SDataCollection <SDataResource> { SyncDigest = new Digest { Origin = "origin" } }; var obj = Helpers.WriteJson(resources); Assert.That(obj["$digest"], Is.InstanceOf <IDictionary <string, object> >()); var digest = (IDictionary <string, object>)obj["$digest"]; Assert.That(digest["origin"], Is.EqualTo("origin")); }
public void ChangeTracking_Primitive_Test() { var collection = new SDataCollection<int> {1}; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection[0] = 2; Assert.That(collection.IsChanged, Is.True); var changes = (SDataCollection<object>) collection.GetChanges(); Assert.That(changes.DeleteMissing, Is.True); Assert.That(changes, Is.EqualTo(new List<object> {2})); collection.Add(3); Assert.That(collection.IsChanged, Is.True); Assert.That(collection.GetChanges(), Is.EqualTo(new List<object> {2, 3})); }
public void ChangeTracking_RejectChanges_Test() { var collection = new SDataCollection<int> {1, 2}; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection.RemoveAt(0); collection[0] = 2; collection.Add(3); Assert.That(collection.IsChanged, Is.True); Assert.That(collection.GetChanges(), Is.Not.Null); collection.RejectChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); }
public void Properties_Test() { var resources = new SDataCollection <SDataResource> { new SDataResource { Descriptor = "Widget" } }; var converter = TypeDescriptor.GetConverter(resources); var props = converter.GetProperties(resources); Assert.That(props, Is.Not.Null); Assert.That(props.Count, Is.EqualTo(1)); var prop = props[0]; Assert.That(prop.Name, Is.EqualTo("[0]")); }
public void Write_Feed_Schema_Test() { var resources = new SDataCollection <SDataResource> { Schema = @"<schema xmlns=""http://www.w3.org/2001/XMLSchema"">" }; var nav = Helpers.WriteAtom(resources); var mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); mgr.AddNamespace("sdata", "http://schemas.sage.com/sdata/2008/1"); var node = nav.SelectSingleNode("atom:feed/sdata:schema", mgr); Assert.That(node, Is.Not.Null); Assert.That(node.Value, Is.EqualTo(@"<schema xmlns=""http://www.w3.org/2001/XMLSchema"">")); }
public void ChangeTracking_RemoveResource_Test() { var resource = new SDataResource(new Dictionary<string, object> {{"FirstName", "Joe"}}) {Key = "abc123"}; var collection = new SDataCollection<SDataResource> {resource}; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection.RemoveAt(0); Assert.That(collection.IsChanged, Is.True); var changes = (SDataCollection<object>) collection.GetChanges(); Assert.That(changes.DeleteMissing, Is.False); Assert.That(changes, Is.EqualTo(new List<object> {new Dictionary<string, object>()})); resource = ((SDataResource) changes[0]); Assert.That(resource.Key, Is.EqualTo("abc123")); Assert.That(resource.IsDeleted, Is.True); }
public void Deserialize_ProtocolObject_Enumerable_Test() { var value = new SDataCollection <string> { "Smith" }; ((ISDataProtocolObject)value).Info = new SDataProtocolInfo { Url = new Uri("http://www.example.com") }; var result = ContentHelper.Deserialize <IList <object> >(value); Assert.That(result, Is.InstanceOf <ISDataProtocolObject>()); Assert.That(result, Is.Not.SameAs(value)); Assert.That(result, Has.Count.EqualTo(1)); Assert.That(result[0], Is.EqualTo("Smith")); Assert.That(((ISDataProtocolObject)result).Info.Url, Is.EqualTo(new Uri("http://www.example.com"))); }
public void ChangeTracking_RejectChanges_Test() { var collection = new SDataCollection <int> { 1, 2 }; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection.RemoveAt(0); collection[0] = 2; collection.Add(3); Assert.That(collection.IsChanged, Is.True); Assert.That(collection.GetChanges(), Is.Not.Null); collection.RejectChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); }
private static SDataCollection <string> ReadSimpleCollection(XContainer source) { var collection = new SDataCollection <string>(); foreach (var item in source.Elements()) { if (string.IsNullOrEmpty(collection.XmlLocalName)) { collection.XmlLocalName = item.Name.LocalName; } var nilAttr = item.Attribute(_xsiNs + "nil"); var value = nilAttr != null && XmlConvert.ToBoolean(nilAttr.Value) ? null : item.Value; collection.Add(value); } return(collection); }
public void Write_Feed_SyncDigest_Test() { var resources = new SDataCollection <SDataResource> { SyncDigest = new Digest { Origin = "origin" } }; var nav = Helpers.WriteAtom(resources); var mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); mgr.AddNamespace("sync", "http://schemas.sage.com/sdata/sync/2008/1"); var node = nav.SelectSingleNode("atom:feed/sync:digest/sync:origin", mgr); Assert.That(node, Is.Not.Null); Assert.That(node.Value, Is.EqualTo("origin")); }
public void Write_Feed_Diagnoses_Test() { var resources = new SDataCollection <SDataResource> { Diagnoses = { new Diagnosis { SDataCode = DiagnosisCode.BadUrlSyntax } } }; var nav = Helpers.WriteAtom(resources); var mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); mgr.AddNamespace("sdata", "http://schemas.sage.com/sdata/2008/1"); var node = nav.SelectSingleNode("atom:feed/sdata:diagnosis/sdata:sdataCode", mgr); Assert.That(node, Is.Not.Null); Assert.That(node.Value, Is.EqualTo("BadUrlSyntax")); }
private static SDataCollection <T> CreateCollection <T>(IEnumerable <T> collection, int?totalResults) { var list = new SDataCollection <T>(); if (collection != null) { foreach (var item in collection) { list.Add(item); } } if (totalResults != null) { ((ISDataProtocolObject)list).Info = new SDataProtocolInfo { TotalResults = totalResults }; } return(list); }
private void UpdateCollection() { _collection = Client.Execute <SDataCollection <SDataResource> >( new SDataParameters { Path = tbCollectionResourceKind.Text, StartIndex = (int)numStartIndex.Value, Count = (int)numCount.Value }).Content; var lookup = _collection.Links.ToLookup(link => link.Relation); btnFirst.Enabled = lookup["first"].Any(); btnPrevious.Enabled = lookup["previous"].Any(); btnNext.Enabled = lookup["next"].Any(); btnLast.Enabled = lookup["last"].Any(); atomEntryGrid.Rows.Clear(); atomEntryGrid.Columns.Clear(); if (_collection.Count > 0) { foreach (var key in _collection[0].Keys) { atomEntryGrid.Columns.Add(key, key); } foreach (var item in _collection) { atomEntryGrid.Rows.Add(item.Values.ToArray()); } } atomEntryGrid.Refresh(); atomEntryGrid.AutoResizeColumns(); if (atomEntryGrid.SelectedRows.Count != 0) { atomEntryGrid_CellClick(null, null); } }
public void Write_Collection_Diagnoses_Test() { var resources = new SDataCollection <SDataResource> { Diagnoses = { new Diagnosis { SDataCode = DiagnosisCode.BadUrlSyntax } } }; var obj = Helpers.WriteJson(resources); Assert.That(obj["$diagnoses"], Is.InstanceOf <IList <object> >()); var diagnoses = (IList <object>)obj["$diagnoses"]; Assert.That(diagnoses, Has.Count.EqualTo(1)); Assert.That(diagnoses[0], Is.InstanceOf <IDictionary <string, object> >()); var diagnosis = (IDictionary <string, object>)diagnoses[0]; Assert.That(diagnosis["sdataCode"], Is.EqualTo("BadUrlSyntax")); }
public void ChangeTracking_Primitive_Test() { var collection = new SDataCollection <int> { 1 }; collection.AcceptChanges(); Assert.That(collection.IsChanged, Is.False); Assert.That(collection.GetChanges(), Is.Null); collection[0] = 2; Assert.That(collection.IsChanged, Is.True); var changes = (SDataCollection <object>)collection.GetChanges(); Assert.That(changes.DeleteMissing, Is.True); Assert.That(changes, Is.EqualTo(new List <object> { 2 })); collection.Add(3); Assert.That(collection.IsChanged, Is.True); Assert.That(collection.GetChanges(), Is.EqualTo(new List <object> { 2, 3 })); }
public void Write_Flat_Collection_Test() { var lines = new SDataCollection <SDataResource>("SalesOrderLine") { new SDataResource(), new SDataResource() }; lines.XmlIsFlat = true; var resource = new SDataResource("SalesOrder") { { "SalesOrderLines", lines } }; var nav = Helpers.WriteAtom(resource); var mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); mgr.AddNamespace("sdata", "http://schemas.sage.com/sdata/2008/1"); var nodes = nav.Select("atom:entry/sdata:payload/SalesOrder/SalesOrderLine", mgr); Assert.That(nodes, Is.Not.Null); Assert.That(nodes.Count, Is.EqualTo(2)); }
private static SDataCollection <SDataResource> ReadFeed(XContainer feed) { var collection = new SDataCollection <SDataResource>(feed.Elements(_atomNs + "entry").Select(ReadEntry)) { Id = ReadElementValue <string>(feed, _atomNs + "id"), Title = ReadElementValue <string>(feed, _atomNs + "title"), Updated = ReadElementValue <DateTimeOffset?>(feed, _atomNs + "updated"), TotalResults = ReadElementValue <int?>(feed, _openSearchNs + "totalResults"), StartIndex = ReadElementValue <int?>(feed, _openSearchNs + "startIndex"), ItemsPerPage = ReadElementValue <int?>(feed, _openSearchNs + "itemsPerPage"), Schema = ReadElementValue <string>(feed, _sDataNs + "schema"), Links = ReadLinks(feed), SyncMode = ReadElementValue <SyncMode?>(feed, _syncNs + "syncMode") }; var diagnoses = feed.Element(_sDataNs + "diagnoses"); if (diagnoses != null) { collection.Diagnoses = DeserializeObject <Diagnoses>(diagnoses); } foreach (var diagnosis in feed.Elements(_sDataNs + "diagnosis")) { collection.Diagnoses.Add(DeserializeObject <Diagnosis>(diagnosis)); } var digest = feed.Element(_syncNs + "digest"); if (digest != null) { collection.SyncDigest = DeserializeObject <Digest>(digest); } return(collection); }
public void Write_Collection_SyncDigest_Test() { var resources = new SDataCollection<SDataResource> { SyncDigest = new Digest {Origin = "origin"} }; var obj = Helpers.WriteJson(resources); Assert.That(obj["$digest"], Is.InstanceOf<IDictionary<string, object>>()); var digest = (IDictionary<string, object>) obj["$digest"]; Assert.That(digest["origin"], Is.EqualTo("origin")); }
public void Write_Collection_Id_Test() { var resources = new SDataCollection<SDataResource> {Id = "id"}; var obj = Helpers.WriteJson(resources); var id = obj["$id"]; Assert.That(id, Is.EqualTo("id")); }
public void Write_Collection_Diagnoses_Test() { var resources = new SDataCollection<SDataResource> { Diagnoses = { new Diagnosis {SDataCode = DiagnosisCode.BadUrlSyntax} } }; var obj = Helpers.WriteJson(resources); Assert.That(obj["$diagnoses"], Is.InstanceOf<IList<object>>()); var diagnoses = (IList<object>) obj["$diagnoses"]; Assert.That(diagnoses, Has.Count.EqualTo(1)); Assert.That(diagnoses[0], Is.InstanceOf<IDictionary<string, object>>()); var diagnosis = (IDictionary<string, object>) diagnoses[0]; Assert.That(diagnosis["sdataCode"], Is.EqualTo("BadUrlSyntax")); }
private void UpdateCollection() { _collection = Client.Execute<SDataCollection<SDataResource>>( new SDataParameters { Path = tbCollectionResourceKind.Text, StartIndex = (int) numStartIndex.Value, Count = (int) numCount.Value }).Content; var lookup = _collection.Links.ToLookup(link => link.Relation); btnFirst.Enabled = lookup["first"].Any(); btnPrevious.Enabled = lookup["previous"].Any(); btnNext.Enabled = lookup["next"].Any(); btnLast.Enabled = lookup["last"].Any(); atomEntryGrid.Rows.Clear(); atomEntryGrid.Columns.Clear(); if (_collection.Count > 0) { foreach (var key in _collection[0].Keys) { atomEntryGrid.Columns.Add(key, key); } foreach (var item in _collection) { atomEntryGrid.Rows.Add(item.Values.ToArray()); } } atomEntryGrid.Refresh(); atomEntryGrid.AutoResizeColumns(); if (atomEntryGrid.SelectedRows.Count != 0) { atomEntryGrid_CellClick(null, null); } }