public void ToPartialResponse_NullDto_ReturnsNull()
        {
            FakeDto fakeDto        = null;
            var     reqContextMock = MockRepository.GenerateMock <IRequestContext>();

            Assert.Null(reqContextMock.ToPartialResponse(fakeDto));
        }
        public void ToPartialResponse_NoPartialFieldsSet_ReturnsSameObject()
        {
            var request = MockRepository.GenerateMock<IRequest>();
            const decimal ExpectedSalary = 1234.56m;
            var fakeDto = new FakeDto {Salary = ExpectedSalary};
            object partialResponse = request.ToPartialResponse(fakeDto);

            Assert.Same(fakeDto, partialResponse);
            Assert.Equal(ExpectedSalary, ((FakeDto) partialResponse).Salary);
        }
        public void ToPartialResponse_NoPartialFieldsSet_ReturnsSameObject()
        {
            var           reqContextMock = MockRepository.GenerateMock <IRequestContext>();
            const decimal ExpectedSalary = 1234.56m;
            var           fakeDto        = new FakeDto {
                Salary = ExpectedSalary
            };
            object partialResponse = reqContextMock.ToPartialResponse(fakeDto);

            Assert.Same(fakeDto, partialResponse);
            Assert.Equal(ExpectedSalary, ((FakeDto)partialResponse).Salary);
        }
        public void ToPartialResponse_UnsupportedType_ReturnsSameObject()
        {
            const decimal ExpectedSalary = 1234.56m;
            var fakeDto = new FakeDto {Salary = ExpectedSalary};
            var config = new DefaultPartialResponseConfig();
            var request = MockRepository.GenerateMock<IRequest>();
            request.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0] + "garbage");
            request.Expect(x => x.Headers.Get(config.FieldsHeaderName)).Return("id");
            object partialResponse = request.ToPartialResponse(fakeDto, config);

            Assert.Same(fakeDto, partialResponse);
            Assert.Equal(ExpectedSalary, ((FakeDto) partialResponse).Salary);
        }
        public void ToPartialResponse_FieldsSetAndSupportedType_ReturnspartialResponse()
        {
            const decimal ExpectedSalary = 1234.56m;
            var fakeDto = new FakeDto {Salary = ExpectedSalary, Person = new FakePerson()};
            var config = new DefaultPartialResponseConfig();
            var request = MockRepository.GenerateMock<IRequest>();
            request.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0]);
            request.Expect(x => x.Headers.Get(config.FieldsHeaderName)).Return("salary");
            dynamic partialResponse = request.ToPartialResponse(fakeDto, config);

            Assert.False(((IDictionary<string, object>) partialResponse).ContainsKey("Person"));
            Assert.True(((IDictionary<string, object>) partialResponse).ContainsKey("Salary"));
            Assert.Equal(ExpectedSalary, partialResponse.Salary);
        }
        public void ToPartialResponse_UnsupportedType_ReturnsSameObject()
        {
            const decimal ExpectedSalary = 1234.56m;
            var           fakeDto        = new FakeDto {
                Salary = ExpectedSalary
            };
            var config         = new DefaultPartialResponseConfig();
            var reqContextMock = MockRepository.GenerateMock <IRequestContext>();

            reqContextMock.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0] + "garbage");
            reqContextMock.Expect(x => x.GetHeader(config.FieldsHeaderName)).Return("id");
            object partialResponse = reqContextMock.ToPartialResponse(fakeDto, config);

            Assert.Same(fakeDto, partialResponse);
            Assert.Equal(ExpectedSalary, ((FakeDto)partialResponse).Salary);
        }
Example #7
0
        public void GetPartialResponse_DtoHasNestedNulList_ReturnsEmptyDynamic()
        {
            var fakeDto = new FakeDto {
                Links = null
            };

            var partialFieldSelectors = new List <FieldSelectorTreeNode>
            {
                new FieldSelectorTreeNode("Links"),
            };
            var     partialResponsinator = new PartialResponsinator(partialFieldSelectors, MockRepository.GenerateStub <IPropertyValueGetterFactory>());
            dynamic partialResp          = partialResponsinator.GetPartialResponse(fakeDto);

            var asDict = partialResp as IDictionary <string, object>;

            Assert.Equal(0, asDict.Count);
        }
        public void ToPartialResponse_FieldsSetAndSupportedType_ReturnspartialResponse()
        {
            const decimal ExpectedSalary = 1234.56m;
            var           fakeDto        = new FakeDto {
                Salary = ExpectedSalary, Person = new FakePerson()
            };
            var config         = new DefaultPartialResponseConfig();
            var reqContextMock = MockRepository.GenerateMock <IRequestContext>();

            reqContextMock.Expect(x => x.ResponseContentType).Return(config.SupportedResponseContentType[0]);
            reqContextMock.Expect(x => x.GetHeader(config.FieldsHeaderName)).Return("salary");
            dynamic partialResponse = reqContextMock.ToPartialResponse(fakeDto, config);

            Assert.False(((IDictionary <string, object>)partialResponse).ContainsKey("Person"));
            Assert.True(((IDictionary <string, object>)partialResponse).ContainsKey("Salary"));
            Assert.Equal(ExpectedSalary, partialResponse.Salary);
        }
