Beispiel #1
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]);
        }
Beispiel #2
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]);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public void Constructing_WithValidKeyAndAbsoluteUrl_CreatesCurie()
        {
            var key = "XX";
            var url = "http://www.myapi.com/api/orders/{order-id}";

            var expectedUri = new Uri(url, UriKind.Absolute);

            var curie = new Curie(key, url);

            Assert.Equal(expected: key, actual: curie.Key);
            Assert.Equal(expected: expectedUri, actual: curie.Href);
        }
Beispiel #5
0
        public void Constructing_WithValidKeyAndRelativeUrl_CreatesCurie()
        {
            var key = "XX";
            var url = "/api/orders/{order-id}";

            var expectedUri = new Uri(url, UriKind.Relative);

            var curie = new Curie(key, url);

            Assert.Equal(expected: key, actual: curie.Key);
            Assert.Equal(expected: expectedUri, actual: curie.Href);
        }
Beispiel #6
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);
        }
        /// <summary>
        /// Checks whether the supplied identity is a valid Curie and if so resolves it against the supplied namespace mappings
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        protected string ResolveIdentity(string identity)
        {
            var curie = new Curie(identity);

            if (curie.IsValidCurie && _namespaceMappings.ContainsKey(curie.Prefix))
            {
                return(Curie.ResolveCurie(curie, _namespaceMappings).ToString());
            }
            if (Uri.IsWellFormedUriString(identity, UriKind.Absolute))
            {
                return(identity);
            }
            throw new ArgumentException(Strings.InvalidDataObjectIdentity, "identity");
        }
Beispiel #8
0
        void WriteCurie(JsonWriter writer, Curie curie)
        {
            writer.WriteStartObject();
            writer.WritePropertyName("name");
            writer.WriteValue(curie.Rel);
            writer.WritePropertyName("href");
            writer.WriteValue(ResolveUri(curie.Href));

            if (curie.IsTemplated)
            {
                writer.WritePropertyName("templated");
                writer.WriteValue(true);
            }

            writer.WriteEndObject();
        }
Beispiel #9
0
        public void AddingMultipleCuries_WhenValid_ReturnsTheHalGraph()
        {
            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}");

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

            Assert.Equal(expected: graph, actual: result);
        }
Beispiel #10
0
        public void AddingCurie_WhenValid_CreateLinkWithNameCurie()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

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

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

            graph.AddCurie(curieToCreate);

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

            Assert.True(((Dictionary <string, object>)graph["_links"]).Count == 1);
            Assert.True(curiesCollection.Count == 1);
            expectedLink.ShouldDeepEqual(curiesCollection[0]);
        }
Beispiel #11
0
        public void AddingCurie_WhenOneAlreadyAdded_AddsTheCurieToTheExistingCurieCollection()
        {
            var model = new SimpleModel();
            var graph = new HalGraph(model);

            var firstCurieToCreate  = new Curie("aa", "http://www.myapi.com/api/orders/{order-id}");
            var secondCurieToCreate = new Curie("bb", "http://www.myapi.com/api/customers/{customer-id}");

            //var expectedLink = new CurieLink("http://www.myapi.com/api/{something}");

            graph
            .AddCurie(firstCurieToCreate)
            .AddCurie(secondCurieToCreate);

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

            Assert.True(((Dictionary <string, object>)graph["_links"]).Count == 1);
            Assert.True(curiesCollection.Count == 2);
        }
Beispiel #12
0
        /// <summary>
        /// The string value can either be a valid absolute URI or a CURIE.
        /// </summary>
        /// <param name="identity">The URI or CURIE that identifies the DataObject</param>
        /// <exception cref="ArgumentException">Thrown if <paramref name="identity"/> could not be parsed as a CURIE or a URI</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="identity"/> is NULL</exception>
        /// <returns></returns>
        public IDataObject MakeDataObject(string identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException("identity");
            }
            var curie = new Curie(identity);

            if (curie.IsValidCurie && _namespaceMappings.ContainsKey(curie.Prefix))
            {
                return(RegisterDataObject(new DataObject(this, Curie.ResolveCurie(new Curie(identity), _namespaceMappings).ToString())));
            }

            Uri uri;
            var validUri = Uri.TryCreate(identity, UriKind.Absolute, out uri);

            if (validUri)
            {
                return(RegisterDataObject(new DataObject(this, identity)));
            }

            throw new ArgumentException(Strings.InvalidDataObjectIdentity, "identity");
        }