Пример #1
0
        public void InitTest()
        {
            this.host = new DataServiceHost2Simulator();
            this.host.AbsoluteServiceUri = new Uri("http://service/");
            this.host.AbsoluteServiceUri = new Uri("http://service/set1");

            var providerSimulator = new DataServiceProviderSimulator();
            this.service = new DataServiceSimulator
            {
                OperationContext = new DataServiceOperationContext(false, this.host),
                Configuration = new DataServiceConfiguration(providerSimulator)
            };

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(this.service.Instance.GetType(), providerSimulator);
            IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            this.service.ProcessingPipeline = new DataServiceProcessingPipeline();
            this.service.Provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    this.service.Configuration, 
                    staticConfiguration), 
                providerSimulator, 
                providerSimulator, 
                this.service,
                false);
            this.service.Provider.ProviderBehavior = providerBehavior;
            this.service.OperationContext.InitializeAndCacheHeaders(this.service);
        }
        public void UrlConventionIntegrationTest()
        {
            var host = new DataServiceHost2Simulator
                       {
                           RequestHeaders = new WebHeaderCollection { { UrlConventionsConstants.UrlConventionHeaderName, "KeyAsSegment" } },
                       };
            var service = new DataServiceSimulator
            {
                Configuration = new DataServiceConfiguration(new DataServiceProviderSimulator()), 
                OperationContext = new DataServiceOperationContext(host)
            };

            service.OperationContext.InitializeAndCacheHeaders(service);

            service.Configuration.DataServiceBehavior.UrlConventions = DataServiceUrlConventions.KeyAsSegment;
            UrlConvention.Create(service).GenerateKeyAsSegment.Should().BeTrue();
        }
        public void Initialize()
        {
            this.host = new DataServiceHost2Simulator();

            var context = new DataServiceOperationContext(this.host);
            this.dataServiceSimulator = new DataServiceSimulator { OperationContext = context };

            var providerSimulator = new DataServiceProviderSimulator();

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(this.dataServiceSimulator.Instance.GetType(), providerSimulator);
            IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "SelectTestNamespace", "Fake", false) { CanReflectOnInstanceType = false, IsOpenType = true };
            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false });
            var resourceSet = new ResourceSet("FakeSet", resourceType);
            resourceSet.SetReadOnly();

            providerSimulator.AddResourceSet(resourceSet);

            var configuration = new DataServiceConfiguration(providerSimulator);
            configuration.SetEntitySetAccessRule("*", EntitySetRights.All);

            var provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    configuration,
                    staticConfiguration), 
                providerSimulator, 
                providerSimulator, 
                this.dataServiceSimulator,
                false);

            this.dataServiceSimulator.ProcessingPipeline = new DataServiceProcessingPipeline();
            this.dataServiceSimulator.Provider = provider;
            provider.ProviderBehavior = providerBehavior;
            this.dataServiceSimulator.Configuration = new DataServiceConfiguration(providerSimulator);
            this.dataServiceSimulator.Configuration.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            this.responseMessageSimulator = new ODataResponseMessageSimulator();
        }