Example #9
0
        public void GetPartialResponse_ComplexObjectWithNestedLists_ResultContainsOnlySelectedFields()
        {
            //I don't particularly like this test. I am open to ideas for improvement.
            //This complex test may be a code smell that PartialResponsinator needs to be refactored and broken up.
            const string uriField           = "Uri";
            const string salaryField        = "Salary";
            const string firstField         = "First";
            const string lastField          = "Last";
            const string childrenNamesFiled = "ChildrenNames";
            const string personField        = "Person";
            const string linksField         = "Links";

            const string  janeDoeHomepageUri = "http://janedoe.com/";
            const decimal janeDoeSalary      = 100000000581.00m;
            const string  janeDoeFirstName   = "Jane";
            const string  janeDoeLastName    = "Doe";
            const string  deeDoeFirstName    = "Dee";
            const string  deeDoeLastName     = "Doe";

            var namesOfChildren = new List <FakeName> {
                new FakeName {
                    First = deeDoeFirstName, Last = deeDoeLastName
                }
            };
            var person = new FakePerson
            {
                Name = new FakeName {
                    First = janeDoeFirstName, Last = janeDoeLastName
                },
                ChildrenNames = namesOfChildren
            };

            var links = new List <FakeLink>
            {
                new FakeLink
                {
                    Rel = "homepage",
                    Uri = janeDoeHomepageUri
                }
            };

            var propertyValues = new Dictionary <string, object>();

            propertyValues[uriField]           = janeDoeHomepageUri;
            propertyValues[salaryField]        = janeDoeSalary;
            propertyValues[childrenNamesFiled] = namesOfChildren;
            propertyValues[personField]        = person;
            propertyValues[firstField]         = janeDoeFirstName;
            propertyValues[lastField]          = janeDoeLastName;
            propertyValues[linksField]         = links;

            var fakDto = new FakeDto
            {
                Salary = janeDoeSalary,
                Person = person,
                Links  = links
            };

            var partialFieldSelectors = new List <FieldSelectorTreeNode>
            {
                new FieldSelectorTreeNode("Salary"),
                new FieldSelectorTreeNode("Person")
                {
                    Children = new List <FieldSelectorTreeNode>
                    {
                        new FieldSelectorTreeNode("ChildrenNames")
                    }
                },
                new FieldSelectorTreeNode("Links")
                {
                    Children = new List <FieldSelectorTreeNode>
                    {
                        new FieldSelectorTreeNode("Uri")
                    }
                }
            };

            var factory = MockRepository.GenerateStub <IPropertyValueGetterFactory>();

            factory.Stub(x => x.CreatePropertyValueGetter(null, null)).IgnoreArguments().Return(null).WhenCalled(x =>
            {
                var getter = MockRepository.GenerateStub <IPropertyValueGetter>();
                getter.Stub(g => g.PropertyName).Return((string)x.Arguments[1]);
                getter.Stub(g => g.GetPropertyValue(null)).IgnoreArguments().Return(propertyValues[(string)x.Arguments[1]]);
                x.ReturnValue = getter;
            });

            var     responsinator   = new PartialResponsinator(partialFieldSelectors, factory);
            dynamic partialResponse = responsinator.GetPartialResponse(fakDto);

            Assert.Equal(janeDoeSalary, partialResponse.Salary);

            Assert.Equal(1, partialResponse.Person.ChildrenNames.Count);
            Assert.Equal(deeDoeFirstName, partialResponse.Person.ChildrenNames[0].First);
            Assert.Equal(deeDoeLastName, partialResponse.Person.ChildrenNames[0].Last);

            var firstLink = partialResponse.Links[0] as IDictionary <string, object>;

            Assert.Equal(1, partialResponse.Links.Count);
            Assert.Equal(janeDoeHomepageUri, firstLink["Uri"]);
            Assert.False(firstLink.ContainsKey("Rel"), "Rel should not be present on link.");

            var actualPerson = partialResponse.Person as IDictionary <string, object>;

            Assert.False(actualPerson.ContainsKey("Name"), "Name should not be present on Person");
        }
        public void GetPartialResponse_DtoHasNestedNulList_ReturnsEmptyDynamic()
        {
            var fakeDto = new FakeDto {Links = null};

            var partialFieldSelectors = new List<FieldSelectorTreeNode>
            {
                new FieldSelectorTreeNode("Links"),
            };
            var partialResponsinator = new PartialResponsinator(partialFieldSelectors, MockRepository.GenerateStub<IPropertyValueGetterFactory>());
            dynamic partialResp = partialResponsinator.GetPartialResponse(fakeDto);

            var asDict = partialResp as IDictionary<string, object>;

            Assert.Equal(0, asDict.Count);
        }
        public void GetPartialResponse_ComplexObjectWithNestedLists_ResultContainsOnlySelectedFields()
        {
            //I don't particularly like this test. I am open to ideas for improvement.
            //This complex test may be a code smell that PartialResponsinator needs to be refactored and broken up.
            const string uriField = "Uri";
            const string salaryField = "Salary";
            const string firstField = "First";
            const string lastField = "Last";
            const string childrenNamesFiled = "ChildrenNames";
            const string personField = "Person";
            const string linksField = "Links";

            const string janeDoeHomepageUri = "http://janedoe.com/";
            const decimal janeDoeSalary = 100000000581.00m;
            const string janeDoeFirstName = "Jane";
            const string janeDoeLastName = "Doe";
            const string deeDoeFirstName = "Dee";
            const string deeDoeLastName = "Doe";

            var namesOfChildren = new List<FakeName> { new FakeName { First = deeDoeFirstName, Last = deeDoeLastName } };
            var person = new FakePerson
            {
                Name = new FakeName {First = janeDoeFirstName, Last = janeDoeLastName},
                ChildrenNames = namesOfChildren
            };

            var links = new List<FakeLink>
            {
                new FakeLink
                {
                    Rel = "homepage",
                    Uri = janeDoeHomepageUri
                }
            };

            var propertyValues = new Dictionary<string, object>();
            propertyValues[uriField] = janeDoeHomepageUri;
            propertyValues[salaryField] = janeDoeSalary;
            propertyValues[childrenNamesFiled] = namesOfChildren;
            propertyValues[personField] = person;
            propertyValues[firstField] = janeDoeFirstName;
            propertyValues[lastField] = janeDoeLastName;
            propertyValues[linksField] = links;

            var fakDto = new FakeDto
            {
                Salary = janeDoeSalary,
                Person = person,
                Links = links
            };

            var partialFieldSelectors = new List<FieldSelectorTreeNode>
            {
                new FieldSelectorTreeNode("Salary"),
                new FieldSelectorTreeNode("Person")
                {
                    Children = new List<FieldSelectorTreeNode>
                    {
                        new FieldSelectorTreeNode("ChildrenNames")
                    }
                },
                new FieldSelectorTreeNode("Links")
                {
                    Children = new List<FieldSelectorTreeNode>
                    {
                        new FieldSelectorTreeNode("Uri")
                    }
                }
            };

            var factory = MockRepository.GenerateStub<IPropertyValueGetterFactory>();
            factory.Stub(x => x.CreatePropertyValueGetter(null, null)).IgnoreArguments().Return(null).WhenCalled(x =>
            {
                var getter = MockRepository.GenerateStub<IPropertyValueGetter>();
                getter.Stub(g => g.PropertyName).Return((string) x.Arguments[1]);
                getter.Stub(g => g.GetPropertyValue(null)).IgnoreArguments().Return(propertyValues[(string)x.Arguments[1]]);
                x.ReturnValue = getter;
            });

            var responsinator = new PartialResponsinator(partialFieldSelectors, factory);
            dynamic partialResponse = responsinator.GetPartialResponse(fakDto);

            Assert.Equal(janeDoeSalary, partialResponse.Salary);

            Assert.Equal(1, partialResponse.Person.ChildrenNames.Count);
            Assert.Equal(deeDoeFirstName, partialResponse.Person.ChildrenNames[0].First);
            Assert.Equal(deeDoeLastName, partialResponse.Person.ChildrenNames[0].Last);

            var firstLink = partialResponse.Links[0] as IDictionary<string, object>;
            Assert.Equal(1, partialResponse.Links.Count);
            Assert.Equal(janeDoeHomepageUri, firstLink["Uri"]);
            Assert.False(firstLink.ContainsKey("Rel"), "Rel should not be present on link.");

            var actualPerson = partialResponse.Person as IDictionary<string, object>;
            Assert.False(actualPerson.ContainsKey("Name"), "Name should not be present on Person");
        }