コード例 #1
0
        public async Task Can_DeserializePayload_WithEnumCollections_InUntypedMode(string actionName, IEdmAction expectedAction, ODataPath path)
        {
            // Arrange
            const string        Body    = @"{ ""Colors"": [ ""Red"", ""Green""] }";
            ODataMessageWrapper message = new ODataMessageWrapper(await GetStringAsStreamAsync(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, ResourceType = typeof(ODataUntypedActionParameters)
            };

            // Act
            ODataUntypedActionParameters payload = await _deserializer.ReadAsync(reader, typeof(ODataUntypedActionParameters), context) as ODataUntypedActionParameters;

            // Assert
            Assert.NotNull(actionName);
            Assert.Same(expectedAction, payload.Action);
            Assert.NotNull(payload);
            Assert.True(payload.ContainsKey("Colors"));
            EdmEnumObjectCollection colors = payload["Colors"] as EdmEnumObjectCollection;
            EdmEnumObject           color  = colors[0] as EdmEnumObject;

            Assert.NotNull(color);
            Assert.Equal("Red", color.Value);
        }
コード例 #2
0
        public void Can_DeserializePayload_WithEnumParameters_InUntypedMode(string actionName, IEdmAction expectedAction, ODataPath path)
        {
            // Arrange
            const string        Body    = @"{ ""Color"": ""Red""}";
            ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(Body));

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

            ODataMessageReaderSettings settings = new ODataMessageReaderSettings();
            ODataMessageReader         reader   = new ODataMessageReader(message as IODataRequestMessage, settings, _model);

            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(expectedAction, payload.Action);
            Assert.True(payload.ContainsKey("Color"));
            EdmEnumObject color = payload["Color"] as EdmEnumObject;

            Assert.IsType <EdmEnumObject>(color);
            Assert.Equal("Red", color.Value);
        }
コード例 #3
0
 public bool EnumFunction(int key, [FromODataUri] EdmEnumObject color)
 {
     Assert.NotNull(color);
     Assert.Equal("NS.Color", color.GetEdmType().FullName());
     Assert.Equal("0", color.Value);
     return(true);
 }
コード例 #4
0
        private static EdmEntityObjectCollection GetCustomers()
        {
            if (_untypedSimpleOpenCustormers != null)
            {
                return(_untypedSimpleOpenCustormers);
            }

            IEdmModel       edmModel     = OpenEntityTypeTests.GetUntypedEdmModel();
            IEdmEntityType  customerType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "UntypedSimpleOpenCustomer");
            EdmEntityObject customer     = new EdmEntityObject(customerType);

            customer.TrySetPropertyValue("CustomerId", 1);

            //Add Numbers primitive collection property
            customer.TrySetPropertyValue("DeclaredNumbers", new[] { 1, 2 });

            //Add Color, Colors enum(collection) property
            IEdmEnumType  colorType = edmModel.SchemaElements.OfType <IEdmEnumType>().First(c => c.Name == "Color");
            EdmEnumObject color     = new EdmEnumObject(colorType, "Red");
            EdmEnumObject color2    = new EdmEnumObject(colorType, "0");
            EdmEnumObject color3    = new EdmEnumObject(colorType, "Red");

            customer.TrySetPropertyValue("Color", color);

            List <IEdmEnumObject> colorList = new List <IEdmEnumObject>();

            colorList.Add(color);
            colorList.Add(color2);
            colorList.Add(color3);
            IEdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(colorType.ToEdmTypeReference(false)));
            EdmEnumObjectCollection     colors             = new EdmEnumObjectCollection(enumCollectionType, colorList);

            customer.TrySetPropertyValue("Colors", colors);
            customer.TrySetPropertyValue("DeclaredColors", colors);

            //Add Addresses complex(collection) property
            EdmComplexType addressType =
                edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "Address") as EdmComplexType;
            EdmComplexObject address = new EdmComplexObject(addressType);

            address.TrySetPropertyValue("Street", "No1");
            EdmComplexObject address2 = new EdmComplexObject(addressType);

            address2.TrySetPropertyValue("Street", "No2");

            List <IEdmComplexObject> addressList = new List <IEdmComplexObject>();

            addressList.Add(address);
            addressList.Add(address2);
            IEdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(addressType.ToEdmTypeReference(false)));
            EdmComplexObjectCollection  addresses             = new EdmComplexObjectCollection(complexCollectionType, addressList);

            customer.TrySetPropertyValue("DeclaredAddresses", addresses);

            EdmEntityObjectCollection customers = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(customerType.ToEdmTypeReference(false))));

            customers.Add(customer);
            _untypedSimpleOpenCustormers = customers;
            return(_untypedSimpleOpenCustormers);
        }
