public void GetODataPayloadSerializer_ThrowsArgumentNull_Context()
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();

            // Act & Assert
            Assert.Throws <ArgumentNullException>("context", () => serialierProvider.GetODataPayloadSerializer(context: null, type: typeof(int)));
        }
        public void GetEdmTypeSerializer_ThrowsArgumentNull_Context()
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();

            // Act & Assert
            Assert.Throws <ArgumentNullException>("context", () => serialierProvider.GetEdmTypeSerializer(context: null, edmType: null));
        }
        public void GetODataPayloadSerializer_ThrowsArgumentNull_Type()
        {
            // Arrange
            HttpContext context = new DefaultHttpContext();
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();

            // Act & Assert
            Assert.Throws <ArgumentNullException>("type", () => serialierProvider.GetODataPayloadSerializer(context, type: null));
        }
        public void GetODataSerializer_ReturnsSameSerializer_ForSameType()
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            // Act
            ODataSerializer firstSerializer  = serialierProvider.GetODataPayloadSerializer(context, typeof(Product));
            ODataSerializer secondSerializer = serialierProvider.GetODataPayloadSerializer(context, typeof(Product));

            // Assert
            Assert.Same(firstSerializer, secondSerializer);
        }
        public void GetODataSerializer_Returns_ExpectedSerializerType(Type payloadType, Type expectedSerializerType)
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            // Act
            ODataSerializer serializer = serialierProvider.GetODataPayloadSerializer(context, payloadType);

            // Assert
            Assert.NotNull(serializer);
            Assert.IsType(expectedSerializerType, serializer);
        }
        public static void GetODataSerializer_Primitive(Type type, EdmPrimitiveTypeKind edmPrimitiveTypeKind)
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, type);

            // Assert
            Assert.NotNull(serializer);
            var primitiveSerializer = Assert.IsType <ODataPrimitiveSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.Property, primitiveSerializer.ODataPayloadKind);
        }
        public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForValueRequests(Type type, EdmPrimitiveTypeKind edmPrimitiveTypeKind)
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            context.ODataFeature().Path =
                new ODataPath(new ValueSegment(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32)));

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, type);

            // Assert
            Assert.NotNull(serializer);
            Assert.Equal(ODataPayloadKind.Value, serializer.ODataPayloadKind);
        }
        public void GetODataSerializer_Enum()
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            context.ODataFeature().Model = GetEdmModel();

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, typeof(TestEnum));

            // Assert
            Assert.NotNull(serializer);
            var enumSerializer = Assert.IsType <ODataEnumSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.Property, enumSerializer.ODataPayloadKind);
        }
        public void GetODataSerializer_ResourceSet_ForCollectionOfStructuralType(Type collectionType)
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            context.ODataFeature().Model = GetEdmModel();

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, collectionType);

            // Assert
            Assert.NotNull(serializer);
            var resourceSetSerializer = Assert.IsType <ODataResourceSetSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.ResourceSet, resourceSetSerializer.ODataPayloadKind);
            Assert.Same(resourceSetSerializer.SerializerProvider, serialierProvider);
        }
        public void GetODataSerializer_ResourceSerializer_ForStructuralType(Type type)
        {
            // Arrange
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            context.ODataFeature().Model = GetEdmModel();

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, type);

            // Assert
            Assert.NotNull(serializer);
            var entitySerializer = Assert.IsType <ODataResourceSerializer>(serializer);

            Assert.Equal(entitySerializer.SerializerProvider, serialierProvider);
            Assert.Equal(ODataPayloadKind.Resource, entitySerializer.ODataPayloadKind);
        }
        public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType)
        {
            // Arrange
            IEdmModel model = ODataCountTest.GetEdmModel();
            Type      type  = typeof(ICollection <>).MakeGenericType(elementType);
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            var pathHandler = new DefaultODataPathHandler(context.RequestServices);
            var path        = pathHandler.Parse(model, "http://localhost/", uri);

            context.ODataFeature().Path  = path;
            context.ODataFeature().Model = model;

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, type);

            // Assert
            Assert.NotNull(serializer);
            var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind);
        }