Ejemplo n.º 1
0
        public void ReadFromStreamAsync()
        {
            // Arrange
            const string content = "{\"value\":{" +
                                   "\"Street\":\"MyStreet\"," +
                                   "\"City\":\"MyCity\"," +
                                   "\"State\":\"MyState\"," +
                                   "\"ZipCode\":\"160202\"," +
                                   "\"Country\":\"MyCountry\"" +
                                   "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataDeserializerContext     readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(ODataEntityDeserializerTests.Address)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), _edmModel),
                                             typeof(ODataEntityDeserializerTests.Address), readContext);

            // Assert

            ODataEntityDeserializerTests.Address address = Assert.IsType <ODataEntityDeserializerTests.Address>(value);
            Assert.NotNull(address);

            Assert.Equal("MyStreet", address.Street);
            Assert.Equal("MyCity", address.City);
            Assert.Equal("MyState", address.State);
            Assert.Equal("160202", address.ZipCode);
            Assert.Equal("MyCountry", address.Country);
        }
Ejemplo n.º 2
0
        public void ReadFromStreamAsync_ForComplexType_WithNestedComplexType()
        {
            // Arrange
            const string content = "{\"value\":{" +
                                   "\"City\":\"UpdatedCity\"," +
                                   "\"Location\": {" +
                                   "\"Latitude\": 30.6," +
                                   "\"Longitude\": 101.313" +
                                   "}," +
                                   "\"SubLocation\": {" + // dynamic property
                                   "\"@odata.type\":\"#System.Web.OData.Formatter.Deserialization.Location\"," +
                                   "\"Latitude\": 15.5," +
                                   "\"Longitude\": 130.88" +
                                   "}" +
                                   "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder  builder      = new ODataConventionModelBuilder();

            builder.ComplexType <Region>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(Delta <Region>)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model),
                                             typeof(Delta <Region>), readContext);

            // Assert
            Delta <Region> region = Assert.IsType <Delta <Region> >(value);

            Assert.NotNull(region);
            Assert.Equal(new[] { "City", "Location" }, region.GetChangedPropertyNames());
            Assert.Empty(region.GetUnchangedPropertyNames());

            object propertyValue;

            Assert.True(region.TryGetPropertyValue("City", out propertyValue));
            string cityValue = Assert.IsType <string>(propertyValue);

            Assert.Equal("UpdatedCity", cityValue);

            Assert.True(region.TryGetPropertyValue("Location", out propertyValue));
            Location locationValue = Assert.IsType <Location>(propertyValue);

            Assert.Equal(30.6, locationValue.Latitude);
            Assert.Equal(101.313, locationValue.Longitude);

            // dynamic property
            Assert.True(region.TryGetPropertyValue("SubLocation", out propertyValue));
            locationValue = Assert.IsType <Location>(propertyValue);
            Assert.Equal(15.5, locationValue.Latitude);
            Assert.Equal(130.88, locationValue.Longitude);
        }
Ejemplo n.º 3
0
        public void ReadFromStreamAsync_ForOpenComplexType_ForPatchModel()
        {
            // Arrange
            const string content = "{\"value\":{" +
                                   "\"Street\":\"UpdateStreet\"," +
                                   "\"[email protected]\":\"#Date\"," +
                                   "\"Publish\":\"2016-02-22\"" +
                                   "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder  builder      = new ODataConventionModelBuilder();

            builder.ComplexType <SimpleOpenAddress>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(Delta <SimpleOpenAddress>)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model),
                                             typeof(Delta <SimpleOpenAddress>), readContext);

            // Assert
            Delta <SimpleOpenAddress> address = Assert.IsType <Delta <SimpleOpenAddress> >(value);

            Assert.NotNull(address);
            Assert.Equal(new[] { "Street" }, address.GetChangedPropertyNames());
            Assert.Equal(new[] { "City" }, address.GetUnchangedPropertyNames());

            SimpleOpenAddress origin = new SimpleOpenAddress();

            Assert.Null(origin.Street);     // guard
            Assert.Null(origin.City);       // guard
            Assert.Null(origin.Properties); // guard

            address.Patch(origin);          // DO PATCH

            Assert.Equal("UpdateStreet", origin.Street);
            Assert.Null(origin.City); // not changed
            KeyValuePair <string, object> dynamicProperty = Assert.Single(origin.Properties);

            Assert.Equal("Publish", dynamicProperty.Key);
            Assert.Equal(new Date(2016, 2, 22), dynamicProperty.Value);
        }
