public void Read_ThrowsArgument_EntitysetMissing()
 {
     var deserializer = new ODataEntityDeserializer(_deserializerProvider);
     Assert.Throws<SerializationException>(
         () => deserializer.Read(ODataTestUtil.GetMockODataMessageReader(), typeof(Product), new ODataDeserializerContext { Path = new ODataPath() }),
         "The related entity set could not be found from the OData path. The related entity set is required to deserialize the payload.");
 }
示例#2
0
        public void ReadEntry_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)
            };
            ODataEntryWithNavigationLinks entry = new ODataEntryWithNavigationLinks(new ODataEntry
            {
                TypeName   = model.SpecialCustomer.FullName(),
                Properties = new ODataProperty[0]
            });

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_deserializerProvider);

            // Act
            var result = deserializer.ReadEntry(entry, customerType, readContext);

            // Assert
            EdmEntityObject resource = Assert.IsType <EdmEntityObject>(result);

            Assert.Equal(model.SpecialCustomer, resource.ActualEdmType);
            Assert.Equal(model.Customer, resource.ExpectedEdmType);
        }
 public void Read_ThrowsArgumentNull_ReadContext()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgumentNull(
         () => deserializer.Read(messageReader: ODataTestUtil.GetMockODataMessageReader(), readContext: null),
         "readContext");
 }
 public void Read_ThrowsArgumentNull_MessageReader()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgumentNull(
         () => deserializer.Read(messageReader: null, readContext: _readContext),
         "messageReader");
 }
示例#5
0
        public void ApplyNavigationProperty_Calls_ReadInlineOnFeed()
        {
            // Arrange
            IEdmCollectionTypeReference      productsType         = new EdmCollectionTypeReference(new EdmCollectionType(_productEdmType), isNullable: false);
            Mock <ODataEdmTypeDeserializer>  productsDeserializer = new Mock <ODataEdmTypeDeserializer>(ODataPayloadKind.Feed);
            Mock <ODataDeserializerProvider> deserializerProvider = new Mock <ODataDeserializerProvider>();
            var deserializer = new ODataEntityDeserializer(deserializerProvider.Object);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink {
                Name = "Products"
            });

            navigationLink.NestedItems.Add(new ODataFeedWithEntries(new ODataFeed()));

            Supplier    supplier = new Supplier();
            IEnumerable products = new[] { new Product {
                                               ID = 42
                                           } };

            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny <IEdmTypeReference>())).Returns(productsDeserializer.Object);
            productsDeserializer
            .Setup(d => d.ReadInline(navigationLink.NestedItems[0], _supplierEdmType.FindNavigationProperty("Products").Type, _readContext))
            .Returns(products).Verifiable();

            // Act
            deserializer.ApplyNavigationProperty(supplier, navigationLink, _supplierEdmType, _readContext);

            // Assert
            productsDeserializer.Verify();
            Assert.Equal(1, supplier.Products.Count());
            Assert.Equal(42, supplier.Products.First().ID);
        }
示例#6
0
        public void ApplyNavigationProperty_Calls_ReadInlineOnEntry()
        {
            // Arrange
            Mock <ODataEdmTypeDeserializer>  supplierDeserializer = new Mock <ODataEdmTypeDeserializer>(ODataPayloadKind.Feed);
            Mock <ODataDeserializerProvider> deserializerProvider = new Mock <ODataDeserializerProvider>();
            var deserializer = new ODataEntityDeserializer(deserializerProvider.Object);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink {
                Name = "Supplier"
            });

            navigationLink.NestedItems.Add(new ODataEntryWithNavigationLinks(new ODataEntry()));

            Product  product  = new Product();
            Supplier supplier = new Supplier {
                ID = 42
            };

            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny <IEdmTypeReference>())).Returns(supplierDeserializer.Object);
            supplierDeserializer
            .Setup(d => d.ReadInline(navigationLink.NestedItems[0], _productEdmType.FindNavigationProperty("Supplier").Type, _readContext))
            .Returns(supplier).Verifiable();

            // Act
            deserializer.ApplyNavigationProperty(product, navigationLink, _productEdmType, _readContext);

            // Assert
            supplierDeserializer.Verify();
            Assert.Equal(supplier, product.Supplier);
        }
示例#7
0
        public void ReadInline_ThrowsArgumentNull_Item()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.ReadInline(item: null, edmType: _productEdmType, readContext: new ODataDeserializerContext()),
                "item");
        }
