Ejemplo n.º 1
0
        public static HalConfiguration Configuration()
        {
            var config = new HalConfiguration();

            Account.Configuration.Setup(config);
            Url.Configuration.Setup(config);

            return config;
        }
        public void ShouldBuildDynamicLinks()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().
                Links(model => new Link("rel1", "/dynamic/{name}").CreateLink(model)).
                Links((model, ctx) => new Link("rel2", "/dynamic/{name}/{operation}").CreateLink(model, ctx.Request.Query));

            var json = Serialize(new PetOwner { Name = "Bob" }, config, CreateTestContext(new { Operation = "Duck" }));

            Assert.Equal("/dynamic/Bob", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Equal("/dynamic/Bob/Duck", GetStringValue(json, "_links", "rel2", "href"));
        }
        public void ShouldBuildStaticLinks()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().
                Links("rel1", "/staticAddress1").
                Links(new Link("rel2", "/staticAddress2"));
            
            var json = Serialize(new PetOwner {Name = "Bob"}, config);

            Assert.Equal("Bob", GetStringValue(json, "Name"));
            Assert.Equal("/staticAddress1", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Equal("/staticAddress2", GetStringValue(json, "_links", "rel2", "href"));
        }
Ejemplo n.º 4
0
        public void ShouldSetContentTypeToApplicationHalJson()
        {
            var context = new NancyContext();
            var config  = new HalConfiguration();

            var processor = new HalJsonResponseProcessor(config, new[] { JsonSerializer });
            var response  = (JsonResponse)processor.Process(new MediaRange("application/hal+json"), new PetOwner()
            {
                Name = "Bob "
            }, context);

            Assert.Equal("application/hal+json", response.ContentType);
        }
Ejemplo n.º 5
0
        public static void Setup(HalConfiguration config)
        {
            config
                .For<Models.Account.Get.Account>()
                .Links(model => new Link("self", "/accounts/{id}").CreateLink(model))
                .Links(model => new Link("urls", "/accounts/{id}/urls").CreateLink(model));

            config
                .For<Paged<Models.Account.Get.Account>>()
                .Embeds("accounts", o => o.Data)
                .Links(model => new Link("accounts", "/accounts").CreateLink("self"))
                .Links((model, context) => new Next(model).CreateLink(context), Next.Predicate)
                .Links((model, context) => new Prev(model).CreateLink(context), Prev.Predicate);
        }
Ejemplo n.º 6
0
        public void ShouldBuildDynamicLinks()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().
            Links(model => new Link("rel1", "/dynamic/{name}").CreateLink(model)).
            Links((model, ctx) => new Link("rel2", "/dynamic/{name}/{operation}").CreateLink(model, ctx.Request.Query));

            var json = Serialize(new PetOwner {
                Name = "Bob"
            }, config, CreateTestContext(new { Operation = "Duck" }));

            Assert.Equal("/dynamic/Bob", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Equal("/dynamic/Bob/Duck", GetStringValue(json, "_links", "rel2", "href"));
        }
        public void ShouldBuildDynamicLinksWithPredicates()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().
                Links(model => new Link("rel1", "/dynamic/on/{name}").CreateLink(model), model => model.Happy).
                Links(model => new Link("rel2", "/dynamic/off/{name}").CreateLink(model), (model, ctx) => !model.Happy).
                Links((model, ctx) => new Link("rel3", "/dynamic/on/{name}/{operation}").CreateLink(model, ctx.Request.Query), model => model.Happy).
                Links((model, ctx) => new Link("rel4", "/dynamic/off/{name}/{operation}").CreateLink(model, ctx.Request.Query), (model, ctx) => !model.Happy);

            var json = Serialize(new PetOwner { Name = "Bob", Happy = true }, config, CreateTestContext(new { Operation = "Duck" }));

            Assert.Equal("/dynamic/on/Bob", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Null(GetStringValue(json, "_links", "rel2", "href"));
            Assert.Equal("/dynamic/on/Bob/Duck", GetStringValue(json, "_links", "rel3", "href"));
            Assert.Null(GetStringValue(json, "_links", "rel4", "href"));
        }
