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 }); IEdmAction rewardAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(rewardAction); //Assert Assert.Equal(expectedUri, reward.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
public void ActionLink_PreservesFollowsConventions(bool value) { // Arrange ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>(); ActionConfiguration configuration = new ActionConfiguration(builder, "IgnoreAction"); Mock <IEdmTypeConfiguration> bindingParameterTypeMock = new Mock <IEdmTypeConfiguration>(); bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity); Type entityType = typeof(object); bindingParameterTypeMock.Setup(o => o.ClrType).Returns(entityType); configuration.SetBindingParameter("IgnoreParameter", bindingParameterTypeMock.Object); configuration.HasActionLink((a) => { throw new NotImplementedException(); }, followsConventions: value); builder.AddProcedure(configuration); builder.AddEntityType(entityType); // Act IEdmModel model = builder.GetEdmModel(); // Assert var action = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(action); Assert.NotNull(actionLinkBuilder); Assert.Equal(value, actionLinkBuilder.FollowsConventions); }
public void FollowsConventions_IsSpecifiedValue(bool value) { // Arrange ActionLinkBuilder builder = new ActionLinkBuilder((a) => { throw new NotImplementedException(); }, followsConventions: value); // Act bool followsConventions = builder.FollowsConventions; // Assert Assert.Equal(value, followsConventions); }
public void FollowsConventions_IsSpecifiedValue(bool value) { // Arrange ActionLinkBuilder builder = new ActionLinkBuilder((EntityInstanceContext a) => { throw new NotImplementedException(); }, followsConventions: value); // Act bool followsConventions = builder.FollowsConventions; // Assert Assert.Equal(value, followsConventions); }
public void GetActionLinkBuilder_After_SetActionLinkBuilder() { // Arrange IEdmModel model = new EdmModel(); IEdmEntityContainer container = new EdmEntityContainer("NS", "Container"); IEdmAction action = new Mock<IEdmAction>().Object; ActionLinkBuilder builder = new ActionLinkBuilder(_=> null, followsConventions: false); // Act model.SetActionLinkBuilder(action, builder); var result = model.GetActionLinkBuilder(action); // Assert Assert.Same(builder, result); }
/// <summary> /// Register a factory that creates actions links. /// </summary> public ActionConfiguration HasActionLink(Func <EntityInstanceContext, Uri> actionLinkFactory, bool followsConventions) { if (actionLinkFactory == null) { throw new ArgumentNullException("actionLinkFactory"); } if (!IsBindable || BindingParameter.TypeConfiguration.Kind != EdmTypeKind.Entity) { throw Error.InvalidOperation(SRResources.HasActionLinkRequiresBindToEntity, Name); } ProcedureLinkBuilder = new ActionLinkBuilder(actionLinkFactory, followsConventions); FollowsConventions = followsConventions; return(this); }
public void BuildActionLink_ForFeed_ReturnsLink() { // Arrange ActionLinkBuilder builder = new ActionLinkBuilder((FeedContext a) => new Uri("http://localhost:456"), followsConventions: true); EntityInstanceContext entityContext = new EntityInstanceContext(); FeedContext feedContext = new FeedContext(); // Act Uri link = builder.BuildActionLink(entityContext); Uri feedLink = builder.BuildActionLink(feedContext); // Assert Assert.Null(link); Assert.NotNull(feedLink); Assert.Equal("http://localhost:456/", feedLink.AbsoluteUri); }
public void BuildActionLink_ForFeed_ReturnsLink() { // Arrange ActionLinkBuilder builder = new ActionLinkBuilder((FeedContext a) => new Uri("http://localhost:456"), followsConventions: true); EntityInstanceContext entityContext = new EntityInstanceContext(); FeedContext feedContext = new FeedContext(); // Act Uri link = builder.BuildActionLink(entityContext); Uri feedLink = builder.BuildActionLink(feedContext); // Assert Assert.Null(link); Assert.NotNull(feedLink); Assert.Equal("http://localhost:456/", feedLink.AbsoluteUri); }
public static ActionLinkBuilder GetActionLinkBuilder(this IEdmModel model, IEdmAction action) { if (model == null) { throw Error.ArgumentNull("model"); } if (action == null) { throw Error.ArgumentNull("action"); } ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue<ActionLinkBuilder>(action); if (actionLinkBuilder == null) { actionLinkBuilder = new ActionLinkBuilder(entityInstanceContext => entityInstanceContext.GenerateActionLink(action), followsConventions: true); model.SetActionLinkBuilder(action, actionLinkBuilder); } return actionLinkBuilder; }
public void WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange string uriTemplate = "http://server/Movies({0})/Default.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.MapODataServiceRoute(routeName, null, model); request.SetConfiguration(configuration); request.ODataProperties().RouteName = routeName; UrlHelper urlHelper = new UrlHelper(request); // Act IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmAction watchAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); ODataSerializerContext serializerContext = new ODataSerializerContext { Model = model, NavigationSource = entitySet, Url = urlHelper }; EntityInstanceContext context = new EntityInstanceContext(serializerContext, movieType.AsReference(), new Movie { ID = 1, Name = "Avatar" }); ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(watchAction); //Assert Assert.Equal(expectedUri, watch.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
public void WhenFeedActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange Uri expectedUri = new Uri("http://server/Movies/Default.Watch"); ODataModelBuilder builder = new ODataConventionModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; ActionConfiguration watch = movie.Collection.Action("Watch"); // action bound to collection IEdmModel model = builder.GetEdmModel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://server/Movies"); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); request.SetConfiguration(configuration); request.ODataProperties().RouteName = routeName; UrlHelper urlHelper = new UrlHelper(request); // Act IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmAction watchAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); FeedContext context = new FeedContext { EntitySetBase = entitySet, Url = urlHelper, Request = request }; ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(watchAction); //Assert Assert.Equal(expectedUri, watch.GetFeedActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
public void CanManuallyConfigureFeedActionLinkFactory() { // Arrange Uri expectedUri = new Uri("http://localhost/service/Customers/Reward"); ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType; customer.HasKey(c => c.CustomerId); customer.Property(c => c.Name); ActionConfiguration reward = customer.Collection.Action("Reward"); reward.HasFeedActionLink(ctx => expectedUri, followsConventions: false); IEdmModel model = builder.GetEdmModel(); // Act IEdmAction rewardAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(rewardAction); FeedContext context = new FeedContext(); //Assert Assert.Equal(expectedUri, reward.GetFeedActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }
public static ActionLinkBuilder GetActionLinkBuilder(this IEdmModel model, IEdmAction action) { if (model == null) { throw Error.ArgumentNull("model"); } if (action == null) { throw Error.ArgumentNull("action"); } ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue<ActionLinkBuilder>(action); if (actionLinkBuilder == null) { // Let ODL to process the action link for entity if (action.Parameters != null) { if (action.Parameters.First().Type.IsEntity()) { actionLinkBuilder = new ActionLinkBuilder( (EntityInstanceContext entityInstanceContext) => entityInstanceContext.GenerateActionLink(action), followsConventions: true); } else if (action.Parameters.First().Type.IsCollection()) { actionLinkBuilder = new ActionLinkBuilder((FeedContext feedContext) => feedContext.GenerateActionLink(action), followsConventions: true); } } model.SetActionLinkBuilder(action, actionLinkBuilder); } return actionLinkBuilder; }
internal static bool ShouldOmitAction(IEdmAction action, ActionLinkBuilder builder, ODataMetadataLevel metadataLevel) { Contract.Assert(builder != null); switch (metadataLevel) { case ODataMetadataLevel.MinimalMetadata: case ODataMetadataLevel.NoMetadata: return action.IsBound && builder.FollowsConventions; case ODataMetadataLevel.FullMetadata: default: // All values already specified; just keeping the compiler happy. return false; } }
public void TestShouldOmitAction(TestODataMetadataLevel metadataLevel, bool followsConventions, bool expectedResult) { // Arrange IEdmActionImport action = CreateFakeActionImport(true); IEdmDirectValueAnnotationsManager annonationsManager = CreateFakeAnnotationsManager(); IEdmModel model = CreateFakeModel(annonationsManager); ActionLinkBuilder builder = new ActionLinkBuilder((a) => { throw new NotImplementedException(); }, followsConventions); // Act bool actualResult = ODataEntityTypeSerializer.ShouldOmitAction(action.Action, builder, (ODataMetadataLevel)metadataLevel); // Assert Assert.Equal(expectedResult, actualResult); }
public void CreateODataAction_OmitsAction_WhenFollowingConventions(TestODataMetadataLevel metadataLevel) { // Arrange IEdmAction action = CreateFakeAction("IgnoreAction", isBindable: true); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => new Uri("aa://Ignore"), followsConventions: true); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(action, linkBuilder); IEdmModel model = CreateFakeModel(annotationsManager); UrlHelper url = CreateMetadataLinkFactory("http://IgnoreMetadataPath"); EntityInstanceContext context = CreateContext(model, url); context.SerializerContext.MetadataLevel = (ODataMetadataLevel)metadataLevel; // Act ODataAction actualAction = _serializer.CreateODataAction(action, context); // Assert Assert.Null(actualAction); }
public void CreateODataAction_IncludesTarget(TestODataMetadataLevel metadataLevel, bool followsConventions) { // Arrange Uri expectedTarget = new Uri("aa://Target"); IEdmAction action = CreateFakeAction("IgnoreAction"); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => expectedTarget, followsConventions); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(action, linkBuilder); IEdmModel model = CreateFakeModel(annotationsManager); UrlHelper url = CreateMetadataLinkFactory("http://IgnoreMetadataPath"); EntityInstanceContext context = CreateContext(model, url); context.SerializerContext.MetadataLevel = (ODataMetadataLevel)metadataLevel; // Act ODataAction actualAction = _serializer.CreateODataAction(action, context); // Assert Assert.NotNull(actualAction); Assert.Equal(expectedTarget, actualAction.Target); }
public void CreateODataAction_IncludesTitle() { // Arrange string expectedActionName = "Action"; IEdmAction action = CreateFakeAction(expectedActionName); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => new Uri("aa://IgnoreTarget"), followsConventions: false); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(action, linkBuilder); IEdmModel model = CreateFakeModel(annotationsManager); UrlHelper url = CreateMetadataLinkFactory("http://IgnoreMetadataPath"); EntityInstanceContext context = CreateContext(model, url); context.SerializerContext.MetadataLevel = (ODataMetadataLevel)TestODataMetadataLevel.FullMetadata; // Act ODataAction actualAction = _serializer.CreateODataAction(action, context); // Assert Assert.NotNull(actualAction); Assert.Equal(expectedActionName, actualAction.Title); }
public void CreateODataAction_SkipsAlwaysAvailableAction_PerShouldOmitAction() { // Arrange IEdmActionImport actionImport = CreateFakeActionImport(true); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => new Uri("aa://IgnoreTarget"), followsConventions: true); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(actionImport, linkBuilder); annotationsManager.SetIsAlwaysBindable(actionImport.Action); IEdmModel model = CreateFakeModel(annotationsManager); EntityInstanceContext context = CreateContext(model); context.SerializerContext.MetadataLevel = ODataMetadataLevel.MinimalMetadata; // Act ODataAction actualAction = _serializer.CreateODataAction(actionImport.Action, context); // Assert Assert.Null(actualAction); }
public void CreateODataAction_ForJsonLight_OmitsContainerName_PerCreateMetadataFragment() { // Arrange string expectedMetadataPrefix = "http://Metadata"; string expectedNamespace = "NS"; string expectedActionName = "Action"; IEdmEntityContainer container = CreateFakeContainer("ContainerShouldNotAppearInResult"); IEdmAction action = CreateFakeAction(expectedNamespace, expectedActionName); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => new Uri("aa://IgnoreTarget"), followsConventions: false); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(action, linkBuilder); IEdmModel model = CreateFakeModel(annotationsManager); UrlHelper url = CreateMetadataLinkFactory(expectedMetadataPrefix); EntityInstanceContext context = CreateContext(model, url); context.SerializerContext.MetadataLevel = ODataMetadataLevel.MinimalMetadata; // Act ODataAction actualAction = _serializer.CreateODataAction(action, context); // Assert Assert.NotNull(actualAction); string expectedMetadata = expectedMetadataPrefix + "#" + expectedNamespace + "." + expectedActionName; AssertEqual(new Uri(expectedMetadata), actualAction.Metadata); }
public void CreateODataAction_OmitsAction_WhenActionLinkBuilderReturnsNull() { // Arrange IEdmAction action = CreateFakeAction("IgnoreAction"); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => null, followsConventions: false); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(action, linkBuilder); IEdmModel model = CreateFakeModel(annotationsManager); EntityInstanceContext context = CreateContext(model); context.SerializerContext.MetadataLevel = ODataMetadataLevel.MinimalMetadata; // Act ODataAction actualAction = _serializer.CreateODataAction(action, context); // Assert Assert.Null(actualAction); }
public void CreateODataAction_IncludesEverything_ForFullMetadata() { // Arrange string expectedContainerName = "Container"; string expectedNamespace = "NS"; string expectedActionName = "Action"; string expectedTarget = "aa://Target"; string expectedMetadataPrefix = "http://Metadata"; IEdmEntityContainer container = CreateFakeContainer(expectedContainerName); IEdmAction action = CreateFakeAction(expectedNamespace, expectedActionName, isBindable: true); ActionLinkBuilder linkBuilder = new ActionLinkBuilder((a) => new Uri(expectedTarget), followsConventions: true); IEdmDirectValueAnnotationsManager annotationsManager = CreateFakeAnnotationsManager(); annotationsManager.SetActionLinkBuilder(action, linkBuilder); annotationsManager.SetIsAlwaysBindable(action); IEdmModel model = CreateFakeModel(annotationsManager); UrlHelper url = CreateMetadataLinkFactory(expectedMetadataPrefix); EntityInstanceContext context = CreateContext(model, url); context.SerializerContext.MetadataLevel = ODataMetadataLevel.FullMetadata; // Act ODataAction actualAction = _serializer.CreateODataAction(action, context); // Assert string expectedMetadata = expectedMetadataPrefix + "#" + expectedNamespace + "." + expectedActionName; ODataAction expectedAction = new ODataAction { Metadata = new Uri(expectedMetadata), Target = new Uri(expectedTarget), Title = expectedActionName }; AssertEqual(expectedAction, actualAction); }
public static void SetActionLinkBuilder(this IEdmModel model, IEdmAction action, ActionLinkBuilder actionLinkBuilder) { if (model == null) { throw Error.ArgumentNull("model"); } model.SetAnnotationValue(action, actionLinkBuilder); }
public void GetActionLinkBuilderForFeed_After_SetActionLinkBuilder() { // Arrange IEdmModel model = new EdmModel(); IEdmAction action = new Mock<IEdmAction>().Object; ActionLinkBuilder builder = new ActionLinkBuilder((FeedContext _) => null, followsConventions: false); // Act model.SetActionLinkBuilder(action, builder); var result = model.GetActionLinkBuilder(action); // Assert Assert.Same(builder, result); }