public void CanUseRegisterExtensionMethod()
        {
            var curie = new CuriesLink("aap", "http://www.helpt.com/{?rel}");

            var builder  = Hypermedia.CreateBuilder();
            var selfLink = new Link <ProductRepresentation>("product", "http://www.product.com?id=1");
            var link2    = new Link("related", "http://www.related.com");
            var link3    = curie.CreateLink <CategoryRepresentation>("category", "http://www.category.com");

            builder.Register(selfLink, link2, link3);

            var config = builder.Build();

            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);                                                                 //, config);

            // act
            using (var stream = new StringWriter())
            {
                mediaFormatter.WriteObject(stream, representation);

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
Exemplo n.º 2
0
        public void ProperlySerializesRepresentationToJson()
        {
            // arrange
            var example            = new CuriesLink("example-namespace", "http://api.example.com/docs/{rel}");
            var productLink        = example.CreateLink("product", "http://api.example.com/products/{id}");
            var relatedProductLink = example.CreateLink("related-product", productLink.Href);
            var saleProductLink    = example.CreateLink("product-on-sale", "http://api.example.com/products/sale/{id}");
            var categoryLink       = example.CreateLink("category", "http://api.example.com/categories/{id}");

            var builder = Hypermedia.CreateBuilder();

            builder.RegisterAppender(new ProductRepresentationHypermediaAppender());
            builder.RegisterAppender(new CategoryRepresentationHypermediaAppender());

            builder.RegisterSelf <ProductRepresentation>(productLink);
            builder.RegisterSelf <CategoryRepresentation>(categoryLink);
            builder.RegisterLinks <ProductRepresentation>(relatedProductLink, saleProductLink);

            var config         = builder.Build();
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);                                                               //, config);

            // act
            using (var stream = new StringWriter())
            {
                mediaFormatter.WriteObject(stream, representation);

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
Exemplo n.º 3
0
        public void CanUseRegisterExtensionMethod()
        {
            var curie = new CuriesLink("aap", "http://www.helpt.com/{?rel}");

            var builder  = Hypermedia.CreateBuilder();
            var selfLink = new Link <ProductRepresentation>("product", "http://www.product.com?id=1");
            var link2    = new Link("related", "http://www.related.com");
            var link3    = curie.CreateLink <CategoryRepresentation>("category", "http://www.category.com");

            builder.Register(selfLink, link2, link3);

            var config = builder.Build();

            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter(config)
            {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void ResolvesNullHypermediaAppenderIfNotRegistered()
        {
            var resource = new ProductRepresentation();
            var builder  = Hypermedia.CreateBuilder();

            var config     = builder.Build();
            var registered = config.ResolveAppender(resource);

            Assert.Null(registered);
        }
        public void CanRegisterAndResolveALinkForASingleRepresentation()
        {
            var builder = Hypermedia.CreateBuilder();
            var link    = new Link();

            builder.RegisterLinks <ProductRepresentation>(link);

            var config     = builder.Build();
            var hypermedia = config.ResolveLinks(representation);

            Assert.Single(hypermedia);
            Assert.Equal(link, hypermedia.First(), Link.EqualityComparer);
        }
        public void CanRegisterAndResolveAHypermediaAppender()
        {
            var appender = new ProductRepresentationHypermediaAppender();
            var resource = new ProductRepresentation();
            var builder  = Hypermedia.CreateBuilder();

            builder.RegisterAppender(appender);

            var config     = builder.Build();
            var registered = config.ResolveAppender(resource);

            Assert.True(ReferenceEquals(appender, registered));
        }
        public void CanRegisterAndResolveASelfLinkForARepresentation()
        {
            const string href = "http://api.example.com/products/{id}";

            var link    = new Link("example-namespace:product", href);
            var builder = Hypermedia.CreateBuilder();

            builder.RegisterSelf <ProductRepresentation>(link);

            var config     = builder.Build();
            var registered = config.ResolveSelf(representation);

            Assert.Equal(Link.RelForSelf, registered.Rel);
            Assert.Equal(href, registered.Href);
        }
        public void CanRegisterAndResolveMultipleLinksForASingleRepresentation()
        {
            var link1   = new Link("foo", "bar");
            var link2   = new Link("baz", "qux");
            var builder = Hypermedia.CreateBuilder();

            builder.RegisterLinks <ProductRepresentation>(link1, link2);

            var config     = builder.Build();
            var hypermedia = config.ResolveLinks(representation);

            Assert.Equal(2, hypermedia.Count());
            Assert.Contains(link1, hypermedia, Link.EqualityComparer);
            Assert.Contains(link2, hypermedia, Link.EqualityComparer);
        }
Exemplo n.º 9
0
        public void ProperlySerializesRepresentationWithoutLinksToJson()
        {
            // arrange
            var builder        = Hypermedia.CreateBuilder();
            var config         = builder.Build();
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);                                                                 //, config);

            // act
            using (var stream = new StringWriter())
            {
                mediaFormatter.WriteObject(stream, representation);

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
        public void ProperlySerializesRepresentationWithoutLinksToJson()
        {
            // arrange
            var builder        = Hypermedia.CreateBuilder();
            var config         = builder.Build();
            var mediaFormatter = new JsonHalMediaTypeFormatter(config)
            {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void ProperlySerializesRepresentationToJson()
        {
            // arrange
            var example            = new CuriesLink("example-namespace", "http://api.example.com/docs/{rel}");
            var productLink        = example.CreateLink("product", "http://api.example.com/products/{id}");
            var relatedProductLink = example.CreateLink("related-product", productLink.Href);
            var saleProductLink    = example.CreateLink("product-on-sale", "http://api.example.com/products/sale/{id}");
            var categoryLink       = example.CreateLink("category", "http://api.example.com/categories/{id}");

            var builder = Hypermedia.CreateBuilder();

            builder.RegisterAppender(new ProductRepresentationHypermediaAppender());
            builder.RegisterAppender(new CategoryRepresentationHypermediaAppender());

            builder.RegisterSelf <ProductRepresentation>(productLink);
            builder.RegisterSelf <CategoryRepresentation>(categoryLink);
            builder.RegisterLinks <ProductRepresentation>(relatedProductLink, saleProductLink);

            var config         = builder.Build();
            var mediaFormatter = new JsonHalMediaTypeFormatter(config)
            {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
Exemplo n.º 12
0
        public ResolvingHalResourceTest()
        {
            //
            // Create representation

            representation = new ProductRepresentation
            {
                Id       = 9,
                Title    = "Morpheus in a chair statuette",
                Price    = 20.14,
                Category = new CategoryRepresentation
                {
                    Id    = 99,
                    Title = "Action Figures"
                }
            };

            //
            // Build hypermedia configuration

            var example            = new CuriesLink("example-namespace", "http://api.example.com/docs/{rel}");
            var productLink        = example.CreateLink("product", "http://api.example.com/products/{id}");
            var relatedProductLink = example.CreateLink("related-product", productLink.Href);
            var saleProductLink    = example.CreateLink("product-on-sale", "http://api.example.com/products/sale/{id}");
            var categoryLink       = example.CreateLink("category", "http://api.example.com/categories/{id}");

            var builder = Hypermedia.CreateBuilder();

            builder.RegisterAppender(new ProductRepresentationHypermediaAppender());
            builder.RegisterAppender(new CategoryRepresentationHypermediaAppender());

            builder.RegisterSelf <ProductRepresentation>(productLink);
            builder.RegisterSelf <CategoryRepresentation>(categoryLink);
            builder.RegisterLinks <ProductRepresentation>(relatedProductLink, saleProductLink);

            config = builder.Build();
        }