public static IEdmModel SimpleCustomerOrderModel() { var model = new EdmModel(); var customerType = new EdmEntityType("Default", "Customer"); customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32); customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String); customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( EdmPrimitiveTypeKind.String, isNullable: true); customerType.AddStructuralProperty( "City", primitiveTypeReference, defaultValue: null, concurrencyMode: EdmConcurrencyMode.Fixed); model.AddElement(customerType); var orderType = new EdmEntityType("Default", "Order"); orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32); orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); orderType.AddStructuralProperty("Shipment", EdmPrimitiveTypeKind.String); model.AddElement(orderType); var addressType = new EdmComplexType("Default", "Address"); addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String); addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String); model.AddElement(addressType); // Add navigations customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many }); orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customer", Target = customerType, TargetMultiplicity = EdmMultiplicity.One }); // Add Entity set var container = new EdmEntityContainer("Default", "Container"); var customerSet = container.AddEntitySet("Customers", customerType); var orderSet = container.AddEntitySet("Orders", orderType); customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet); orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet); NavigationSourceLinkBuilderAnnotation linkAnnotation = new MockNavigationSourceLinkBuilderAnnotation(); model.SetNavigationSourceLinkBuilder(customerSet, linkAnnotation); model.SetNavigationSourceLinkBuilder(orderSet, linkAnnotation); model.AddElement(container); return model; }
public void CreateODataFeed_Sets_FeedSelfLink() { // Arrange var feedInstance = new object[0]; ODataSerializerContext writeContext = new ODataSerializerContext { NavigationSource = _customerSet, Model = _model, Request = new HttpRequestMessage() }; writeContext.Url = new UrlHelper(writeContext.Request); ODataFeedSerializer serializer = new ODataFeedSerializer(new DefaultODataSerializerProvider()); Uri feedSelfLink = new Uri("http://feed_self_link/"); NavigationSourceLinkBuilderAnnotation linkBuilder = new MockNavigationSourceLinkBuilderAnnotation { FeedSelfLinkBuilder = (context) => { Assert.Equal(_customerSet, context.EntitySet); Assert.Equal(feedInstance, context.FeedInstance); Assert.Equal(writeContext.Request, context.Request); Assert.Equal(writeContext.Url, context.Url); return feedSelfLink; } }; _model.SetNavigationSourceLinkBuilder(_customerSet, linkBuilder); // Act ODataFeed feed = serializer.CreateODataFeed(feedInstance, _customersType, writeContext); // Assert AtomFeedMetadata feedMetadata = feed.GetAnnotation<AtomFeedMetadata>(); Assert.Equal(feedSelfLink, feedMetadata.SelfLink.Href); Assert.Equal("self", feedMetadata.SelfLink.Relation); }
public void WriteObjectInline_WritesCorrectEditLink() { // Arrange EntityInstanceContext instanceContext = new EntityInstanceContext { SerializerContext = _writeContext, EntityType = _customerType.EntityDefinition() }; bool customEditLinkbuilderCalled = false; NavigationSourceLinkBuilderAnnotation linkAnnotation = new MockNavigationSourceLinkBuilderAnnotation { EditLinkBuilder = new SelfLinkBuilder<Uri>((EntityInstanceContext context) => { Assert.Same(instanceContext, context); customEditLinkbuilderCalled = true; return new Uri("http://sample_edit_link"); }, followsConventions: false) }; _model.SetNavigationSourceLinkBuilder(_customerSet, linkAnnotation); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; SelectExpandNode selectExpandNode = new SelectExpandNode(); // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, instanceContext); // Assert Assert.True(customEditLinkbuilderCalled); }
public void CreateNavigationLink_CreatesCorrectNavigationLink() { // Arrange Uri navigationLinkUri = new Uri("http://navigation_link"); IEdmNavigationProperty property1 = CreateFakeNavigationProperty("Property1", _customerType); NavigationSourceLinkBuilderAnnotation linkAnnotation = new MockNavigationSourceLinkBuilderAnnotation { NavigationLinkBuilder = (ctxt, property, metadataLevel) => navigationLinkUri }; _model.SetNavigationSourceLinkBuilder(_customerSet, linkAnnotation); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; // Act ODataNavigationLink navigationLink = serializer.Object.CreateNavigationLink(property1, _entityInstanceContext); // Assert Assert.Equal("Property1", navigationLink.Name); Assert.Equal(navigationLinkUri, navigationLink.Url); }