コード例 #1
0
        public void SirenEntity_Equality_MissingAttributes_ShouldNotBeEqual()
        {
            ISirenEntity entity = TestHelpers.GetEntity();
            ISirenEntity other  = new SirenEntity();

            TestHelpers.BidirectionalEquality(entity, other, false);
        }
コード例 #2
0
        public void SirenEntity_Serialize_ExcludesRelClassEntitiesLinksAndActionsIfEmpty()
        {
            ISirenEntity entity = new SirenEntity(
                @class: new[] { "foo" },
                rel: new[] { "bar" },
                entities: new[] {
                new SirenEntity()
            },
                links: new[] {
                new SirenLink(rel: new[] { "self" }, href: new Uri("http://example.com"), @class: new[] { "class" })
            },
                actions: new[] {
                new SirenAction(name: "action-name", href: new Uri("http://example.com"), @class: new[] { "class" })
            }
                );
            string serialized = JsonConvert.SerializeObject(entity);

            Assert.GreaterOrEqual(serialized.IndexOf("rel", StringComparison.Ordinal), -1);
            Assert.GreaterOrEqual(serialized.IndexOf("class", StringComparison.Ordinal), -1);
            Assert.GreaterOrEqual(serialized.IndexOf("entities", StringComparison.Ordinal), -1);
            Assert.GreaterOrEqual(serialized.IndexOf("links", StringComparison.Ordinal), -1);
            Assert.GreaterOrEqual(serialized.IndexOf("actions", StringComparison.Ordinal), -1);

            entity     = new SirenEntity();
            serialized = JsonConvert.SerializeObject(entity);
            Assert.AreEqual(-1, serialized.IndexOf("rel", StringComparison.Ordinal));
            Assert.AreEqual(-1, serialized.IndexOf("class", StringComparison.Ordinal));
            Assert.AreEqual(-1, serialized.IndexOf("entities", StringComparison.Ordinal));
            Assert.AreEqual(-1, serialized.IndexOf("links", StringComparison.Ordinal));
            Assert.AreEqual(-1, serialized.IndexOf("actions", StringComparison.Ordinal));
        }
コード例 #3
0
        public void SirenEntity_DeserializesCorrectly()
        {
            ISirenEntity sirenEntity = new SirenEntity(
                properties: new {
                foo = "bar"
            },
                links: new[] {
                new SirenLink(rel: new[] { "self" }, href: new Uri("http://example.com"), @class: new[] { "class" })
            },
                rel: new[] { "organization" },
                @class: new[] { "some-class" },
                entities: new[] {
                new SirenEntity()
            },
                actions: new[] {
                new SirenAction(name: "action-name", href: new Uri("http://example.com"), @class: new[] { "class" })
            },
                title: "Entity title",
                href: new Uri("http://example.com/3"),
                type: "text/html"
                );

            string       serialized = JsonConvert.SerializeObject(sirenEntity);
            ISirenEntity entity     = JsonConvert.DeserializeObject <SirenEntity>(serialized);

            Assert.AreEqual("bar", (string)entity.Properties.foo);
            Assert.AreEqual(1, entity.Links.ToList().Count);
            Assert.Contains("organization", entity.Rel);
            Assert.Contains("some-class", entity.Class);
            Assert.AreEqual(1, entity.Entities.ToList().Count);
            Assert.AreEqual(1, entity.Actions.ToList().Count);
            Assert.AreEqual("Entity title", entity.Title);
            Assert.AreEqual("http://example.com/3", entity.Href.ToString());
            Assert.AreEqual("text/html", entity.Type);
        }