Ejemplo n.º 8
0
        public void ShouldBuildStaticLinks()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().
            Links("rel1", "/staticAddress1").
            Links(new Link("rel2", "/staticAddress2"));

            var json = Serialize(new PetOwner {
                Name = "Bob"
            }, config);

            Assert.Equal("Bob", GetStringValue(json, "Name"));
            Assert.Equal("/staticAddress1", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Equal("/staticAddress2", GetStringValue(json, "_links", "rel2", "href"));
        }
        public void ShouldBuildMultipleLinksForSingleRel()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().
                Links(new Link("rel1", "/static1")).
                Links(new Link("rel1", "/static2")).
                Links(model => new Link("rel2", "/dynamic/{name}").CreateLink(model)).
                Links((model, ctx) => new Link("rel2", "/dynamic/{name}/{operation}").CreateLink(model, ctx.Request.Query));

            var json = Serialize(new PetOwner { Name = "Bob" }, config, CreateTestContext(new { Operation = "Duck" }));

            var rel1Links = GetData(json, "_links", "rel1");
            Assert.Equal(rel1Links.Count(), 2);
            Assert.Equal(new[] { "/static1", "/static2" }, rel1Links.Select(token => token["href"].ToString()));
            var rel2Links = GetData(json, "_links", "rel2");
            Assert.Equal(rel2Links.Count(), 2);
            Assert.Equal(new[] { "/dynamic/Bob", "/dynamic/Bob/Duck" }, rel2Links.Select(token => token["href"].ToString()));
        }
Ejemplo n.º 10
0
        private static HalConfiguration HypermediaConfiguration()
        {
            var config = new HalConfiguration();

            config.For<UserSummary>()
                .Links(model => new Link("self", "/users/{id}").CreateLink(model));

            config.For<PagedList<UserSummary>>()
                  .Embeds("users", x => x.Data)
                  .Links(
                      (model, ctx) =>
                      LinkTemplates.Users.GetUsersPaged.CreateLink("self", ctx.Request.Query, new { blah = "123" }))
                  .Links(
                      (model, ctx) =>
                      LinkTemplates.Users.GetUsersPaged.CreateLink("next", ctx.Request.Query, new { page = model.PageNumber + 1 }),
                      model => model.PageNumber < model.TotalPages)
                  .Links(
                      (model, ctx) =>
                      LinkTemplates.Users.GetUsersPaged.CreateLink("prev", ctx.Request.Query, new { page = model.PageNumber - 1 }),
                      model => model.PageNumber > 0);


            config.For<UserDetails>()
                  .Embeds("role", model => model.Role)
                  .Links(model => LinkTemplates.Users.GetUser.CreateLink("self", model))
                  .Links(model => LinkTemplates.Users.GetUser.CreateLink("edit", model))
                  .Links(model => LinkTemplates.User.ChangeRole.CreateLink(model))
                  .Links(model => LinkTemplates.User.Deactivate.CreateLink(model), model => model.Active)
                  .Links(model => LinkTemplates.User.Reactivate.CreateLink(model), model => !model.Active);

            config.For<Role>()
                .Links(model => LinkTemplates.Roles.GetRole.CreateLink("self", model));

            config.For<List<Role>>()
                  .Links((model, ctx) => LinkTemplates.Roles.GetRolesPaged.CreateLink("self", ctx.Request.Query));

            config.For<RoleDetails>()
                  .Links(model => LinkTemplates.Roles.GetRole.CreateLink("self", model))
                  .Links(model => LinkTemplates.Roles.GetRole.CreateLink("edit", model))
                  .Links(model => LinkTemplates.Roles.GetRole.CreateLink("delete", model));

            return config;
        }
Ejemplo n.º 11
0
        public void ShouldBuildDynamicLinksWithPredicates()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().
            Links(model => new Link("rel1", "/dynamic/on/{name}").CreateLink(model), model => model.Happy).
            Links(model => new Link("rel2", "/dynamic/off/{name}").CreateLink(model), (model, ctx) => !model.Happy).
            Links((model, ctx) => new Link("rel3", "/dynamic/on/{name}/{operation}").CreateLink(model, ctx.Request.Query), model => model.Happy).
            Links((model, ctx) => new Link("rel4", "/dynamic/off/{name}/{operation}").CreateLink(model, ctx.Request.Query), (model, ctx) => !model.Happy);

            var json = Serialize(new PetOwner {
                Name = "Bob", Happy = true
            }, config, CreateTestContext(new { Operation = "Duck" }));

            Assert.Equal("/dynamic/on/Bob", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Null(GetStringValue(json, "_links", "rel2", "href"));
            Assert.Equal("/dynamic/on/Bob/Duck", GetStringValue(json, "_links", "rel3", "href"));
            Assert.Null(GetStringValue(json, "_links", "rel4", "href"));
        }
