Beispiel #1
0
        public ODataSingletonDeserializerTest()
        {
            EdmModel model        = new EdmModel();
            var      employeeType = new EdmEntityType("NS", "Employee");

            employeeType.AddStructuralProperty("EmployeeId", EdmPrimitiveTypeKind.Int32);
            employeeType.AddStructuralProperty("EmployeeName", EdmPrimitiveTypeKind.String);
            model.AddElement(employeeType);

            EdmEntityContainer defaultContainer = new EdmEntityContainer("NS", "Default");

            model.AddElement(defaultContainer);

            _singleton = new EdmSingleton(defaultContainer, "CEO", employeeType);
            defaultContainer.AddElement(_singleton);

            model.SetAnnotationValue <ClrTypeAnnotation>(employeeType, new ClrTypeAnnotation(typeof(EmployeeModel)));

            _edmModel = model;

            _readContext = new ODataDeserializerContext
            {
                Path         = new ODataPath(new SingletonSegment(_singleton)),
                Model        = _edmModel,
                ResourceType = typeof(EmployeeModel)
            };

            _deserializerProvider = ODataDeserializerProviderFactory.Create();
        }
 static ODataActionPayloadDeserializerTest()
 {
     _model                = GetModel();
     _container            = _model.EntityContainer;
     _deserializerProvider = ODataDeserializerProviderFactory.Create();
     _deserializer         = new ODataActionPayloadDeserializer(_deserializerProvider);
 }
Beispiel #3
0
        public async Task Posting_NonDerivedType_To_Action_Expecting_BaseType_Throws()
        {
            // Arrange
            StringContent        content      = new StringContent("{ '@odata.type' : '#Microsoft.AspNet.OData.Test.Builder.TestModels.Motorcycle' }");
            var                  headers      = FormatterTestHelper.GetContentHeaders("application/json");
            IODataRequestMessage oDataRequest = ODataMessageWrapperHelper.Create(await content.ReadAsStreamAsync(), headers);
            ODataMessageReader   reader       = new ODataMessageReader(oDataRequest, new ODataMessageReaderSettings(), _model);

            ODataDeserializerProvider deserializerProvider = ODataDeserializerProviderFactory.Create();

            ODataDeserializerContext context = new ODataDeserializerContext {
                Model = _model
            };
            IEdmActionImport action = _model.EntityContainer
                                      .OperationImports()
                                      .Single(f => f.Name == "PostMotorcycle_When_Expecting_Car") as IEdmActionImport;

            Assert.NotNull(action);
            IEdmEntitySetBase actionEntitySet;

            action.TryGetStaticEntitySet(_model, out actionEntitySet);
            context.Path = new ODataPath(new OperationImportSegment(new[] { action }, actionEntitySet, null));

            // Act & Assert
            ExceptionAssert.Throws <ODataException>(
                () => new ODataResourceDeserializer(deserializerProvider).Read(reader, typeof(Car), context),
                "A resource with type 'Microsoft.AspNet.OData.Test.Builder.TestModels.Motorcycle' was found, " +
                "but it is not assignable to the expected type 'Microsoft.AspNet.OData.Test.Builder.TestModels.Car'. " +
                "The type specified in the resource must be equal to either the expected type or a derived type.");
        }
        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 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"]);
        }
Beispiel #6
0
 public ODataResourceSetDeserializerTest()
 {
     _model                = GetEdmModel();
     _customerType         = _model.GetEdmTypeReference(typeof(Customer)).AsEntity();
     _customersType        = new EdmCollectionTypeReference(new EdmCollectionType(_customerType));
     _serializerProvider   = ODataSerializerProviderFactory.Create();
     _deserializerProvider = ODataDeserializerProviderFactory.Create();
 }
        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();
        }
Beispiel #9
0
        public EnableNestedPathsTest()
        {
            _deserializerProvider    = ODataDeserializerProviderFactory.Create();
            _resourceSetDeserializer = new ODataResourceSetDeserializer(_deserializerProvider);
            _resourceDeserializer    = new ODataResourceDeserializer(_deserializerProvider);
            _primitiveDeserializer   = new ODataPrimitiveDeserializer();

            ODataModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <EnableNestedPathsCustomer>("EnableNestedPathsCustomers");
            builder.EntitySet <EnableNestedPathsProduct>("EnableNestedPathsProducts");
            builder.Singleton <EnableNestedPathsCustomer>("EnableNestedPathsTopCustomer");
            builder.EntityType <EnableNestedPathsVipCustomer>();

            builder.EntityType <EnableNestedPathsVipCustomer>()
            .Function("GetMostPurchasedProduct")
            .ReturnsFromEntitySet <EnableNestedPathsProduct>("EnableNestedPathsProduct");

            builder.EntityType <EnableNestedPathsProduct>()
            .Collection
            .Action("SetDiscountRate");

            _model = builder.GetEdmModel();

            Type[] controllers = new Type[] {
                typeof(EnableNestedPathsCustomersController),
                typeof(EnableNestedPathsProductsController),
                typeof(EnableNestedPathsTopCustomerController)
            };

            _server = TestServerFactory.Create(controllers, (config) =>
            {
                config.MapODataServiceRoute("odata", "odata", _model);
                config.Count().OrderBy().Filter().Expand().MaxTop(null).Select();
            });

            _client = TestServerFactory.CreateClient(_server);

            _db = new EnableNestedPathsDatabase();
        }