public void WriteObjectInline_WritesEachEntityInstance()
        {
            // Arrange
            SpyODataSerializer spy = new SpyODataSerializer(ODataPayloadKind.Entry);
            ODataSerializerProvider provider = new FakeODataSerializerProvider(spy);
            var mockWriter = new Mock<ODataWriter>();

            mockWriter
                .Setup(w => w.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    Assert.Equal("http://schemas.datacontract.org/2004/07/", feed.Id);
                })
                .Verifiable();

            _serializer = new ODataFeedSerializer(_customersType, provider);

            // Act
            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);

            // Assert
            WriteObjectInlineCall[] expectedWriteCalls = new WriteObjectInlineCall[]
            {
                new WriteObjectInlineCall { Graph = _customers[0], WriteContext = _writeContext },
                new WriteObjectInlineCall { Graph = _customers[1], WriteContext = _writeContext }
            };
            AssertEqual(expectedWriteCalls, spy.WriteObjectInlineCalls);

            mockWriter.Verify();
        }
        public void WriteObjectInline_Writes_RequestNextPageLink()
        {
            // Arrange
            var mockSerializerProvider = new Mock <ODataSerializerProvider>(_model);
            var mockCustomerSerializer = new Mock <ODataSerializer>(ODataPayloadKind.Entry);
            var mockWriter             = new Mock <ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");

            _writeContext.Request = new HttpRequestMessage();
            _writeContext.Request.Properties.Add("MS_NextPageLink", expectedNextLink);

            mockSerializerProvider
            .Setup(p => p.GetODataPayloadSerializer(typeof(Customer)))
            .Returns(mockCustomerSerializer.Object);
            mockWriter
            .Setup(m => m.WriteStart(It.IsAny <ODataFeed>()))
            .Callback((ODataFeed feed) =>
            {
                Assert.Equal(expectedNextLink, feed.NextPageLink);
            });
            _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object);

            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);
        }
 public void WriteObject_ThrowsArgumentNull_MessageWriter()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
     Assert.ThrowsArgumentNull(
         () => serializer.WriteObject(graph: null, type: null, messageWriter: null, writeContext: new ODataSerializerContext()),
         "messageWriter");
 }
        public void WriteObjectInline_WritesEachEntityInstance()
        {
            // Arrange
            var mockSerializerProvider = new Mock <ODataSerializerProvider>(MockBehavior.Strict, _model);
            var mockCustomerSerializer = new Mock <ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry);
            var mockWriter             = new Mock <ODataWriter>();

            mockSerializerProvider
            .Setup(p => p.GetODataPayloadSerializer(typeof(Customer)))
            .Returns(mockCustomerSerializer.Object);
            mockCustomerSerializer
            .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny <ODataWriter>(), _writeContext))
            .Verifiable();
            mockCustomerSerializer
            .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny <ODataWriter>(), _writeContext))
            .Verifiable();
            mockWriter
            .Setup(w => w.WriteStart(It.IsAny <ODataFeed>()))
            .Callback((ODataFeed feed) =>
            {
                Assert.Equal("http://schemas.datacontract.org/2004/07/", feed.Id);
            })
            .Verifiable();

            _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object);

            // Act
            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);

            // Assert
            mockSerializerProvider.Verify();
            mockCustomerSerializer.Verify();
            mockWriter.Verify();
        }
 public void WriteObject_ThrowsArgumentNull_WriteContext()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
     Assert.ThrowsArgumentNull(
         () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
         "writeContext");
 }
 public void WriteObject_ThrowsEntitySetMissingDuringSerialization()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
     Assert.Throws<SerializationException>(
         () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: new ODataSerializerContext()),
         "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.");
 }
        public void WriteObjectInline_WritesEachEntityInstance()
        {
            // Arrange
            var mockSerializerProvider = new Mock<ODataSerializerProvider>(MockBehavior.Strict, _model);
            var mockCustomerSerializer = new Mock<ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry);
            var mockWriter = new Mock<ODataWriter>();

            mockSerializerProvider
                .Setup(p => p.CreateEdmTypeSerializer(_customersType.ElementType()))
                .Returns(mockCustomerSerializer.Object);
            mockCustomerSerializer
                .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny<ODataWriter>(), _writeContext))
                .Verifiable();
            mockCustomerSerializer
                .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny<ODataWriter>(), _writeContext))
                .Verifiable();
            mockWriter
                .Setup(w => w.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    Assert.Equal("http://schemas.datacontract.org/2004/07/", feed.Id);
                })
                .Verifiable();

            _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object);

            // Act
            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);

            // Assert
            mockSerializerProvider.Verify();
            mockCustomerSerializer.Verify();
            mockWriter.Verify();
        }
        public void WriteObjectInline_Can_WriteCollectionOfIEdmObjects()
        {
            // Arrange
            IEdmTypeReference           edmType   = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false);
            IEdmCollectionTypeReference feedType  = new EdmCollectionTypeReference(new EdmCollectionType(edmType), isNullable: false);
            Mock <IEdmObject>           edmObject = new Mock <IEdmObject>();

            edmObject.Setup(e => e.GetEdmType()).Returns(edmType);

            var mockWriter = new Mock <ODataWriter>();

            Mock <ODataEdmTypeSerializer> customSerializer = new Mock <ODataEdmTypeSerializer>(ODataPayloadKind.Entry);

            customSerializer.Setup(s => s.WriteObjectInline(edmObject.Object, edmType, mockWriter.Object, _writeContext)).Verifiable();

            Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>();

            serializerProvider.Setup(s => s.GetEdmTypeSerializer(edmType)).Returns(customSerializer.Object);

            ODataFeedSerializer serializer = new ODataFeedSerializer(serializerProvider.Object);

            // Act
            serializer.WriteObjectInline(new[] { edmObject.Object }, feedType, mockWriter.Object, _writeContext);

            // Assert
            customSerializer.Verify();
        }
        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_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);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataSerializerProvider"/> class.
 /// </summary>
 public DefaultODataSerializerProvider()
 {
     _feedSerializer        = new ODataFeedSerializer(this);
     _collectionSerializer  = new ODataCollectionSerializer(this);
     _complexTypeSerializer = new ODataComplexTypeSerializer(this);
     _entityTypeSerializer  = new ODataEntityTypeSerializer(this);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataSerializerProvider"/> class.
 /// </summary>
 public DefaultODataSerializerProvider()
 {
     _feedSerializer = new ODataFeedSerializer(this);
     _collectionSerializer = new ODataCollectionSerializer(this);
     _complexTypeSerializer = new ODataComplexTypeSerializer(this);
     _entityTypeSerializer = new ODataEntityTypeSerializer(this);
 }
        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);
        }