Ejemplo n.º 4
0
        public void ReadFromStreamAsync_ForOpenComplexType()
        {
            // Arrange
            const string content = "{\"value\":{" +
                                   "\"Street\":\"MyStreet\"," +
                                   "\"City\":\"MyCity\"," +
                                   "\"[email protected]\":\"#Date\"," +
                                   "\"Publish\":\"2016-02-22\"" +
                                   "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder  builder      = new ODataConventionModelBuilder();

            builder.ComplexType <SimpleOpenAddress>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(SimpleOpenAddress)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model), typeof(SimpleOpenAddress), readContext);

            // Assert

            SimpleOpenAddress address = Assert.IsType <SimpleOpenAddress>(value);

            Assert.NotNull(address);

            Assert.Equal("MyStreet", address.Street);
            Assert.Equal("MyCity", address.City);
            Assert.NotNull(address.Properties);

            KeyValuePair <string, object> dynamicProperty = Assert.Single(address.Properties);

            Assert.Equal("Publish", dynamicProperty.Key);
            Assert.Equal(new Date(2016, 2, 22), dynamicProperty.Value);
        }
Ejemplo n.º 5
0
        public void ReadFromStreamAsync_ForDerivedComplexType()
        {
            // Arrange
            const string content = "{" +
                                   "\"@odata.type\":\"System.Web.OData.Formatter.Serialization.Models.CnAddress\"," +
                                   "\"Street\":\"StreetValue\"," +
                                   "\"City\":\"CityValue\"," +
                                   "\"State\":\"MyState\"," +
                                   "\"ZipCode\":\"160202\"," +
                                   "\"Country\":\"MyCountry\"," +
                                   "\"CnProp\":\"8E8375AA-D348-49DD-94A0-46E4FB42973C\"" +
                                   "}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder  builder      = new ODataConventionModelBuilder();

            builder.ComplexType <Address>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(Address)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model), typeof(Address), readContext);

            // Assert
            CnAddress address = Assert.IsType <CnAddress>(value);

            Assert.NotNull(address);
            Assert.Equal("StreetValue", address.Street);
            Assert.Equal("CityValue", address.City);
            Assert.Equal("MyState", address.State);
            Assert.Equal("160202", address.ZipCode);
            Assert.Equal("MyCountry", address.Country);
            Assert.Equal(new Guid("8E8375AA-D348-49DD-94A0-46E4FB42973C"), address.CnProp);
        }
        public void ReadFromStreamAsync_ForDerivedComplexType()
        {
            // Arrange
            const string content = "{" +
              "\"@odata.type\":\"System.Web.OData.Formatter.Serialization.Models.CnAddress\"," +
              "\"Street\":\"StreetValue\"," +
              "\"City\":\"CityValue\"," +
              "\"State\":\"MyState\"," +
              "\"ZipCode\":\"160202\"," +
              "\"Country\":\"MyCountry\"," +
              "\"CnProp\":\"8E8375AA-D348-49DD-94A0-46E4FB42973C\"" +
            "}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.ComplexType<Address>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = model,
                ResourceType = typeof(Address)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model), typeof(Address), readContext);

            // Assert
            CnAddress address = Assert.IsType<CnAddress>(value);
            Assert.NotNull(address);
            Assert.Equal("StreetValue", address.Street);
            Assert.Equal("CityValue", address.City);
            Assert.Equal("MyState", address.State);
            Assert.Equal("160202", address.ZipCode);
            Assert.Equal("MyCountry", address.Country);
            Assert.Equal(new Guid("8E8375AA-D348-49DD-94A0-46E4FB42973C"), address.CnProp);
        }
        public void ReadFromStreamAsync_ForComplexType_WithNestedComplexType()
        {
            // Arrange
            const string content = "{\"value\":{" +
              "\"City\":\"UpdatedCity\"," +
              "\"Location\": {" +
                  "\"Latitude\": 30.6," +
                  "\"Longitude\": 101.313" +
                  "}," +
              "\"SubLocation\": {" + // dynamic property
                  "\"@odata.type\":\"#System.Web.OData.Formatter.Deserialization.Location\"," +
                  "\"Latitude\": 15.5," +
                  "\"Longitude\": 130.88" +
                  "}" +
            "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.ComplexType<Region>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = model,
                ResourceType = typeof(Delta<Region>)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model),
                typeof(Delta<Region>), readContext);

            // Assert
            Delta<Region> region = Assert.IsType<Delta<Region>>(value);
            Assert.NotNull(region);
            Assert.Equal(new[] { "City", "Location" }, region.GetChangedPropertyNames());
            Assert.Empty(region.GetUnchangedPropertyNames());

            object propertyValue;
            Assert.True(region.TryGetPropertyValue("City", out propertyValue));
            string cityValue = Assert.IsType<string>(propertyValue);
            Assert.Equal("UpdatedCity", cityValue);

            Assert.True(region.TryGetPropertyValue("Location", out propertyValue));
            Location locationValue = Assert.IsType<Location>(propertyValue);
            Assert.Equal(30.6, locationValue.Latitude);
            Assert.Equal(101.313, locationValue.Longitude);

            // dynamic property
            Assert.True(region.TryGetPropertyValue("SubLocation", out propertyValue));
            locationValue = Assert.IsType<Location>(propertyValue);
            Assert.Equal(15.5, locationValue.Latitude);
            Assert.Equal(130.88, locationValue.Longitude);
        }
        public void ReadFromStreamAsync_ForOpenComplexType_ForPatchModel()
        {
            // Arrange
            const string content = "{\"value\":{" +
              "\"Street\":\"UpdateStreet\"," +
              "\"[email protected]\":\"#Date\"," +
              "\"Publish\":\"2016-02-22\"" +
            "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.ComplexType<SimpleOpenAddress>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = model,
                ResourceType = typeof(Delta<SimpleOpenAddress>)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model),
                typeof(Delta<SimpleOpenAddress>), readContext);

            // Assert
            Delta<SimpleOpenAddress> address = Assert.IsType<Delta<SimpleOpenAddress>>(value);
            Assert.NotNull(address);
            Assert.Equal(new[] { "Street" }, address.GetChangedPropertyNames());
            Assert.Equal(new[] { "City" }, address.GetUnchangedPropertyNames());

            SimpleOpenAddress origin = new SimpleOpenAddress();
            Assert.Null(origin.Street); // guard
            Assert.Null(origin.City); // guard
            Assert.Null(origin.Properties); // guard

            address.Patch(origin); // DO PATCH

            Assert.Equal("UpdateStreet", origin.Street);
            Assert.Null(origin.City); // not changed
            KeyValuePair<string, object> dynamicProperty = Assert.Single(origin.Properties);
            Assert.Equal("Publish", dynamicProperty.Key);
            Assert.Equal(new Date(2016, 2, 22), dynamicProperty.Value);
        }
        public void ReadFromStreamAsync_ForOpenComplexType()
        {
            // Arrange
            const string content = "{\"value\":{" +
              "\"Street\":\"MyStreet\"," +
              "\"City\":\"MyCity\"," +
              "\"[email protected]\":\"#Date\"," +
              "\"Publish\":\"2016-02-22\"" +
            "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.ComplexType<SimpleOpenAddress>();
            IEdmModel model = builder.GetEdmModel();

            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = model,
                ResourceType = typeof(SimpleOpenAddress)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), model), typeof(SimpleOpenAddress), readContext);

            // Assert

            SimpleOpenAddress address = Assert.IsType<SimpleOpenAddress>(value);
            Assert.NotNull(address);

            Assert.Equal("MyStreet", address.Street);
            Assert.Equal("MyCity", address.City);
            Assert.NotNull(address.Properties);

            KeyValuePair<string, object> dynamicProperty = Assert.Single(address.Properties);
            Assert.Equal("Publish", dynamicProperty.Key);
            Assert.Equal(new Date(2016, 2, 22), dynamicProperty.Value);
        }
        public void ReadFromStreamAsync()
        {
            // Arrange
            const string content = "{\"value\":{" +
              "\"Street\":\"MyStreet\"," +
              "\"City\":\"MyCity\"," +
              "\"State\":\"MyState\"," +
              "\"ZipCode\":\"160202\"," +
              "\"Country\":\"MyCountry\"" +
            "}}";

            ODataComplexTypeDeserializer deserializer = new ODataComplexTypeDeserializer(new DefaultODataDeserializerProvider());
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = _edmModel,
                ResourceType = typeof(ODataEntityDeserializerTests.Address)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), _edmModel),
                typeof(ODataEntityDeserializerTests.Address), readContext);

            // Assert

            ODataEntityDeserializerTests.Address address = Assert.IsType<ODataEntityDeserializerTests.Address>(value);
            Assert.NotNull(address);

            Assert.Equal("MyStreet", address.Street);
            Assert.Equal("MyCity", address.City);
            Assert.Equal("MyState", address.State);
            Assert.Equal("160202", address.ZipCode);
            Assert.Equal("MyCountry", address.Country);
        }