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"]); }
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(typeof(ODataActionParameters), new DefaultODataDeserializerProvider(model)); string url = "http://server/service/Customers(10)/" + actionName; HttpRequestMessage request = GetPostRequest(url); ODataDeserializerContext context = new ODataDeserializerContext { Request = request, Model = model }; ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters; Assert.NotNull(payload); Assert.Same(model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Complex"), payload.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); }
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); }
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)); }
public void Can_DeserializePayload_InUntypedMode() { // Arrange IEdmModel model = GetModel(); IEdmFunctionImport action = model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "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"); 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(action, payload.Action); Assert.True(payload.ContainsKey("Quantity")); Assert.Equal(1, payload["Quantity"]); Assert.True(payload.ContainsKey("Address")); dynamic address = payload["Address"] as EdmComplexObject; Assert.IsType <EdmComplexObject>(address); Assert.Equal("1 Microsoft Way", address.StreetAddress); Assert.Equal("Redmond", address.City); Assert.Equal("WA", address.State); Assert.Equal(98052, address.ZipCode); }
public void Can_deserialize_payload_with_complex_collection_parameters() { string actionName = "ComplexCollection"; string body = @"{ ""Name"": ""Microsoft"", ""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"); 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.True(payload.ContainsKey("Name")); Assert.Equal("Microsoft", payload["Name"]); Assert.True(payload.ContainsKey("Addresses")); IList <MyAddress> addresses = payload["Addresses"] as IList <MyAddress>; Assert.NotNull(addresses); Assert.Equal(1, addresses.Count); MyAddress address = addresses[0]; 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); }
public void Read_ThrowsArgumentNull_MessageReader() { ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider()); Assert.ThrowsArgumentNull( () => deserializer.Read(messageReader: null, readContext: new ODataDeserializerContext()), "messageReader"); }
public void Read_Throws_SerializationException_ODataPathMissing() { ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider()); ODataMessageReader messageReader = ODataTestUtil.GetMockODataMessageReader(); Assert.Throws<SerializationException>( () => deserializer.Read(messageReader, readContext: new ODataDeserializerContext()), "The operation cannot be completed because no ODataPath is available for the request."); }
public void Read_ThrowsArgumentNull_ReadContext() { ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider()); ODataMessageReader messageReader = ODataTestUtil.GetMockODataMessageReader(); Assert.ThrowsArgumentNull( () => deserializer.Read(messageReader, readContext: null), "readContext"); }
public void Read_Throws_SerializationException_ODataPathMissing() { ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(new DefaultODataDeserializerProvider()); ODataMessageReader messageReader = ODataTestUtil.GetMockODataMessageReader(); Assert.Throws <SerializationException>( () => deserializer.Read(messageReader, readContext: new ODataDeserializerContext()), "The operation cannot be completed because no ODataPath is available for the request."); }
public void Throws_ODataException_when_parameter_not_found() { string body = @"{ ""Quantity"": 1 , ""ProductCode"": ""PCode"", ""MissingParameter"": 1 }"; 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, "Primitive"); ODataDeserializerContext context = new ODataDeserializerContext { Path = path, Model = model }; Assert.Throws <ODataException>(() => { ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters; }, "The parameter 'MissingParameter' in the request payload is not a valid parameter for the function import 'Primitive'."); }
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)); }
public void Can_Deserialize_PrimitiveCollections_InUntypedMode() { // Arrange IEdmModel model = GetModel(); IEdmActionImport action = model.EntityContainers() .Single() .OperationImports() .SingleOrDefault(o => o.Name == "PrimitiveCollection") as IEdmActionImport; Assert.NotNull(action); 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() .OperationImports() .SingleOrDefault(f => f.Name == "PrimitiveCollection") as IEdmActionImport, ODataActionPayloadDeserializer.GetActionImport(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)); }
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)); }
public void Can_deserialize_payload_with_complex_collection_parameters() { string actionName = "ComplexCollection"; string body = @"{ ""Name"": ""Microsoft"", ""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"); 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.True(payload.ContainsKey("Name")); Assert.Equal("Microsoft", payload["Name"]); Assert.True(payload.ContainsKey("Addresses")); IList<MyAddress> addresses = payload["Addresses"] as IList<MyAddress>; Assert.NotNull(addresses); Assert.Equal(1, addresses.Count); MyAddress address = addresses[0]; 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); }
public void Throws_ODataException_when_parameter_not_found() { string body = @"{ ""Quantity"": 1 , ""ProductCode"": ""PCode"", ""MissingParameter"": 1 }"; 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, "Primitive"); ODataDeserializerContext context = new ODataDeserializerContext { Path = path, Model = model }; Assert.Throws<ODataException>(() => { ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters; }, "The parameter 'MissingParameter' in the request payload is not a valid parameter for the function import 'Primitive'."); }
public void Can_DeserializePayload_InUntypedMode() { // Arrange IEdmModel model = GetModel(); IEdmActionImport action = model.EntityContainers().Single().OperationImports() .SingleOrDefault(o => o.Name == "Complex") as IEdmActionImport; 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"); 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(action, payload.Action); Assert.True(payload.ContainsKey("Quantity")); Assert.Equal(1, payload["Quantity"]); Assert.True(payload.ContainsKey("Address")); dynamic address = payload["Address"] as EdmComplexObject; Assert.IsType<EdmComplexObject>(address); Assert.Equal("1 Microsoft Way", address.StreetAddress); Assert.Equal("Redmond", address.City); Assert.Equal("WA", address.State); Assert.Equal(98052, address.ZipCode); }
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); }