示例#14
0
        public void WriteObjectInline_ThrowsArgumentNull_Writer()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());

            Assert.ThrowsArgumentNull(
                () => serializer.WriteObjectInline(graph: null, writer: null, writeContext: new ODataSerializerContext()),
                "writer");
        }
        public void WriteObjectInline_ThrowsArgumentNull_WriteContext()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());

            Assert.ThrowsArgumentNull(
                () => serializer.WriteObjectInline(graph: null, expectedType: null, writer: new Mock <ODataWriter>().Object, writeContext: null),
                "writeContext");
        }
        public void WriteObject_ThrowsArgumentNull_WriteContext()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());

            Assert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
                "writeContext");
        }
示例#17
0
        public void WriteObjectInline_ThrowsSerializationException_IfGraphIsNotEnumerable()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());

            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(graph: 42, writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "ODataFeedSerializer cannot write an object of type 'System.Int32'.");
        }
示例#18
0
        public void WriteObjectInline_ThrowsSerializationException_CannotSerializerNull()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());

            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(graph: null, writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "Cannot serialize a null 'feed'.");
        }
        public void WriteObject_ThrowsEntitySetMissingDuringSerialization()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());

            Assert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: new ODataSerializerContext()),
                "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.");
        }
        public void WriteObject_ThrowsArgumentNull_MessageWriter()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());

            Assert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: null, writeContext: new ODataSerializerContext()),
                "messageWriter");
        }
        public void WriteObjectInline_Throws_NullElementInCollection_IfFeedContainsNullElement()
        {
            // Arrange
            IEnumerable         instance   = new object[] { null };
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());

            // Act
            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(instance, _customersType, new Mock <ODataWriter>().Object, _writeContext),
                "Collections cannot contain null elements.");
        }