Ejemplo n.º 12
0
        private static HalConfiguration HypermediaConfiguration()
        {
            var config = new HalConfiguration();

            config.For <UserSummary>()
            .Links(model => new Link("self", "/users/{id}").CreateLink(model));

            config.For <PagedList <UserSummary> >()
            .Embeds("users", x => x.Data)
            .Links(
                (model, ctx) =>
                LinkTemplates.Users.GetUsersPaged.CreateLink("self", ctx.Request.Query, new { blah = "123" }))
            .Links(
                (model, ctx) =>
                LinkTemplates.Users.GetUsersPaged.CreateLink("next", ctx.Request.Query, new { page = model.PageNumber + 1 }),
                model => model.PageNumber < model.TotalPages)
            .Links(
                (model, ctx) =>
                LinkTemplates.Users.GetUsersPaged.CreateLink("prev", ctx.Request.Query, new { page = model.PageNumber - 1 }),
                model => model.PageNumber > 0);


            config.For <UserDetails>()
            .Embeds("role", model => model.Role)
            .Links(model => LinkTemplates.Users.GetUser.CreateLink("self", model))
            .Links(model => LinkTemplates.Users.GetUser.CreateLink("edit", model))
            .Links(model => LinkTemplates.User.ChangeRole.CreateLink(model))
            .Links(model => LinkTemplates.User.Deactivate.CreateLink(model), model => model.Active)
            .Links(model => LinkTemplates.User.Reactivate.CreateLink(model), model => !model.Active);

            config.For <Role>()
            .Links(model => LinkTemplates.Roles.GetRole.CreateLink("self", model));

            config.For <List <Role> >()
            .Links((model, ctx) => LinkTemplates.Roles.GetRolesPaged.CreateLink("self", ctx.Request.Query));

            config.For <RoleDetails>()
            .Links(model => LinkTemplates.Roles.GetRole.CreateLink("self", model))
            .Links(model => LinkTemplates.Roles.GetRole.CreateLink("edit", model))
            .Links(model => LinkTemplates.Roles.GetRole.CreateLink("delete", model));

            return(config);
        }