コード例 #5
0
        private EdmEntityObjectCollection GetCustomers()
        {
            IEdmModel edmModel = Request.ODataProperties().Model;

            IEdmEntityType  customerType = edmModel.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Customer");
            IEdmEnumType    colorType    = edmModel.SchemaElements.OfType <IEdmEnumType>().Single(e => e.Name == "Color");
            IEdmComplexType addressType  = edmModel.SchemaElements.OfType <IEdmComplexType>().Single(e => e.Name == "Address");

            // an enum object
            EdmEnumObject color = new EdmEnumObject(colorType, "Red");

            // a complex object
            EdmComplexObject address1 = new EdmComplexObject(addressType);

            address1.TrySetPropertyValue("Street", "ZiXing Rd");                                            // Declared property
            address1.TrySetPropertyValue("StringProperty", "CN");                                           // a string dynamic property
            address1.TrySetPropertyValue("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); // a guid dynamic property

            // another complex object with complex dynamic property
            EdmComplexObject address2 = new EdmComplexObject(addressType);

            address2.TrySetPropertyValue("Street", "ZiXing Rd");       // Declared property
            address2.TrySetPropertyValue("AddressProperty", address1); // a complex dynamic property

            // an entity object
            EdmEntityObject customer = new EdmEntityObject(customerType);

            customer.TrySetPropertyValue("CustomerId", 1);      // Declared property
            customer.TrySetPropertyValue("Name", "Mike");       // Declared property
            customer.TrySetPropertyValue("Color", color);       // an enum dynamic property
            customer.TrySetPropertyValue("Address1", address1); // a complex dynamic property
            customer.TrySetPropertyValue("Address2", address2); // another complex dynamic property

            // a collection with one object
            EdmEntityObjectCollection collection =
                new EdmEntityObjectCollection(
                    new EdmCollectionTypeReference(
                        new EdmCollectionType(new EdmEntityTypeReference(customerType, isNullable: true))));

            collection.Add(customer);

            return(collection);
        }
コード例 #6
0
        public bool EnumCollectionFunction(int key, [FromODataUri] EdmEnumObjectCollection colors)
        {
            Assert.NotNull(colors);
            IList <IEdmEnumObject> results = colors.ToList();

            Assert.Equal(2, results.Count);

            // #1
            EdmEnumObject color = results[0] as EdmEnumObject;

            Assert.NotNull(color);
            Assert.Equal("NS.Color", color.GetEdmType().FullName());
            Assert.Equal("Red", color.Value);

            // #2
            EdmEnumObject color2 = results[1] as EdmEnumObject;

            Assert.NotNull(color2);
            Assert.Equal("NS.Color", color2.GetEdmType().FullName());
            Assert.Equal("Green", color2.Value);
            return(true);
        }
コード例 #7
0
        public IHttpActionResult GetColor(int key)
        {
            EdmEntityObjectCollection customers = GetCustomers();
            EdmEntityObject           customer  = customers.FirstOrDefault(e =>
            {
                object customerId;
                if (e.TryGetPropertyValue("CustomerId", out customerId))
                {
                    return((int)customerId == key);
                }

                return(false);
            }) as EdmEntityObject;

            if (customer == null)
            {
                return(NotFound());
            }

            object color;

            customer.TryGetPropertyValue("Color", out color);
            if (color == null)
            {
                return(NotFound());
            }

            // return Ok(color, color.GetType());

            EdmEnumObject enumColor = color as EdmEnumObject;

            if (enumColor == null)
            {
                return(NotFound());
            }

            return(Ok(enumColor));
        }
コード例 #8
0
        public void ReadFromStreamAsync_ForUnType()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(IEdmEnumObject)
            };

            // Act
            object value = deserializer.Read(ODataDeserializationTestsCommon.GetODataMessageReader(ODataDeserializationTestsCommon.GetODataMessage(content, new HttpRequestMessage(new HttpMethod("Post"), "http://localhost/OData/TestUri")), _edmModel),
                                             typeof(Color), readContext);

            // Assert
            EdmEnumObject color = Assert.IsType <EdmEnumObject>(value);

            Assert.NotNull(color);

            Assert.Equal("Blue", color.Value);
        }
コード例 #9
0
        public void ReadFromStreamAsync_ForUnType()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(IEdmEnumObject)
            };

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

            // Assert
            EdmEnumObject color = Assert.IsType <EdmEnumObject>(value);

            Assert.NotNull(color);

            Assert.Equal("Blue", color.Value);
        }
コード例 #10
0
        public async Task ReadFromStreamAsync_ForUnType()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(IEdmEnumObject)
            };
            HttpRequest request = RequestFactory.Create("Post", "http://localhost/", _edmModel);

            // Act
            object value = await deserializer.ReadAsync(ODataTestUtil.GetODataMessageReader(request.GetODataMessage(content), _edmModel),
                                                        typeof(Color), readContext);

            // Assert
            EdmEnumObject color = Assert.IsType <EdmEnumObject>(value);

            Assert.NotNull(color);

            Assert.Equal("Blue", color.Value);
        }
コード例 #11
0
 public bool EnumFunctionOnAttribute(int key, [FromODataUri] EdmEnumObject color)
 {
     return(EnumFunction(key, color));
 }