public void WriteObjectInline_Can_WriteCollectionOfIEdmObjects()
        {
            // Arrange
            IEdmTypeReference           edmType        = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false);
            IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(edmType));
            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.Resource);

            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);

            ODataResourceSetSerializer serializer = new ODataResourceSetSerializer(serializerProvider.Object);

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

            // Assert
            customSerializer.Verify();
        }
        public void WriteObjectInline_ThrowsArgumentNull_WriteContext()
        {
            ODataResourceSetSerializer serializer = new ODataResourceSetSerializer(_serializerProvider);

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

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

            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(graph: null, expectedType: _customersType,
                                                   writer: new Mock <ODataWriter>().Object, writeContext: _writeContext),
                "Cannot serialize a null 'ResourceSet'.");
        }
        public void WriteObjectInline_Throws_NullElementInCollection_IfResourceSetContainsNullElement()
        {
            // Arrange
            IEnumerable instance = new object[] { null };
            ODataResourceSetSerializer serializer = new ODataResourceSetSerializer(_serializerProvider);

            // Act & Assert
            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(instance, _customersType, new Mock <ODataWriter>().Object, _writeContext),
                "Collections cannot contain null elements.");
        }
        public void WriteObjectInline_DoesnotThrow_NullElementInCollection_IfResourceSetContainsNullComplexElement()
        {
            // Arrange
            IEnumerable instance = new object[] { null };
            ODataResourceSetSerializer serializer   = new ODataResourceSetSerializer(_serializerProvider);
            ODataSerializerContext     writeContext = new ODataSerializerContext {
                NavigationSource = null, Model = _model
            };

            // Act & Assert
            Assert.DoesNotThrow(() => serializer.WriteObjectInline(instance, _addressesType, new Mock <ODataWriter>().Object, writeContext));
        }
        public void WriteObjectInline_Throws_TypeCannotBeSerialized_IfResourceSetContainsEntityThatCannotBeSerialized()
        {
            // Arrange
            Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>();
            HttpRequestMessage             request            = new HttpRequestMessage();

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

            // Act & Assert
            Assert.Throws <SerializationException>(
                () => serializer.WriteObjectInline(instance, _customersType, new Mock <ODataWriter>().Object, _writeContext),
                "'Default.Customer' cannot be serialized using the ODataMediaTypeFormatter.");
        }
        public void WriteObjectInline_WritesEachEntityInstance()
        {
            // Arrange
            Mock <ODataEdmTypeSerializer> customerSerializer = new Mock <ODataEdmTypeSerializer>(ODataPayloadKind.Resource);
            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 ODataResourceSetSerializer(provider);

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

            // Assert
            customerSerializer.Verify();
        }