Ejemplo n.º 13
0
        public void ShouldTakeLocalConfigurationIntoAccount()
        {
            var globalConfig = new HalConfiguration();

            globalConfig.For <PetOwner>().
            Links("rel1", "/staticAddress1");

            var context = new NancyContext();

            context.LocalHalConfigFor <PetOwner>()
            .Links("rel2", "/staticAddress2");

            var json = Serialize(new PetOwner {
                Name = "Bob"
            }, globalConfig, context);

            Assert.Equal("Bob", GetStringValue(json, "Name"));
            Assert.Equal("/staticAddress1", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Equal("/staticAddress2", GetStringValue(json, "_links", "rel2", "href"));
        }
Ejemplo n.º 14
0
        private static HalConfiguration HypermediaConfiguration()
        {
            var config = new HalConfiguration();

            config.For <Services.Artists.ArtistPmo>()
            .Links(model => HomeModule.GetArtists.CreateLink(model));

            config.For <PagedList <Services.Artists.ArtistPmo> >()
            .Embeds("artists", (x) => x.Data)
            .Links(
                (model, ctx) => HomeModule.GetArtistsPaged.CreateLink("self", new { page = model.PageNumber, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key).ToArray()), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) })
                )
            .Links(
                (model, ctx) => HomeModule.GetArtistsPaged.CreateLink("prev", new { page = model.PageNumber - 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
                (model, ctx) => model.PageNumber > 1
                )
            .Links(
                (model, ctx) => HomeModule.GetArtistsPaged.CreateLink("next", new { page = model.PageNumber + 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
                (model, ctx) => model.PageNumber < model.TotalPages
                );

            config.For <Services.Album.AlbumPmo>()
            .Links(model => HomeModule.GetAlbums.CreateLink(model));

            config.For <PagedList <Services.Album.AlbumPmo> >()
            .Embeds("albums", (x) => x.Data)
            .Links(
                (model, ctx) => HomeModule.GetAlbumsPaged.CreateLink("self", new { page = model.PageNumber, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key).ToArray()), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) })
                )
            .Links(
                (model, ctx) => HomeModule.GetAlbumsPaged.CreateLink("prev", new { page = model.PageNumber - 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
                (model, ctx) => model.PageNumber > 1
                )
            .Links(
                (model, ctx) => HomeModule.GetAlbumsPaged.CreateLink("next", new { page = model.PageNumber + 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
                (model, ctx) => model.PageNumber < model.TotalPages
                );


            return(config);
        }
        public void ShouldIgnoreIgnoredProperties()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().Ignores(owner => owner.Pets);

            var model = new PetOwner
            {
                Name  = "Bob",
                Happy = true,
                Pets  = new[] { new Animal {
                                    Type = "Cat"
                                } },
                LiveStock = new Animal {
                    Type = "Chicken"
                }
            };
            var json = Serialize(model, config, CreateTestContext(new { Operation = "Duck" }));

            Assert.Null(json[AdjustName("Pets")]);
        }
Ejemplo n.º 16
0
		private static HalConfiguration HypermediaConfiguration() {
			var config = new HalConfiguration();

			config.For<Services.Artists.ArtistPmo>()
				.Links(model => HomeModule.GetArtists.CreateLink(model));

			config.For<PagedList<Services.Artists.ArtistPmo>>()
				.Embeds("artists", (x) => x.Data)
				.Links(
					(model, ctx) => HomeModule.GetArtistsPaged.CreateLink("self", new { page = model.PageNumber, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key).ToArray()), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) })
				)
				.Links(
					(model, ctx) => HomeModule.GetArtistsPaged.CreateLink("prev", new { page = model.PageNumber - 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
					(model, ctx) => model.PageNumber > 1
				)
				.Links(
					(model, ctx) => HomeModule.GetArtistsPaged.CreateLink("next", new { page = model.PageNumber + 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
					(model, ctx) => model.PageNumber < model.TotalPages
				);

			config.For<Services.Album.AlbumPmo>()
				.Links(model => HomeModule.GetAlbums.CreateLink(model));

			config.For<PagedList<Services.Album.AlbumPmo>>()
				.Embeds("albums", (x) => x.Data)
				.Links(
					(model, ctx) => HomeModule.GetAlbumsPaged.CreateLink("self", new { page = model.PageNumber, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key).ToArray()), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) })
				)
				.Links(
					(model, ctx) => HomeModule.GetAlbumsPaged.CreateLink("prev", new { page = model.PageNumber - 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
					(model, ctx) => model.PageNumber > 1
				)
				.Links(
					(model, ctx) => HomeModule.GetAlbumsPaged.CreateLink("next", new { page = model.PageNumber + 1, pageSize = model.PageSize, keywords = string.Join(",", model.Keywords), sortBy = string.Join(",", model.SortedBy.Select(kvp => kvp.Key)), sortByDir = string.Join(",", model.SortedBy.Select(kvp => kvp.Value == ListSortDirection.Ascending ? "asc" : "desc")) }),
					(model, ctx) => model.PageNumber < model.TotalPages
				);


			return config;
		}
Ejemplo n.º 17
0
        public void ShouldBuildMultipleLinksForSingleRel()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().
            Links(new Link("rel1", "/static1")).
            Links(new Link("rel1", "/static2")).
            Links(model => new Link("rel2", "/dynamic/{name}").CreateLink(model)).
            Links((model, ctx) => new Link("rel2", "/dynamic/{name}/{operation}").CreateLink(model, ctx.Request.Query));

            var json = Serialize(new PetOwner {
                Name = "Bob"
            }, config, CreateTestContext(new { Operation = "Duck" }));

            var rel1Links = GetData(json, "_links", "rel1");

            Assert.Equal(rel1Links.Count(), 2);
            Assert.Equal(new[] { "/static1", "/static2" }, rel1Links.Select(token => token["href"].ToString()));
            var rel2Links = GetData(json, "_links", "rel2");

            Assert.Equal(rel2Links.Count(), 2);
            Assert.Equal(new[] { "/dynamic/Bob", "/dynamic/Bob/Duck" }, rel2Links.Select(token => token["href"].ToString()));
        }
        public void ShouldEmbedSubResourcesWhenPredicateIsTrue()
        {
            var model = new PetOwner
            {
                Happy = true,
                Pets  = new[] { new Animal {
                                    Type = "Cat"
                                } }
            };

            Action <PetOwner, HalConfiguration, string> assertPetIsCat = (owner, configuration, rel) =>
                                                                         Assert.Equal("Cat", GetData(Serialize(owner, configuration), "_embedded", rel)[0][AdjustName("Type")]);


            var config = new HalConfiguration();

            config.For <PetOwner>().
            Embeds("pampered", owner => owner.Pets, x => x.Happy);
            assertPetIsCat(model, config, "pampered");

            config = new HalConfiguration();
            config.For <PetOwner>().
            Embeds("pampered", owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsCat(model, config, "pampered");


            config = new HalConfiguration();
            config.For <PetOwner>().
            Embeds(owner => owner.Pets, x => x.Happy);
            assertPetIsCat(model, config, "pets");

            config = new HalConfiguration();
            config.For <PetOwner>().
            Embeds(owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsCat(model, config, "pets");
        }
Ejemplo n.º 19
0
        public void ShouldEmbedSubResourceProjections()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().
            Projects("pampered", owner => owner.Pets, pets => new { petCount = pets.Count() }).
            Projects(owner => owner.LiveStock, stock => new { stockType = stock.Type });

            var model = new PetOwner
            {
                Name  = "Bob",
                Happy = true,
                Pets  = new[] { new Animal {
                                    Type = "Cat"
                                } },
                LiveStock = new Animal {
                    Type = "Chicken"
                }
            };
            var json = Serialize(model, config, CreateTestContext(new{ Operation = "Duck" }));

            Assert.Equal("1", GetData(json, "_embedded", "pampered", "petCount"));
            Assert.Equal("Chicken", GetStringValue(json, "_embedded", "liveStock", "stockType"));
        }
Ejemplo n.º 20
0
        public void ShouldEmbedSubResources()
        {
            var config = new HalConfiguration();

            config.For <PetOwner>().
            Embeds("pampered", owner => owner.Pets).
            Embeds(owner => owner.LiveStock);

            var model = new PetOwner
            {
                Name  = "Bob",
                Happy = true,
                Pets  = new[] { new Animal {
                                    Type = "Cat"
                                } },
                LiveStock = new Animal {
                    Type = "Chicken"
                }
            };
            var json = Serialize(model, config);

            Assert.Equal("Cat", GetData(json, "_embedded", "pampered")[0][AdjustName("Type")]);
            Assert.Equal("Chicken", GetStringValue(json, "_embedded", "liveStock", "Type"));
        }
        public void ShouldNotEmbedSubResourcesWhenPredicateIsFalse()
        {
            var model = new PetOwner
            {
                Happy = false,
                Pets  = new[] { new Animal {
                                    Type = "Cat"
                                } }
            };

            Action <PetOwner, HalConfiguration, string> assertPetIsNull = (owner, configuration, rel) =>
                                                                          Assert.Null(GetData(Serialize(owner, configuration), "_embedded", rel));


            var config = new HalConfiguration();

            config.For <PetOwner>().
            Embeds("pampered", owner => owner.Pets, x => x.Happy);
            assertPetIsNull(model, config, "pampered");

            config = new HalConfiguration();
            config.For <PetOwner>().
            Embeds("pampered", owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsNull(model, config, "pampered");


            config = new HalConfiguration();
            config.For <PetOwner>().
            Embeds(owner => owner.Pets, x => x.Happy);
            assertPetIsNull(model, config, "pets");

            config = new HalConfiguration();
            config.For <PetOwner>().
            Embeds(owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsNull(model, config, "pets");
        }
        public void ShouldSetContentTypeToApplicationHalJson()
        {
            var context = new NancyContext();
            var config = new HalConfiguration();

            var processor = new HalJsonResponseProcessor(config, new[] { JsonSerializer });
            var response = (JsonResponse)processor.Process(new MediaRange("application/hal+json"), new PetOwner(){ Name = "Bob "}, context);

            Assert.Equal("application/hal+json", response.ContentType);
        }
        public void ShouldEmbedSubResourcesWhenPredicateIsTrue()
        {
            var model = new PetOwner
            {
                Happy = true,
                Pets = new[] { new Animal { Type = "Cat" } }
            };

            Action<PetOwner, HalConfiguration, string> assertPetIsCat = (owner, configuration, rel) =>
                Assert.Equal("Cat", GetData(Serialize(owner, configuration), "_embedded", rel)[0][AdjustName("Type")]);


            var config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds("pampered", owner => owner.Pets, x => x.Happy);
            assertPetIsCat(model, config, "pampered");

            config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds("pampered", owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsCat(model, config, "pampered");


            config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds(owner => owner.Pets, x => x.Happy);
            assertPetIsCat(model, config, "pets");

            config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds(owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsCat(model, config, "pets");

        }
        public void ShouldIgnoreIgnoredProperties()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().Ignores(owner => owner.Pets);

            var model = new PetOwner
            {
                Name = "Bob",
                Happy = true,
                Pets = new[] { new Animal { Type = "Cat" } },
                LiveStock = new Animal { Type = "Chicken" }
            };
            var json = Serialize(model, config, CreateTestContext(new { Operation = "Duck" }));

            Assert.Null(json[AdjustName("Pets")]);
        }
        public void ShouldEmbedSubResources()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds("pampered", owner => owner.Pets).
                Embeds(owner => owner.LiveStock);

            var model = new PetOwner
            {
                Name = "Bob",
                Happy = true,
                Pets = new[] { new Animal { Type = "Cat" } },
                LiveStock = new Animal { Type = "Chicken" }
            };
            var json = Serialize(model, config);
             
            Assert.Equal("Cat", GetData(json, "_embedded", "pampered")[0][AdjustName("Type")]);
            Assert.Equal("Chicken", GetStringValue(json, "_embedded", "liveStock", "Type"));
        }
        public void ShouldNotEmbedSubResourcesWhenPredicateIsFalse()
        {
            var model = new PetOwner
            {
                Happy = false,
                Pets = new[] { new Animal { Type = "Cat" } }
            };

            Action<PetOwner, HalConfiguration, string> assertPetIsNull = (owner, configuration, rel) =>
                Assert.Null(GetData(Serialize(owner, configuration), "_embedded", rel));


            var config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds("pampered", owner => owner.Pets, x => x.Happy);
            assertPetIsNull(model, config, "pampered");

            config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds("pampered", owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsNull(model, config, "pampered");


            config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds(owner => owner.Pets, x => x.Happy);
            assertPetIsNull(model, config, "pets");

            config = new HalConfiguration();
            config.For<PetOwner>().
                Embeds(owner => owner.Pets, (x, ctx) => x.Happy);
            assertPetIsNull(model, config, "pets");

        }
        public void ShouldEmbedSubResourceProjections()
        {
            var config = new HalConfiguration();
            config.For<PetOwner>().
                Projects("pampered", owner => owner.Pets, pets => new {petCount=pets.Count()}).
                Projects(owner => owner.LiveStock, stock => new {stockType=stock.Type});

            var model = new PetOwner
            {
                Name = "Bob",
                Happy = true,
                Pets = new[] { new Animal { Type = "Cat" } },
                LiveStock = new Animal { Type = "Chicken" }
            };
            var json = Serialize(model, config, CreateTestContext(new{Operation="Duck"}));

            Assert.Equal("1", GetData(json, "_embedded", "pampered", "petCount"));
            Assert.Equal("Chicken", GetStringValue(json, "_embedded", "liveStock", "stockType"));
        }
        public void ShouldTakeLocalConfigurationIntoAccount()
        {
            var globalConfig = new HalConfiguration();
            globalConfig.For<PetOwner>().
                Links("rel1", "/staticAddress1");

            var context = new NancyContext();

            context.LocalHalConfigFor<PetOwner>()
                .Links("rel2", "/staticAddress2");

            var json = Serialize(new PetOwner { Name = "Bob" }, globalConfig, context);

            Assert.Equal("Bob", GetStringValue(json, "Name"));
            Assert.Equal("/staticAddress1", GetStringValue(json, "_links", "rel1", "href"));
            Assert.Equal("/staticAddress2", GetStringValue(json, "_links", "rel2", "href"));
        }