Пример #1
0
        public void organisation_get_json_with_multi_link_embedded()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);
            var org = new OrganisationRepresentationWithEmbeddedResources(1, "Org Name");

            org.EmbeddedSingleResource = new TestRepresentation
            {
                Rel  = "single-resource",
                Href = "~/single"
            };
            org.EmbeddedMultiResource = new[] { new TestRepresentation
                                                {
                                                    Rel  = "multi-resource",
                                                    Href = "~/multi"
                                                } };

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

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
Пример #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(opts =>
            {
                var jsonHalOutputFormatter = new JsonHalMediaTypeOutputFormatter();


                opts.OutputFormatters.Insert(0, jsonHalOutputFormatter);

                opts.Filters.Add(new CorsAuthorizationFilterFactory("any"));
            });

            services.AddCors(opts =>
            {
                opts.AddPolicy("any", builder =>
                {
                    builder.AllowAnyMethod().AllowAnyOrigin().AllowCredentials();
                });
            });


            services.AddEntityFramework()
            .AddSqlServer();

            services.AddScoped <DbContext, QuizPromoContext>();
            services.AddScoped <IDbSession, EFDbSession>();
            services.AddScoped <IRepositoryWithTypedId <Question, int>, EFRepository <Question, int> >();

            //return container.CreateServiceProvider(services);
        }
Пример #3
0
        public void organisation_get_json_with_multi_link_linkrel()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);
            var resourceWithAppPath = new OrganisationWithLinkTitleRepresentation(1, "Org Name");

            resourceWithAppPath.Links.Add(new Link("multi-rel-with-single-link", "~/api/organisations/test")
            {
                IsMultiLink = true
            });
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links", "~/api/organisations/test1")
            {
                IsMultiLink = true
            });
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links", "~/api/organisations/test2")
            {
                IsMultiLink = true
            });
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links-with-is-multilink-false", "~/api/organisations/test-f1"));
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links-with-is-multilink-false", "~/api/organisations/test-f2"));

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

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
        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);
            }
        }
Пример #5
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);
            }
        }
Пример #6
0
        public void peopledetail_empty_resource_list_get_json_test()
        {
            // arrange
            var emptyResource = new OrganisationWithPeopleDetailRepresentation(1, "Org Name")
            {
                Boss   = new Boss(2, "Eunice PHB", 1, true),
                People = new ResourceList <Person>("person")
            };

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

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

                var serializedResult = stream.ToString();

                // assert
                this.Assent(serializedResult);
            }
        }
Пример #7
0
        public void organisation_parse_json_with_multi_link_linkrel()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);
            var resourceWithAppPath = new OrganisationWithLinkTitleRepresentation(1, "Org Name");

            resourceWithAppPath.Links.Add(new Link("multi-rel-with-single-link", "~/api/organisations/test")
            {
                IsMultiLink = true
            });
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links", "~/api/organisations/test1")
            {
                IsMultiLink = true
            });
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links", "~/api/organisations/test2")
            {
                IsMultiLink = true
            });
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links-with-is-multilink-false", "~/api/organisations/test1"));
            resourceWithAppPath.Links.Add(new Link("multi-rel-with-multiple-links-with-is-multilink-false", "~/api/organisations/test2"));

            string serialisedResource;

            // serialize
            using (var stream = new StringWriter())
            {
                mediaFormatter.WriteObject(stream, resourceWithAppPath);

                serialisedResource = stream.ToString();
            }

            // parse again
            var inputFormatter = new JsonHalMediaTypeInputFormatterWithCreateSerializer(
                NullLogger.Instance,
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared,
                new DefaultObjectPoolProvider(), new MvcOptions(), new MvcJsonOptions());
            var inputSerializer = inputFormatter.CreateJsonSerializer();

            using (var stream = new StringReader(serialisedResource))
            {
                using (var jsonReader = new JsonTextReader(stream))
                {
                    var parsedResource = inputSerializer.Deserialize <OrganisationWithLinkTitleRepresentation>(jsonReader);
                    Assert.True(parsedResource.Links.Where(l => l.Rel == "multi-rel-with-single-link").All(l => l.IsMultiLink));
                    Assert.True(parsedResource.Links.Where(l => l.Rel == "multi-rel-with-multiple-links").All(l => l.IsMultiLink));
                    Assert.True(parsedResource.Links.Where(l => l.Rel == "multi-rel-with-multiple-links-with-is-multilink-false").All(l => l.IsMultiLink));
                    Assert.True(parsedResource.Links.Where(l => l.Rel == "someRel").All(l => !l.IsMultiLink));
                }
            }
        }
Пример #8
0
        public void peopledetail_get_json_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);

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

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
Пример #9
0
        public void organisation_get_json_with_link_title_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);
            var resourceWithAppPath = new OrganisationWithLinkTitleRepresentation(1, "Org Name");

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

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
Пример #10
0
        public void When_Formatting_A_Representation_With_HalPlusJson_Then_Json_Is_Returned()
        {
            // ARRANGE
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter();

            using (var stream = new MemoryStream())
            {
                using (var textWritter = new StreamWriter(stream))
                {
                    // ACT
                    mediaFormatter.WriteObject(textWritter, resource);
                    textWritter.Flush();

                    // ASSERT
                    stream.Seek(0, SeekOrigin.Begin);
                    var serialisedResult = new StreamReader(stream).ReadToEnd();
                    Assert.NotEmpty(serialisedResult);
                }
            }
        }
Пример #11
0
        public void one_item_organisation_list_get_json_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
                new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }, ArrayPool <char> .Shared);
            var content = new StringContent(string.Empty);
            var type    = oneitemrepresentation.GetType();

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

                string serialisedResult = stream.ToString();

                // assert
                this.Assent(serialisedResult);
            }
        }
Пример #12
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);
            }
        }