public void ApplyProperty_DoesNotIgnoreKeyProperty_WithInstanceAnnotation()
        {
            // Arrange
            ODataProperty property = new ODataProperty {
                Name = "Key1", Value = "Value1"
            };
            EdmEntityType entityType = new EdmEntityType("namespace", "name");

            entityType.AddKeys(entityType.AddStructuralProperty("Key1",
                                                                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string))));

            EdmEntityTypeReference    entityTypeReference = new EdmEntityTypeReference(entityType, isNullable: false);
            ODataDeserializerProvider provider            = ODataDeserializerProviderFactory.Create();

            var  resource     = new Mock <IDelta>(MockBehavior.Strict);
            Type propertyType = typeof(string);

            resource.Setup(r => r.TryGetPropertyType("Key1", out propertyType)).Returns(true).Verifiable();
            resource.Setup(r => r.TrySetPropertyValue("Key1", "Value1")).Returns(true).Verifiable();

            // Act
            DeserializationHelpers.ApplyInstanceAnnotations(resource.Object, entityTypeReference, null, provider,
                                                            new ODataDeserializerContext {
                Model = new EdmModel()
            });

            DeserializationHelpers.ApplyProperty(property, entityTypeReference, resource.Object, provider,
                                                 new ODataDeserializerContext {
                Model = new EdmModel()
            });

            // Assert
            resource.Verify();
        }
        public void ApplyAnnotations_FailsWithUsefulErrorMessageOnUnknownProperty()
        {
            // Arrange
            const string HelpfulErrorMessage =
                "The property 'Unknown' does not exist on type 'namespace.name'. Make sure to only use property names " +
                "that are defined by the type.";

            var property = new ODataProperty {
                Name = "Unknown", Value = "Value"
            };
            var entityType = new EdmComplexType("namespace", "name");

            entityType.AddStructuralProperty("Known", EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)));

            var entityTypeReference = new EdmComplexTypeReference(entityType, isNullable: false);

            // Act
            var exception = Assert.Throws <ODataException>(() =>
                                                           DeserializationHelpers.ApplyProperty(
                                                               property,
                                                               entityTypeReference,
                                                               resource: null,
                                                               deserializerProvider: null,
                                                               readContext: null));

            // Assert
            Assert.Equal(HelpfulErrorMessage, exception.Message);
        }
        public void ApplyProperty_UntypedValueOfTypeStringUnescaped(char token, string tokenEscaped)
        {
            // Arrange
            var escapedValue  = "\"Update of memo" + tokenEscaped + "4" + tokenEscaped + "40\"";
            var propertyValue = new ODataUntypedValue {
                RawValue = escapedValue
            };
            var property = new ODataProperty {
                Name = "Memo", Value = propertyValue
            };

            EdmEntityType openType = new EdmEntityType("NS", "OpenType", null, false, true);

            openType.AddKeys(openType.AddStructuralProperty("Id",
                                                            EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int))));
            EdmEntityTypeReference    openTypeReference = new EdmEntityTypeReference(openType, isNullable: false);
            ODataDeserializerProvider provider          = ODataDeserializerProviderFactory.Create();

            var resource = new OpenTypeTestClass();
            var model    = new EdmModel();

            model.AddElement(openType);
            model.SetAnnotationValue(openType, new DynamicPropertyDictionaryAnnotation(
                                         typeof(OpenTypeTestClass).GetProperty("DynamicProperties")));

            // Act
            DeserializationHelpers.ApplyProperty(property, openTypeReference, resource, provider,
                                                 new ODataDeserializerContext {
                Model = model
            });

            // Assert
            Assert.True(resource.DynamicProperties.ContainsKey("Memo"));
            Assert.Equal("Update of memo" + token + "4" + token + "40", resource.DynamicProperties["Memo"]);
        }
        public void ApplyProperty_FailWithCaseInsensitiveMatchesAndDisabledCaseSensitiveRequestPropertyBinding()
        {
            const string expectedErrorMessage = "The property 'proPerty1' does not exist on type 'namespace.name'. Make sure to only use property names that are defined by the type.";
            // Arrange
            ODataProperty property = new ODataProperty {
                Name = "proPerty1", Value = "Value1"
            };
            EdmEntityType entityType = new EdmEntityType("namespace", "name");

            entityType.AddStructuralProperty("Property1", EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)));
            entityType.AddKeys(entityType.AddStructuralProperty("Key1",
                                                                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string))));

            EdmEntityTypeReference    entityTypeReference = new EdmEntityTypeReference(entityType, isNullable: false);
            ODataDeserializerProvider provider            = ODataDeserializerProviderFactory.Create();

#if NETCORE
            IRouteBuilder builder = RoutingConfigurationFactory.CreateWithDisabledCaseInsensitiveRequestPropertyBinding();

            HttpRequest request = RequestFactory.Create(builder);
#else
            HttpConfiguration configuration = RoutingConfigurationFactory.CreateWithRootContainer("OData");
            configuration.SetCompatibilityOptions(CompatibilityOptions.DisableCaseInsensitiveRequestPropertyBinding);
            HttpRequestMessage request = RequestFactory.Create(configuration);
#endif

            ODataDeserializerContext context = new ODataDeserializerContext
            {
                Model   = new EdmModel(),
                Request = request
            };

            // Act
            var exception = Assert.Throws <ODataException>(() =>
                                                           DeserializationHelpers.ApplyProperty(
                                                               property,
                                                               entityTypeReference,
                                                               resource: null,
                                                               provider,
                                                               context));

            // Assert
            Assert.Equal(expectedErrorMessage, exception.Message);
        }
        public void ApplyProperty_PassesWithCaseInsensitivePropertyName()
        {
            // Arrange
            ODataProperty property = new ODataProperty {
                Name = "keY1", Value = "Value1"
            };
            EdmEntityType entityType = new EdmEntityType("namespace", "name");

            entityType.AddKeys(entityType.AddStructuralProperty("Key1",
                                                                EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string))));

            EdmEntityTypeReference    entityTypeReference = new EdmEntityTypeReference(entityType, isNullable: false);
            ODataDeserializerProvider provider            = ODataDeserializerProviderFactory.Create();

            var  resource     = new Mock <IDelta>(MockBehavior.Strict);
            Type propertyType = typeof(string);

            resource.Setup(r => r.TryGetPropertyType("Key1", out propertyType)).Returns(true).Verifiable();
            resource.Setup(r => r.TrySetPropertyValue("Key1", "Value1")).Returns(true).Verifiable();

#if NETCORE
            IRouteBuilder builder = RoutingConfigurationFactory.Create();

            HttpRequest request = RequestFactory.Create(builder);
#else
            HttpConfiguration  configuration = RoutingConfigurationFactory.CreateWithRootContainer("OData");
            HttpRequestMessage request       = RequestFactory.Create(configuration);
#endif

            ODataDeserializerContext context = new ODataDeserializerContext
            {
                Model   = new EdmModel(),
                Request = request
            };

            // Act
            DeserializationHelpers.ApplyProperty(property, entityTypeReference, resource.Object, provider,
                                                 context);

            // Assert
            resource.Verify();
        }