Пример #1
0
        public IHttpActionResult Post(Employee employee)
        {
            employee.Id = _employees.Max(e => e.Id) + 1;
            _employees.Add(employee);

            Manager manager = employee as Manager;

            if (manager != null)
            {
                Assert.Equal("Peter", manager.Name);
                Assert.NotNull(manager.Address);
                UsAddress usAddress = Assert.IsType <UsAddress>(manager.Address);
                Assert.Equal("NewYork", usAddress.City);

                return(Ok("Post() with Manager")); // a guard
            }

            Seller seller = employee as Seller;

            if (seller != null)
            {
                Assert.Equal("John", seller.Name);
                Assert.NotNull(seller.Address);
                CnAddress cnAddress = Assert.IsType <CnAddress>(seller.Address);
                Assert.Equal("Shanghai", cnAddress.City);

                return(Ok("Post() with Seller")); // a guard
            }

            Assert.NotNull(employee.Address);
            Assert.Equal("London", employee.Address.City);

            return(Ok("Post()"));
        }
        public void CreateODataComplexValue_WritesBaseAndDerivedProperties_ForDerivedComplexType()
        {
            // Arrange
            IEdmModel model = SerializationTestsHelpers.SimpleCustomerOrderModel();

            IEdmComplexType addressType = model.FindDeclaredType("Default.CnAddress") as IEdmComplexType;
            Type            cnAddress   = typeof(CnAddress);

            model.SetAnnotationValue <ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(cnAddress));

            IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex();

            ODataSerializerProvider    serializerProvider = new DefaultODataSerializerProvider();
            ODataComplexTypeSerializer serializer         = new ODataComplexTypeSerializer(serializerProvider);
            ODataSerializerContext     context            = new ODataSerializerContext
            {
                Model = model
            };

            Address address = new CnAddress()
            {
                Street  = "One Microsoft Way",
                City    = "Redmond",
                State   = "Washington",
                Country = "United States",
                ZipCode = "98052",
                CnProp  = new Guid("F83FB4CC-84BD-403B-B411-79926800F9A5")
            };

            // Act
            var odataValue = serializer.CreateODataComplexValue(address, addressTypeRef, context);

            // Assert
            ODataComplexValue complexValue = Assert.IsType <ODataComplexValue>(odataValue);

            Assert.Equal(complexValue.TypeName, "Default.CnAddress");
            Assert.Equal(6, complexValue.Properties.Count());

            // Verify the derived property
            ODataProperty street = Assert.Single(complexValue.Properties.Where(p => p.Name == "CnProp"));

            Assert.Equal(new Guid("F83FB4CC-84BD-403B-B411-79926800F9A5"), street.Value);
        }
Пример #3
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);
        }