示例#22
0
        public void WriteObjectInline_Throws_TypeCannotBeSerialized_IfFeedContainsEntityThatCannotBeSerialized()
        {
            // Arrange
            Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>();

            serializerProvider.Setup(s => s.GetODataPayloadSerializer(_model, typeof(int))).Returns <ODataSerializer>(null);
            IEnumerable         instance   = new object[] { 42 };
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, serializerProvider.Object);

            // Act
            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(instance, new Mock <ODataWriter>().Object, _writeContext),
                "'System.Int32' cannot be serialized using the ODataMediaTypeFormatter.");
        }
        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 WriteObjectInline_WritesEachEntityInstance()
        {
            // Arrange
            Mock <ODataEdmTypeSerializer> customerSerializer = new Mock <ODataEdmTypeSerializer>(ODataPayloadKind.Entry);
            ODataSerializerProvider       provider           = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock <ODataWriter>();

            customerSerializer.Setup(s => s.WriteObjectInline(_customers[0], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable();
            customerSerializer.Setup(s => s.WriteObjectInline(_customers[1], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable();

            _serializer = new ODataFeedSerializer(provider);

            // Act
            _serializer.WriteObjectInline(_customers, _customersType, mockWriter.Object, _writeContext);

            // Assert
            customerSerializer.Verify();
        }
        public void WriteObjectInline_WritesEachEntityInstance()
        {
            // Arrange
            Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry);
            ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock<ODataWriter>();

            customerSerializer.Setup(s => s.WriteObjectInline(_customers[0], mockWriter.Object, _writeContext)).Verifiable();
            customerSerializer.Setup(s => s.WriteObjectInline(_customers[1], mockWriter.Object, _writeContext)).Verifiable();

            _serializer = new ODataFeedSerializer(_customersType, provider);

            // Act
            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);

            // Assert
            customerSerializer.Verify();
        }
        public void WriteObjectInline_Writes_InlineCountAndNextLink()
        {
            // Arrange
            var mockSerializerProvider = new Mock <ODataSerializerProvider>(MockBehavior.Strict, _model);
            var mockCustomerSerializer = new Mock <ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry);
            var mockWriter             = new Mock <ODataWriter>();

            Uri  expectedNextLink    = new Uri("http://nextlink.com");
            long expectedInlineCount = 1000;

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

            mockSerializerProvider
            .Setup(p => p.GetODataPayloadSerializer(typeof(Customer)))
            .Returns(mockCustomerSerializer.Object);
            mockCustomerSerializer
            .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny <ODataWriter>(), _writeContext))
            .Verifiable();
            mockCustomerSerializer
            .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny <ODataWriter>(), _writeContext))
            .Verifiable();
            mockWriter
            .Setup(m => m.WriteStart(It.IsAny <ODataFeed>()))
            .Callback((ODataFeed feed) =>
            {
                Assert.Equal(expectedNextLink, feed.NextPageLink);
                Assert.Equal(expectedInlineCount, feed.Count);
            });
            _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object);

            _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext);

            // Assert
            mockSerializerProvider.Verify();
            mockCustomerSerializer.Verify();
            mockWriter.Verify();
        }
        public void WriteObjectInline_Sets_InlineCount_OnWriteStart()
        {
            // Arrange
            Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry);
            ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock<ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");
            long expectedInlineCount = 1000;

            var result = new PageResult<Customer>(_customers, expectedNextLink, expectedInlineCount);
            mockWriter
                .Setup(m => m.WriteStart(It.Is<ODataFeed>(feed => feed.Count == expectedInlineCount)))
                .Verifiable();

            _serializer = new ODataFeedSerializer(_customersType, provider);

            // Act
            _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext);

            // Assert
            mockWriter.Verify();
        }
        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 WriteObjectInline_Can_WriteCollectionOfIEdmObjects()
        {
            // Arrange
            IEdmTypeReference edmType = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false);
            IEdmCollectionTypeReference feedType = new EdmCollectionTypeReference(new EdmCollectionType(edmType), isNullable: false);
            Mock<IEdmObject> edmObject = new Mock<IEdmObject>();
            edmObject.Setup(e => e.GetEdmType()).Returns(edmType);

            var mockWriter = new Mock<ODataWriter>();

            Mock<ODataEdmTypeSerializer> customSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Entry);
            customSerializer.Setup(s => s.WriteObjectInline(edmObject.Object, edmType, mockWriter.Object, _writeContext)).Verifiable();

            Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>();
            serializerProvider.Setup(s => s.GetEdmTypeSerializer(edmType)).Returns(customSerializer.Object);

            ODataFeedSerializer serializer = new ODataFeedSerializer(serializerProvider.Object);

            // Act
            serializer.WriteObjectInline(new[] { edmObject.Object }, feedType, mockWriter.Object, _writeContext);

            // Assert
            customSerializer.Verify();
        }
