public void WriteDeltaFeedInline_Can_WriteCollectionOfIEdmChangedObjects()
        {
            // Arrange
            IEdmTypeReference           edmType   = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false);
            IEdmCollectionTypeReference feedType  = new EdmCollectionTypeReference(new EdmCollectionType(edmType));
            Mock <IEdmChangedObject>    edmObject = new Mock <IEdmChangedObject>();

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

            var mockWriter = new Mock <ODataWriter>();

            Mock <ODataResourceSerializer> customerSerializer = new Mock <ODataResourceSerializer>(_serializerProvider);

            customerSerializer.Setup(s => s.WriteDeltaObjectInline(edmObject.Object, edmType, mockWriter.Object, _writeContext)).Verifiable();

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

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

            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(serializerProvider.Object);

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

            // Assert
            customerSerializer.Verify();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataSerializerProvider"/> class.
 /// </summary>
 public DefaultODataSerializerProvider()
 {
     _feedSerializer = new ODataFeedSerializer(this);
     _deltaFeedSerializer = new ODataDeltaFeedSerializer(this);
     _collectionSerializer = new ODataCollectionSerializer(this);
     _complexTypeSerializer = new ODataComplexTypeSerializer(this);
     _entityTypeSerializer = new ODataEntityTypeSerializer(this);
 }
        public void WriteDeltaFeedInline_ThrowsArgumentNull_WriteContext()
        {
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteDeltaFeedInline(graph: null, expectedType: null, writer: new Mock <ODataWriter>().Object, writeContext: null),
                "writeContext");
        }
        public void WriteObject_ThrowsArgumentNull_WriteContext()
        {
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
                "writeContext");
        }
        public void WriteObject_ThrowsArgumentNull_MessageWriter()
        {
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: null, writeContext: new ODataSerializerContext()),
                "messageWriter");
        }
        public void WriteDeltaFeedInline_ThrowsSerializationException_IfGraphIsNotEnumerable()
        {
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            ExceptionAssert.Throws <SerializationException>(
                () => serializer.WriteDeltaFeedInline(graph: 42, expectedType: _customersType,
                                                      writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "ODataDeltaFeedSerializer cannot write an object of type 'System.Int32'.");
        }
        public void WriteObject_ThrowsEntitySetMissingDuringSerialization()
        {
            object graph = new object();
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            ExceptionAssert.Throws <SerializationException>(
                () => serializer.WriteObject(graph: graph, 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 WriteDeltaFeedInline_ThrowsSerializationException_CannotSerializerNull()
        {
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            ExceptionAssert.Throws <SerializationException>(
                () => serializer.WriteDeltaFeedInline(graph: null, expectedType: _customersType,
                                                      writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "Cannot serialize a null 'deltafeed'.");
        }
        public void WriteObject_ThrowsArgumentNull_WriteContext()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteObject(graph: null, type: null, messageWriter: ODataTestUtil.GetMockODataMessageWriter(), writeContext: null),
                "writeContext");
        }
        public void WriteDeltaFeedInline_Throws_NullElementInCollection_IfFeedContainsNullElement()
        {
            // Arrange
            IEnumerable instance = new object[] { null };
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);

            // Act
            ExceptionAssert.Throws <SerializationException>(
                () => serializer.WriteDeltaFeedInline(instance, _customersType, new Mock <ODataWriter>().Object, _writeContext),
                "Collections cannot contain null elements.");
        }
        public void WriteDeltaFeedInline_ThrowsArgumentNull_Writer()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => serializer.WriteDeltaFeedInline(graph: null, expectedType: null, writer: null, writeContext: new ODataSerializerContext()),
                "writer");
        }
        public async Task WriteObjectAsync_ThrowsArgumentNull_MessageWriter()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(
                () => serializer.WriteObjectAsync(graph: null, type: null, messageWriter: null, writeContext: new ODataSerializerContext()),
                "messageWriter");
        }
        public async Task WriteDeltaFeedInlineAsync_ThrowsArgumentNull_WriteContext()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(
                () => serializer.WriteDeltaFeedInlineAsync(graph: null, expectedType: null, writer: new Mock <ODataWriter>().Object, writeContext: null),
                "writeContext");
        }
        public async Task WriteObjectAsync_Throws_EntitySetMissingDuringSerialization()
        {
            // Arrange
            object graph = new object();
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            await ExceptionAssert.ThrowsAsync <SerializationException>(
                () => serializer.WriteObjectAsync(graph: graph, 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 async Task WriteDeltaFeedInlineAsync_ThrowsSerializationException_IfGraphIsNotEnumerable()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            await ExceptionAssert.ThrowsAsync <SerializationException>(
                () => serializer.WriteDeltaFeedInlineAsync(graph: 42, expectedType: _customersType,
                                                           writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "ODataDeltaFeedSerializer cannot write an object of type 'System.Int32'.");
        }
        public async Task WriteDeltaFeedInlineAsync_ThrowsSerializationException_CannotSerializerNull()
        {
            // Arrange
            Mock <ODataSerializerProvider> provider   = new Mock <ODataSerializerProvider>();
            ODataDeltaFeedSerializer       serializer = new ODataDeltaFeedSerializer(provider.Object);

            // Act & Assert
            await ExceptionAssert.ThrowsAsync <SerializationException>(
                () => serializer.WriteDeltaFeedInlineAsync(graph: null, expectedType: _customersType,
                                                           writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "Cannot serialize a null 'deltafeed'.");
        }
        public void CreateODataDeltaFeed_Sets_NextPageLinkForPageResult()
        {
            // Arrange
            ODataDeltaFeedSerializer serializer = new ODataDeltaFeedSerializer(_serializerProvider);
            Uri        expectedNextLink         = new Uri("http://nextlink.com");
            const long ExpectedCountValue       = 1000;

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

            // Act
            ODataDeltaResourceSet feed = serializer.CreateODataDeltaFeed(result, _customersType, new ODataSerializerContext());

            // Assert
            Assert.Equal(expectedNextLink, feed.NextPageLink);
        }
        public void WriteDeltaFeedInline_Throws_TypeCannotBeSerialized_IfFeedContainsEntityThatCannotBeSerialized()
        {
            // Arrange
            Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>();
            var request = RequestFactory.Create();

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

            // Act
            ExceptionAssert.Throws <SerializationException>(
                () => serializer.WriteDeltaFeedInline(instance, _customersType, new Mock <ODataWriter>().Object, _writeContext),
                "ODataDeltaFeedSerializer cannot write an object of type 'System.Object[]'.");
        }
        public void WriteDeltaFeedInline_WritesEachEntityInstance()
        {
            // Arrange
            Mock <ODataResourceSerializer> customerSerializer = new Mock <ODataResourceSerializer>(_serializerProvider);
            ODataSerializerProvider        provider           = ODataTestUtil.GetMockODataSerializerProvider(customerSerializer.Object);
            var mockWriter = new Mock <ODataWriter>();

            customerSerializer.Setup(s => s.WriteDeltaObjectInline(_deltaFeedCustomers[0], _customersType.ElementType(), mockWriter.Object, _writeContext)).Verifiable();
            _serializer = new ODataDeltaFeedSerializer(provider);

            // Act
            _serializer.WriteDeltaFeedInline(_deltaFeedCustomers, _customersType, mockWriter.Object, _writeContext);

            // Assert
            customerSerializer.Verify();
        }