コード例 #4
0
        public static ISirenEntity GetEntity(string title = "title")
        {
            ISirenEntity entity = new SirenEntity(
                title: title,
                rel: new[] { "rel" },
                @class: new[] { "class" },
                properties: new {
                foo = "bar"
            },
                href: new Uri("http://example.com"),
                type: "text/html",
                links: new[] {
                new SirenLink(rel: new[] { "self" }, href: new Uri("http://example.com"), @class: new [] { "class" }, type: "text/html", title: "link1"),
                new SirenLink(rel: new[] { "next" }, href: new Uri("http://example.com"), @class: new [] { "class" }, type: "text/html", title: "link2"),
                new SirenLink(rel: new[] { "next" }, href: new Uri("http://example.com"), @class: new [] { "not-class" }, type: "text/html", title: "link3")
            },
                actions: new[] {
                new SirenAction(name: "action1", href: new Uri("http://example.com"), @class: new[] { "class" }),
                new SirenAction(name: "action2", href: new Uri("http://example.com"), @class: new[] { "class" }),
                new SirenAction(name: "action3", href: new Uri("http://example.com"), @class: new[] { "not-class" })
            },
                entities: new[] {
                new SirenEntity(rel: new [] { "child" }, @class: new [] { "class" }, type: "text/html", title: "entity1"),
                new SirenEntity(rel: new [] { "child" }, @class: new [] { "class" }, type: "text/html", title: "entity2"),
                new SirenEntity(rel: new [] { "not-child" }, @class: new [] { "class" }, type: "text/xml", title: "entity3")
            }
                );

            return(entity);
        }
コード例 #5
0
        public void Unparsing_only_links()
        {
            // Arrange
            var doc = new SirenEntity();

            doc.Links.Add(new Link("https://localhost/test", new[] { "self", "previous" }));
            doc.Links.Add(new Link("https://localhost/test2", new[] { "next" })
            {
                Title = "testtitle", Type = new MediaTypeHeaderValue("application/json")
            });

            // Act
            var jobj = SirenJson.Unparse(doc);

            // Assert
            Assert.NotNull(jobj);
            var links = Assert.IsAssignableFrom <JArray>(jobj[SirenJson.LINKS]);

            var link0     = Assert.IsAssignableFrom <JObject>(links.Single(x => x[SirenJson.HREF].ToString() == "https://localhost/test"));
            var link0rels = Assert.IsAssignableFrom <JArray>(link0[SirenJson.RELS]);

            Assert.Contains("self", link0rels);
            Assert.Contains("previous", link0rels);

            var link1     = Assert.IsAssignableFrom <JObject>(links.Single(x => x[SirenJson.HREF].ToString() == "https://localhost/test2"));
            var link1rels = Assert.IsAssignableFrom <JArray>(link1[SirenJson.RELS]);

            Assert.Equal("next", link1rels.Single());
            Assert.Equal("testtitle", link1[SirenJson.TITLE]);
            Assert.Equal("application/json", link1[SirenJson.TYPE]);
        }
コード例 #6
0
        public void Unparsing_only_actions()
        {
            // Arrange
            var doc = new SirenEntity();

            doc.Actions.Add(
                new Action("action1", "https://localhost/testaction")
            {
                Classes = new[] { "actionclass" },
                Fields  = new[] {
                    new Field("field1")
                    {
                        Type  = FieldType.Button,
                        Value = "value"
                    }
                },
                Method = new HttpMethod("TEST"),
                Title  = "actiontitle",
                Type   = new MediaTypeHeaderValue("application/json")
            }
                );
            doc.Actions.Add(
                new Action("action2", "https://localhost/testaction2")
                );

            // Act
            var jobj = SirenJson.Unparse(doc);

            // Assert
            Assert.NotNull(jobj);
            var actions = Assert.IsAssignableFrom <JArray>(jobj[SirenJson.ACTIONS]);

            var action1 = Assert.IsAssignableFrom <JObject>(actions.Single(x => x[SirenJson.NAME].ToString() == "action1"));

            Assert.Equal("https://localhost/testaction", action1[SirenJson.HREF]);
            var action1classes = Assert.IsAssignableFrom <JArray>(action1[SirenJson.CLASSES]);

            Assert.Equal("actionclass", action1classes.Single());
            Assert.Equal("TEST", action1[SirenJson.METHOD]);
            Assert.Equal("actiontitle", action1[SirenJson.TITLE]);
            Assert.Equal("application/json", action1[SirenJson.TYPE]);
            var fields = Assert.IsAssignableFrom <JArray>(action1[SirenJson.FIELDS]);
            var field  = Assert.IsAssignableFrom <JObject>(fields[0]);

            Assert.Equal("field1", field[SirenJson.NAME]);
            Assert.Equal("button", field[SirenJson.TYPE]);
            Assert.Equal("value", field[SirenJson.VALUE]);

            var action2 = Assert.IsAssignableFrom <JObject>(actions.Single(x => x[SirenJson.NAME].ToString() == "action2"));

            Assert.Equal("https://localhost/testaction2", action2[SirenJson.HREF]);
            Assert.Null(action2[SirenJson.METHOD]);
            Assert.Null(action2[SirenJson.CLASSES]);
            Assert.Null(action2[SirenJson.TITLE]);
            Assert.Null(action2[SirenJson.TYPE]);
            Assert.Null(action2[SirenJson.FIELDS]);
        }
