/// <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 Ctor_SetsProperty_ElementType()
        {
            // Arrange
            Mock<IEdmCollectionType> collectionType = new Mock<IEdmCollectionType>();
            Mock<IEdmTypeReference> elementType = new Mock<IEdmTypeReference>();
            collectionType.Setup(c => c.ElementType).Returns(elementType.Object);
            IEdmCollectionTypeReference collectionTypeReference = new EdmCollectionTypeReference(collectionType.Object, isNullable: true);

            // Act
            var serializer = new ODataCollectionSerializer(collectionTypeReference, new Mock<ODataSerializerProvider>().Object);

            // Assert
            Assert.Equal(elementType.Object, serializer.ElementType);
        }
        public ODataCollectionSerializerTests()
        {
            _model = SerializationTestsHelpers.SimpleCustomerOrderModel();
            _customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers");
            _edmIntType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);
            _customer = new Customer()
            {
                FirstName = "Foo",
                LastName = "Bar",
                ID = 10,
            };

            ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider();
            _collectionType = new EdmCollectionTypeReference(new EdmCollectionType(_edmIntType), isNullable: false);
            _serializer = new ODataCollectionSerializer(serializerProvider);
        }
        public ODataCollectionSerializerTests()
        {
            _model       = SerializationTestsHelpers.SimpleCustomerOrderModel();
            _customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers");
            _edmIntType  = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);
            _customer    = new Customer()
            {
                FirstName = "Foo",
                LastName  = "Bar",
                ID        = 10,
            };

            ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider();

            _collectionType = new EdmCollectionTypeReference(new EdmCollectionType(_edmIntType), isNullable: false);
            _serializer     = new ODataCollectionSerializer(serializerProvider);
        }
        public void AddTypeNameAnnotationAsNeeded_AddsAnnotationWithNull_InJsonLightNoMetadataMode()
        {
            // Arrange
            string expectedTypeName    = "TypeName";
            ODataCollectionValue value = new ODataCollectionValue
            {
                TypeName = expectedTypeName
            };

            // Act
            ODataCollectionSerializer.AddTypeNameAnnotationAsNeeded(value, ODataMetadataLevel.NoMetadata);

            // Assert
            SerializationTypeNameAnnotation annotation = value.GetAnnotation <SerializationTypeNameAnnotation>();

            Assert.NotNull(annotation); // Guard
            Assert.Null(annotation.TypeName);
        }
        public void CreateODataCollectionValue_CanSerialize_IEdmObjects()
        {
            // Arrange
            IEdmComplexObject[]         collection        = new IEdmComplexObject[] { new Mock <IEdmComplexObject>().Object };
            ODataSerializerContext      serializerContext = new ODataSerializerContext();
            IEdmComplexTypeReference    elementType       = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true);
            IEdmCollectionTypeReference collectionType    = new EdmCollectionTypeReference(new EdmCollectionType(elementType), isNullable: false);

            Mock <ODataSerializerProvider>    serializerProvider = new Mock <ODataSerializerProvider>();
            Mock <ODataComplexTypeSerializer> elementSerializer  = new Mock <ODataComplexTypeSerializer>(MockBehavior.Strict, serializerProvider.Object);

            serializerProvider.Setup(s => s.GetEdmTypeSerializer(elementType)).Returns(elementSerializer.Object);
            elementSerializer.Setup(s => s.CreateODataComplexValue(collection[0], elementType, serializerContext)).Returns(new ODataComplexValue()).Verifiable();

            ODataCollectionSerializer serializer = new ODataCollectionSerializer(serializerProvider.Object);

            // Act
            var result = serializer.CreateODataCollectionValue(collection, elementType, serializerContext);

            // Assert
            elementSerializer.Verify();
        }
        public void CreateODataCollectionValue_CanSerialize_IEdmObjects()
        {
            // Arrange
            IEdmComplexObject[] collection = new IEdmComplexObject[] { new Mock<IEdmComplexObject>().Object };
            ODataSerializerContext serializerContext = new ODataSerializerContext();
            IEdmComplexTypeReference elementType = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true);
            IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType), isNullable: false);

            Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>();
            Mock<ODataComplexTypeSerializer> elementSerializer = new Mock<ODataComplexTypeSerializer>(MockBehavior.Strict, serializerProvider.Object);
            serializerProvider.Setup(s => s.GetEdmTypeSerializer(elementType)).Returns(elementSerializer.Object);
            elementSerializer.Setup(s => s.CreateODataComplexValue(collection[0], elementType, serializerContext)).Returns(new ODataComplexValue()).Verifiable();

            ODataCollectionSerializer serializer = new ODataCollectionSerializer(serializerProvider.Object);

            // Act
            var result = serializer.CreateODataCollectionValue(collection, elementType, serializerContext);

            // Assert
            elementSerializer.Verify();
        }
        public void CreateODataCollectionValue_ThrowsSerializationException_TypeCannotBeSerialized()
        {
            IEnumerable enumerable = new[] { 0 };
            Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>();
            var serializer = new ODataCollectionSerializer(serializerProvider.Object);
            serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny<IEdmTypeReference>())).Returns<IEdmTypeReference>(null);

            Assert.Throws<SerializationException>(
                () => serializer.CreateODataCollectionValue(enumerable, _edmIntType, new ODataSerializerContext()),
                "'Edm.Int32' cannot be serialized using the ODataMediaTypeFormatter.");
        }
        public void CreateODataValue_ThrowsArgument_IfGraphIsNotEnumerable()
        {
            object nonEnumerable = new object();
            Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>();
            var serializer = new ODataCollectionSerializer(serializerProvider.Object);
            serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny<IEdmTypeReference>())).Returns<IEdmTypeReference>(null);

            Assert.ThrowsArgument(
                () => serializer.CreateODataValue(graph: nonEnumerable, expectedType: _collectionType, writeContext: new ODataSerializerContext()),
                "graph",
                "The argument must be of type 'IEnumerable'.");
        }
        public void CreateODataValue_ThrowsArgument_IfGraphIsNull()
        {
            // Arrange
            IEnumerable nullEnumerable = null;
            var serializerProvider = new Mock<ODataSerializerProvider>();
            var serializer = new ODataCollectionSerializer(serializerProvider.Object);
            serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny<IEdmTypeReference>())).Returns<IEdmTypeReference>(null);

            // Act and Assert
            Assert.Throws<SerializationException>(
                () => serializer.CreateODataValue(graph: nullEnumerable, expectedType: _collectionType, writeContext: new ODataSerializerContext()),
                "Null collections cannot be serialized.");
        }