public void ReadResource_SetsExpectedAndActualEdmType_OnCreatedEdmObject_TypelessMode() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); IEdmEntityTypeReference customerType = EdmLibHelpers.ToEdmTypeReference(model.Customer, isNullable: false).AsEntity(); ODataDeserializerContext readContext = new ODataDeserializerContext { Model = model.Model, ResourceType = typeof(IEdmObject) }; ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(new ODataResource { TypeName = model.SpecialCustomer.FullName(), Properties = new ODataProperty[0] }); ODataResourceDeserializer deserializer = new ODataResourceDeserializer(_deserializerProvider); // Act var result = deserializer.ReadResource(resourceWrapper, customerType, readContext); // Assert EdmEntityObject resource = Assert.IsType <EdmEntityObject>(result); Assert.Equal(model.SpecialCustomer, resource.ActualEdmType); Assert.Equal(model.Customer, resource.ExpectedEdmType); }
public void ReadSource_CanReadDynamicPropertiesForInheritanceOpenEntityType() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityType <SimpleOpenCustomer>(); builder.EnumType <SimpleEnum>(); IEdmModel model = builder.GetEdmModel(); IEdmEntityTypeReference vipCustomerTypeReference = model.GetEdmTypeReference(typeof(SimpleVipCustomer)).AsEntity(); var deserializer = new ODataResourceDeserializer(_deserializerProvider); ODataResource resource = new ODataResource { Properties = new[] { // declared properties new ODataProperty { Name = "CustomerId", Value = 121 }, new ODataProperty { Name = "Name", Value = "VipName #121" }, new ODataProperty { Name = "VipNum", Value = "Vip Num 001" }, // dynamic properties new ODataProperty { Name = "GuidProperty", Value = new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA") }, }, TypeName = typeof(SimpleVipCustomer).FullName }; ODataDeserializerContext readContext = new ODataDeserializerContext() { Model = model }; ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(resource); // Act SimpleVipCustomer customer = deserializer.ReadResource(resourceWrapper, vipCustomerTypeReference, readContext) as SimpleVipCustomer; // Assert Assert.NotNull(customer); // Verify the declared properties Assert.Equal(121, customer.CustomerId); Assert.Equal("VipName #121", customer.Name); Assert.Equal("Vip Num 001", customer.VipNum); // Verify the dynamic properties Assert.NotNull(customer.CustomerProperties); Assert.Equal(1, customer.CustomerProperties.Count()); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), customer.CustomerProperties["GuidProperty"]); }
public void ReadResource_ThrowsArgumentNull_ResourceWrapper() { // Arrange var deserializer = new ODataResourceDeserializer(_deserializerProvider); // Act & Assert Assert.ThrowsArgumentNull( () => deserializer.ReadResource(resourceWrapper: null, structuredType: _productEdmType, readContext: _readContext), "resourceWrapper"); }
public void ReadResource_ThrowsODataException_EntityTypeNotInModel() { // Arrange var deserializer = new ODataResourceDeserializer(_deserializerProvider); ODataResourceWrapper entry = new ODataResourceWrapper(new ODataResource { TypeName = "MissingType" }); // Act & Assert Assert.Throws <ODataException>( () => deserializer.ReadResource(entry, _productEdmType, _readContext), "Cannot find the resource type 'MissingType' in the model."); }
public void ReadResource_ThrowsArgument_ModelMissingFromReadContext() { // Arrange var deserializer = new ODataResourceDeserializer(_deserializerProvider); ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(new ODataResource { TypeName = _supplierEdmType.FullName() }); // Act & Assert Assert.ThrowsArgument( () => deserializer.ReadResource(resourceWrapper, _productEdmType, new ODataDeserializerContext()), "readContext", "The EDM model is missing on the read context. The model is required on the read context to deserialize the payload."); }
public void ReadResource_ThrowsSerializationException_TypeCannotBeDeserialized() { // Arrange Mock <ODataDeserializerProvider> deserializerProvider = new Mock <ODataDeserializerProvider>(); deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny <IEdmTypeReference>())).Returns <ODataEdmTypeDeserializer>(null); var deserializer = new ODataResourceDeserializer(deserializerProvider.Object); ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(new ODataResource { TypeName = _supplierEdmType.FullName() }); // Act & Assert Assert.Throws <SerializationException>( () => deserializer.ReadResource(resourceWrapper, _productEdmType, _readContext), "'ODataDemo.Supplier' cannot be deserialized using the ODataMediaTypeFormatter."); }
public void ReadResource_CanReadDatTimeRelatedProperties() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityType <MyCustomer>().Namespace = "NS"; IEdmModel model = builder.GetEdmModel(); IEdmEntityTypeReference vipCustomerTypeReference = model.GetEdmTypeReference(typeof(MyCustomer)).AsEntity(); var deserializer = new ODataResourceDeserializer(_deserializerProvider); ODataResource resource = new ODataResource { Properties = new[] { new ODataProperty { Name = "Id", Value = 121 }, new ODataProperty { Name = "Birthday", Value = new Date(2015, 12, 12) }, new ODataProperty { Name = "ReleaseTime", Value = new TimeOfDay(1, 2, 3, 4) }, }, TypeName = "NS.MyCustomer" }; ODataDeserializerContext readContext = new ODataDeserializerContext { Model = model }; ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(resource); // Act var customer = deserializer.ReadResource(resourceWrapper, vipCustomerTypeReference, readContext) as MyCustomer; // Assert Assert.NotNull(customer); Assert.Equal(121, customer.Id); Assert.Equal(new DateTime(2015, 12, 12), customer.Birthday); Assert.Equal(new TimeSpan(0, 1, 2, 3, 4), customer.ReleaseTime); }
public void ReadResource_DispatchesToRightDeserializer_IfEntityTypeNameIsDifferent() { // Arrange Mock <ODataEdmTypeDeserializer> supplierDeserializer = new Mock <ODataEdmTypeDeserializer>(ODataPayloadKind.Resource); Mock <ODataDeserializerProvider> deserializerProvider = new Mock <ODataDeserializerProvider>(); var deserializer = new ODataResourceDeserializer(deserializerProvider.Object); ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(new ODataResource { TypeName = _supplierEdmType.FullName() }); deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny <IEdmTypeReference>())).Returns(supplierDeserializer.Object); supplierDeserializer .Setup(d => d.ReadInline(resourceWrapper, It.Is <IEdmTypeReference>(e => _supplierEdmType.Definition == e.Definition), _readContext)) .Returns(42).Verifiable(); // Act object result = deserializer.ReadResource(resourceWrapper, _productEdmType, _readContext); // Assert supplierDeserializer.Verify(); Assert.Equal(42, result); }
public void ReadResource_ThrowsODataException_CannotInstantiateAbstractResourceType() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityType <BaseType>().Abstract(); IEdmModel model = builder.GetEdmModel(); var deserializer = new ODataResourceDeserializer(_deserializerProvider); ODataResourceWrapper resourceWrapper = new ODataResourceWrapper(new ODataResource { TypeName = "System.Web.OData.Formatter.Deserialization.BaseType" }); // Act & Assert Assert.Throws <ODataException>( () => deserializer.ReadResource(resourceWrapper, _productEdmType, new ODataDeserializerContext { Model = model }), "An instance of the abstract resource type 'System.Web.OData.Formatter.Deserialization.BaseType' was found. " + "Abstract resource types cannot be instantiated."); }
public void ReadResource_CanReadDynamicPropertiesForOpenEntityType() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityType <SimpleOpenCustomer>(); builder.EnumType <SimpleEnum>(); IEdmModel model = builder.GetEdmModel(); IEdmEntityTypeReference customerTypeReference = model.GetEdmTypeReference(typeof(SimpleOpenCustomer)).AsEntity(); var deserializer = new ODataResourceDeserializer(_deserializerProvider); ODataEnumValue enumValue = new ODataEnumValue("Third", typeof(SimpleEnum).FullName); ODataResource[] complexResources = { new ODataResource { TypeName = typeof(SimpleOpenAddress).FullName, Properties = new[] { // declared properties new ODataProperty { Name = "Street", Value = "Street 1" }, new ODataProperty { Name = "City", Value = "City 1" }, // dynamic properties new ODataProperty { Name = "DateTimeProperty", Value = new DateTimeOffset(new DateTime(2014, 5, 6)) } } }, new ODataResource { TypeName = typeof(SimpleOpenAddress).FullName, Properties = new[] { // declared properties new ODataProperty { Name = "Street", Value = "Street 2" }, new ODataProperty { Name = "City", Value = "City 2" }, // dynamic properties new ODataProperty { Name = "ArrayProperty", Value = new ODataCollectionValue{ TypeName = "Collection(Edm.Int32)", Items = new[]{ 1, 2, 3, 4 }.Cast <object>() } } } } }; ODataResource odataResource = new ODataResource { Properties = new[] { // declared properties new ODataProperty { Name = "CustomerId", Value = 991 }, new ODataProperty { Name = "Name", Value = "Name #991" }, // dynamic properties new ODataProperty { Name = "GuidProperty", Value = new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA") }, new ODataProperty { Name = "EnumValue", Value = enumValue }, }, TypeName = typeof(SimpleOpenCustomer).FullName }; ODataDeserializerContext readContext = new ODataDeserializerContext() { Model = model }; ODataResourceWrapper topLevelResourceWrapper = new ODataResourceWrapper(odataResource); ODataNestedResourceInfo resourceInfo = new ODataNestedResourceInfo { IsCollection = true, Name = "CollectionProperty" }; ODataNestedResourceInfoWrapper resourceInfoWrapper = new ODataNestedResourceInfoWrapper(resourceInfo); ODataResourceSetWrapper resourceSetWrapper = new ODataResourceSetWrapper(new ODataResourceSet { TypeName = String.Format("Collection({0})", typeof(SimpleOpenAddress).FullName) }); foreach (var complexResource in complexResources) { resourceSetWrapper.Resources.Add(new ODataResourceWrapper(complexResource)); } resourceInfoWrapper.NestedItems.Add(resourceSetWrapper); topLevelResourceWrapper.NestedResourceInfos.Add(resourceInfoWrapper); // Act SimpleOpenCustomer customer = deserializer.ReadResource(topLevelResourceWrapper, customerTypeReference, readContext) as SimpleOpenCustomer; // Assert Assert.NotNull(customer); // Verify the declared properties Assert.Equal(991, customer.CustomerId); Assert.Equal("Name #991", customer.Name); // Verify the dynamic properties Assert.NotNull(customer.CustomerProperties); Assert.Equal(3, customer.CustomerProperties.Count()); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), customer.CustomerProperties["GuidProperty"]); Assert.Equal(SimpleEnum.Third, customer.CustomerProperties["EnumValue"]); // Verify the dynamic collection property var collectionValues = Assert.IsType <List <SimpleOpenAddress> >(customer.CustomerProperties["CollectionProperty"]); Assert.NotNull(collectionValues); Assert.Equal(2, collectionValues.Count()); Assert.Equal(new DateTimeOffset(new DateTime(2014, 5, 6)), collectionValues[0].Properties["DateTimeProperty"]); Assert.Equal(new List <int> { 1, 2, 3, 4 }, collectionValues[1].Properties["ArrayProperty"]); }