コード例 #7
0
        public void MatchingHelpers_ThrowsWhenGivenProperties()
        {
            ISirenEntity expected = new SirenEntity(
                properties: new {
                foo = "bar"
            }
                );
            ISirenEntity actual = expected;

            Assert.Throws <ArgumentException>(() => SirenMatchers.Matches(expected, actual, out string _));
        }
コード例 #8
0
        public void Unparsing_the_empty_entity()
        {
            // Arrange
            var emptyDoc = new SirenEntity();

            // Act
            var jobj = SirenJson.Unparse(emptyDoc);

            // Assert
            Assert.NotNull(jobj);
            Assert.Empty(jobj);
        }
コード例 #9
0
        public static SirenEntity Get(Order order, string siteBase)
        {
            var resourceUrl = new Uri(new Uri(siteBase), $"order/{order.OrderNumber}");
            var links       = new List <Link>()
            {
                new Link()
                {
                    Href = $"{resourceUrl}", Rel = new[] { "self" }
                }
            };

            var actions = new List <ApiEntity.Action>();

            if (order.Status == Order.State.Active)
            {
                actions.Add(new ApiEntity.Action()
                {
                    Name = "cancel-order", HRef = $"{resourceUrl}/cancellation", Method = "POST"
                });
            }

            if (order.Status == Order.State.Cancelled)
            {
                actions.Add(new ApiEntity.Action()
                {
                    Name = "activate-order", HRef = $"{resourceUrl}/activation", Method = "POST"
                });
            }

            var orderProperties = new OrderSirenProperties()
            {
                Description = order.Description,
                Id          = order.OrderNumber,
                Status      = GetStatus(order.Status),
                Lines       = order.Lines.Select(GetLine).ToList()
            };

            var result = new SirenEntity()
            {
                Class = new List <string>()
                {
                    "order"
                },
                Links      = links,
                Actions    = actions,
                Properties = orderProperties
            };

            return(result);
        }
コード例 #10
0
        public void Unparsing_an_entity_title()
        {
            // Arrange
            var doc = new SirenEntity();

            doc.Title = "testtitle";

            // Act
            var jobj = SirenJson.Unparse(doc);

            // Assert
            Assert.NotNull(jobj);
            Assert.Equal("testtitle", jobj[SirenJson.TITLE]);
        }
コード例 #11
0
        public void SirenEntity_Serialized_DoesNotIncludeOptionalParametersIfNull()
        {
            ISirenEntity sirenEntity = new SirenEntity();

            string serialized = JsonConvert.SerializeObject(sirenEntity);

            ISirenEntity entity = JsonConvert.DeserializeObject <SirenEntity>(serialized);

            Assert.IsEmpty(entity.Class);
            Assert.IsNull(entity.Properties);
            Assert.IsEmpty(entity.Entities);
            Assert.IsEmpty(entity.Links);
            Assert.IsEmpty(entity.Actions);
            Assert.IsNull(entity.Title);
            Assert.IsEmpty(entity.Rel);
            Assert.IsNull(entity.Href);
            Assert.IsNull(entity.Type);
        }
コード例 #12
0
        public SirenEntity Build()
        {
            SirenEntity entity = new SirenEntity()
            {
                Class      = _class?.ToArray(),
                Properties = _properties?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                Entities   = _subEntityBuilders?.Select(x => x.Build()).ToArray(),
                Links      = _linkBuilders?.Select(x => x.Build()).ToArray(),
                Actions    = _actionBuilders?.Select(x => x.Build()).ToArray(),
                Title      = _title
            };

            if (entity.Actions != null && new HashSet <string>(entity.Actions.Select(x => x.Name)).Count != entity.Actions.Count)
            {
                throw new ArgumentException("Action names MUST be unique within the set of actions for an entity.");
            }

            return(entity);
        }
