private void AddActionsToProduct(EntityTypeConfiguration<Product> config) { config.Action("FinalPrice") .Returns<decimal>(); config.Action("LowestPrice") .Returns<decimal>(); }
public void CanBuildBoundProcedureCacheForIEdmModel() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType; customer.HasKey(c => c.ID); customer.Property(c => c.Name); customer.ComplexProperty(c => c.Address); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; movie.HasKey(m => m.ID); movie.HasKey(m => m.Name); EntityTypeConfiguration <Blockbuster> blockBuster = builder.Entity <Blockbuster>().DerivesFrom <Movie>(); EntityTypeConfiguration movieConfiguration = builder.StructuralTypes.OfType <EntityTypeConfiguration>().Single(t => t.Name == "Movie"); // build actions that are bindable to a single entity customer.Action("InCache1_CustomerAction"); customer.Action("InCache2_CustomerAction"); movie.Action("InCache3_MovieAction"); ActionConfiguration incache4_MovieAction = builder.Action("InCache4_MovieAction"); incache4_MovieAction.SetBindingParameter("bindingParameter", movieConfiguration, true); blockBuster.Action("InCache5_BlockbusterAction"); // build actions that are either: bindable to a collection of entities, have no parameter, have only complex parameter customer.Collection.Action("NotInCache1_CustomersAction"); movie.Collection.Action("NotInCache2_MoviesAction"); ActionConfiguration notInCache3_NoParameters = builder.Action("NotInCache3_NoParameters"); ActionConfiguration notInCache4_AddressParameter = builder.Action("NotInCache4_AddressParameter"); notInCache4_AddressParameter.Parameter <Address>("address"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType customerType = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Customer"); IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Movie"); IEdmEntityType blockBusterType = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Blockbuster"); // Act BindableProcedureFinder annotation = new BindableProcedureFinder(model); IEdmFunctionImport[] movieActions = annotation.FindProcedures(movieType).ToArray(); IEdmFunctionImport[] customerActions = annotation.FindProcedures(customerType).ToArray(); IEdmFunctionImport[] blockBusterActions = annotation.FindProcedures(blockBusterType).ToArray(); // Assert Assert.Equal(2, customerActions.Length); Assert.NotNull(customerActions.SingleOrDefault(a => a.Name == "InCache1_CustomerAction")); Assert.NotNull(customerActions.SingleOrDefault(a => a.Name == "InCache2_CustomerAction")); Assert.Equal(2, movieActions.Length); Assert.NotNull(movieActions.SingleOrDefault(a => a.Name == "InCache3_MovieAction")); Assert.NotNull(movieActions.SingleOrDefault(a => a.Name == "InCache4_MovieAction")); Assert.Equal(3, blockBusterActions.Length); Assert.NotNull(blockBusterActions.SingleOrDefault(a => a.Name == "InCache3_MovieAction")); Assert.NotNull(blockBusterActions.SingleOrDefault(a => a.Name == "InCache4_MovieAction")); Assert.NotNull(blockBusterActions.SingleOrDefault(a => a.Name == "InCache5_BlockbusterAction")); }
public void CanManuallyConfigureActionLinkFactory() { // Arrange string uriTemplate = "http://server/service/Customers({0})/Reward"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType; customer.HasKey(c => c.CustomerId); customer.Property(c => c.Name); // Act ActionConfiguration reward = customer.Action("Reward"); reward.HasActionLink(ctx => new Uri(string.Format(uriTemplate, ctx.GetPropertyValue("CustomerId"))), followsConventions: false); IEdmModel model = builder.GetEdmModel(); IEdmEntityType customerType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); ODataSerializerContext serializerContext = new ODataSerializerContext { Model = model }; EntityInstanceContext context = new EntityInstanceContext(serializerContext, customerType.AsReference(), new Customer { CustomerId = 1 }); IEdmFunctionImport rewardAction = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault().FunctionImports().SingleOrDefault(); ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(rewardAction); //Assert Assert.Equal(expectedUri, reward.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
public void CanCreateEdmModel_WithBindableAction() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.Entity <Customer>(); customer.HasKey(c => c.CustomerId); customer.Property(c => c.Name); // Act ActionConfiguration sendEmail = customer.Action("ActionName"); IEdmModel model = builder.GetEdmModel(); // Assert IEdmEntityContainer container = model.EntityContainers().SingleOrDefault(); Assert.NotNull(container); Assert.Equal(1, container.Elements.OfType <IEdmFunctionImport>().Count()); IEdmFunctionImport action = container.Elements.OfType <IEdmFunctionImport>().Single(); Assert.False(action.IsComposable); Assert.True(action.IsSideEffecting); Assert.True(action.IsBindable); Assert.True(model.IsAlwaysBindable(action)); Assert.Equal("ActionName", action.Name); Assert.Null(action.ReturnType); Assert.Equal(1, action.Parameters.Count()); Assert.Equal(BindingParameterConfiguration.DefaultBindingParameterName, action.Parameters.Single().Name); Assert.Equal(typeof(Customer).FullName, action.Parameters.Single().Type.FullName()); }
private void AddActionsToOrder(EntityTypeConfiguration<Order> config) { config.Action("PaymentPending") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders); config.Action("PaymentPaid") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders) .Parameter<string>("PaymentMethodName"); config.Action("PaymentRefund") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders) .Parameter<bool>("Online"); config.Action("Cancel") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders); }
public void WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange string uriTemplate = "http://server/Movies({0})/Watch"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = new ODataConventionModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://server/Movies"); HttpConfiguration configuration = new HttpConfiguration(); configuration.Routes.MapHttpRoute(ODataRouteNames.InvokeBoundAction, "{controller}({boundId})/{odataAction}"); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration; request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute()); UrlHelper urlHelper = new UrlHelper(request); // Act ActionConfiguration watch = movie.Action("Watch"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmFunctionImport watchAction = container.FunctionImports().SingleOrDefault(); IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); EntityInstanceContext <Movie> context = new EntityInstanceContext <Movie>(model, entitySet, movieType, urlHelper, new Movie { ID = 1, Name = "Avatar" }, false); ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(watchAction); //Assert Assert.Equal(expectedUri, watch.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
private void AddActionsToProduct(EntityTypeConfiguration<Product> config) { config.Action("FinalPrice") .Returns<decimal>(); config.Action("LowestPrice") .Returns<decimal>(); config.Action("CreateAttributeCombinations") .ReturnsCollectionFromEntitySet<ProductVariantAttributeCombination>(WebApiOdataEntitySet.ProductVariantAttributeCombinations); var manageAttributes = config.Action("ManageAttributes") .ReturnsCollectionFromEntitySet<ProductVariantAttribute>(WebApiOdataEntitySet.ProductVariantAttributes); manageAttributes.Parameter<bool>("Synchronize"); manageAttributes.CollectionParameter<ManageAttributeType>("Attributes"); }
public void CanCreateActionThatBindsToEntity() { // Arrange // Act ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.Entity <Customer>(); ActionConfiguration sendEmail = customer.Action("SendEmail"); // Assert Assert.True(sendEmail.IsBindable); Assert.NotNull(sendEmail.Parameters); Assert.Equal(1, sendEmail.Parameters.Count()); Assert.Equal(BindingParameterConfiguration.DefaultBindingParameterName, sendEmail.Parameters.Single().Name); Assert.Equal(typeof(Customer).FullName, sendEmail.Parameters.Single().TypeConfiguration.FullName); }
public void WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange string uriTemplate = "http://server/Movies({0})/Watch"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = new ODataConventionModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; ActionConfiguration watch = movie.Action("Watch"); IEdmModel model = builder.GetEdmModel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://server/Movies"); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.Routes.MapODataRoute(routeName, null, model); request.SetConfiguration(configuration); request.SetODataRouteName(routeName); UrlHelper urlHelper = new UrlHelper(request); // Act IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmFunctionImport watchAction = container.FunctionImports().SingleOrDefault(); IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); EntityInstanceContext <Movie> context = new EntityInstanceContext <Movie>() { EdmModel = model, EntitySet = entitySet, EntityType = movieType, Url = urlHelper, EntityInstance = new Movie { ID = 1, Name = "Avatar" }, SkipExpensiveAvailabilityChecks = false }; ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(watchAction); //Assert Assert.Equal(expectedUri, watch.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
public void BuilderIncludesMapFromEntityTypeToBindableProcedures() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType; customer.HasKey(c => c.Id); customer.Property(c => c.Name); customer.Action("Reward"); IEdmModel model = builder.GetEdmModel(); IEdmEntityType customerType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); // Act BindableProcedureFinder finder = model.GetAnnotationValue <BindableProcedureFinder>(model); // Assert Assert.NotNull(finder); Assert.NotNull(finder.FindProcedures(customerType).SingleOrDefault()); Assert.Equal("Reward", finder.FindProcedures(customerType).SingleOrDefault().Name); }
public void GetEdmModel_SetsNullableIffParameterTypeIsNullable() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; var actionBuilder = movie.Action("Watch"); actionBuilder.Parameter <int>("int"); actionBuilder.Parameter <Nullable <int> >("nullableOfInt"); actionBuilder.Parameter <DateTime>("dateTime"); actionBuilder.Parameter <string>("string"); // Act IEdmModel model = builder.GetEdmModel(); //Assert IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmFunctionImport action = container.FindFunctionImports("Watch").Single(); Assert.False(action.FindParameter("int").Type.IsNullable); Assert.True(action.FindParameter("nullableOfInt").Type.IsNullable); Assert.False(action.FindParameter("dateTime").Type.IsNullable); Assert.True(action.FindParameter("string").Type.IsNullable); }
private void AddActionsToOrder(EntityTypeConfiguration<Order> config) { config.Action("PaymentPending") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders); config.Action("PaymentPaid") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders) .Parameter<string>("PaymentMethodName"); config.Action("PaymentRefund") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders) .Parameter<bool>("Online"); config.Action("Cancel") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders); var addShipment = config.Action("AddShipment") .ReturnsFromEntitySet<Order>(WebApiOdataEntitySet.Orders); addShipment.Parameter<string>("TrackingNumber"); addShipment.Parameter<bool?>("SetAsShipped"); }