Пример #1
0
        public void AddingEmbeddedItemCollection_WithDuplicateKeyName_ThrowsException()
        {
            const string initialKey = "I-AM-THE-KEY-THE-FIRST-TIME";
            const string secondKey  = "I-AM-THE-KEY-THE-FIRST-TIME";

            var model       = new SimpleModel();
            var parentGraph = new HalGraph(model);

            var childModel1 = new SimpleModel();
            var childModel2 = new SimpleModel();

            var childGraph1 = new HalGraph(childModel1);
            var childGraph2 = new HalGraph(childModel2);

            parentGraph.AddEmbeddedItemCollection(
                initialKey,
                new List <IHalGraph> {
                childGraph1, childGraph2
            });

            Assert.Throws <ArgumentException>(() =>
            {
                parentGraph.AddEmbeddedItemCollection(
                    secondKey,
                    new List <IHalGraph> {
                    childGraph1, childGraph2
                });
            });
        }
Пример #2
0
        public void WritingHalGraphCollection_EachWithOneEmbeddedItem_WritesPropertyAndAddsEmbeddedItem()
        {
            var propertyNameConvention = new Mock <IPropertyNameConvention>();

            propertyNameConvention.Setup(convention => convention.Apply("StringProperty")).Returns("string-Property");
            propertyNameConvention.Setup(convention => convention.Apply("IntegerProperty")).Returns("integer-Property");
            propertyNameConvention.Setup(convention => convention.Apply("TEST")).Returns("test");
            propertyNameConvention.Setup(convention => convention.Apply("href")).Returns("href");

            List <OnePropertyClass> topLevelModels = GetOnePropertyClassModelCollection();
            var halGraphCollection = new List <IHalGraph>();

            var modelToEmbedd = new OneIntegerPropertyClass {
                IntegerProperty = 999
            };
            var halGraphToEmbed = new HalGraph(modelToEmbedd);

            foreach (var model in topLevelModels)
            {
                halGraphCollection.Add(
                    new HalGraph(model).AddEmbeddedItem("TEST", halGraphToEmbed));
            }

            var serializerOptions = new JsonSerializerOptions
            {
                Converters    = { new HalGraphCollectionJsonConvertor(propertyNameConvention.Object) },
                WriteIndented = true
            };

            var responseJson = JsonSerializer.Serialize(halGraphCollection, typeof(List <IHalGraph>), serializerOptions);


            Approvals.Verify(responseJson);
        }
Пример #3
0
        public void AddingMultipleCuries_WhenValid_AddsThemAllToTheCollection()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var firstCurie  = new Curie("a", "/api/some-resource1/{template}");
            var secondCurie = new Curie("b", "/api/some-resource2/{template}");
            var thirdCurie  = new Curie("c", "/api/some-resource3/{template}");
            var fourthCurie = new Curie("d", "/api/some-resource4/{template}");

            graph.AddCuries(new List <Curie> {
                firstCurie, secondCurie, thirdCurie, fourthCurie
            });

            var curiesCollection = (List <CurieLink>)((Dictionary <string, object>)graph["_links"])["curies"];

            var expectedCurieLink1 = new CurieLink("a", "/api/some-resource1/{template}");
            var expectedCurieLink2 = new CurieLink("b", "/api/some-resource2/{template}");
            var expectedCurieLink3 = new CurieLink("c", "/api/some-resource3/{template}");
            var expectedCurieLink4 = new CurieLink("d", "/api/some-resource4/{template}");

            Assert.True(((Dictionary <string, object>)graph["_links"]).Count == 1);
            Assert.True(curiesCollection.Count == 4);
            expectedCurieLink1.ShouldDeepEqual(curiesCollection[0]);
            expectedCurieLink2.ShouldDeepEqual(curiesCollection[1]);
            expectedCurieLink3.ShouldDeepEqual(curiesCollection[2]);
            expectedCurieLink4.ShouldDeepEqual(curiesCollection[3]);
        }
Пример #4
0
        public void AddingEmbeddedItemCollection_WithVaryingModelType_ThrowsException()
        {
            const string embeddedItemName = "SOME-VALID-STRING";

            var modelParent = new SimpleModel {
                StringProperty = "Some string value"
            };
            var modelChild1 = new ExampleModel {
                Id = Guid.NewGuid(), Name = "SOME NAME"
            };
            var modelChild2IsOfDifferentType = new SimpleModelWithDateField {
                SomeDateField = DateTime.Now
            };

            var graph          = new HalGraph(modelParent);
            var embeddedGraph1 = new HalGraph(modelChild1);
            var embeddedGraph2 = new HalGraph(modelChild2IsOfDifferentType);

            Assert.Throws <ArgumentException>(() =>
            {
                graph.AddEmbeddedItemCollection(
                    embeddedItemName,
                    new List <IHalGraph> {
                    embeddedGraph1, embeddedGraph2
                });
            });
        }