コード例 #13
0
        public void Unparse_only_classes()
        {
            // Arrange
            var doc = new SirenEntity();

            doc.Classes.Add("person");
            doc.Classes.Add("programmer");

            // Act
            var jobj = SirenJson.Unparse(doc);

            // Assert
            Assert.NotNull(jobj);
            var classes = Assert.IsAssignableFrom <JArray>(jobj[SirenJson.CLASSES]);

            Assert.Equal(2, classes.Count);
            Assert.Contains("person", classes);
            Assert.Contains("programmer", classes);
        }
コード例 #14
0
        public void Unparsing_only_entities()
        {
            // Arrange
            var doc = new SirenEntity();

            doc.EmbeddedLinks.Add(new EmbeddedLink("https://localhost/testchild", new[] { "child" }));
            doc.EmbeddedRepresentations.Add(
                new EmbeddedRepresentation(new[] { "child2" })
            {
                EmbeddedRepresentations = new[]
                {
                    new EmbeddedRepresentation(new [] { "grandchild" })
                }
            }
                );

            // Act
            var jobj = SirenJson.Unparse(doc);

            // Assert
            Assert.NotNull(jobj);
            var entities = Assert.IsAssignableFrom <JArray>(jobj[SirenJson.ENTITIES]);

            var link0     = Assert.IsAssignableFrom <JObject>(entities.Single(x => (x[SirenJson.HREF] ?? "").ToString() == "https://localhost/testchild"));
            var link0rels = Assert.IsAssignableFrom <JArray>(link0[SirenJson.RELS]);

            Assert.Equal("child", link0rels.Single());

            var rep1     = Assert.IsAssignableFrom <JObject>(entities.Single(x => x[SirenJson.HREF] == null));
            var rep1rels = Assert.IsAssignableFrom <JArray>(rep1[SirenJson.RELS]);

            Assert.Equal("child2", rep1rels.Single());

            var rep2     = Assert.IsAssignableFrom <JObject>(rep1[SirenJson.ENTITIES][0]);
            var rep2rels = Assert.IsAssignableFrom <JArray>(rep2[SirenJson.RELS]);

            Assert.Equal("grandchild", rep2rels.Single());
        }
コード例 #15
0
        private ISirenEntity GetEntity()
        {
            ISirenEntity entity = new SirenEntity(
                rel: new [] { "rel" },
                @class: new [] { "class" },
                links: new [] {
                new SirenLink(rel: new[] { "self" }, href: new Uri("http://example.com"), @class: new [] { "class" }, type: "text/html", title: "link1"),
                new SirenLink(rel: new[] { "next" }, href: new Uri("http://example.com"), @class: new [] { "class" }, type: "text/html", title: "link2"),
                new SirenLink(rel: new[] { "next" }, href: new Uri("http://example.com"), @class: new [] { "not-class" }, type: "text/html", title: "link3")
            },
                actions: new [] {
                new SirenAction(name: "action1", href: new Uri("http://example.com"), @class: new[] { "class" }),
                new SirenAction(name: "action2", href: new Uri("http://example.com"), @class: new[] { "class" }),
                new SirenAction(name: "action3", href: new Uri("http://example.com"), @class: new[] { "not-class" })
            },
                entities: new [] {
                new SirenEntity(rel: new [] { "child" }, @class: new [] { "class" }, type: "text/html", title: "entity1"),
                new SirenEntity(rel: new [] { "child" }, @class: new [] { "class" }, type: "text/html", title: "entity2"),
                new SirenEntity(rel: new [] { "not-child" }, @class: new [] { "class" }, type: "text/xml", title: "entity3")
            }
                );

            return(entity);
        }
コード例 #16
0
        public void Unparsing_only_properties()
        {
            // Arrange
            var doc = new SirenEntity();

            doc.Properties.Add("prop1", "value1");
            doc.Properties.Add("prop_arr", new[] { 42 });

            // Act
            var jobj = SirenJson.Unparse(doc);

            // Assert
            Assert.NotNull(jobj);
            var properties = jobj[SirenJson.PROPERTIES];

            Assert.NotNull(properties);
            Assert.Equal("value1", properties["prop1"]);
            Assert.IsAssignableFrom <JArray>(properties["prop_arr"]);
            Assert.Equal(42, properties["prop_arr"][0]);
            Assert.Null(jobj[SirenJson.LINKS]);
            Assert.Null(jobj[SirenJson.ENTITIES]);
            Assert.Null(jobj[SirenJson.CLASSES]);
            Assert.Null(jobj[SirenJson.TITLE]);
        }