public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndBindingType() { var entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false) { CanReflectOnInstanceType = false }; entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))) {CanReflectOnInstanceTypeProperty = false}); entityType.SetReadOnly(); var resourceSet = new ResourceSet("MyEntitySet", entityType); resourceSet.SetReadOnly(); ResourceType stringType = ResourceType.GetPrimitiveResourceType(typeof(string)); var duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType), new ServiceActionParameter("p2", stringType) }, null); duplicateAction1.SetReadOnly(); var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", entityType) }, null); duplicateAction2.SetReadOnly(); var actionProvider = new TestActionProvider { GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 } }; var providerWrapper = CreateProviderWrapper(actionProvider, p => p.AddResourceSet(resourceSet)); Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList(); getVisibleOperations.ShouldThrow<DataServiceException>() .WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate")) .And.StatusCode.Should().Be(500); }
public void MetadataProviderModelShouldGoDirectlyToProviderWhenLookingUpOperationsInSelect() { var entityType = CreateResourceTypeWithKeyProperties("Id"); var derivedType = CreateDerivedType("Derived", entityType); var veryDerivedType = CreateDerivedType("VeryDerived", derivedType); var unrelatedDerivedType = CreateDerivedType("UnrelatedDerived", entityType); var action1 = new ServiceAction("Action", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType) }); var action2 = new ServiceAction("Action", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", derivedType) }); var actionWithDifferentName = new ServiceAction("Action2", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", derivedType) }); var action3 = new ServiceAction("Action", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", veryDerivedType) }); action1.SetReadOnly(); action2.SetReadOnly(); actionWithDifferentName.SetReadOnly(); action3.SetReadOnly(); int calls = 0; var actionProvider = new TestActionProvider { GetByBindingTypeCallback = (o, t) => { calls++; if (t == entityType) { return(new[] { action1 }); } if (t == derivedType) { return(new[] { action2, actionWithDifferentName }); } if (t == veryDerivedType) { return(new[] { action3 }); } t.Should().NotBeSameAs(unrelatedDerivedType); return(null); } }; var metadataProvider = new DataServiceProviderSimulator(); metadataProvider.AddResourceType(entityType); metadataProvider.AddResourceType(derivedType); metadataProvider.AddResourceType(veryDerivedType); metadataProvider.AddResourceType(unrelatedDerivedType); var model = CreateMetadataProviderEdmModel(metadataProvider, actionProvider); var schemaType = (IEdmEntityType)model.EnsureSchemaType(derivedType); var result = model.FindDeclaredBoundOperations(schemaType).FilterByName(false, "Action"); result.Should().NotBeNull(); result.Should().HaveCount(3); result.Should().OnlyContain(f => f.Name == "Action"); calls.Should().Be(3); }
private static DataServiceProviderWrapper CreateProviderWrapper(TestActionProvider actionProvider = null, Action <DataServiceProviderSimulator> addMetadata = null) { var provider = new DataServiceProviderSimulator(); if (addMetadata != null) { addMetadata(provider); } var dataService = new DataServiceSimulatorWithGetService(); dataService.Configuration = new DataServiceConfiguration(provider); dataService.ProcessingPipeline = new DataServiceProcessingPipeline(); dataService.Configuration.SetServiceActionAccessRule("*", ServiceActionRights.Invoke); dataService.Configuration.SetEntitySetAccessRule("*", EntitySetRights.All); dataService.Configuration.SetServiceOperationAccessRule("*", ServiceOperationRights.All); dataService.Configuration.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4; dataService.OperationContext = new DataServiceOperationContext(false, new DataServiceHost2Simulator { AbsoluteRequestUri = new Uri("http://fake.org/$metadata") }); dataService.OperationContext.InitializeAndCacheHeaders(dataService); DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(dataService.Instance.GetType(), provider); IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior; var providerWrapper = new DataServiceProviderWrapper( new DataServiceCacheItem( dataService.Configuration, staticConfiguration), provider, provider, dataService, false); dataService.Provider = providerWrapper; providerWrapper.ProviderBehavior = providerBehavior; dataService.ActionProvider = DataServiceActionProviderWrapper.Create(dataService); if (actionProvider != null) { dataService.Providers.Add(typeof(IDataServiceActionProvider), actionProvider); } return(providerWrapper); }
public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndBindingType() { var entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false) { CanReflectOnInstanceType = false }; entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false }); entityType.SetReadOnly(); var resourceSet = new ResourceSet("MyEntitySet", entityType); resourceSet.SetReadOnly(); ResourceType stringType = ResourceType.GetPrimitiveResourceType(typeof(string)); var duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType), new ServiceActionParameter("p2", stringType) }, null); duplicateAction1.SetReadOnly(); var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", entityType) }, null); duplicateAction2.SetReadOnly(); var actionProvider = new TestActionProvider { GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 } }; var providerWrapper = CreateProviderWrapper(actionProvider, p => p.AddResourceSet(resourceSet)); Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList(); getVisibleOperations.ShouldThrow <DataServiceException>() .WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate")) .And.StatusCode.Should().Be(500); }
public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndNoBindingType() { ResourceType stringType = ResourceType.GetPrimitiveResourceType(typeof(string)); var duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Never, new[] { new ServiceActionParameter("p1", stringType) }, null); duplicateAction1.SetReadOnly(); var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Never, new ServiceActionParameter[0], null); duplicateAction2.SetReadOnly(); var actionProvider = new TestActionProvider { GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 } }; var providerWrapper = CreateProviderWrapper(actionProvider); Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList(); getVisibleOperations.ShouldThrow <DataServiceException>() .WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate")) .And.StatusCode.Should().Be(500); }
public void MetadataProviderModelShouldGoDirectlyToProviderWhenLookingUpOperationsInSelect() { var entityType = CreateResourceTypeWithKeyProperties("Id"); var derivedType = CreateDerivedType("Derived", entityType); var veryDerivedType = CreateDerivedType("VeryDerived", derivedType); var unrelatedDerivedType = CreateDerivedType("UnrelatedDerived", entityType); var action1 = new ServiceAction("Action", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType) }); var action2 = new ServiceAction("Action", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", derivedType) }); var actionWithDifferentName = new ServiceAction("Action2", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", derivedType) }); var action3 = new ServiceAction("Action", null, null, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", veryDerivedType) }); action1.SetReadOnly(); action2.SetReadOnly(); actionWithDifferentName.SetReadOnly(); action3.SetReadOnly(); int calls = 0; var actionProvider = new TestActionProvider { GetByBindingTypeCallback = (o, t) => { calls++; if (t == entityType) { return new[] { action1 }; } if (t == derivedType) { return new[] { action2, actionWithDifferentName }; } if (t == veryDerivedType) { return new[] { action3 }; } t.Should().NotBeSameAs(unrelatedDerivedType); return null; }}; var metadataProvider = new DataServiceProviderSimulator(); metadataProvider.AddResourceType(entityType); metadataProvider.AddResourceType(derivedType); metadataProvider.AddResourceType(veryDerivedType); metadataProvider.AddResourceType(unrelatedDerivedType); var model = CreateMetadataProviderEdmModel(metadataProvider, actionProvider); var schemaType = (IEdmEntityType)model.EnsureSchemaType(derivedType); var result = model.FindDeclaredBoundOperations(schemaType).FilterByName(false, "Action"); result.Should().NotBeNull(); result.Should().HaveCount(3); result.Should().OnlyContain(f => f.Name == "Action"); calls.Should().Be(3); }
private static DataServiceProviderWrapper CreateProviderWrapper(TestActionProvider actionProvider = null, Action<DataServiceProviderSimulator> addMetadata = null) { var provider = new DataServiceProviderSimulator(); if (addMetadata != null) { addMetadata(provider); } var dataService = new DataServiceSimulatorWithGetService(); dataService.Configuration = new DataServiceConfiguration(provider); dataService.ProcessingPipeline = new DataServiceProcessingPipeline(); dataService.Configuration.SetServiceActionAccessRule("*", ServiceActionRights.Invoke); dataService.Configuration.SetEntitySetAccessRule("*", EntitySetRights.All); dataService.Configuration.SetServiceOperationAccessRule("*", ServiceOperationRights.All); dataService.Configuration.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4; dataService.OperationContext = new DataServiceOperationContext(false, new DataServiceHost2Simulator {AbsoluteRequestUri = new Uri("http://fake.org/$metadata") }); dataService.OperationContext.InitializeAndCacheHeaders(dataService); DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(dataService.Instance.GetType(), provider); IDataServiceProviderBehavior providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior; var providerWrapper = new DataServiceProviderWrapper( new DataServiceCacheItem( dataService.Configuration, staticConfiguration), provider, provider, dataService, false); dataService.Provider = providerWrapper; providerWrapper.ProviderBehavior = providerBehavior; dataService.ActionProvider = DataServiceActionProviderWrapper.Create(dataService); if (actionProvider != null) { dataService.Providers.Add(typeof(IDataServiceActionProvider), actionProvider); } return providerWrapper; }
public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndNoBindingType() { ResourceType stringType = ResourceType.GetPrimitiveResourceType(typeof(string)); var duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Never, new[] { new ServiceActionParameter("p1", stringType) }, null); duplicateAction1.SetReadOnly(); var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Never, new ServiceActionParameter[0], null); duplicateAction2.SetReadOnly(); var actionProvider = new TestActionProvider { GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 } }; var providerWrapper = CreateProviderWrapper(actionProvider); Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList(); getVisibleOperations.ShouldThrow<DataServiceException>() .WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate")) .And.StatusCode.Should().Be(500); }