public void CreateODataFeed_SetsNextPageLink_WhenWritingTruncatedCollection_ForExpandedProperties()
        {
            // Arrange
            CustomersModelWithInheritance model              = new CustomersModelWithInheritance();
            IEdmCollectionTypeReference   customersType      = new EdmCollectionTypeReference(new EdmCollectionType(model.Customer.AsReference()), isNullable: false);
            ODataFeedSerializer           serializer         = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            SelectExpandClause            selectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true);
            IEdmNavigationProperty        ordersProperty     = model.Customer.NavigationProperties().First();
            EntityInstanceContext         entity             = new EntityInstanceContext
            {
                SerializerContext = new ODataSerializerContext {
                    EntitySet = model.Customers, Model = model.Model
                }
            };
            ODataSerializerContext      nestedContext = new ODataSerializerContext(entity, selectExpandClause, ordersProperty);
            TruncatedCollection <Order> orders        = new TruncatedCollection <Order>(new[] { new Order(), new Order() }, pageSize: 1);

            Mock <EntitySetLinkBuilderAnnotation> linkBuilder = new Mock <EntitySetLinkBuilderAnnotation>();

            linkBuilder.Setup(l => l.BuildNavigationLink(entity, ordersProperty, ODataMetadataLevel.Default)).Returns(new Uri("http://navigation-link/"));
            model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object);
            model.Model.SetEntitySetLinkBuilder(model.Orders, new EntitySetLinkBuilderAnnotation());

            // Act
            ODataFeed feed = serializer.CreateODataFeed(orders, _customersType, nestedContext);

            // Assert
            Assert.Equal("http://navigation-link/?$skip=1", feed.NextPageLink.AbsoluteUri);
        }
        public void CreateODataFeed_Sets_FeedSelfLink()
        {
            // Arrange
            var feedInstance = new object[0];
            ODataSerializerContext writeContext = new ODataSerializerContext {
                EntitySet = _customerSet, Model = _model, Request = new HttpRequestMessage()
            };

            writeContext.Url = new UrlHelper(writeContext.Request);
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri feedSelfLink = new Uri("http://feed_self_link/");
            EntitySetLinkBuilderAnnotation linkBuilder = new MockEntitySetLinkBuilderAnnotation
            {
                FeedSelfLinkBuilder = (context) =>
                {
                    Assert.Equal(_customerSet, context.EntitySet);
                    Assert.Equal(feedInstance, context.FeedInstance);
                    Assert.Equal(writeContext.Request, context.Request);
                    Assert.Equal(writeContext.Url, context.Url);
                    return(feedSelfLink);
                }
            };

            _model.SetEntitySetLinkBuilder(_customerSet, linkBuilder);

            // Act
            ODataFeed feed = serializer.CreateODataFeed(feedInstance, _customersType, writeContext);

            // Assert
            AtomFeedMetadata feedMetadata = feed.GetAnnotation <AtomFeedMetadata>();

            Assert.Equal(feedSelfLink, feedMetadata.SelfLink.Href);
            Assert.Equal("self", feedMetadata.SelfLink.Relation);
        }
        public void CreateODataFeed_Ignores_NextPageLink_ForInnerFeeds()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri nextLink = new Uri("http://somelink");
            HttpRequestMessage request = new HttpRequestMessage();

            request.ODataProperties().NextLink = nextLink;
            var result = new object[0];
            IEdmNavigationProperty navProp            = new Mock <IEdmNavigationProperty>().Object;
            SelectExpandClause     selectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true);
            EntityInstanceContext  entity             = new EntityInstanceContext
            {
                SerializerContext = new ODataSerializerContext {
                    Request = request, EntitySet = _customerSet
                }
            };
            ODataSerializerContext nestedContext = new ODataSerializerContext(entity, selectExpandClause, navProp);

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, nestedContext);

            // Assert
            Assert.Null(feed.NextPageLink);
        }
        public void CreateODataFeed_Sets_NextPageLinkForPageResult()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri  expectedNextLink          = new Uri("http://nextlink.com");
            long expectedInlineCount       = 1000;

            var result = new PageResult <Customer>(_customers, expectedNextLink, expectedInlineCount);

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, new ODataSerializerContext());

            // Assert
            Assert.Equal(expectedNextLink, feed.NextPageLink);
        }
        public void CreateODataFeed_Sets_NextPageLinkFromContext()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri expectedNextLink           = new Uri("http://nextlink.com");
            HttpRequestMessage request     = new HttpRequestMessage();

            request.ODataProperties().NextLink = expectedNextLink;
            var result = new object[0];

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, new ODataSerializerContext {
                Request = request
            });

            // Assert
            Assert.Equal(expectedNextLink, feed.NextPageLink);
        }
        public void CreateODataFeed_Sets_InlineCountFromContext()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            long expectedInlineCount       = 1000;
            HttpRequestMessage request     = new HttpRequestMessage();

            request.ODataProperties().TotalCount = expectedInlineCount;
            var result = new object[0];

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, new ODataSerializerContext {
                Request = request
            });

            // Assert
            Assert.Equal(expectedInlineCount, feed.Count);
        }
        public void CreateODataFeed_SetsNextPageLink_WhenWritingTruncatedCollection_ForExpandedProperties()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            IEdmCollectionTypeReference customersType = new EdmCollectionTypeReference(new EdmCollectionType(model.Customer.AsReference()), isNullable: false);
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            SelectExpandClause selectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true);
            IEdmNavigationProperty ordersProperty = model.Customer.NavigationProperties().First();
            EntityInstanceContext entity = new EntityInstanceContext
            {
                SerializerContext = new ODataSerializerContext { EntitySet = model.Customers, Model = model.Model }
            };
            ODataSerializerContext nestedContext = new ODataSerializerContext(entity, selectExpandClause, ordersProperty);
            TruncatedCollection<Order> orders = new TruncatedCollection<Order>(new[] { new Order(), new Order() }, pageSize: 1);

            Mock<EntitySetLinkBuilderAnnotation> linkBuilder = new Mock<EntitySetLinkBuilderAnnotation>();
            linkBuilder.Setup(l => l.BuildNavigationLink(entity, ordersProperty, ODataMetadataLevel.Default)).Returns(new Uri("http://navigation-link/"));
            model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object);
            model.Model.SetEntitySetLinkBuilder(model.Orders, new EntitySetLinkBuilderAnnotation());

            // Act
            ODataFeed feed = serializer.CreateODataFeed(orders, _customersType, nestedContext);

            // Assert
            Assert.Equal("http://navigation-link/?$skip=1", feed.NextPageLink.AbsoluteUri);
        }
        public void CreateODataFeed_Ignores_InlineCount_ForInnerFeeds()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            HttpRequestMessage request = new HttpRequestMessage();
            request.SetInlineCount(42);
            var result = new object[0];
            IEdmNavigationProperty navProp = new Mock<IEdmNavigationProperty>().Object;
            SelectExpandClause selectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true);
            EntityInstanceContext entity = new EntityInstanceContext
            {
                SerializerContext = new ODataSerializerContext { Request = request, EntitySet = _customerSet }
            };
            ODataSerializerContext nestedContext = new ODataSerializerContext(entity, selectExpandClause, navProp);

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, nestedContext);

            // Assert
            Assert.Null(feed.Count);
        }
        public void CreateODataFeed_Sets_FeedSelfLink()
        {
            // Arrange
            var feedInstance = new object[0];
            ODataSerializerContext writeContext = new ODataSerializerContext { EntitySet = _customerSet, Model = _model, Request = new HttpRequestMessage() };
            writeContext.Url = new UrlHelper(writeContext.Request);
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri feedSelfLink = new Uri("http://feed_self_link/");
            EntitySetLinkBuilderAnnotation linkBuilder = new MockEntitySetLinkBuilderAnnotation
            {
                FeedSelfLinkBuilder = (context) =>
                    {
                        Assert.Equal(_customerSet, context.EntitySet);
                        Assert.Equal(feedInstance, context.FeedInstance);
                        Assert.Equal(writeContext.Request, context.Request);
                        Assert.Equal(writeContext.Url, context.Url);
                        return feedSelfLink;
                    }
            };
            _model.SetEntitySetLinkBuilder(_customerSet, linkBuilder);

            // Act
            ODataFeed feed = serializer.CreateODataFeed(feedInstance, _customersType, writeContext);

            // Assert
            AtomFeedMetadata feedMetadata = feed.GetAnnotation<AtomFeedMetadata>();
            Assert.Equal(feedSelfLink, feedMetadata.SelfLink.Href);
            Assert.Equal("self", feedMetadata.SelfLink.Relation);
        }
        public void CreateODataFeed_Sets_NextPageLinkFromContext()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri expectedNextLink = new Uri("http://nextlink.com");
            HttpRequestMessage request = new HttpRequestMessage();
            request.SetNextPageLink(expectedNextLink);
            var result = new object[0];

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, new ODataSerializerContext { Request = request });

            // Assert
            Assert.Equal(expectedNextLink, feed.NextPageLink);
        }
        public void CreateODataFeed_Sets_InlineCountFromContext()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            long expectedInlineCount = 1000;
            HttpRequestMessage request = new HttpRequestMessage();
            request.SetInlineCount(expectedInlineCount);
            var result = new object[0];

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, new ODataSerializerContext { Request = request });

            // Assert
            Assert.Equal(expectedInlineCount, feed.Count);
        }
        public void CreateODataFeed_Sets_NextPageLinkForPageResult()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
            Uri expectedNextLink = new Uri("http://nextlink.com");
            long expectedInlineCount = 1000;

            var result = new PageResult<Customer>(_customers, expectedNextLink, expectedInlineCount);

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, _customersType, new ODataSerializerContext());

            // Assert
            Assert.Equal(expectedNextLink, feed.NextPageLink);
        }
        public void CreateODataFeed_Ignores_InlineCount_ForInnerFeeds()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());
            HttpRequestMessage request = new HttpRequestMessage();
            request.SetInlineCount(42);
            var result = new object[0];

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, new ODataSerializerContext { Request = request, IsNested = true });

            // Assert
            Assert.Null(feed.Count);
        }
        public void CreateODataFeed_Ignores_NextPageLink_ForInnerFeeds()
        {
            // Arrange
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());
            Uri nextLink = new Uri("http://somelink");
            HttpRequestMessage request = new HttpRequestMessage();
            request.SetNextPageLink(nextLink);
            var result = new object[0];

            // Act
            ODataFeed feed = serializer.CreateODataFeed(result, new ODataSerializerContext { Request = request, IsNested = true });

            // Assert
            Assert.Null(feed.NextPageLink);
        }