/// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataDeserializerProvider"/> class.
 /// </summary>
 public DefaultODataDeserializerProvider()
 {
     _actionPayloadDeserializer = new ODataActionPayloadDeserializer(this);
     _entityDeserializer = new ODataEntityDeserializer(this);
     _feedDeserializer = new ODataFeedDeserializer(this);
     _collectionDeserializer = new ODataCollectionDeserializer(this);
     _complexDeserializer = new ODataComplexTypeDeserializer(this);
 }
        public void Read_ThrowsArgumentNull_MessageReader()
        {
            // Arrange
            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => deserializer.Read(messageReader: null, type: typeof(ODataActionParameters), readContext: new ODataDeserializerContext()),
                "messageReader");
        }
        public void Read_ThrowsArgumentNull_ReadContext()
        {
            // Arrange
            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataMessageReader messageReader = ODataTestUtil.GetMockODataMessageReader();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => deserializer.Read(messageReader, typeof(ODataActionParameters), readContext: null),
                "readContext");
        }
        public void Ctor_SetsProperty_ODataPayloadKind()
        {
            // Arrange
            ODataDeserializerProvider deserializerProvider = new Mock<ODataDeserializerProvider>().Object;

            // Act
            var deserializer = new ODataActionPayloadDeserializer(deserializerProvider);

            // Assert
            Assert.Equal(ODataPayloadKind.Parameter, deserializer.ODataPayloadKind);
        }
        public void Ctor_SetsProperty_DeserializerProvider()
        {
            // Arrange
            ODataDeserializerProvider deserializerProvider = new Mock<ODataDeserializerProvider>().Object;

            // Act
            var deserializer = new ODataActionPayloadDeserializer(deserializerProvider);

            // Assert
            Assert.Same(deserializerProvider, deserializer.DeserializerProvider);
        }
        public void Can_DeserializePayload_WithPrimitiveCollectionParameters(string actionName, IEdmAction expectedAction, ODataPath path)
        {
            // Arrange
            const string Body =
                @"{ ""Name"": ""Avatar"", ""Ratings"": [ 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 ], ""Time"": [""01:02:03.0040000"", ""12:13:14.1150000""], ""Colors"": [ ""Red"", null, ""Green""] }";

            int[] expectedRatings = new int[] { 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 };

            ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(Body));

            message.SetHeader("Content-Type", "application/json");
            ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), _model);

            ODataDeserializerContext context = new ODataDeserializerContext {
                Path = path, Model = _model
            };

            // Act
            ODataActionParameters payload = _deserializer.Read(reader, typeof(ODataActionParameters), context) as ODataActionParameters;
            IEdmAction            action  = ODataActionPayloadDeserializer.GetAction(context);

            // Assert
            Assert.NotNull(payload);
            Assert.Same(expectedAction, action);
            Assert.True(payload.ContainsKey("Name"));
            Assert.Equal("Avatar", payload["Name"]);
            Assert.True(payload.ContainsKey("Ratings"));
            IEnumerable <int> ratings = payload["Ratings"] as IEnumerable <int>;

            Assert.Equal(10, ratings.Count());
            Assert.True(expectedRatings.Zip(ratings, (expected, actual) => expected - actual).All(diff => diff == 0));

            Assert.True(payload.ContainsKey("Time"));
            IEnumerable <TimeOfDay> times = payload["Time"] as IEnumerable <TimeOfDay>;

            Assert.Equal(2, times.Count());
            Assert.Equal(new[] { new TimeOfDay(1, 2, 3, 4), new TimeOfDay(12, 13, 14, 115) }, times.ToList());

            Assert.True(payload.ContainsKey("Colors"));
            IEnumerable <AColor?> colors = payload["Colors"] as IEnumerable <AColor?>;

            Assert.Equal("Red|null|Green", String.Join("|", colors.Select(e => e == null ? "null" : e.ToString())));
        }
        public void Can_DeserializePayload_WithPrimitiveParameters(string actionName, IEdmAction expectedAction, ODataPath path)
        {
            // Arrange
            const int    Quantity    = 1;
            const string ProductCode = "PCode";
            string       body        = "{" +
                                       string.Format(@" ""Quantity"": {0} , ""ProductCode"": ""{1}"" , ""Birthday"": ""2015-02-27"", ""BkgColor"": ""Red"", ""InnerColor"": null", Quantity, ProductCode) +
                                       "}";

            ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body));

            message.SetHeader("Content-Type", "application/json");
            ODataMessageReader       reader  = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), _model);
            ODataDeserializerContext context = new ODataDeserializerContext()
            {
                Path = path, Model = _model
            };

            // Act
            ODataActionParameters payload = _deserializer.Read(reader, typeof(ODataActionParameters), context) as ODataActionParameters;
            IEdmAction            action  = ODataActionPayloadDeserializer.GetAction(context);

            // Assert
            Assert.Same(expectedAction, action);
            Assert.NotNull(payload);
            Assert.True(payload.ContainsKey("Quantity"));
            Assert.Equal(Quantity, payload["Quantity"]);
            Assert.True(payload.ContainsKey("ProductCode"));
            Assert.Equal(ProductCode, payload["ProductCode"]);

            Assert.True(payload.ContainsKey("Birthday"));
            Assert.Equal(new Date(2015, 2, 27), payload["Birthday"]);

            Assert.True(payload.ContainsKey("BkgColor"));
            AColor bkgColor = Assert.IsType <AColor>(payload["BkgColor"]);

            Assert.Equal(AColor.Red, bkgColor);

            Assert.True(payload.ContainsKey("InnerColor"));
            Assert.Null(payload["InnerColor"]);
        }
        public void Can_DeserializePayload_WithEntityCollectionParameters(IEdmAction expectedAction, ODataPath path)
        {
            // Arrange
            ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(EntityCollectionPayload));

            message.SetHeader("Content-Type", "application/json");
            ODataMessageReader       reader  = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), _model);
            ODataDeserializerContext context = new ODataDeserializerContext()
            {
                Path = path, Model = _model
            };

            // Act
            ODataActionParameters payload = _deserializer.Read(reader, typeof(ODataActionParameters), context) as ODataActionParameters;
            IEdmAction            action  = ODataActionPayloadDeserializer.GetAction(context);

            // Assert
            Assert.Same(expectedAction, action);
            Assert.NotNull(payload);
            Assert.True(payload.ContainsKey("Id"));
            Assert.Equal(1, payload["Id"]);

            IList <Customer> customers = (payload["Customers"] as IEnumerable <Customer>).ToList();

            Assert.NotNull(customers);
            Assert.Equal(2, customers.Count);
            Customer customer = customers[0];

            Assert.NotNull(customer);
            Assert.Equal(109, customer.Id);
            Assert.Equal("Avatar", customer.Name);

            customer = customers[1];
            Assert.NotNull(customer);
            Assert.Equal(901, customer.Id);
            Assert.Equal("Robot", customer.Name);
        }
 static ODataActionPayloadDeserializerTest()
 {
     _model        = GetModel();
     _container    = _model.EntityContainer;
     _deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
 }
        public void Read_Throws_SerializationException_ODataPathMissing()
        {
            // Arrange
            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataMessageReader messageReader = ODataTestUtil.GetMockODataMessageReader();

            // Act & Assert
            Assert.Throws<SerializationException>(
                () => deserializer.Read(messageReader, typeof(ODataActionParameters), readContext: new ODataDeserializerContext()),
                "The operation cannot be completed because no ODataPath is available for the request.");
        }
 static ODataActionPayloadDeserializerTest()
 {
     _model = GetModel();
     _container = _model.EntityContainer;
     _deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
 }