示例#8
0
        public void CreateEntityResource_ThrowsArgumentNull_ReadContext()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.CreateEntityResource(_productEdmType, readContext: null),
                "readContext");
        }
示例#9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataDeserializerProvider"/> class.
 /// </summary>
 public DefaultODataDeserializerProvider()
 {
     _actionPayloadDeserializer = new ODataActionPayloadDeserializer(this);
     _entityDeserializer        = new ODataEntityDeserializer(this);
     _feedDeserializer          = new ODataFeedDeserializer(this);
     _collectionDeserializer    = new ODataCollectionDeserializer(this);
     _complexDeserializer       = new ODataComplexTypeDeserializer(this);
 }
示例#10
0
        public void ApplyStructuralProperties_ThrowsArgumentNull_entryWrapper()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.ApplyStructuralProperties(42, entryWrapper: null, entityType: _productEdmType, readContext: _readContext),
                "entryWrapper");
        }
 public void Read_ThrowsArgument_ODataPathMissing()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgument(
         () => deserializer.Read(ODataTestUtil.GetMockODataMessageReader(), new ODataDeserializerContext()),
         "readContext",
         "The operation cannot be completed because no ODataPath is available for the request.");
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataDeserializerProvider"/> class.
 /// </summary>
 public DefaultODataDeserializerProvider()
 {
     _actionPayloadDeserializer = new ODataActionPayloadDeserializer(this);
     _entityDeserializer = new ODataEntityDeserializer(this);
     _feedDeserializer = new ODataFeedDeserializer(this);
     _collectionDeserializer = new ODataCollectionDeserializer(this);
     _complexDeserializer = new ODataComplexTypeDeserializer(this);
 }
示例#13
0
        public void ReadEntry_ThrowsArgumentNull_EntryWrapper()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.ReadEntry(entryWrapper: null, entityType: _productEdmType, readContext: _readContext),
                "entryWrapper");
        }
        public void ApplyStructuralProperty_ThrowsArgumentNull_EntityResource()
        {
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.ApplyStructuralProperty(entityResource: null, structuralProperty: new ODataProperty(), readContext: _readContext),
                "entityResource");
        }
示例#15
0
        public void Read_ThrowsArgumentNull_MessageReader()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.Read(messageReader: null, type: typeof(Product), readContext: _readContext),
                "messageReader");
        }
        public void ApplyNavigationProperty_ThrowsArgumentNull_NavigationLinkWrapper()
        {
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.ApplyNavigationProperty(42, navigationLinkWrapper: null, readContext: _readContext),
                "navigationLinkWrapper");
        }
示例#17
0
        public void Read_ThrowsArgumentNull_ReadContext()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.Read(messageReader: ODataTestUtil.GetMockODataMessageReader(), type: typeof(Product), readContext: null),
                "readContext");
        }
示例#18
0
        public void ApplyStructuralProperty_ThrowsArgumentNull_StructuralProperty()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgumentNull(
                () => deserializer.ApplyStructuralProperty(42, structuralProperty: null, entityType: _productEdmType, readContext: _readContext),
                "structuralProperty");
        }
示例#19
0
        public void Read_ThrowsArgument_ODataPathMissing()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgument(
                () => deserializer.Read(ODataTestUtil.GetMockODataMessageReader(), typeof(Product), new ODataDeserializerContext()),
                "readContext",
                "The operation cannot be completed because no ODataPath is available for the request.");
        }
        public void ApplyNavigationProperty_ThrowsArgumentNull_EntityResource()
        {
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink());

            Assert.ThrowsArgumentNull(
                () => deserializer.ApplyNavigationProperty(entityResource: null, navigationLinkWrapper: navigationLink, readContext: _readContext),
                "entityResource");
        }
示例#21
0
        public void ReadEntry_ThrowsArgumentNull_ReadContext()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);
            ODataEntryWithNavigationLinks entry = new ODataEntryWithNavigationLinks(new ODataEntry());

            Assert.ThrowsArgumentNull(
                () => deserializer.ReadEntry(entry, entityType: _productEdmType, readContext: null),
                "readContext");
        }
示例#22
0
        public void CreateEntityResource_ThrowsArgument_ModelMissingFromReadContext()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgument(
                () => deserializer.CreateEntityResource(_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.");
        }
示例#23
0
        public void ReadInline_Throws_ArgumentMustBeOfType()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.ThrowsArgument(
                () => deserializer.ReadInline(item: 42, edmType: _productEdmType, readContext: new ODataDeserializerContext()),
                "item",
                "The argument must be of type 'ODataEntry'");
        }
