public void CanCreateActionWithNoArguments() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); builder.Namespace = "MyNamespace"; builder.ContainerName = "MyContainer"; ActionConfiguration action = builder.Action("Format"); ActionConfiguration actionII = builder.Action("FormatII"); actionII.Namespace = "MyNamespaceII"; // Assert Assert.Equal("Format", action.Name); Assert.Equal(OperationKind.Action, action.Kind); Assert.NotNull(action.Parameters); Assert.Empty(action.Parameters); Assert.Null(action.ReturnType); Assert.True(action.IsSideEffecting); Assert.False(action.IsComposable); Assert.False(action.IsBindable); Assert.Equal("MyNamespace", action.Namespace); Assert.Equal("MyNamespace.Format", action.FullyQualifiedName); Assert.Equal("MyNamespaceII", actionII.Namespace); Assert.Equal("MyNamespaceII.FormatII", actionII.FullyQualifiedName); Assert.NotNull(builder.Operations); Assert.Equal(2, builder.Operations.Count()); }
public void CanCreateActionWithComplexReturnType() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration createAddress = builder.Action("CreateAddress").Returns <Address>(); ActionConfiguration createAddresses = builder.Action("CreateAddresses").ReturnsCollection <Address>(); // Assert ComplexTypeConfiguration address = createAddress.ReturnType as ComplexTypeConfiguration; Assert.NotNull(address); Assert.Equal(typeof(Address).FullName, address.FullName); Assert.Null(createAddress.NavigationSource); CollectionTypeConfiguration addresses = createAddresses.ReturnType as CollectionTypeConfiguration; Assert.NotNull(addresses); Assert.Equal(string.Format("Collection({0})", typeof(Address).FullName), addresses.FullName); address = addresses.ElementType as ComplexTypeConfiguration; Assert.NotNull(address); Assert.Equal(typeof(Address).FullName, address.FullName); Assert.Null(createAddresses.NavigationSource); }
public void CanCreateActionWithEntityReturnType() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration createGoodCustomer = builder.Action("CreateGoodCustomer").ReturnsFromEntitySet <Customer>("GoodCustomers"); ActionConfiguration createBadCustomers = builder.Action("CreateBadCustomers").ReturnsCollectionFromEntitySet <Customer>("BadCustomers"); // Assert EntityTypeConfiguration customer = createGoodCustomer.ReturnType as EntityTypeConfiguration; Assert.NotNull(customer); Assert.Equal(typeof(Customer).FullName, customer.FullName); EntitySetConfiguration goodCustomers = builder.EntitySets.SingleOrDefault(s => s.Name == "GoodCustomers"); Assert.NotNull(goodCustomers); Assert.Same(createGoodCustomer.NavigationSource, goodCustomers); CollectionTypeConfiguration customers = createBadCustomers.ReturnType as CollectionTypeConfiguration; Assert.NotNull(customers); customer = customers.ElementType as EntityTypeConfiguration; Assert.NotNull(customer); EntitySetConfiguration badCustomers = builder.EntitySets.SingleOrDefault(s => s.Name == "BadCustomers"); Assert.NotNull(badCustomers); Assert.Same(createBadCustomers.NavigationSource, badCustomers); }
public void GetEdmModel_ThrowsException_WhenUnboundActionOverloaded() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.Action("ActionName").Parameter <int>("Param1"); builder.Action("ActionName").Returns <string>(); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => builder.GetEdmModel()); Assert.Equal(exception.Message, "Found more than one unbound action with name 'ActionName'. " + "Each unbound action must have an unique action name."); }
public void CanCreateEdmModel_WithNonBindableAction() { // Arrange ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>(); // Act ActionConfiguration actionConfiguration = builder.Action("ActionName"); actionConfiguration.ReturnsFromEntitySet <Customer>("Customers"); IEdmModel model = builder.GetEdmModel(); // Assert IEdmEntityContainer container = model.EntityContainer; Assert.NotNull(container); Assert.Single(container.Elements.OfType <IEdmActionImport>()); Assert.Single(container.Elements.OfType <IEdmEntitySet>()); IEdmActionImport action = container.Elements.OfType <IEdmActionImport>().Single(); Assert.False(action.Action.IsBound); Assert.Equal("ActionName", action.Name); Assert.NotNull(action.Action.ReturnType); Assert.NotNull(action.EntitySet); Assert.Equal("Customers", (action.EntitySet as IEdmPathExpression).Path); Assert.Empty(action.Action.Parameters); }
public void ReturnsCollectionFromEntitySet_ThrowsArgumentNull_EntitySetConfiguration() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); var action = builder.Action("action"); // Act & Assert Assert.Throws <ArgumentNullException>("entitySetConfiguration", () => action.ReturnsCollectionFromEntitySet <Movie>(entitySetConfiguration: null)); }
public void CanCreateActionWithReturnTypeAsNullableByDefault() { // Arrange & Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration action = builder.Action("MyAction").Returns <Address>(); // Assert Assert.True(action.OptionalReturn); }
public void HasActionLink_ThrowsException_OnNonBindableActions() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration action = builder.Action("NoBindableAction"); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => action.HasActionLink(ctx => new Uri("http://any"), followsConventions: false)); Assert.Equal(exception.Message, "To register an action link factory, actions must be bindable to a single entity. " + "Action 'NoBindableAction' does not meet this requirement."); }
public void CanCreateActionWithPrimitiveCollectionReturnType() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration action = builder.Action("CreateMessages"); action.ReturnsCollection <string>(); // Assert Assert.NotNull(action.ReturnType); Assert.Equal("Collection(Edm.String)", action.ReturnType.FullName); }
public void ReturnsCollection_ThrowsInvalidOperationException_IfReturnTypeIsEntity() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.EntityType <Movie>(); var action = builder.Action("action"); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => action.ReturnsCollection <Movie>()); Assert.Equal(exception.Message, "The EDM type 'Microsoft.AspNetCore.OData.Builder.Movie' is already declared as an entity type. Use the " + "method 'ReturnsCollectionFromEntitySet' if the return type is an entity collection."); }
public void ReturnsCollectionFromEntitySet_Sets_EntitySetAndReturnType() { // Arrange string entitySetName = "movies"; ODataModelBuilder builder = new ODataModelBuilder(); var movies = builder.EntitySet <Movie>(entitySetName); var action = builder.Action("Action"); // Act action.ReturnsCollectionFromEntitySet(movies); // Assert Assert.Equal(entitySetName, action.NavigationSource.Name); Assert.Equal(typeof(IEnumerable <Movie>), action.ReturnType.ClrType); }
public void AttemptToRemoveNonExistantEntityReturnsFalse() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); ODataModelBuilder builder2 = new ODataModelBuilder(); OperationConfiguration toRemove = builder2.Action("ToRemove"); // Act bool removedByName = builder.RemoveOperation("ToRemove"); bool removed = builder.RemoveOperation(toRemove); //Assert Assert.False(removedByName); Assert.False(removed); }
public void CanCreateActionWithNonbindingParameters() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration action = builder.Action("MyAction"); action.Parameter <string>("p0"); action.Parameter <int>("p1"); action.Parameter <Address>("p2"); action.CollectionParameter <string>("p3"); action.CollectionParameter <int>("p4"); action.CollectionParameter <ZipCode>("p5"); action.EntityParameter <Customer>("p6"); action.CollectionEntityParameter <Employee>("p7"); ParameterConfiguration[] parameters = action.Parameters.ToArray(); ComplexTypeConfiguration[] complexTypes = builder.StructuralTypes.OfType <ComplexTypeConfiguration>().ToArray(); EntityTypeConfiguration[] entityTypes = builder.StructuralTypes.OfType <EntityTypeConfiguration>().ToArray(); // Assert Assert.Equal(2, complexTypes.Length); Assert.Equal(typeof(Address).FullName, complexTypes[0].FullName); Assert.Equal(typeof(ZipCode).FullName, complexTypes[1].FullName); Assert.Equal(2, entityTypes.Length); Assert.Equal(typeof(Customer).FullName, entityTypes[0].FullName); Assert.Equal(typeof(Employee).FullName, entityTypes[1].FullName); Assert.Equal(8, parameters.Length); Assert.Equal("p0", parameters[0].Name); Assert.Equal("Edm.String", parameters[0].TypeConfiguration.FullName); Assert.Equal("p1", parameters[1].Name); Assert.Equal("Edm.Int32", parameters[1].TypeConfiguration.FullName); Assert.Equal("p2", parameters[2].Name); Assert.Equal(typeof(Address).FullName, parameters[2].TypeConfiguration.FullName); Assert.Equal("p3", parameters[3].Name); Assert.Equal("Collection(Edm.String)", parameters[3].TypeConfiguration.FullName); Assert.Equal("p4", parameters[4].Name); Assert.Equal("Collection(Edm.Int32)", parameters[4].TypeConfiguration.FullName); Assert.Equal("p5", parameters[5].Name); Assert.Equal(string.Format("Collection({0})", typeof(ZipCode).FullName), parameters[5].TypeConfiguration.FullName); Assert.Equal("p6", parameters[6].Name); Assert.Equal(typeof(Customer).FullName, parameters[6].TypeConfiguration.FullName); Assert.Equal("p7", parameters[7].Name); Assert.Equal(string.Format("Collection({0})", typeof(Employee).FullName), parameters[7].TypeConfiguration.FullName); }
public void CanCreateActionWithNonbindingParametersAsNullable() { // Arrange & Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration action = builder.Action("MyAction"); action.Parameter <string>("p0"); action.Parameter <string>("p1").OptionalParameter = false; action.Parameter <int>("p2").OptionalParameter = true; action.Parameter <int>("p3"); action.Parameter <Address>("p4"); action.Parameter <Address>("p5").OptionalParameter = false; action.CollectionParameter <ZipCode>("p6"); action.CollectionParameter <ZipCode>("p7").OptionalParameter = false; action.EntityParameter <Customer>("p8"); action.EntityParameter <Customer>("p9").OptionalParameter = false; action.CollectionEntityParameter <Customer>("p10"); action.CollectionEntityParameter <Customer>("p11").OptionalParameter = false; Dictionary <string, ParameterConfiguration> parameters = action.Parameters.ToDictionary(e => e.Name, e => e); // Assert Assert.True(parameters["p0"].OptionalParameter); Assert.False(parameters["p1"].OptionalParameter); Assert.True(parameters["p2"].OptionalParameter); Assert.False(parameters["p3"].OptionalParameter); Assert.True(parameters["p4"].OptionalParameter); Assert.False(parameters["p5"].OptionalParameter); Assert.True(parameters["p6"].OptionalParameter); Assert.False(parameters["p7"].OptionalParameter); Assert.True(parameters["p8"].OptionalParameter); Assert.False(parameters["p9"].OptionalParameter); Assert.True(parameters["p10"].OptionalParameter); Assert.False(parameters["p11"].OptionalParameter); }
public void CanCreateActionWithNonbindingParameters_AddParameterNonGenericMethod() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); ActionConfiguration action = builder.Action("MyAction"); action.Parameter(typeof(string), "p0"); action.Parameter(typeof(int), "p1"); action.Parameter(typeof(Address), "p2"); ParameterConfiguration[] parameters = action.Parameters.ToArray(); // Assert Assert.Equal(3, parameters.Length); Assert.Equal("p0", parameters[0].Name); Assert.Equal("Edm.String", parameters[0].TypeConfiguration.FullName); Assert.Equal("p1", parameters[1].Name); Assert.Equal("Edm.Int32", parameters[1].TypeConfiguration.FullName); Assert.Equal("p2", parameters[2].Name); Assert.Equal(typeof(Address).FullName, parameters[2].TypeConfiguration.FullName); }