public void GetPartialResponse_FlatObject_ResultContainsOnlySelectedFields()
        {
            const string uriField = "Uri";
            const string salaryField = "Salary";
            const string homepageField = "HomePage";
            const string nameField = "Name";

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

            var homepage = new FakeLink {Rel = "homepage", Uri = janeDoeHomepageUri};
            var name = new FakeName {First = deeDoeFirstName, Last = deeDoeLastName};

            var propertyValues = new Dictionary<string, object>();
            propertyValues[uriField] = janeDoeHomepageUri;
            propertyValues[salaryField] = janeDoeSalary;
            propertyValues[homepageField] = homepage;
            propertyValues[nameField] = name;
     
            var fakeDto =
                new FakeFlatDto
                {
                    HomePage = homepage,
                    Name = name,
                    Salary = janeDoeSalary
                };

            var partialFieldSelectors = new List<FieldSelectorTreeNode>
            {
                new FieldSelectorTreeNode("Salary"),
                new FieldSelectorTreeNode("Name"),
                new FieldSelectorTreeNode("HomePage")
                {
                    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 responsible = responsinator.GetPartialResponse(fakeDto);

            Assert.Equal(deeDoeFirstName, responsible.Name.First);
            Assert.Equal(deeDoeLastName, responsible.Name.Last);
            Assert.Equal(janeDoeHomepageUri, responsible.HomePage.Uri);
            var homePage = responsible.HomePage as IDictionary<string, object>;
            Assert.False(homePage.ContainsKey("Rel"));
        }
Esempio n. 2
0
        public void GetPropertyValue_PropertyDoesNotExistOnInstance_TargetException()
        {
            const string propertyName = "First";

            var fakeName = new FakeName {
                First = "Thor", Last = "Jenkins"
            };

            var getter = PropertyInfoPropertyValueGetter.CreatePropertyInfoPropertyValueGetter(fakeName, propertyName);

            var fakeLink = new FakeLink {
                Rel = "self", Uri = "http://localhost/"
            };

            Assert.Throws <TargetException>(
                () =>
                getter.GetPropertyValue(fakeLink)
                );
        }
Esempio n. 3
0
        public void GetPartialResponse_FlatObject_ResultContainsOnlySelectedFields()
        {
            const string uriField      = "Uri";
            const string salaryField   = "Salary";
            const string homepageField = "HomePage";
            const string nameField     = "Name";

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

            var homepage = new FakeLink {
                Rel = "homepage", Uri = janeDoeHomepageUri
            };
            var name = new FakeName {
                First = deeDoeFirstName, Last = deeDoeLastName
            };

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

            propertyValues[uriField]      = janeDoeHomepageUri;
            propertyValues[salaryField]   = janeDoeSalary;
            propertyValues[homepageField] = homepage;
            propertyValues[nameField]     = name;

            var fakeDto =
                new FakeFlatDto
            {
                HomePage = homepage,
                Name     = name,
                Salary   = janeDoeSalary
            };

            var partialFieldSelectors = new List <FieldSelectorTreeNode>
            {
                new FieldSelectorTreeNode("Salary"),
                new FieldSelectorTreeNode("Name"),
                new FieldSelectorTreeNode("HomePage")
                {
                    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 responsible   = responsinator.GetPartialResponse(fakeDto);

            Assert.Equal(deeDoeFirstName, responsible.Name.First);
            Assert.Equal(deeDoeLastName, responsible.Name.Last);
            Assert.Equal(janeDoeHomepageUri, responsible.HomePage.Uri);
            var homePage = responsible.HomePage as IDictionary <string, object>;

            Assert.False(homePage.ContainsKey("Rel"));
        }
        public void GetPropertyValue_PropertyDoesNotExistOnInstance_TargetException()
        {
            const string propertyName = "First";
            
            var fakeName = new FakeName { First = "Thor", Last = "Jenkins" };

            var getter = PropertyInfoPropertyValueGetter.CreatePropertyInfoPropertyValueGetter(fakeName, propertyName);

            var fakeLink = new FakeLink {Rel = "self", Uri = "http://localhost/"};
            Assert.Throws<TargetException>(
                () =>
                    getter.GetPropertyValue(fakeLink)
                );
        }