示例#24
0
        public void Read_Throws_On_UnknownEntityType()
        {
            IEdmEntityType supplierEntityType = EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);

            Assert.Throws <ODataException>(
                () => deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.SuppliersInsertData), _edmModel), _readContext),
                "An entry with type 'ODataDemo.Supplier' was found, but it is not assignable to the expected type 'ODataDemo.Product'. The type specified in the entry must be equal to either the expected type or a derived type.");
        }
示例#25
0
        private void Read_ThrowsOnUnknownEntityType(string content, bool json, string expectedMessage)
        {
            IEdmEntityType supplierEntityType =
                EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.Throws <ODataException>(() => deserializer.Read(GetODataMessageReader(GetODataMessage(content, json), _edmModel),
                                                                   typeof(Product), _readContext), expectedMessage);
        }
示例#26
0
        public void CreateEntityResource_ThrowsODataException_MappingDoesNotContainEntityType()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.Throws <ODataException>(
                () => deserializer.CreateEntityResource(_productEdmType, new ODataDeserializerContext {
                Model = EdmCoreModel.Instance
            }),
                "The provided mapping doesn't contain an entry for the entity type 'ODataDemo.Product'.");
        }
示例#27
0
        public void Read_ThrowsArgument_EntitysetMissing()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);

            Assert.Throws <SerializationException>(
                () => deserializer.Read(ODataTestUtil.GetMockODataMessageReader(), typeof(Product), new ODataDeserializerContext {
                Path = new ODataPath()
            }),
                "The related entity set could not be found from the OData path. The related entity set is required to deserialize the payload.");
        }
        public void Read_Throws_On_UnknownEntityType()
        {
            IEdmEntityType supplierEntityType = EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);

            Assert.Throws<ODataException>(
                () => deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.SuppliersInsertData), _edmModel), _readContext),
                "An entry with type 'ODataDemo.Supplier' was found, but it is not assignable to the expected type 'ODataDemo.Product'. The type specified in the entry must be equal to either the expected type or a derived type.");
        }
示例#29
0
        public void SetCollectionProperty_CanConvertEnumCollection()
        {
            SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties();

            ODataEntityDeserializer.SetCollectionProperty(value, "FlagsEnum", isDelta: false, value: new List <string> {
                "One", "Four, Two"
            });
            Assert.Equal(
                new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two },
                value.FlagsEnum);
        }
示例#30
0
        public void SetCollectionProperty_CollectionTypeCannotBeInstantiated_And_SettableProperty_Throws(string propertyName)
        {
            object value = new SampleClassWithSettableCollectionProperties();

            Assert.Throws <InvalidOperationException>(
                () => ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.ODataEntryDeserializerTests+SampleClassWithSettableCollectionProperties' returned a null value. " +
                              "The input stream contains collection items which cannot be added if the instance is null.", propertyName));
        }
示例#31
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_WithAddMethod(string propertyName)
        {
            object value = new SampleClassWithNonSettableCollectionProperties();

            ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            });
            Assert.Equal(
                new[] { 1, 2, 3 },
                value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>);
        }
        public void ReadFromStreamAsync()
        {
            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            Product product = deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.ProductInsertData), _edmModel), _readContext) as Product;

            Assert.Equal(product.ID, 0);
            Assert.Equal(product.Rating, 4);
            Assert.Equal(product.Price, 2.5m);
            Assert.Equal(product.ReleaseDate, new DateTime(1992, 1, 1, 0, 0, 0));
            Assert.Null(product.DiscontinuedDate);
        }
示例#33
0
        public void CreateEntityResource_CreatesT_IfNotPatchMode()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model        = _readContext.Model,
                ResourceType = typeof(Product)
            };

            Assert.IsType <Product>(deserializer.CreateEntityResource(_productEdmType, readContext));
        }
示例#34
0
        public void ApplyNavigationProperty_ThrowsODataException_NavigationPropertyNotfound()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink {
                Name = "SomeProperty"
            });

            Assert.Throws <ODataException>(
                () => deserializer.ApplyNavigationProperty(42, navigationLink, _productEdmType, _readContext),
                "Cannot find navigation property 'SomeProperty' on the entity type 'ODataDemo.Product'.");
        }