示例#32
0
        public void Ctor_SetsProperty_EntityType()
        {
            ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());

            Assert.Equal(_customersType.ElementType(), serializer.EntityType);
        }
        public void WriteObjectInline_Sets_NextPageLink_OnWriteEnd()
        {
            // Arrange
            Mock<ODataEntrySerializer> customerSerializer = new Mock<ODataEntrySerializer>(_customersType.ElementType(), ODataPayloadKind.Entry);
            ODataSerializerProvider provider = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock<ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");

            var result = new PageResult<Customer>(_customers, expectedNextLink, count: 0);
            ODataFeed actualFeed = null;
            mockWriter
                .Setup(m => m.WriteStart(It.Is<ODataFeed>(feed => feed.NextPageLink == null)))
                .Callback<ODataFeed>(feed => { actualFeed = feed; })
                .Verifiable();
            mockWriter
                .Setup(m => m.WriteEnd())
                .Callback(() =>
                {
                    if (actualFeed != null)
                    {
                        Assert.Equal(expectedNextLink, actualFeed.NextPageLink);
                    }
                })
                .Verifiable();

            _serializer = new ODataFeedSerializer(_customersType, provider);

            // Act
            _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext);

            // Assert
            mockWriter.Verify();
        }
        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_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 Ctor_SetsProperty_EntityType()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());
     Assert.Equal(_customersType.ElementType(), serializer.EntityType);
 }
 public void WriteObjectInline_ThrowsArgumentNull_Writer()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(_customersType, new DefaultODataSerializerProvider());
     Assert.ThrowsArgumentNull(
         () => serializer.WriteObjectInline(graph: null, writer: null, writeContext: new ODataSerializerContext()),
         "writer");
 }
        public void WriteObjectInline_Writes_RequestNextPageLink()
        {
            // Arrange
            var mockSerializerProvider = new Mock<ODataSerializerProvider>(_model);
            var mockCustomerSerializer = new Mock<ODataSerializer>(ODataPayloadKind.Entry);
            var mockWriter = new Mock<ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");
            _writeContext.Request = new HttpRequestMessage();
            _writeContext.Request.Properties.Add("MS_NextPageLink", expectedNextLink);

            mockSerializerProvider
                .Setup(p => p.GetODataPayloadSerializer(typeof(Customer)))
                .Returns(mockCustomerSerializer.Object);
            mockWriter
                .Setup(m => m.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    Assert.Equal(expectedNextLink, feed.NextPageLink);
                });
            _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object);

            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);
        }
        public void WriteObjectInline_Writes_RequestCount()
        {
            // Arrange
            ODataSerializer customerSerializer = new StubODataSerializer(ODataPayloadKind.Entry);
            ODataSerializerProvider provider = new FakeODataSerializerProvider(customerSerializer);
            var mockWriter = new Mock<ODataWriter>();

            long expectedCount = 12345;
            _writeContext.InlineCount = expectedCount;

            mockWriter
                .Setup(m => m.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    Assert.Equal(expectedCount, feed.Count);
                });
            _serializer = new ODataFeedSerializer(_customersType, provider);

            // Act
            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);

            // Assert
            mockWriter.Verify();
        }
        public void WriteObjectInline_Writes_InlineCountAndNextLink()
        {
            // Arrange
            SpyODataSerializer spy = new SpyODataSerializer(ODataPayloadKind.Entry);
            ODataSerializerProvider provider = new FakeODataSerializerProvider(spy);
            var mockWriter = new Mock<ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");
            long expectedInlineCount = 1000;

            var result = new PageResult<Customer>(
                _customers,
                expectedNextLink,
                expectedInlineCount
            );
            ODataFeed actualFeed = null;
            mockWriter
                .Setup(m => m.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    actualFeed = feed;
                    Assert.Equal(expectedInlineCount, feed.Count);
                });
            _serializer = new ODataFeedSerializer(_customersType, provider);

            _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext);

            // Assert
            WriteObjectInlineCall[] expectedWriteCalls = new WriteObjectInlineCall[]
            {
                new WriteObjectInlineCall { Graph = _customers[0], WriteContext = _writeContext },
                new WriteObjectInlineCall { Graph = _customers[1], WriteContext = _writeContext }
            };
            AssertEqual(expectedWriteCalls, spy.WriteObjectInlineCalls);

            mockWriter.Verify();
            Assert.Equal(expectedNextLink, actualFeed.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_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 WriteObjectInline_ThrowsArgumentNull_WriteContext()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
     Assert.ThrowsArgumentNull(
         () => serializer.WriteObjectInline(graph: null, expectedType: null, writer: new Mock<ODataWriter>().Object, writeContext: null),
         "writeContext");
 }
        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 WriteObjectInline_ThrowsSerializationException_CannotSerializerNull()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
     Assert.Throws<SerializationException>(
         () => serializer.WriteObjectInline(graph: null, expectedType: _customersType,
             writer: new Mock<ODataWriter>().Object, writeContext: _writeContext),
         "Cannot serialize a null 'feed'.");
 }
        public void WriteObjectInline_Writes_RequestNextPageLink()
        {
            // Arrange
            ODataSerializer customerSerializer = new StubODataSerializer(ODataPayloadKind.Entry);
            ODataSerializerProvider provider = new FakeODataSerializerProvider(customerSerializer);
            var mockWriter = new Mock<ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");
            _writeContext.NextPageLink = expectedNextLink;

            ODataFeed actualFeed = null;
            mockWriter
                .Setup(m => m.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    actualFeed = feed;
                });
            _serializer = new ODataFeedSerializer(_customersType, provider);

            // Act
            _serializer.WriteObjectInline(_customers, mockWriter.Object, _writeContext);

            // Assert
            Assert.Equal(expectedNextLink, actualFeed.NextPageLink);
        }
 public void WriteObjectInline_ThrowsSerializationException_IfGraphIsNotEnumerable()
 {
     ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());
     Assert.Throws<SerializationException>(
         () => serializer.WriteObjectInline(graph: 42, expectedType: _customersType,
             writer: new Mock<ODataWriter>().Object, writeContext: _writeContext),
         "ODataFeedSerializer cannot write an object of type 'System.Int32'.");
 }
        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);
        }
        public void WriteObjectInline_Throws_NullElementInCollection_IfFeedContainsNullElement()
        {
            // Arrange
            IEnumerable instance = new object[] { null };
            ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider());

            // Act
            Assert.Throws<SerializationException>(
                () => serializer.WriteObjectInline(instance, _customersType, new Mock<ODataWriter>().Object, _writeContext),
                "Collections cannot contain null elements.");
        }
        public void WriteObjectInline_Writes_InlineCountAndNextLink()
        {
            // Arrange
            var mockSerializerProvider = new Mock<ODataSerializerProvider>(MockBehavior.Strict, _model);
            var mockCustomerSerializer = new Mock<ODataSerializer>(MockBehavior.Strict, ODataPayloadKind.Entry);
            var mockWriter = new Mock<ODataWriter>();

            Uri expectedNextLink = new Uri("http://nextlink.com");
            long expectedInlineCount = 1000;

            var result = new ODataResult<Customer>(
                _customers,
                expectedNextLink,
                expectedInlineCount
            );
            mockSerializerProvider
                .Setup(p => p.CreateEdmTypeSerializer(_customersType.ElementType()))
                .Returns(mockCustomerSerializer.Object);
            mockCustomerSerializer
                .Setup(s => s.WriteObjectInline(_customers[0], It.IsAny<ODataWriter>(), _writeContext))
                .Verifiable();
            mockCustomerSerializer
                .Setup(s => s.WriteObjectInline(_customers[1], It.IsAny<ODataWriter>(), _writeContext))
                .Verifiable();
            mockWriter
                .Setup(m => m.WriteStart(It.IsAny<ODataFeed>()))
                .Callback((ODataFeed feed) =>
                {
                    Assert.Equal(expectedNextLink, feed.NextPageLink);
                    Assert.Equal(expectedInlineCount, feed.Count);
                });
            _serializer = new ODataFeedSerializer(_customersType, mockSerializerProvider.Object);

            _serializer.WriteObjectInline(result, mockWriter.Object, _writeContext);

            // Assert
            mockSerializerProvider.Verify();
            mockCustomerSerializer.Verify();
            mockWriter.Verify();
        }
        public void WriteObjectInline_Throws_TypeCannotBeSerialized_IfFeedContainsEntityThatCannotBeSerialized()
        {
            // Arrange
            Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>();
            HttpRequestMessage request = new HttpRequestMessage();
            serializerProvider.Setup(s => s.GetODataPayloadSerializer(_model, typeof(int), request)).Returns<ODataSerializer>(null);
            IEnumerable instance = new object[] { 42 };
            ODataFeedSerializer serializer = new ODataFeedSerializer(serializerProvider.Object);

            // Act
            Assert.Throws<SerializationException>(
                () => serializer.WriteObjectInline(instance, _customersType, new Mock<ODataWriter>().Object, _writeContext),
                "'Default.Customer' cannot be serialized using the ODataMediaTypeFormatter.");
        }