Пример #5
0
        public void AddingEmbeddedItemCollection_WhenValid_AddsItToTheCollection()
        {
            const string embeddedItemName = "SOME-VALID-STRING";

            var modelParent = new SimpleModel {
                StringProperty = "Some string value"
            };
            var modelChild1 = new ExampleModel {
                Id = Guid.NewGuid(), Name = "SOME NAME"
            };
            var modelChild2 = new ExampleModel {
                Id = Guid.NewGuid(), Name = "ANOTHER NAME"
            };
            var graph = new HalGraph(modelParent);

            var embeddedGraph1 = new HalGraph(modelChild1);
            var embeddedGraph2 = new HalGraph(modelChild2);

            graph.AddEmbeddedItemCollection(
                embeddedItemName,
                new List <IHalGraph> {
                embeddedGraph1, embeddedGraph2
            });

            var storedHalGraph = (List <IHalGraph>)((Dictionary <string, object>)graph["_embedded"])[embeddedItemName];

            Assert.True(((Dictionary <string, object>)graph["_embedded"]).Count == 1);
            Assert.True(storedHalGraph.Count == 2);
            Assert.Equal(expected: embeddedGraph1, actual: storedHalGraph[0]);
            Assert.Equal(expected: embeddedGraph2, actual: storedHalGraph[1]);
        }
Пример #6
0
        public void AddingMultipleLink_WhenValid_AddsThemAllToTheSameRelCollection()
        {
            const string rel = "myrel";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var firstLink  = new Link("/api/some-resource1");
            var secondLink = new Link("/api/some-resource2");
            var thirdLink  = new Link("/api/some-resource3");
            var fourthLink = new Link("/api/some-resource4");

            graph.AddLinks(rel, new List <Link> {
                firstLink, secondLink, thirdLink, fourthLink
            });

            var linksCollection = (List <Link>)((Dictionary <string, object>)graph["_links"])[rel];

            Assert.True(((Dictionary <string, object>)graph["_links"]).Count == 1);
            Assert.True(linksCollection.Count == 4);
            Assert.Equal(expected: firstLink, actual: linksCollection[0]);
            Assert.Equal(expected: secondLink, actual: linksCollection[1]);
            Assert.Equal(expected: thirdLink, actual: linksCollection[2]);
            Assert.Equal(expected: fourthLink, actual: linksCollection[3]);
        }
Пример #7
0
        public void Constructing_WithModelProvided_SetsModelType()
        {
            var model = new SimpleModel();

            var graph = new HalGraph(model);

            Assert.True(graph.ModelType == typeof(SimpleModel));
        }
Пример #8
0
        public void AddingCurie_WhichIsNull_ThrowsException()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            Assert.Throws <ArgumentNullException>(() =>
            {
                graph.AddCurie(null);
            });
        }
Пример #9
0
        public void Constructing_WithPropertyValueSetToNull_AddsThePropertyWithNullValue()
        {
            var model = new SimpleModel()
            {
                StringProperty = null
            };

            var graph = new HalGraph(model);

            Assert.Null(graph["StringProperty"]);
        }
Пример #10
0
        public void AddingCurie_WhenValid_ReturnsTheHalGraph()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var expectedCurie = new Curie("xx", "http://www.myapi.com/api/{placeholder}");

            var result = graph.AddCurie(expectedCurie);

            Assert.Equal(expected: graph, actual: result);
        }
Пример #11
0
        public void AddingCurie_WhenValid_MakesLinksTheFirstKey()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var curieToCreate = new Curie("xx", "http://www.myapi.com/api/{placeholder}");

            var result = (HalGraph)graph.AddCurie(curieToCreate);

            Assert.IsType <Dictionary <string, object> >(result[0]);
        }
Пример #12
0
        public void AddingLink_WhichIsNull_ThrowsException()
        {
            const string rel = "myrel";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            Assert.Throws <ArgumentNullException>(() =>
            {
                graph.AddLink(rel, null);
            });
        }
Пример #13
0
        public void AddingEmbeddedItemCollection_WhichIsNull_ThrowsException()
        {
            const string embeddedItemName = "some-name";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            Assert.Throws <ArgumentNullException>(() =>
            {
                graph.AddEmbeddedItemCollection(embeddedItemName, null);
            });
        }
Пример #14
0
        public void AddingMultipleCuries_WithEmptyCuriesCollection_ThrowsException()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var curiesToAdd = new List <Curie>();

            Assert.Throws <ArgumentException>(() =>
            {
                graph.AddCuries(curiesToAdd);
            });
        }
