public void CreateODataComplexValue_WritesNestedOpenComplexTypes() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue<ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexType zipCodeType = model.FindDeclaredType("Default.ZipCode") as IEdmComplexType; Type simpleOpenZipCode = typeof(SimpleOpenZipCode); model.SetAnnotationValue<ClrTypeAnnotation>(zipCodeType, new ClrTypeAnnotation(simpleOpenZipCode)); model.SetAnnotationValue(zipCodeType, new DynamicPropertyDictionaryAnnotation( simpleOpenZipCode.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress topAddress = new SimpleOpenAddress() { Street = "TopStreet", City = "TopCity", Properties = new Dictionary<string, object>() { { "PropertOfAddress", "Value1"} } }; SimpleOpenZipCode zipCode = new SimpleOpenZipCode() { Code = 101, Properties = new Dictionary<string, object>() { { "PropertyOfZipCode", "Value2"} } }; SimpleOpenAddress subAddress = new SimpleOpenAddress() { Street = "SubStreet", City = "SubCity", Properties = new Dictionary<string, object>() { { "PropertOfSubAddress", "Value3" } } }; // TopAddress (SimpleOpenAddress) // |-- Street (declare property, string) // |-- City (declare property, string) // |-- PropertyOfAddress (dynamic property, string) // |-- ZipCodeOfAddress (dynamic property, SimpleOpenZipCode) // |-- Code (declare property, int) // |-- PropertyOfZipCode (dynamic property, string) // |-- SubAddressOfZipCode (dynamic property, SimpleOpenAddress) // |-- Street (declare property, string) // |-- City (declare property, string) // |-- PropertyOfAddress (dynamic property, string) zipCode.Properties.Add("SubAddressOfZipCode", subAddress); topAddress.Properties.Add("ZipCodeOfAddress", zipCode); // Act var odataValue = serializer.CreateODataComplexValue(topAddress, addressTypeRef, context); // Assert ODataComplexValue topAddressComplexValue = Assert.IsType<ODataComplexValue>(odataValue); Assert.Equal(topAddressComplexValue.TypeName, "Default.Address"); Assert.Equal(4, topAddressComplexValue.Properties.Count()); // Verify the dynamic "ZipCodeOfAddress" property, it's nested open complex type ODataProperty dynamicProperty = Assert.Single(topAddressComplexValue.Properties.Where(p => p.Name == "ZipCodeOfAddress")); ODataComplexValue zipCodeComplexValue = Assert.IsType<ODataComplexValue>(dynamicProperty.Value); Assert.Equal(zipCodeComplexValue.TypeName, "Default.ZipCode"); Assert.Equal(3, zipCodeComplexValue.Properties.Count()); // Verify the declared "Code" property of ZipCode ODataProperty code = Assert.Single(zipCodeComplexValue.Properties.Where(p => p.Name == "Code")); Assert.Equal(101, code.Value); // Verify the dynamic "SubAddressOfZipCode" property of ZipCode dynamicProperty = Assert.Single(zipCodeComplexValue.Properties.Where(p => p.Name == "SubAddressOfZipCode")); ODataComplexValue subAddressComplexValue = Assert.IsType<ODataComplexValue>(dynamicProperty.Value); Assert.Equal(subAddressComplexValue.TypeName, "Default.Address"); Assert.Equal(3, subAddressComplexValue.Properties.Count()); }
public void CreateODataComplexValue_Throws_IfDynamicPropertyNameSameAsDeclaredPropertyName() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue<ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress address = new SimpleOpenAddress() { Street = "My Way", City = "Redmond", Properties = new Dictionary<string, object>() }; address.Properties.Add("StringProperty", "My Country"); address.Properties.Add("Street", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); // Act & Assert Assert.Throws<InvalidOperationException>(() => serializer.CreateODataComplexValue(address, addressTypeRef, context), "The name of dynamic property 'Street' was already used as the declared property name " + "of open complex type 'Default.Address'."); }
public void CreateODataComplexValue_WritesNullDynamicProperties_ForOpenComplexType(bool enableNullDynamicProperty, int count) { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue<ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue<ClrTypeAnnotation>(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); HttpConfiguration config = new HttpConfiguration(); if (enableNullDynamicProperty) { config.EnableNullDynamicProperty(); } HttpRequestMessage request = new HttpRequestMessage(); request.SetConfiguration(config); ODataSerializerContext context = new ODataSerializerContext { Model = model, Request = request }; SimpleOpenAddress address = new SimpleOpenAddress() { Street = "My Way", City = "Redmond", Properties = new Dictionary<string, object>() }; address.Properties.Add("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); address.Properties.Add("NullProperty", null); // Act var odataValue = serializer.CreateODataComplexValue(address, addressTypeRef, context); // Assert ODataComplexValue complexValue = Assert.IsType<ODataComplexValue>(odataValue); Assert.Equal(complexValue.TypeName, "Default.Address"); Assert.Equal(count, complexValue.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(complexValue.Properties.Where(p => p.Name == "Street")); Assert.Equal("My Way", street.Value); ODataProperty city = Assert.Single(complexValue.Properties.Where(p => p.Name == "City")); Assert.Equal("Redmond", city.Value); // Verify the dynamic properties ODataProperty guidProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "GuidProperty")); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), guidProperty.Value); ODataProperty nullProperty = complexValue.Properties.SingleOrDefault(p => p.Name == "NullProperty"); if (enableNullDynamicProperty) { Assert.NotNull(nullProperty); Assert.Null(nullProperty.Value); } else { Assert.Null(nullProperty); } }
public void CreateODataComplexValue_WritesAllDeclaredAndDynamicProperties_ForOpenComplexType() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue<ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue<ClrTypeAnnotation>(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress address = new SimpleOpenAddress() { Street = "My Way", City = "Redmond", Properties = new Dictionary<string, object>() }; address.Properties.Add("EnumProperty", SimpleEnum.Fourth); address.Properties.Add("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); address.Properties.Add("DoubleProperty", 99.109); // Act var odataValue = serializer.CreateODataComplexValue(address, addressTypeRef, context); // Assert ODataComplexValue complexValue = Assert.IsType<ODataComplexValue>(odataValue); Assert.Equal(complexValue.TypeName, "Default.Address"); Assert.Equal(5, complexValue.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(complexValue.Properties.Where(p => p.Name == "Street")); Assert.Equal("My Way", street.Value); ODataProperty city = Assert.Single(complexValue.Properties.Where(p => p.Name == "City")); Assert.Equal("Redmond", city.Value); // Verify the dynamic properties ODataProperty enumProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "EnumProperty")); ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(enumProperty.Value); Assert.Equal("Fourth", enumValue.Value); Assert.Equal("Default.SimpleEnum", enumValue.TypeName); ODataProperty guidProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "GuidProperty")); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), guidProperty.Value); ODataProperty doubleProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "DoubleProperty")); Assert.Equal(99.109, doubleProperty.Value); }
public void CreateODataComplexValue_WritesNestedOpenComplexTypes() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue <ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexType zipCodeType = model.FindDeclaredType("Default.ZipCode") as IEdmComplexType; Type simpleOpenZipCode = typeof(SimpleOpenZipCode); model.SetAnnotationValue <ClrTypeAnnotation>(zipCodeType, new ClrTypeAnnotation(simpleOpenZipCode)); model.SetAnnotationValue(zipCodeType, new DynamicPropertyDictionaryAnnotation( simpleOpenZipCode.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress topAddress = new SimpleOpenAddress() { Street = "TopStreet", City = "TopCity", Properties = new Dictionary <string, object>() { { "PropertOfAddress", "Value1" } } }; SimpleOpenZipCode zipCode = new SimpleOpenZipCode() { Code = 101, Properties = new Dictionary <string, object>() { { "PropertyOfZipCode", "Value2" } } }; SimpleOpenAddress subAddress = new SimpleOpenAddress() { Street = "SubStreet", City = "SubCity", Properties = new Dictionary <string, object>() { { "PropertOfSubAddress", "Value3" } } }; // TopAddress (SimpleOpenAddress) // |-- Street (declare property, string) // |-- City (declare property, string) // |-- PropertyOfAddress (dynamic property, string) // |-- ZipCodeOfAddress (dynamic property, SimpleOpenZipCode) // |-- Code (declare property, int) // |-- PropertyOfZipCode (dynamic property, string) // |-- SubAddressOfZipCode (dynamic property, SimpleOpenAddress) // |-- Street (declare property, string) // |-- City (declare property, string) // |-- PropertyOfAddress (dynamic property, string) zipCode.Properties.Add("SubAddressOfZipCode", subAddress); topAddress.Properties.Add("ZipCodeOfAddress", zipCode); // Act var odataValue = serializer.CreateODataComplexValue(topAddress, addressTypeRef, context); // Assert ODataComplexValue topAddressComplexValue = Assert.IsType <ODataComplexValue>(odataValue); Assert.Equal(topAddressComplexValue.TypeName, "Default.Address"); Assert.Equal(4, topAddressComplexValue.Properties.Count()); // Verify the dynamic "ZipCodeOfAddress" property, it's nested open complex type ODataProperty dynamicProperty = Assert.Single(topAddressComplexValue.Properties.Where(p => p.Name == "ZipCodeOfAddress")); ODataComplexValue zipCodeComplexValue = Assert.IsType <ODataComplexValue>(dynamicProperty.Value); Assert.Equal(zipCodeComplexValue.TypeName, "Default.ZipCode"); Assert.Equal(3, zipCodeComplexValue.Properties.Count()); // Verify the declared "Code" property of ZipCode ODataProperty code = Assert.Single(zipCodeComplexValue.Properties.Where(p => p.Name == "Code")); Assert.Equal(101, code.Value); // Verify the dynamic "SubAddressOfZipCode" property of ZipCode dynamicProperty = Assert.Single(zipCodeComplexValue.Properties.Where(p => p.Name == "SubAddressOfZipCode")); ODataComplexValue subAddressComplexValue = Assert.IsType <ODataComplexValue>(dynamicProperty.Value); Assert.Equal(subAddressComplexValue.TypeName, "Default.Address"); Assert.Equal(3, subAddressComplexValue.Properties.Count()); }
public void CreateODataComplexValue_WritesDynamicCollectionProperty_ForOpenComplexType() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue<ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue<ClrTypeAnnotation>(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress address = new SimpleOpenAddress() { Street = "One Way", City = "One City", Properties = new Dictionary<string, object>() }; address.Properties.Add("CollectionProperty", new[] { 1.1, 2.2 }); // Act var odataValue = serializer.CreateODataComplexValue(address, addressTypeRef, context); // Assert ODataComplexValue complexValue = Assert.IsType<ODataComplexValue>(odataValue); Assert.Equal(complexValue.TypeName, "Default.Address"); Assert.Equal(3, complexValue.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(complexValue.Properties.Where(p => p.Name == "Street")); Assert.Equal("One Way", street.Value); ODataProperty city = Assert.Single(complexValue.Properties.Where(p => p.Name == "City")); Assert.Equal("One City", city.Value); // Verify the dynamic properties ODataProperty enumProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "CollectionProperty")); ODataCollectionValue collectionValue = Assert.IsType<ODataCollectionValue>(enumProperty.Value); Assert.Equal("Collection(Edm.Double)", collectionValue.TypeName); Assert.Equal(new List<double> { 1.1, 2.2 }, collectionValue.Items.OfType<double>().ToList()); }
public void CreateODataComplexValue_WritesDynamicCollectionProperty_ForOpenComplexType() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue <ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue <ClrTypeAnnotation>(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress address = new SimpleOpenAddress() { Street = "One Way", City = "One City", Properties = new Dictionary <string, object>() }; address.Properties.Add("CollectionProperty", new[] { 1.1, 2.2 }); // Act var odataValue = serializer.CreateODataComplexValue(address, addressTypeRef, context); // Assert ODataComplexValue complexValue = Assert.IsType <ODataComplexValue>(odataValue); Assert.Equal(complexValue.TypeName, "Default.Address"); Assert.Equal(3, complexValue.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(complexValue.Properties.Where(p => p.Name == "Street")); Assert.Equal("One Way", street.Value); ODataProperty city = Assert.Single(complexValue.Properties.Where(p => p.Name == "City")); Assert.Equal("One City", city.Value); // Verify the dynamic properties ODataProperty enumProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "CollectionProperty")); ODataCollectionValue collectionValue = Assert.IsType <ODataCollectionValue>(enumProperty.Value); Assert.Equal("Collection(Edm.Double)", collectionValue.TypeName); Assert.Equal(new List <double> { 1.1, 2.2 }, collectionValue.Items.OfType <double>().ToList()); }
public void CreateODataComplexValue_WritesAllDeclaredAndDynamicProperties_ForOpenComplexType() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue <ClrTypeAnnotation>(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue <ClrTypeAnnotation>(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); IEdmComplexTypeReference addressTypeRef = addressType.ToEdmTypeReference(isNullable: false).AsComplex(); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(serializerProvider); ODataSerializerContext context = new ODataSerializerContext { Model = model }; SimpleOpenAddress address = new SimpleOpenAddress() { Street = "My Way", City = "Redmond", Properties = new Dictionary <string, object>() }; address.Properties.Add("EnumProperty", SimpleEnum.Fourth); address.Properties.Add("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); address.Properties.Add("DoubleProperty", 99.109); // Act var odataValue = serializer.CreateODataComplexValue(address, addressTypeRef, context); // Assert ODataComplexValue complexValue = Assert.IsType <ODataComplexValue>(odataValue); Assert.Equal(complexValue.TypeName, "Default.Address"); Assert.Equal(5, complexValue.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(complexValue.Properties.Where(p => p.Name == "Street")); Assert.Equal("My Way", street.Value); ODataProperty city = Assert.Single(complexValue.Properties.Where(p => p.Name == "City")); Assert.Equal("Redmond", city.Value); // Verify the dynamic properties ODataProperty enumProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "EnumProperty")); ODataEnumValue enumValue = Assert.IsType <ODataEnumValue>(enumProperty.Value); Assert.Equal("Fourth", enumValue.Value); Assert.Equal("Default.SimpleEnum", enumValue.TypeName); ODataProperty guidProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "GuidProperty")); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), guidProperty.Value); ODataProperty doubleProperty = Assert.Single(complexValue.Properties.Where(p => p.Name == "DoubleProperty")); Assert.Equal(99.109, doubleProperty.Value); }
private static IList <SimpleOpenCustomer> CreateCustomers() { int[] IntValues = { 200, 100, 300, 0, 400 }; IList <SimpleOpenCustomer> customers = Enumerable.Range(0, 5).Select(i => new SimpleOpenCustomer { CustomerId = i, Name = "FirstName " + i, Address = new SimpleOpenAddress { Street = "Street " + i, City = "City " + i, Properties = new Dictionary <string, object> { { "IntProp", IntValues[i] } } }, Website = "WebSite #" + i }).ToList(); customers[2].CustomerProperties = new Dictionary <string, object> { { "Token", new Guid("2C1F450A-A2A7-4FE1-A25D-4D9332FC0694") }, { "IntList", new List <int> { 1, 2, 3, 4, 5, 6, 7 } }, }; customers[4].CustomerProperties = new Dictionary <string, object> { { "Token", new Guid("A6A594ED-375B-424E-AC0A-945D89CF7B9B") }, { "IntList", new List <int> { 1, 2, 3, 4, 5, 6, 7 } }, }; SimpleOpenAddress address = new SimpleOpenAddress { Street = "SubStreet", City = "City" }; customers[3].CustomerProperties = new Dictionary <string, object> { { "ComplexList", new[] { address, address } } }; SimpleVipCustomer vipCustomer = new SimpleVipCustomer { CustomerId = 9, Name = "VipCustomer", Address = new SimpleOpenAddress { Street = "Vip Street ", City = "Vip City ", }, VipNum = "99-001", CustomerProperties = new Dictionary <string, object> { { "ListProp", IntValues }, { "DateList", new[] { Date.MinValue, Date.MaxValue } }, { "Receipt", null } } }; customers.Add(vipCustomer); return(customers); }
public void ReadComplexValue_CanReadDynamicCollectionPropertiesForOpenComplexType() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.ComplexType <SimpleOpenAddress>(); builder.EnumType <SimpleEnum>(); IEdmModel model = builder.GetEdmModel(); IEdmComplexTypeReference addressTypeReference = model.GetEdmTypeReference(typeof(SimpleOpenAddress)).AsComplex(); var deserializerProvider = new DefaultODataDeserializerProvider(); var deserializer = new ODataComplexTypeDeserializer(deserializerProvider); ODataEnumValue enumValue = new ODataEnumValue("Third", typeof(SimpleEnum).FullName); ODataCollectionValue collectionValue = new ODataCollectionValue { TypeName = "Collection(" + typeof(SimpleEnum).FullName + ")", Items = new[] { enumValue, enumValue } }; ODataComplexValue complexValue = new ODataComplexValue { Properties = new[] { // declared properties new ODataProperty { Name = "Street", Value = "My Way #599" }, // dynamic properties new ODataProperty { Name = "CollectionProperty", Value = collectionValue } }, TypeName = typeof(SimpleOpenAddress).FullName }; ODataDeserializerContext readContext = new ODataDeserializerContext() { Model = model }; // Act SimpleOpenAddress address = deserializer.ReadComplexValue(complexValue, addressTypeReference, readContext) as SimpleOpenAddress; // Assert Assert.NotNull(address); // Verify the declared properties Assert.Equal("My Way #599", address.Street); Assert.Null(address.City); // Verify the dynamic properties Assert.NotNull(address.Properties); Assert.Equal(1, address.Properties.Count()); var collectionValues = Assert.IsType <List <SimpleEnum> >(address.Properties["CollectionProperty"]); Assert.NotNull(collectionValues); Assert.Equal(2, collectionValues.Count()); Assert.Equal(SimpleEnum.Third, collectionValues[0]); Assert.Equal(SimpleEnum.Third, collectionValues[1]); }
public void ReadComplexValue_CanReadNestedOpenComplexType() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.ComplexType <SimpleOpenAddress>(); builder.ComplexType <SimpleOpenZipCode>(); IEdmModel model = builder.GetEdmModel(); IEdmComplexTypeReference addressTypeReference = model.GetEdmTypeReference(typeof(SimpleOpenAddress)).AsComplex(); var deserializerProvider = new DefaultODataDeserializerProvider(); var deserializer = new ODataComplexTypeDeserializer(deserializerProvider); ODataComplexValue zipCodeComplexValue = new ODataComplexValue { Properties = new[] { // declared property new ODataProperty { Name = "Code", Value = 101 }, // dynamic property new ODataProperty { Name = "DateTimeProperty", Value = new DateTimeOffset(new DateTime(2014, 4, 22)) } }, TypeName = typeof(SimpleOpenZipCode).FullName }; ODataComplexValue addressComplexValue = new ODataComplexValue { Properties = new[] { // declared properties new ODataProperty { Name = "Street", Value = "TopStreet" }, new ODataProperty { Name = "City", Value = "TopCity" }, // dynamic properties new ODataProperty { Name = "DoubleProperty", Value = 1.179 }, new ODataProperty { Name = "ZipCodeProperty", Value = zipCodeComplexValue } }, TypeName = typeof(SimpleOpenAddress).FullName }; ODataDeserializerContext readContext = new ODataDeserializerContext() { Model = model }; // Act SimpleOpenAddress address = deserializer.ReadComplexValue(addressComplexValue, addressTypeReference, readContext) as SimpleOpenAddress; // Assert Assert.NotNull(address); // Verify the declared properties Assert.Equal("TopStreet", address.Street); Assert.Equal("TopCity", address.City); // Verify the dynamic properties Assert.NotNull(address.Properties); Assert.Equal(2, address.Properties.Count()); Assert.Equal(1.179, address.Properties["DoubleProperty"]); // nested open complex type SimpleOpenZipCode zipCode = Assert.IsType <SimpleOpenZipCode>(address.Properties["ZipCodeProperty"]); Assert.Equal(101, zipCode.Code); Assert.Equal(1, zipCode.Properties.Count()); Assert.Equal(new DateTimeOffset(new DateTime(2014, 4, 22)), zipCode.Properties["DateTimeProperty"]); }
public void ReadComplexValue_CanReadDynamicPropertiesForOpenComplexType() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.ComplexType <SimpleOpenAddress>(); builder.EnumType <SimpleEnum>(); IEdmModel model = builder.GetEdmModel(); IEdmComplexTypeReference addressTypeReference = model.GetEdmTypeReference(typeof(SimpleOpenAddress)).AsComplex(); var deserializerProvider = new Mock <ODataDeserializerProvider>().Object; var deserializer = new ODataComplexTypeDeserializer(deserializerProvider); ODataEnumValue enumValue = new ODataEnumValue("Third", typeof(SimpleEnum).FullName); ODataComplexValue complexValue = new ODataComplexValue { Properties = new[] { // declared properties new ODataProperty { Name = "Street", Value = "My Way #599" }, new ODataProperty { Name = "City", Value = "Redmond & Shanghai" }, // dynamic properties new ODataProperty { Name = "GuidProperty", Value = new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA") }, new ODataProperty { Name = "EnumValue", Value = enumValue }, new ODataProperty { Name = "DateTimeProperty", Value = new DateTimeOffset(new DateTime(1992, 1, 1)) } }, TypeName = typeof(SimpleOpenAddress).FullName }; ODataDeserializerContext readContext = new ODataDeserializerContext() { Model = model }; // Act SimpleOpenAddress address = deserializer.ReadComplexValue(complexValue, addressTypeReference, readContext) as SimpleOpenAddress; // Assert Assert.NotNull(address); // Verify the declared properties Assert.Equal("My Way #599", address.Street); Assert.Equal("Redmond & Shanghai", address.City); // Verify the dynamic properties Assert.NotNull(address.Properties); Assert.Equal(3, address.Properties.Count()); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), address.Properties["GuidProperty"]); Assert.Equal(SimpleEnum.Third, address.Properties["EnumValue"]); Assert.Equal(new DateTimeOffset(new DateTime(1992, 1, 1)), address.Properties["DateTimeProperty"]); }
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); }