public SirenSample() { this.Class = new string[] { "order" }; this.Properties = new { OrderNumber = 42, ItemCount = 3, Status = "pending" }; ISubEntity entity1 = new EmbeddedLink { Class = new string[] { "items", "collection" }, Rel = new string[] { "http://x.io/rels/order-items" }, Href = "http://api.x.io/orders/42/items" }; this.AddEntity(entity1); SirenObject entity2 = new SirenObject("info", "customer"); entity2.Properties = new { CustomerId= "pj123", Name= "Peter Joseph" }; entity2.AddLink(new SirenLink("http://api.x.io/customers/pj123", "self")); this.AddEntity(entity2); var addItemAction = new SirenAction() { Name ="add-item", Title = "Add item", Method = "POST", Href = "http://api.x.io/orders/42/items", Type = "application/x-www-form-urlencoded" }; addItemAction.AddField(new SirenField("orderNumber", SirenField.TypeHidden, "42")); addItemAction.AddField(new SirenField("productCode", SirenField.TypeText)); addItemAction.AddField(new SirenField("orderNumber", SirenField.TypeNumber)); this.AddAction(addItemAction); this.AddLink("http://api.x.io/orders/42", "self"); this.AddLink("http://api.x.io/orders/41", "previous"); this.AddLink("http://api.x.io/orders/43", "next"); }
public void SirenAction_DeserializesCorrectly() { ISirenAction sirenAction = new SirenAction( name: "foo", href: new Uri("http://example.com"), @class: new [] { "bar" }, method: "GET", title: "Some action", type: "text/html", fields: new [] { new SirenField(name: "field") } ); string serialized = JsonConvert.SerializeObject(sirenAction); ISirenAction action = JsonConvert.DeserializeObject <SirenAction>(serialized); Assert.AreEqual("foo", action.Name); Assert.AreEqual("http://example.com/", action.Href.ToString()); Assert.Contains("bar", action.Class); Assert.AreEqual("GET", action.Method); Assert.AreEqual("Some action", action.Title); Assert.AreEqual("text/html", action.Type); Assert.AreEqual(1, action.Fields.ToList().Count); }
public void SirenAction_Equality_MissingAttributes_ShouldNotBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = new SirenAction( name: action.Name, href: action.Href ); TestHelpers.BidirectionalEquality(action, other, false); }
private ISirenAction GetAction() { ISirenAction action = new SirenAction( name: "action-name", href: new Uri("http://example.com"), fields: new[] { new SirenField(name: "field1", @class: new [] { "class" }, type: "text/html"), new SirenField(name: "field2", @class: new [] { "class" }, type: "text/html"), new SirenField(name: "field3", @class: new [] { "not-class" }, type: "text/xml"), } ); return(action); }
public void SirenAction_Equality_DifferentFields_ShouldNotBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: new[] { new SirenField("fieldName1") } ); TestHelpers.BidirectionalEquality(action, other, false); }
public void SirenAction_Serialized_DoesNotIncludeOptionalParametersIfNull() { ISirenAction sirenAction = new SirenAction( name: "foo", href: new Uri("http://example.com")); string serialized = JsonConvert.SerializeObject(sirenAction); ISirenAction action = JsonConvert.DeserializeObject <SirenAction>(serialized); Assert.AreEqual("foo", action.Name); Assert.AreEqual("http://example.com/", action.Href.ToString()); Assert.IsEmpty(action.Class); Assert.IsNull(action.Method); Assert.IsNull(action.Title); Assert.IsNull(action.Type); Assert.IsEmpty(action.Fields); }
public static ISirenAction GetAction(string name = "action-name") { ISirenAction action = new SirenAction( name: name, href: new Uri("http://example.com"), @class: new[] { "foo" }, method: "GET", title: "Action title", type: "some-type", fields: new[] { new SirenField(name: "field1", @class: new [] { "class" }, type: SirenFieldType.Text), new SirenField(name: "field2", @class: new [] { "class" }, type: SirenFieldType.Text), new SirenField(name: "field3", @class: new [] { "not-class" }, type: SirenFieldType.Range) } ); return(action); }
public void SirenAction_Equality_DifferentFieldOrder_ShouldBeEqual() { ISirenAction action = TestHelpers.GetAction(); ISirenAction other = new SirenAction( name: action.Name, href: action.Href, @class: action.Class, method: action.Method, title: action.Title, type: action.Type, fields: new[] { action.Fields.ElementAt(1), action.Fields.ElementAt(2), action.Fields.ElementAt(0) } ); TestHelpers.BidirectionalEquality(action, other, true); }
public void SirenAction_Serialize_ExcludesClassAndFieldsIfEmpty() { ISirenAction action = new SirenAction( name: "foo", href: new Uri("http://example.com"), @class: new [] { "bar" }, fields: new [] { new SirenField("baz") } ); string serialized = JsonConvert.SerializeObject(action); Assert.GreaterOrEqual(serialized.IndexOf("class", StringComparison.Ordinal), 0); Assert.GreaterOrEqual(serialized.IndexOf("fields", StringComparison.Ordinal), 0); action = new SirenAction( name: "foo", href: new Uri("http://example.com") ); serialized = JsonConvert.SerializeObject(action); Assert.AreEqual(-1, serialized.IndexOf("class", StringComparison.Ordinal)); Assert.AreEqual(-1, serialized.IndexOf("fields", StringComparison.Ordinal)); }
public SirenSample() { this.Class = new string[] { "order" }; this.Properties = new { OrderNumber = 42, ItemCount = 3, Status = "pending" }; ISubEntity entity1 = new EmbeddedLink { Class = new string[] { "items", "collection" }, Rel = new string[] { "http://x.io/rels/order-items" }, Href = "http://api.x.io/orders/42/items" }; this.AddEntity(entity1); SirenObject entity2 = new SirenObject("info", "customer"); entity2.Properties = new { CustomerId = "pj123", Name = "Peter Joseph" }; entity2.AddLink(new SirenLink("http://api.x.io/customers/pj123", "self")); this.AddEntity(entity2); var addItemAction = new SirenAction() { Name = "add-item", Title = "Add item", Method = "POST", Href = "http://api.x.io/orders/42/items", Type = "application/x-www-form-urlencoded" }; addItemAction.AddField(new SirenField("orderNumber", SirenField.TypeHidden, "42")); addItemAction.AddField(new SirenField("productCode", SirenField.TypeText)); addItemAction.AddField(new SirenField("orderNumber", SirenField.TypeNumber)); this.AddAction(addItemAction); this.AddLink("http://api.x.io/orders/42", "self"); this.AddLink("http://api.x.io/orders/41", "previous"); this.AddLink("http://api.x.io/orders/43", "next"); }