Пример #4
0
        private static DataServiceProviderWrapper CreateProvider(out DataServiceConfiguration config, out DataServiceOperationContext operationContext)
        {
            var baseUri = new Uri("http://localhost");
            var host = new DataServiceHostSimulator()
            {
                AbsoluteServiceUri = baseUri,
                AbsoluteRequestUri = new Uri(baseUri.AbsoluteUri + "/$metadata", UriKind.Absolute),
                RequestHttpMethod = "GET",
                RequestAccept = "application/xml+atom",
                RequestVersion = "4.0",
                RequestMaxVersion = "4.0",
            };

            operationContext = new DataServiceOperationContext(host);
            var dataService = new DataServiceSimulator() { OperationContext = operationContext };
            operationContext.InitializeAndCacheHeaders(dataService);

            DataServiceProviderSimulator providerSimulator = new DataServiceProviderSimulator();
            providerSimulator.ContainerNamespace = "MyModel";
            providerSimulator.ContainerName = "CustomersContainer";

            ResourceType customerEntityType = new ResourceType(
                typeof(object), ResourceTypeKind.EntityType, null, "MyModel", "Customer", false)
            { 
                CanReflectOnInstanceType = false 
            };

            ResourcePropertyKind idPropertyKind = ResourcePropertyKind.Primitive | ResourcePropertyKind.Key;
            ResourceProperty idProperty = new ResourceProperty(
                "Id", idPropertyKind, ResourceType.GetPrimitiveResourceType(typeof(int)))
            { 
                CanReflectOnInstanceTypeProperty = false 
            };
            customerEntityType.AddProperty(idProperty);

            ResourcePropertyKind firstNamePropertyKind = ResourcePropertyKind.Primitive | ResourcePropertyKind.Key;
            ResourceProperty firstNameProperty = new ResourceProperty(
                "FirstName", firstNamePropertyKind, ResourceType.GetPrimitiveResourceType(typeof(string)))
            {
                CanReflectOnInstanceTypeProperty = false
            };
            customerEntityType.AddProperty(firstNameProperty);            
            
            customerEntityType.SetReadOnly();
            providerSimulator.AddResourceType(customerEntityType);

            ResourceSet customerSet = new ResourceSet("Customers", customerEntityType);
            customerSet.SetReadOnly();
            providerSimulator.AddResourceSet(customerSet);

            config = new DataServiceConfiguration(providerSimulator);
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;
            DataServiceStaticConfiguration staticConfig = new DataServiceStaticConfiguration(dataService.Instance.GetType(), providerSimulator);

            DataServiceProviderWrapper provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(config, staticConfig), providerSimulator, providerSimulator, dataService, false);

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            dataService.Provider = provider;
            provider.ProviderBehavior = providerBehavior;
            dataService.ActionProvider = DataServiceActionProviderWrapper.Create(dataService);
#if DEBUG
            dataService.ProcessingPipeline.SkipDebugAssert = true;