示例#35
0
        public void SetCollectionProperty_CanConvertNonStandardEdmTypes()
        {
            SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties();

            ODataEntityDeserializer.SetCollectionProperty(value, "UnsignedArray", isDelta: false, value: new List <int> {
                1, 2, 3
            });
            Assert.Equal(
                new uint[] { 1, 2, 3 },
                value.UnsignedArray);
        }
示例#36
0
        public void ReadFromStreamAsync()
        {
            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            Product product = deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.ProductInsertData), _edmModel), _readContext) as Product;

            Assert.Equal(product.ID, 0);
            Assert.Equal(product.Rating, 4);
            Assert.Equal(product.Price, 2.5m);
            Assert.Equal(product.ReleaseDate, new DateTime(1992, 1, 1, 0, 0, 0));
            Assert.Null(product.DiscontinuedDate);
        }
示例#37
0
        public void ReadEntry_ThrowsODataException_EntityTypeNotInModel()
        {
            var deserializer = new ODataEntityDeserializer(_deserializerProvider);
            ODataEntryWithNavigationLinks entry = new ODataEntryWithNavigationLinks(new ODataEntry {
                TypeName = "MissingType"
            });

            Assert.Throws <ODataException>(
                () => deserializer.ReadEntry(entry, _productEdmType, _readContext),
                "Cannot find the entity type 'MissingType' in the model.");
        }
        private void ReadFromStreamAsync(string content, bool json)
        {
            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            Product product = deserializer.Read(GetODataMessageReader(GetODataMessage(content, json), _edmModel),
                _readContext) as Product;

            Assert.Equal(product.ID, 0);
            Assert.Equal(product.Rating, 4);
            Assert.Equal(product.Price, 2.5m);
            Assert.Equal(product.ReleaseDate, new DateTime(1992, 1, 1, 0, 0, 0));
            Assert.Null(product.DiscontinuedDate);
        }
        public void Read_PatchMode()
        {
            IEdmEntityType supplierEntityType = EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;
            _readContext.IsPatchMode = true;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_supplierEdmType, _deserializerProvider);
            Delta<Supplier> supplier = deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.SuppliersPatchData), _edmModel), _readContext) as Delta<Supplier>;

            Assert.NotNull(supplier);
            Assert.Equal(supplier.GetChangedPropertyNames(), new string[] { "Name", "Address" });

            Assert.Equal((supplier as dynamic).Name, "Supplier Name");
            Assert.Equal("Supplier City", (supplier as dynamic).Address.City);
            Assert.Equal("123456", (supplier as dynamic).Address.ZipCode);
        }
        public void ReadFromStreamAsync_ComplexTypeAndInlineData()
        {
            IEdmEntityType supplierEntityType = EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_supplierEdmType, _deserializerProvider);
            Supplier supplier = deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.SuppliersInsertData), _edmModel), _readContext) as Supplier;

            Assert.Equal(supplier.Name, "Supplier Name");

            Assert.NotNull(supplier.Products);
            Assert.Equal(6, supplier.Products.Count);
            Assert.Equal("soda", supplier.Products.ToList()[1].Name);

            Assert.NotNull(supplier.Address);
            Assert.Equal("Supplier City", supplier.Address.City);
            Assert.Equal("123456", supplier.Address.ZipCode);
        }
        public void ReadEntry_ThrowsSerializationException_TypeCannotBeDeserialized()
        {
            Mock<ODataDeserializerProvider> deserializerProvider = new Mock<ODataDeserializerProvider>();
            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny<IEdmTypeReference>())).Returns<ODataEdmTypeDeserializer>(null);
            var deserializer = new ODataEntityDeserializer(_productEdmType, deserializerProvider.Object);
            ODataEntryWithNavigationLinks entry = new ODataEntryWithNavigationLinks(new ODataEntry { TypeName = _supplierEdmType.FullName() });

            Assert.Throws<SerializationException>(
                () => deserializer.ReadEntry(entry, _readContext),
                "'ODataDemo.Supplier' cannot be deserialized using the ODataMediaTypeFormatter.");
        }
 public void CreateEntityResource_ThrowsArgumentNull_ReadContext()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgumentNull(
         () => deserializer.CreateEntityResource(readContext: null),
         "readContext");
 }
        public void ReadEntry_DispatchesToRightDeserializer_IfEntityTypeNameIsDifferent()
        {
            // Arrange
            Mock<ODataEdmTypeDeserializer> supplierDeserializer = new Mock<ODataEdmTypeDeserializer>(_supplierEdmType, ODataPayloadKind.Entry);
            Mock<ODataDeserializerProvider> deserializerProvider = new Mock<ODataDeserializerProvider>();
            var deserializer = new ODataEntityDeserializer(_productEdmType, deserializerProvider.Object);
            ODataEntryWithNavigationLinks entry = new ODataEntryWithNavigationLinks(new ODataEntry { TypeName = _supplierEdmType.FullName() });

            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny<IEdmTypeReference>())).Returns(supplierDeserializer.Object);
            supplierDeserializer.Setup(d => d.ReadInline(entry, _readContext)).Returns(42).Verifiable();

            // Act
            object result = deserializer.ReadEntry(entry, _readContext);

            // Assert
            Assert.Equal(42, result);
            supplierDeserializer.Verify();
        }
 public void CreateEntityResource_ThrowsODataException_MappingDoesNotContainEntityType()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.Throws<ODataException>(
         () => deserializer.CreateEntityResource(new ODataDeserializerContext { Model = EdmCoreModel.Instance }),
         "The provided mapping doesn't contain an entry for the entity type 'ODataDemo.Product'.");
 }
 public void CreateEntityResource_ThrowsArgument_ModelMissingFromReadContext()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgument(
         () => deserializer.CreateEntityResource(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 ApplyStructuralProperty_ThrowsArgumentNull_StructuralProperty()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgumentNull(
         () => deserializer.ApplyStructuralProperty(42, structuralProperty: null, readContext: _readContext),
         "structuralProperty");
 }
        public void CreateEntityResource_CreatesDeltaOfT_IfPatchMode()
        {
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = _readContext.Model,
                IsPatchMode = true,
                PatchEntityType = typeof(Delta<Product>)
            };

            Assert.IsType<Delta<Product>>(deserializer.CreateEntityResource(readContext));
        }
 public void ApplyNavigationProperty_ThrowsArgumentNull_EntityResource()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink());
     Assert.ThrowsArgumentNull(
         () => deserializer.ApplyNavigationProperty(entityResource: null, navigationLinkWrapper: navigationLink, readContext: _readContext),
         "entityResource");
 }
 public void ApplyNavigationProperty_ThrowsArgumentNull_NavigationLinkWrapper()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgumentNull(
         () => deserializer.ApplyNavigationProperty(42, navigationLinkWrapper: null, readContext: _readContext),
         "navigationLinkWrapper");
 }
        public void ApplyNavigationProperty_ThrowsODataException_WhenPatchingCollectionNavigationProperty()
        {
            var deserializer = new ODataEntityDeserializer(_supplierEdmType, _deserializerProvider);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink { Name = "Products" });
            navigationLink.NestedItems.Add(new ODataFeedWithEntries(new ODataFeed()));
            _readContext.IsPatchMode = true;

            Assert.Throws<ODataException>(
                () => deserializer.ApplyNavigationProperty(42, navigationLink, _readContext),
                "Cannot apply PATCH to navigation property 'Products' on entity type 'ODataDemo.Supplier'.");
        }
        public void ApplyNavigationProperty_ThrowsODataException_NavigationPropertyNotfound()
        {
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink { Name = "SomeProperty" });

            Assert.Throws<ODataException>(
                () => deserializer.ApplyNavigationProperty(42, navigationLink, _readContext),
                "Cannot find navigation property 'SomeProperty' on the entity type 'ODataDemo.Product'.");
        }
        public void ApplyNavigationProperty_Calls_ReadInlineOnFeed()
        {
            // Arrange
            IEdmCollectionTypeReference productsType = new EdmCollectionTypeReference(new EdmCollectionType(_productEdmType), isNullable: false);
            Mock<ODataEdmTypeDeserializer> productsDeserializer = new Mock<ODataEdmTypeDeserializer>(productsType, ODataPayloadKind.Feed);
            Mock<ODataDeserializerProvider> deserializerProvider = new Mock<ODataDeserializerProvider>();
            var deserializer = new ODataEntityDeserializer(_supplierEdmType, deserializerProvider.Object);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink { Name = "Products" });
            navigationLink.NestedItems.Add(new ODataFeedWithEntries(new ODataFeed()));

            Supplier supplier = new Supplier();
            IEnumerable products = new[] { new Product { ID = 42 } };

            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny<IEdmTypeReference>())).Returns(productsDeserializer.Object);
            productsDeserializer.Setup(d => d.ReadInline(navigationLink.NestedItems[0], _readContext)).Returns(products).Verifiable();

            // Act
            deserializer.ApplyNavigationProperty(supplier, navigationLink, _readContext);

            // Assert
            productsDeserializer.Verify();
            Assert.Equal(1, supplier.Products.Count());
            Assert.Equal(42, supplier.Products.First().ID);
        }
        public void ApplyNavigationProperty_Calls_ReadInlineOnEntry()
        {
            // Arrange
            Mock<ODataEdmTypeDeserializer> supplierDeserializer = new Mock<ODataEdmTypeDeserializer>(_supplierEdmType, ODataPayloadKind.Feed);
            Mock<ODataDeserializerProvider> deserializerProvider = new Mock<ODataDeserializerProvider>();
            var deserializer = new ODataEntityDeserializer(_productEdmType, deserializerProvider.Object);
            ODataNavigationLinkWithItems navigationLink = new ODataNavigationLinkWithItems(new ODataNavigationLink { Name = "Supplier" });
            navigationLink.NestedItems.Add(new ODataEntryWithNavigationLinks(new ODataEntry()));

            Product product = new Product();
            Supplier supplier = new Supplier { ID = 42 };

            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny<IEdmTypeReference>())).Returns(supplierDeserializer.Object);
            supplierDeserializer.Setup(d => d.ReadInline(navigationLink.NestedItems[0], _readContext)).Returns(supplier).Verifiable();

            // Act
            deserializer.ApplyNavigationProperty(product, navigationLink, _readContext);

            // Assert
            supplierDeserializer.Verify();
            Assert.Equal(supplier, product.Supplier);
        }
 public void ApplyStructuralProperties_ThrowsArgumentNull_entryWrapper()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.ThrowsArgumentNull(
         () => deserializer.ApplyStructuralProperties(42, entryWrapper: null, readContext: _readContext),
         "entryWrapper");
 }
        public void ReadEntry_ThrowsODataException_CannotInstantiateAbstractEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.Entity<BaseType>().Abstract();
            IEdmModel model = builder.GetEdmModel();
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            ODataEntryWithNavigationLinks entry = new ODataEntryWithNavigationLinks(new ODataEntry { TypeName = "System.Web.Http.OData.Formatter.Deserialization.BaseType" });

            Assert.Throws<ODataException>(
                () => deserializer.ReadEntry(entry, new ODataDeserializerContext { Model = model }),
                "An instance of the abstract entity type 'System.Web.Http.OData.Formatter.Deserialization.BaseType' was found. Abstract entity types cannot be instantiated.");
        }
        public void CreateEntityResource_CreatesT_IfNotPatchMode()
        {
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model = _readContext.Model,
                IsPatchMode = false
            };

            Assert.IsType<Product>(deserializer.CreateEntityResource(readContext));
        }
        public void ApplyStructuralProperty_SetsProperty()
        {
            // Arrange
            var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
            Product product = new Product();
            ODataProperty property = new ODataProperty { Name = "ID", Value = 42 };

            // Act
            deserializer.ApplyStructuralProperty(product, property, _readContext);

            // Assert
            Assert.Equal(42, product.ID);
        }
        private void Read_ThrowsOnUnknownEntityType(string content, bool json, string expectedMessage)
        {
            IEdmEntityType supplierEntityType =
                EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);

            Assert.Throws<ODataException>(() => deserializer.Read(GetODataMessageReader(GetODataMessage(content, json),
                _edmModel), _readContext), expectedMessage);
        }
        public void Read_PatchMode_PatchKeyModeIsThrow()
        {
            IEdmEntityType supplierEntityType = EdmTestHelpers.GetModel().FindType("ODataDemo.Supplier") as IEdmEntityType;
            _readContext.IsPatchMode = true;
            _readContext.PatchKeyMode = PatchKeyMode.Throw;

            ODataEntityDeserializer deserializer = new ODataEntityDeserializer(_supplierEdmType, _deserializerProvider);

            Assert.Throws<InvalidOperationException>(
                () => deserializer.Read(GetODataMessageReader(GetODataMessage(BaselineResource.SuppliersPatchData), _edmModel), _readContext),
                "Cannot apply PATCH on key property 'ID' on entity type 'ODataDemo.Supplier' when 'PatchKeyMode' is 'Throw'.");
        }
 public void Ctor_SetsProperty_EntityType()
 {
     var deserializer = new ODataEntityDeserializer(_productEdmType, _deserializerProvider);
     Assert.Equal(_productEdmType, deserializer.EntityType);
 }