示例#1
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"));
        }
示例#2
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"));
        }
        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 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 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 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 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 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 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");

        }