#endif
            operationContext.RequestMessage.InitializeRequestVersionHeaders(VersionUtil.ToVersion(config.DataServiceBehavior.MaxProtocolVersion));
            return provider;
        }
        public void GetTargetSetTestsSetBindingTypeShouldPerformCorrectValidation()
        {
            ResourceType actorEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "foo", "Actor", false) { CanReflectOnInstanceType = false };
            ResourceProperty idProperty = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false };
            actorEntityType.AddProperty(idProperty);

            ResourceType addressComplexType = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "foo", "Address", false) { CanReflectOnInstanceType = false };
            addressComplexType.AddProperty(new ResourceProperty("StreetAddress", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(string))) { CanReflectOnInstanceTypeProperty = false });
            addressComplexType.AddProperty(new ResourceProperty("ZipCode", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false });

            actorEntityType.AddProperty(new ResourceProperty("PrimaryAddress", ResourcePropertyKind.ComplexType, addressComplexType) { CanReflectOnInstanceTypeProperty = false });
            actorEntityType.AddProperty(new ResourceProperty("OtherAddresses", ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(addressComplexType)) { CanReflectOnInstanceTypeProperty = false });

            ResourceType movieEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "foo", "Movie", false) { CanReflectOnInstanceType = false };
            movieEntityType.AddProperty(idProperty);

            ResourceProperty moviesNavProp = new ResourceProperty("Movies", ResourcePropertyKind.ResourceSetReference, movieEntityType) { CanReflectOnInstanceTypeProperty = false };
            actorEntityType.AddProperty(moviesNavProp);
            ResourceProperty actorsNavProp = new ResourceProperty("Actors", ResourcePropertyKind.ResourceSetReference, actorEntityType) { CanReflectOnInstanceTypeProperty = false };
            movieEntityType.AddProperty(actorsNavProp);

            ResourceType derivedActorEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, actorEntityType, "foo", "DerivedActor", false) { CanReflectOnInstanceType = false };
            ResourceType derivedMovieEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, movieEntityType, "foo", "DerivedMovie", false) { CanReflectOnInstanceType = false };

            actorEntityType.SetReadOnly();
            derivedActorEntityType.SetReadOnly();
            movieEntityType.SetReadOnly();
            derivedMovieEntityType.SetReadOnly();
            addressComplexType.SetReadOnly();
            DataServiceProviderSimulator providerSimulator = new DataServiceProviderSimulator();
            providerSimulator.AddResourceType(actorEntityType);
            providerSimulator.AddResourceType(derivedActorEntityType);
            providerSimulator.AddResourceType(movieEntityType);
            providerSimulator.AddResourceType(derivedMovieEntityType);
            providerSimulator.AddResourceType(addressComplexType);

            ResourceSet actorSet = new ResourceSet("Actors", actorEntityType);
            ResourceSet movieSet = new ResourceSet("Movies", movieEntityType);
            actorSet.SetReadOnly();
            movieSet.SetReadOnly();
            providerSimulator.AddResourceSet(actorSet);
            providerSimulator.AddResourceSet(movieSet);

            providerSimulator.AddResourceAssociationSet(new ResourceAssociationSet("Actors_Movies", new ResourceAssociationSetEnd(actorSet, actorEntityType, moviesNavProp), new ResourceAssociationSetEnd(movieSet, movieEntityType, actorsNavProp)));

            DataServiceConfiguration config = new DataServiceConfiguration(providerSimulator);
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            var dataService = new DataServiceSimulator()
            {
                OperationContext = new DataServiceOperationContext(new DataServiceHostSimulator())
            };

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(dataService.Instance.GetType(), providerSimulator);
            IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            DataServiceProviderWrapper provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    config, 
                    staticConfiguration), 
                providerSimulator, 
                providerSimulator, 
                dataService,
                false);
            dataService.Provider = provider;
            provider.ProviderBehavior = providerBehavior;

            var testCases = new[]
            {
                new
                {
                    AppendParameterName = true,
                    Path = "",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Movies",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Movies/Actors/Movies",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/foo.DerivedActor/Movies/foo.DerivedMovie/Actors/foo.DerivedActor/Movies",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/' is not a valid expression because it contains an empty segment or it ends with '/'."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Movies/",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Movies/' is not a valid expression because it contains an empty segment or it ends with '/'."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Movies//Actors",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = "The path expression '{0}/Movies//Actors' is not a valid expression because it contains an empty segment or it ends with '/'."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/foo.DerivedActor",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/foo.DerivedActor' is not a valid expression because it ends with the type identifier 'foo.DerivedActor'. A valid path expression must not end in a type identifier."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Movies/foo.DerivedMovie",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Movies/foo.DerivedMovie' is not a valid expression because it ends with the type identifier 'foo.DerivedMovie'. A valid path expression must not end in a type identifier."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Foo",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Foo' is not a valid expression because the segment 'Foo' is not a type identifier or a property on the resource type 'foo.Actor'."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/ID",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/ID' is not a valid expression because the segment 'ID' is a property of type 'Edm.Int32'. A valid path expression must only contain properties of entity type."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/OtherAddresses",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/OtherAddresses' is not a valid expression because the segment 'OtherAddresses' is a property of type 'Collection(foo.Address)'. A valid path expression must only contain properties of entity type."
                },
                new
                {
                    AppendParameterName = true,
                    Path = "/Movies/Actors/PrimaryAddress",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Movies/Actors/PrimaryAddress' is not a valid expression because the segment 'PrimaryAddress' is a property of type 'foo.Address'. A valid path expression must only contain properties of entity type."
                },
                new
                {
                    AppendParameterName = false,
                    Path = "foo",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'foo' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
                new
                {
                    AppendParameterName = false,
                    Path = "abc/pqr",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'abc/pqr' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
                new
                {
                    AppendParameterName = false,
                    Path = "actorParameter",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'actorParameter' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
                new
                {
                    AppendParameterName = false,
                    Path = "actorsParameter",
                    BindingSet = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'actorsParameter' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
            };

            ServiceActionParameter actorParameter = new ServiceActionParameter("actor", actorEntityType);
            ServiceActionParameter actorsParameter = new ServiceActionParameter("actors", ResourceType.GetEntityCollectionResourceType(actorEntityType));
            var parameters = new ServiceActionParameter[] { actorParameter, actorsParameter };

            foreach (var parameter in parameters)
            {
                foreach (var testCase in testCases)
                {
                    string pathString = testCase.AppendParameterName ? parameter.Name + testCase.Path : testCase.Path;
                    var path = new ResourceSetPathExpression(pathString);
                    Assert.AreEqual(pathString, path.PathExpression);
                    string expectedErrorMessage = testCase.ErrorMessage == null ? null : string.Format(testCase.ErrorMessage, parameter.Name);
                    try
                    {
                        path.SetBindingParameter(parameter);
                        path.InitializePathSegments(provider);
                        var targetSet = path.GetTargetSet(provider, testCase.BindingSet);
                        Assert.IsNull(expectedErrorMessage, "Expecting exception but received none.");
                        Assert.AreEqual(targetSet.Name, testCase.TargetSet.Name);
                    }
                    catch (InvalidOperationException e)
                    {
                        Assert.AreEqual(expectedErrorMessage, e.Message);
                    }
                }
            }
        }
Пример #6
0
        private static void RunNegotiatedFormatTest(string requestAccept, string requestMaxVersion, Microsoft.OData.Client.ODataProtocolVersion maxProtocolVersion, ODataFormat expectedFormat)
        {
            DataServiceHostSimulator host = new DataServiceHostSimulator
            {
                RequestHttpMethod = "GET",
                RequestAccept = requestAccept,
                RequestMaxVersion = requestMaxVersion,
                RequestVersion = "4.0",
            };
            DataServiceSimulator service = new DataServiceSimulator
            {
                OperationContext = new DataServiceOperationContext(host),
                Configuration = new DataServiceConfiguration(new DataServiceProviderSimulator()),
            };

            service.Configuration.DataServiceBehavior.MaxProtocolVersion = maxProtocolVersion;
            service.OperationContext.InitializeAndCacheHeaders(service);
            service.OperationContext.RequestMessage.InitializeRequestVersionHeaders(VersionUtil.ToVersion(maxProtocolVersion));

            var d = new RequestDescription(RequestTargetKind.Primitive, RequestTargetSource.Property, new Uri("http://temp.org/"));

            d.DetermineWhetherResponseBodyOrETagShouldBeWritten(service.OperationContext.RequestMessage.HttpVerb);
            d.DetermineWhetherResponseBodyShouldBeWritten(service.OperationContext.RequestMessage.HttpVerb);
            d.DetermineResponseFormat(service);
            d.ResponseFormat.Should().NotBeNull();
            d.ResponseFormat.Format.Should().BeSameAs(expectedFormat);
        }
        private static IEdmEntityType CreateModelAndGetEdmType(ResourceType resourceType)
        {
            var provider = new DataServiceProviderSimulator();
            DataServiceSimulator dataService = new DataServiceSimulator();

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(dataService.Instance.GetType(), provider);
            IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            DataServiceProviderWrapper providerWrapper = new DataServiceProviderWrapper(
                    new DataServiceCacheItem(
                        new DataServiceConfiguration(provider),
                        staticConfiguration),
                    provider,
                    provider,
                    dataService,
                    false);

            dataService.Provider = providerWrapper;
            var model = new MetadataProviderEdmModel(providerWrapper, new DataServiceStreamProviderWrapper(dataService), DataServiceActionProviderWrapper.Create(dataService));

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            model.MetadataProvider.ProviderBehavior = providerBehavior;

            var edmType = model.EnsureSchemaType(resourceType) as IEdmEntityType;
            return edmType;
        }
Пример #8
0
        private DataServiceProviderWrapper CreateProviderWrapper(DataServiceConfiguration config, bool hasReflectionOrEFProviderQueryBehavior)
        {
            IDataServiceMetadataProvider provider;
            if (hasReflectionOrEFProviderQueryBehavior)
            {
                provider = new ReflectionDataServiceProvider(new object(), new object());
            }
            else
            {
                provider = new DataServiceProviderSimulator();
            }

            var dataService = new DataServiceSimulator();

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(dataService.Instance.GetType(), provider);
            IDataServiceProviderBehavior providerBehavior = hasReflectionOrEFProviderQueryBehavior ? new DataServiceProviderBehavior(new ProviderBehavior(ProviderQueryBehaviorKind.ReflectionProviderQueryBehavior)) : DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            var providerWrapper = new DataServiceProviderWrapper(
                new DataServiceCacheItem(config, staticConfiguration),
                provider,
                (IDataServiceQueryProvider)provider,
                dataService,
                hasReflectionOrEFProviderQueryBehavior ? true : false);

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            dataService.Provider = providerWrapper;
            typeof(DataServiceProviderWrapper).GetField("providerBehavior", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(providerWrapper, providerBehavior);
#if DEBUG
            dataService.ProcessingPipeline.SkipDebugAssert = true;
#endif
            return providerWrapper;
        }