Пример #15
0
        public void AddingEmbeddedItem_WhenValid_ReturnsTheHalGraph()
        {
            const string embeddedItemName = "SOME-VALID-STRING";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var expectedEmbeddedGraph = new HalGraph(model);

            var result = graph.AddEmbeddedItem(embeddedItemName, expectedEmbeddedGraph);

            Assert.Equal(expected: graph, actual: result);
        }
Пример #16
0
        public void AddingCurie_WhenValid_CreatesCurieCollection()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var curieToCreate = new Curie("xx", "http://www.myapi.com/api/{something}");

            graph.AddCurie(curieToCreate);

            var actualLink = ((Dictionary <string, object>)graph["_links"])["curies"];

            Assert.IsAssignableFrom <IEnumerable <Link> >(actualLink);
        }
Пример #17
0
        public void AddingLink_WhenValid_ReturnsTheHalGraph()
        {
            const string rel = "myrel";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var expectedLink = new Link("/api/some-resource");

            var result = graph.AddLink(rel, expectedLink);

            Assert.Equal(expected: graph, actual: result);
        }
Пример #18
0
        public void AddingLink_WhenValid_MakesLinksTheFirstKey()
        {
            const string rel = "myrel";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var expectedLink = new Link("/api/some-resource");

            var result = (HalGraph)graph.AddLink(rel, expectedLink);

            Assert.IsType <Dictionary <string, object> >(result[0]);
        }
Пример #19
0
        public void Constructing_WithSinglePropertyModelProvided_AddsProperty()
        {
            const string expectedKeyName = "StringProperty";
            const string expectedValue   = "I AM A STRING FIELD";

            var model = new SimpleModel {
                StringProperty = expectedValue
            };

            var graph = new HalGraph(model);

            Assert.True(graph.Contains(expectedKeyName));
            Assert.Equal(expected: expectedValue, actual: graph[expectedKeyName]);
        }
Пример #20
0
        public void Constructing_WithModelContainingGuidProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            Guid         value        = Guid.Parse("346d6b20-a2ec-4bf1-98c4-bbf897e5f87a");

            var model = new ModelWithGuidProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #21
0
        public void AddingEmbeddedItem_WithEmptyName_ThrowsException()
        {
            var embeddedItemName = string.Empty;

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var embeddedGraph = new HalGraph(model);

            Assert.Throws <ArgumentException>(() =>
            {
                graph.AddEmbeddedItem(embeddedItemName, embeddedGraph);
            });
        }
Пример #22
0
        public void Constructing_WithModelContainingTimeSpanProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            TimeSpan     value        = new TimeSpan(1000);

            var model = new ModelWithTimeSpanProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #23
0
        public void Constructing_WithModelContainingNullableIntegerProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            int?         value        = 12;

            var model = new ModelWithNullableIntegerProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #24
0
        public void Constructing_WithModelContainingUriProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            Uri          value        = new Uri("http://www.google.com");

            var model = new ModelWithUriProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #25
0
        public void AddingLink_WithEmptyRel_ThrowsException()
        {
            string rel = "";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var linkToAdd = new Link("/api/some-resource");

            Assert.Throws <ArgumentException>(() =>
            {
                graph.AddLink(rel, linkToAdd);
            });
        }
Пример #26
0
        public void Constructing_WithModelContainingStringProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            const string value        = "MY-VALUE";

            var model = new ModelWithStringProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #27
0
        public void Constructing_WithModelContainingFloatProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            const float  value        = 134.45E-2f;

            var model = new ModelWithFloatProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #28
0
        public void AddingMultipleLinks_WithEmptyLinksCollection_ThrowsException()
        {
            string rel = "my-rel";

            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var linksToAdd = new List <Link>();

            Assert.Throws <ArgumentException>(() =>
            {
                graph.AddLinks(rel, linksToAdd);
            });
        }
Пример #29
0
        public void Constructing_WithModelContainingDateTimeProperty_AddsTheProperty()
        {
            const string propertyName = "Property";
            DateTime     value        = new DateTime(2020, 6, 28);

            var model = new ModelWithDateTimeProperty {
                Property = value
            };

            var graph = new HalGraph(model);

            Assert.Equal(expected: value, graph[propertyName]);
            Assert.Equal(expected: value.GetType(), actual: graph[propertyName].GetType());
        }
Пример #30
0
        public void Constructing_WithModelContainingNoOutputProperty_DoesNotAddProperty()
        {
            const string fieldExpectedToBeMissing = "Integer2";

            var model = new ExampleWithIgnoredProperty
            {
                Integer1 = 1,
                Integer2 = 2,
                Integer3 = 3
            };

            var graph = new HalGraph(model);

            Assert.True(!graph.Contains(fieldExpectedToBeMissing));
        }