public void ApplyNestedProperty_UsesThePropertyAlias_ForResourceSet() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(Customer))); model.Model.SetAnnotationValue(model.Order, new ClrTypeAnnotation(typeof(Order))); model.Model.SetAnnotationValue( model.Customer.FindProperty("Orders"), new ClrPropertyInfoAnnotation(typeof(Customer).GetProperty("AliasedOrders"))); ODataResourceSetWrapper resourceSetWrapper = new ODataResourceSetWrapper(new ODataResourceSet()); resourceSetWrapper.Resources.Add(new ODataResourceWrapper( new ODataResource { Properties = new[] { new ODataProperty { Name = "ID", Value = 42 } } })); Customer customer = new Customer(); ODataNestedResourceInfoWrapper resourceInfoWrapper = new ODataNestedResourceInfoWrapper(new ODataNestedResourceInfo { Name = "Orders" }); resourceInfoWrapper.NestedItems.Add(resourceSetWrapper); ODataDeserializerContext context = new ODataDeserializerContext { Model = model.Model }; // Act new ODataResourceDeserializer(_deserializerProvider) .ApplyNestedProperty(customer, resourceInfoWrapper, model.Customer.AsReference(), context); // Assert Assert.Equal(1, customer.AliasedOrders.Count()); Assert.Equal(42, customer.AliasedOrders[0].ID); }
public void GenerateLocationHeader_ForContainment() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.OrderLine, new ClrTypeAnnotation(typeof(OrderLine))); var path = new DefaultODataPathHandler().Parse( model.Model, "http://localhost/", "MyOrders(1)/OrderLines"); var request = RequestFactory.CreateFromModel(model.Model, path: path); var orderLine = new OrderLine { ID = 2 }; var createdODataResult = GetCreatedODataResult <OrderLine>(orderLine, request); // Act var locationHeader = createdODataResult.GenerateLocationHeader(request); // Assert Assert.Equal("http://localhost/MyOrders(1)/OrderLines(2)", locationHeader.ToString()); }
public void GetETagTEntity_Returns_ETagInHeader() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); HttpRequestMessage request = new HttpRequestMessage(); request.EnableHttpDependencyInjectionSupport(model.Model); Dictionary<string, object> properties = new Dictionary<string, object> { { "City", "Foo" } }; EntityTagHeaderValue etagHeaderValue = new DefaultODataETagHandler().CreateETag(properties); ODataPath odataPath = new ODataPath(new EntitySetSegment(model.Customers)); request.ODataProperties().Path = odataPath; // Act ETag<Customer> result = request.GetETag<Customer>(etagHeaderValue); dynamic dynamicResult = result; // Assert Assert.Equal("Foo", result["City"]); Assert.Equal("Foo", dynamicResult.City); }
public void SelectAction_Returns_DollarCount(string method, string[] methodsInController, string expectedSelectedAction) { // Arrange const int key = 42; CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var ordersProperty = model.Customer.FindProperty("Orders") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath(new EntitySetPathSegment(model.Customers), new KeyValuePathSegment(key.ToString()), new NavigationPathSegment(ordersProperty), new CountPathSegment()); HttpControllerContext controllerContext = CreateControllerContext(method); controllerContext.Request.ODataProperties().Model = model.Model; var actionMap = GetMockActionMap(methodsInController); // Act string selectedAction = new NavigationRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal(expectedSelectedAction, selectedAction); Assert.Equal(1, controllerContext.RouteData.Values.Count); Assert.Equal(key, controllerContext.RouteData.Values["key"]); }
public void ValidateThrowException_IfNotNavigable() { CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext queryContext = new ODataQueryContext(model.Model, typeof(Customer)); queryContext.RequestContainer = new MockContainer(); model.Model.SetAnnotationValue( model.Customer.FindProperty("Orders"), new QueryableRestrictionsAnnotation(new QueryableRestrictions { NotNavigable = true })); string select = "Orders"; SelectExpandQueryValidator validator = SelectExpandQueryValidator.GetSelectExpandQueryValidator(queryContext); SelectExpandQueryOption selectExpandQueryOption = new SelectExpandQueryOption(select, null, queryContext); ExceptionAssert.Throws <ODataException>( () => validator.Validate(selectExpandQueryOption, new ODataValidationSettings()), "The property 'Orders' cannot be used for navigation."); }
public void ValidateThrowException_IfBaseOrDerivedClassPropertyNotExpandable(string className, string propertyName) { CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.SpecialCustomer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext queryContext = new ODataQueryContext(model.Model, typeof(Customer)); queryContext.RequestContainer = new MockContainer(); EdmEntityType classType = (className == "Customer") ? model.Customer : model.SpecialCustomer; model.Model.SetAnnotationValue(classType.FindProperty(propertyName), new QueryableRestrictionsAnnotation(new QueryableRestrictions { NotExpandable = true })); string expand = "NS.SpecialCustomer/" + propertyName; SelectExpandQueryValidator validator = SelectExpandQueryValidator.GetSelectExpandQueryValidator(queryContext); SelectExpandQueryOption selectExpandQueryOption = new SelectExpandQueryOption(null, expand, queryContext); ExceptionAssert.Throws <ODataException>( () => validator.Validate(selectExpandQueryOption, new ODataValidationSettings()), String.Format(CultureInfo.InvariantCulture, "The property '{0}' cannot be used in the $expand query option.", propertyName)); }
public void SelectAction_Returns_ExpectedMethod_OnNonCollectionValuedNavigationProperty(string method, string[] methodsInController, string expectedSelectedAction) { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var customerProperty = model.Order.FindProperty("Customer") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath(new EntitySetPathSegment(model.Orders), new KeyValuePathSegment("1"), new NavigationPathSegment(customerProperty)); HttpControllerContext controllerContext = CreateControllerContext(method); controllerContext.Request.ODataProperties().Model = model.Model; var actionMap = GetMockActionMap(methodsInController); // Act string selectedAction = new NavigationRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal(expectedSelectedAction, selectedAction); Assert.Equal(1, controllerContext.RouteData.Values["key"]); }
public void SelectAction_Returns_ExpectedMethod_OnNonCollectionValuedNavigationProperty(string method, string[] methodsInController, string expectedSelectedAction) { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var customerProperty = model.Order.FindProperty("Customer") as IEdmNavigationProperty; var keys = new[] { new KeyValuePair <string, object>("ID", 1) }; ODataPath odataPath = new ODataPath(new EntitySetSegment(model.Orders), new KeySegment(keys, model.Order, model.Orders), new NavigationPropertySegment(customerProperty, model.Customers)); var request = RequestFactory.Create(new HttpMethod(method), "http://localhost/"); var actionMap = SelectActionHelper.CreateActionMap(methodsInController); // Act string selectedAction = SelectActionHelper.SelectAction(new NavigationRoutingConvention(), odataPath, request, actionMap); // Assert Assert.Equal(expectedSelectedAction, selectedAction); Assert.Equal(1, SelectActionHelper.GetRouteData(request).Values["key"]); Assert.Equal(1, SelectActionHelper.GetRouteData(request).Values["keyID"]); }
public void ApplyNavigationProperty_UsesThePropertyAlias_ForFeed() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(Customer))); model.Model.SetAnnotationValue(model.Order, new ClrTypeAnnotation(typeof(Order))); model.Model.SetAnnotationValue( model.Customer.FindProperty("Orders"), new ClrPropertyInfoAnnotation(typeof(Customer).GetProperty("AliasedOrders"))); ODataFeedWithEntries feedWrapper = new ODataFeedWithEntries(new ODataFeed()); feedWrapper.Entries.Add(new ODataEntryWithNavigationLinks( new ODataEntry { Properties = new[] { new ODataProperty { Name = "ID", Value = 42 } } })); Customer customer = new Customer(); ODataNavigationLinkWithItems navLink = new ODataNavigationLinkWithItems(new ODataNavigationLink { Name = "Orders" }); navLink.NestedItems.Add(feedWrapper); ODataDeserializerContext context = new ODataDeserializerContext { Model = model.Model }; // Act new ODataEntityDeserializer(_deserializerProvider) .ApplyNavigationProperty(customer, navLink, model.Customer.AsReference(), context); // Assert Assert.Equal(1, customer.AliasedOrders.Count()); Assert.Equal(42, customer.AliasedOrders[0].ID); }
public void Ctor_ThatBuildsNestedContext_InitializesRightValues() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); SelectExpandClause selectExpand = new SelectExpandClause(new SelectItem[0], allSelected: true); IEdmNavigationProperty navProp = model.Customer.NavigationProperties().First(); ODataSerializerContext context = new ODataSerializerContext { EntitySet = model.Customers, Model = model.Model }; EntityInstanceContext entity = new EntityInstanceContext { SerializerContext = context }; // Act ODataSerializerContext nestedContext = new ODataSerializerContext(entity, selectExpand, navProp); // Assert Assert.Same(entity, nestedContext.ExpandedEntity); Assert.Same(navProp, nestedContext.NavigationProperty); Assert.Same(selectExpand, nestedContext.SelectExpandClause); Assert.Same(model.Orders, nestedContext.EntitySet); }
public void SelectAction_OnEntitySetPath_OpenEntityType_ReturnsTheActionName(string url) { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath odataPath = new DefaultODataPathHandler().Parse(model.Model, "http://localhost/", url); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/"); var actionMap = SelectActionHelper.CreateActionMap("GetDynamicProperty"); // Act string selectedAction = SelectActionHelper.SelectAction(_routingConvention, odataPath, request, actionMap); // Assert Assert.NotNull(selectedAction); Assert.Equal("GetDynamicProperty", selectedAction); var routeData = SelectActionHelper.GetRouteData(request); Assert.Equal(3, routeData.Values.Count); Assert.Equal(7, routeData.Values["key"]); Assert.Equal("DynamicPropertyA", routeData.Values["dynamicProperty"]); Assert.Equal("DynamicPropertyA", (routeData.Values[ODataParameterValue.ParameterValuePrefix + "dynamicProperty"] as ODataParameterValue).Value); }
public void SelectAction_Returns_DollarCount(string method, string[] methodsInController, string expectedSelectedAction) { // Arrange var keys = new[] { new KeyValuePair <string, object>("ID", 42) }; CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var ordersProperty = model.Customer.FindProperty("Orders") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath(new EntitySetSegment(model.Customers), new KeySegment(keys, model.Customer, model.Customers), new NavigationPropertySegment(ordersProperty, model.Orders), CountSegment.Instance); var request = RequestFactory.Create(new HttpMethod(method), "http://localhost/"); var actionMap = SelectActionHelper.CreateActionMap(methodsInController); // Act string selectedAction = SelectActionHelper.SelectAction(new NavigationRoutingConvention(), odataPath, request, actionMap); // Assert Assert.Equal(expectedSelectedAction, selectedAction); Assert.Equal(2, SelectActionHelper.GetRouteData(request).Values.Count); Assert.Equal(42, SelectActionHelper.GetRouteData(request).Values["key"]); Assert.Equal(42, SelectActionHelper.GetRouteData(request).Values["keyID"]); Assert.Equal(1, SelectActionHelper.GetRoutingConventionsStore(request)[ODataRouteConstants.KeyCount]); }
public void SelectAction_SetsRouteData_ForGetOrCreateRefRequests(string method, string actionName) { // Arrange var keys = new[] { new KeyValuePair <string, object>("ID", 42) }; CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var specialOrdersProperty = model.SpecialCustomer.FindProperty("SpecialOrders") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath( new EntitySetSegment(model.Customers), new KeySegment(keys, model.Customer, model.Customers), new TypeSegment(model.SpecialCustomer, model.Customers), new NavigationPropertyLinkSegment(specialOrdersProperty, model.Orders)); var request = RequestFactory.Create(new HttpMethod(method), "http://localhost/"); var actionMap = SelectActionHelper.CreateActionMap(actionName); // Act string selectedAction = SelectActionHelper.SelectAction(new RefRoutingConvention(), odataPath, request, actionMap); // Assert Assert.Equal(42, SelectActionHelper.GetRouteData(request).Values["key"]); Assert.Equal("SpecialOrders", SelectActionHelper.GetRouteData(request).Values["navigationProperty"]); }
public SelectExpandBinderTest() { _settings = new ODataQuerySettings { HandleNullPropagation = HandleNullPropagationOption.False }; _model = new CustomersModelWithInheritance(); _model.Model.SetAnnotationValue <ClrTypeAnnotation>(_model.Customer, new ClrTypeAnnotation(typeof(Customer))); _model.Model.SetAnnotationValue <ClrTypeAnnotation>(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(SpecialCustomer))); _context = new ODataQueryContext(_model.Model, typeof(Customer)) { RequestContainer = new MockContainer() }; _binder = new SelectExpandBinder(_settings, new SelectExpandQueryOption("*", "", _context)); Customer customer = new Customer(); Order order = new Order { Customer = customer }; customer.Orders.Add(order); _queryable = new[] { customer }.AsQueryable(); }
public void CtorTakingHttpConfiguration_InitializesAttributeMappings_OnFirstSelectControllerCall() { // Arrange HttpConfiguration config = new HttpConfiguration(); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPathTemplate pathTemplate = new ODataPathTemplate(); Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>(); pathTemplateHandler.Setup(p => p.ParseTemplate(model.Model, "Customers")).Returns(pathTemplate).Verifiable(); AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, config, pathTemplateHandler.Object); config.EnsureInitialized(); // Act convention.SelectController(new ODataPath(), new HttpRequestMessage()); // Assert pathTemplateHandler.VerifyAll(); Assert.NotNull(convention.AttributeMappings); Assert.Equal("GetCustomers", convention.AttributeMappings[pathTemplate].ActionName); }
public void Ctor_ThatBuildsNestedContext_InitializesRightValues_ForComplex() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); SelectExpandClause selectExpand = new SelectExpandClause(new SelectItem[0], allSelected: true); IEdmProperty complexProperty = model.Customer.Properties().First(p => p.Name == "Address"); ODataSerializerContext context = new ODataSerializerContext { NavigationSource = model.Customers, Model = model.Model }; ResourceContext resource = new ResourceContext { SerializerContext = context }; // Act ODataSerializerContext nestedContext = new ODataSerializerContext(resource, selectExpand, complexProperty); // Assert Assert.Same(resource, nestedContext.ExpandedResource); Assert.Null(nestedContext.NavigationProperty); Assert.Same(selectExpand, nestedContext.SelectExpandClause); Assert.Null(nestedContext.NavigationSource); Assert.Same(complexProperty, nestedContext.EdmProperty); }
public void SelectAction_ReturnsNull_IfActionIsMissing() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath odataPath = new DefaultODataPathHandler().Parse(model.Model, _serviceRoot, "Customers(10)/Name"); ILookup <string, HttpActionDescriptor> emptyActionMap = new HttpActionDescriptor[0].ToLookup(desc => (string)null); HttpRequestContext requestContext = new HttpRequestContext(); HttpControllerContext controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); // Act string selectedAction = new PropertyRoutingConvention().SelectAction(odataPath, controllerContext, emptyActionMap); // Assert Assert.Null(selectedAction); Assert.Empty(controllerContext.Request.GetRouteData().Values); }
public async Task AttributeRouting_SelectsExpectedControllerAndAction(string method, string requestUri, string expectedResult) { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var controllers = new[] { typeof(CustomersController), typeof(MetadataAndServiceController), typeof(OrdersController) }; TestAssemblyResolver resolver = new TestAssemblyResolver(new MockAssembly(controllers)); HttpConfiguration config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; config.Services.Replace(typeof(IAssembliesResolver), resolver); config.MapODataServiceRoute("odata", "", model.Model); HttpServer server = new HttpServer(config); config.EnsureInitialized(); HttpClient client = new HttpClient(server); HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), requestUri); request.SetFakeODataRouteName(); // Act var response = await client.SendAsync(request); // Assert if (!response.IsSuccessStatusCode) { Assert.False(true, await response.Content.ReadAsStringAsync()); } var result = await response.Content.ReadAsAsync <AttributeRoutingTestODataResponse>(); Assert.Equal(expectedResult, result.Value); }
public void SelectAction_OnSingletonPath_ReturnsTheActionNameWithCast(string httpMethod, string prefix) { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath odataPath = new DefaultODataPathHandler().Parse(model.Model, _serviceRoot, "VipCustomer/Account/NS.SpecialAccount"); ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => prefix + "AccountOfSpecialAccountFromCustomer"); HttpRequestContext requestContext = new HttpRequestContext(); HttpControllerContext controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(new HttpMethod(httpMethod), "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); // Act string selectedAction = new PropertyRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.NotNull(selectedAction); Assert.Equal(prefix + "AccountOfSpecialAccountFromCustomer", selectedAction); Assert.Empty(controllerContext.Request.GetRouteData().Values); }
public void SelectAction_ReturnsTheActionName_ForSingletonActionBoundToEntity() { // Arrange ActionRoutingConvention actionConvention = new ActionRoutingConvention(); IEdmModel model = new CustomersModelWithInheritance().Model; ODataPath odataPath = new DefaultODataPathHandler().Parse(model, _serviceRoot, "VipCustomer/NS.upgrade"); HttpRequestContext requestContext = new HttpRequestContext(); HttpControllerContext controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "upgrade"); // Act string action = actionConvention.SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal("upgrade", action); Assert.Equal(0, controllerContext.Request.GetRouteData().Values.Count); }
public void CreateODataFeed_SetsODataOperations() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); IEdmCollectionTypeReference customersType = new EdmCollectionTypeReference(new EdmCollectionType(model.Customer.AsReference())); ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); ODataSerializerContext context = new ODataSerializerContext { NavigationSource = model.Customers, Request = new HttpRequestMessage(), Model = model.Model, MetadataLevel = ODataMetadataLevel.FullMetadata, Url = CreateMetadataLinkFactory("http://IgnoreMetadataPath") }; var result = new object[0]; // Act ODataFeed feed = serializer.CreateODataFeed(result, customersType, context); // Assert Assert.Equal(1, feed.Actions.Count()); Assert.Equal(2, feed.Functions.Count()); }
public void SelectAction_ReturnsFunctionName_ForFunctionOnEntityCollection() { // Arrange FunctionRoutingConvention functionConvention = new FunctionRoutingConvention(); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath odataPath = new DefaultODataPathHandler().Parse(model.Model, _serviceRoot, "Customers/NS.IsAnyUpgraded"); HttpRequestContext requestContext = new HttpRequestContext(); HttpControllerContext controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "IsAnyUpgraded"); // Act string function = functionConvention.SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal("IsAnyUpgraded", function); Assert.Empty(controllerContext.Request.GetRouteData().Values); }
public void SelectAction_ReturnsNull_IfNotCorrectMethod(string methodName) { HttpMethod method = new HttpMethod(methodName); // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataPath odataPath = new DefaultODataPathHandler().Parse(model.Model, "http://localhost/", "Orders(7)/DynamicPropertyA"); ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "GetDynamicProperty"); HttpRequestContext requestContext = new HttpRequestContext(); HttpControllerContext controllerContext = new HttpControllerContext { Request = new HttpRequestMessage(method, "http://localhost/"), RequestContext = requestContext, RouteData = new HttpRouteData(new HttpRoute()) }; controllerContext.Request.SetRequestContext(requestContext); // Act string selectedAction = _routingConvention.SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Null(selectedAction); Assert.Empty(controllerContext.Request.GetRouteData().Values); }
public void SelectAction_SetsRelatedKey_ForDeleteLinkRequests() { // Arrange string key = "42"; string relatedKey = "24"; CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var specialOrdersProperty = model.SpecialCustomer.FindProperty("SpecialOrders") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath(new EntitySetPathSegment(model.Customers), new KeyValuePathSegment(key), new CastPathSegment(model.SpecialCustomer), new LinksPathSegment(), new NavigationPathSegment(specialOrdersProperty), new KeyValuePathSegment(relatedKey)); HttpControllerContext controllerContext = CreateControllerContext("DELETE"); var actionMap = GetMockActionMap("DeleteLink"); // Act new LinksRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); var routeData = controllerContext.RouteData; // Assert Assert.Equal(key, routeData.Values["key"]); Assert.Equal("SpecialOrders", routeData.Values["navigationProperty"]); Assert.Equal(relatedKey, routeData.Values["relatedKey"]); }
public void SelectAction_Returns_ExpectedMethodOnDerivedType(string method, string[] methodsInController, string expectedSelectedAction) { // Arrange int key = 42; CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var specialOrdersProperty = model.SpecialCustomer.FindProperty("SpecialOrders") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath( new EntitySetPathSegment(model.Customers), new KeyValuePathSegment(key.ToString()), new CastPathSegment(model.SpecialCustomer), new NavigationPathSegment(specialOrdersProperty), new RefPathSegment()); HttpControllerContext controllerContext = CreateControllerContext(method); controllerContext.Request.ODataProperties().Model = model.Model; var actionMap = GetMockActionMap(methodsInController); // Act string selectedAction = new RefRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal(expectedSelectedAction, selectedAction); if (expectedSelectedAction == null) { Assert.Empty(controllerContext.RouteData.Values); } else { Assert.Equal(2, controllerContext.RouteData.Values.Count); Assert.Equal(key, controllerContext.RouteData.Values["key"]); Assert.Equal(specialOrdersProperty.Name, controllerContext.RouteData.Values["navigationProperty"]); } }
public void GenerateLocationHeader_ForContainment() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.OrderLine, new ClrTypeAnnotation(typeof(OrderLine))); ODataPath path = new ODataUriParser(model.Model, new Uri("http://localhost/"), new Uri("MyOrders(1)/OrderLines", UriKind.Relative)).ParsePath(); var request = RequestFactory.Create(opt => opt.AddRouteComponents(model.Model)); request.Configure("", model.Model, path); var orderLine = new OrderLine { ID = 2 }; var createdODataResult = GetCreatedODataResult <OrderLine>(orderLine, request); // Act var locationHeader = createdODataResult.GenerateLocationHeader(request); // Assert Assert.Equal("http://localhost/MyOrders(1)/OrderLines(2)", locationHeader.ToString()); }
public void SelectAction_SetsRouteData_ForGetOrCreateRefRequests(string method, string actionName) { // Arrange var keys = new[] { new KeyValuePair <string, object>("ID", 42) }; CustomersModelWithInheritance model = new CustomersModelWithInheritance(); var specialOrdersProperty = model.SpecialCustomer.FindProperty("SpecialOrders") as IEdmNavigationProperty; ODataPath odataPath = new ODataPath( new EntitySetSegment(model.Customers), new KeySegment(keys, model.Customer, model.Customers), new TypeSegment(model.SpecialCustomer, model.Customers), new NavigationPropertyLinkSegment(specialOrdersProperty, model.Orders)); HttpControllerContext controllerContext = CreateControllerContext(method); var actionMap = new[] { GetMockActionDescriptor(actionName) }.ToLookup(a => a.ActionName); // Act new RefRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); var routeData = controllerContext.RouteData; // Assert Assert.Equal(42, routeData.Values["key"]); Assert.Equal("SpecialOrders", routeData.Values["navigationProperty"]); }
public void SelectAction_WithCast_Returns_ExpectedActionName(string method, string expected) { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); IEdmCollectionType collection = new EdmCollectionType(new EdmEntityTypeReference(model.SpecialCustomer, isNullable: false)); ODataPath odataPath = new ODataPath(new EntitySetSegment(model.Customers), new TypeSegment(collection, model.Customers)); var controllerContext = new HttpControllerContext() { Request = new HttpRequestMessage(new HttpMethod(method), "http://localhost/"), RouteData = new HttpRouteData(new HttpRoute()) }; ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => expected); // Act string actionName = new EntitySetRoutingConvention().SelectAction(odataPath, controllerContext, actionMap); // Assert Assert.Equal(expected, actionName); }
public void Property_LocationHeader_IsEvaluatedLazily() { // Arrange Uri editLink = new Uri("http://edit-link"); Mock <NavigationSourceLinkBuilderAnnotation> linkBuilder = new Mock <NavigationSourceLinkBuilderAnnotation>(); linkBuilder.Setup(b => b.BuildEditLink(It.IsAny <ResourceContext>(), ODataMetadataLevel.Full, null)) .Returns(editLink); CustomersModelWithInheritance model = new CustomersModelWithInheritance(); model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); model.Model.SetNavigationSourceLinkBuilder(model.Customers, linkBuilder.Object); ODataPath path = new ODataPath(new EntitySetSegment(model.Customers)); var request = RequestFactory.Create(model.Model, path: path); TestController controller = CreateController(request); var createdODataResult = GetCreatedODataResult <TestEntity>(_entity, request, controller); // Act Uri locationHeader = createdODataResult.GenerateLocationHeader(request); // Assert Assert.Same(editLink, locationHeader); }
public SelectExpandWrapperTest() { _model = new CustomersModelWithInheritance(); }