Exemple #1
0
        public void Can_deserialize_payload_with_primitive_parameters()
        {
            string actionName  = "Primitive";
            int    quantity    = 1;
            string productCode = "PCode";
            string body        = "{" + string.Format(@" ""Quantity"": {0} , ""ProductCode"": ""{1}"" ", quantity, productCode) + "}";

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

            message.SetHeader("Content-Type", "application/json;odata=verbose");

            IEdmModel                      model        = GetModel();
            ODataMessageReader             reader       = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model);
            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataPath                      path         = CreatePath(model, actionName);
            ODataDeserializerContext       context      = new ODataDeserializerContext {
                Path = path, Model = model
            };
            ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters;

            Assert.NotNull(payload);
            Assert.Same(
                model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Primitive"),
                ODataActionPayloadDeserializer.GetFunctionImport(context));
            Assert.True(payload.ContainsKey("Quantity"));
            Assert.Equal(quantity, payload["Quantity"]);
            Assert.True(payload.ContainsKey("ProductCode"));
            Assert.Equal(productCode, payload["ProductCode"]);
        }
Exemple #2
0
        public void Can_deserialize_payload_with_primitive_collection_parameters()
        {
            string actionName = "PrimitiveCollection";
            string body       = @"{ ""Name"": ""Avatar"", ""Ratings"": [ 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 ] }";

            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;odata=verbose");
            IEdmModel          model  = GetModel();
            ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model);

            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataPath path = CreatePath(model, actionName);
            ODataDeserializerContext context = new ODataDeserializerContext {
                Path = path, Model = model
            };
            ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters;

            Assert.NotNull(payload);
            Assert.Same(
                model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "PrimitiveCollection"),
                ODataActionPayloadDeserializer.GetFunctionImport(context));
            Assert.True(payload.ContainsKey("Name"));
            Assert.Equal("Avatar", payload["Name"]);
            Assert.True(payload.ContainsKey("Ratings"));
            IList <int> ratings = payload["Ratings"] as IList <int>;

            Assert.Equal(10, ratings.Count);
            Assert.True(expectedRatings.Zip(ratings, (expected, actual) => expected - actual).All(diff => diff == 0));
        }
Exemple #3
0
        public void Can_deserialize_payload_with_complex_parameters()
        {
            string actionName = "Complex";
            string body       = @"{ ""Quantity"": 1 , ""Address"": { ""StreetAddress"":""1 Microsoft Way"", ""City"": ""Redmond"", ""State"": ""WA"", ""ZipCode"": 98052 } }";

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

            message.SetHeader("Content-Type", "application/json;odata=verbose");
            IEdmModel          model  = GetModel();
            ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model);

            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataPath path = CreatePath(model, actionName);
            ODataDeserializerContext context = new ODataDeserializerContext {
                Path = path, Model = model
            };
            ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters;

            Assert.NotNull(payload);
            Assert.Same(
                model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Complex"),
                ODataActionPayloadDeserializer.GetFunctionImport(context));
            Assert.True(payload.ContainsKey("Quantity"));
            Assert.Equal(1, payload["Quantity"]);
            Assert.True(payload.ContainsKey("Address"));
            MyAddress address = payload["Address"] as MyAddress;

            Assert.NotNull(address);
            Assert.Equal("1 Microsoft Way", address.StreetAddress);
            Assert.Equal("Redmond", address.City);
            Assert.Equal("WA", address.State);
            Assert.Equal(98052, address.ZipCode);
        }
Exemple #4
0
        void Can_Deserialize_ComplexCollections_InUntypedMode()
        {
            //Arrange
            IEdmModel          model  = GetModel();
            IEdmFunctionImport action = model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "ComplexCollection");
            string             body   = @"{ ""Name"": ""Avatar"" , ""Addresses"": [{ ""StreetAddress"":""1 Microsoft Way"", ""City"": ""Redmond"", ""State"": ""WA"", ""ZipCode"": 98052 }] }";

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

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

            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataPath path = CreatePath(model, action.Name);
            ODataDeserializerContext context = new ODataDeserializerContext {
                Path = path, Model = model, ResourceType = typeof(ODataUntypedActionParameters)
            };

            //Act
            ODataUntypedActionParameters payload = deserializer.Read(reader, typeof(ODataUntypedActionParameters), context) as ODataUntypedActionParameters;

            //Assert
            Assert.NotNull(payload);
            Assert.Same(
                model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "ComplexCollection"),
                ODataActionPayloadDeserializer.GetFunctionImport(context));
            Assert.True(payload.ContainsKey("Name"));
            Assert.Equal("Avatar", payload["Name"]);
            Assert.True(payload.ContainsKey("Addresses"));
            IEnumerable <IEdmObject> addresses = payload["Addresses"] as EdmComplexObjectCollection;
            dynamic address = addresses.SingleOrDefault();

            Assert.NotNull(address);
            Assert.Equal("1 Microsoft Way", address.StreetAddress);
            Assert.Equal("Redmond", address.City);
            Assert.Equal("WA", address.State);
            Assert.Equal(98052, address.ZipCode);
        }
Exemple #5
0
        void Can_Deserialize_PrimitiveCollections_InUntypedMode()
        {
            // Arrange
            IEdmModel          model  = GetModel();
            IEdmFunctionImport action = model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "PrimitiveCollection");
            string             body   = @"{ ""Name"": ""Avatar"", ""Ratings"": [ 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 ] }";

            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;odata=verbose");
            ODataMessageReaderSettings settings = new ODataMessageReaderSettings();
            ODataMessageReader         reader   = new ODataMessageReader(message as IODataRequestMessage, settings, model);

            ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider());
            ODataPath path = CreatePath(model, action.Name);
            ODataDeserializerContext context = new ODataDeserializerContext {
                Path = path, Model = model, ResourceType = typeof(ODataUntypedActionParameters)
            };

            // Act
            ODataUntypedActionParameters payload = deserializer.Read(reader, typeof(ODataUntypedActionParameters), context) as ODataUntypedActionParameters;

            //Assert
            Assert.NotNull(payload);
            Assert.Same(
                model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "PrimitiveCollection"),
                ODataActionPayloadDeserializer.GetFunctionImport(context));
            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));
        }