public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext) { ODataEntry entry = base.CreateEntry(selectExpandNode, entityInstanceContext); Document document = entityInstanceContext.EntityInstance as Document; if (entry != null && document != null) { // annotate the document with the score. entry.InstanceAnnotations.Add(new ODataInstanceAnnotation("org.northwind.search.score", new ODataPrimitiveValue(document.Score))); } return entry; }
[InlineData("ID", "Orders($select=ID),Orders($expand=Customer($select=ID))", true, "ID")] // deep expand and selects public void GetPropertiesToBeSelected_Selects_ExpectedProperties_OnCustomer( string select, string expand, bool specialCustomer, string structuralPropertiesToSelect) { // Arrange SelectExpandClause selectExpandClause = new ODataUriParser(_model.Model, serviceRoot: null).ParseSelectAndExpand(select, expand, _model.Customer, _model.Customers); IEdmEntityType entityType = specialCustomer ? _model.SpecialCustomer : _model.Customer; // Act SelectExpandNode selectExpandNode = new SelectExpandNode(selectExpandClause, entityType, _model.Model); var result = selectExpandNode.SelectedStructuralProperties; // Assert Assert.Equal(structuralPropertiesToSelect, String.Join(",", result.Select(p => p.Name).OrderBy(n => n))); }
[InlineData("ID", "Orders($select=ID),Orders($expand=Customer($select=ID))", true, "ID")] // deep expand and selects public void GetPropertiesToBeSelected_Selects_ExpectedProperties_OnCustomer( string select, string expand, bool specialCustomer, string structuralPropertiesToSelect) { // Arrange ODataQueryOptionParser parser = new ODataQueryOptionParser(_model.Model, _model.Customer, _model.Customers, new Dictionary<string, string> { { "$select", select }, { "$expand", expand } }); SelectExpandClause selectExpandClause = parser.ParseSelectAndExpand(); IEdmEntityType entityType = specialCustomer ? _model.SpecialCustomer : _model.Customer; // Act SelectExpandNode selectExpandNode = new SelectExpandNode(selectExpandClause, entityType, _model.Model); var result = selectExpandNode.SelectedStructuralProperties; // Assert Assert.Equal(structuralPropertiesToSelect, String.Join(",", result.Select(p => p.Name).OrderBy(n => n))); }
public void CreateEntry_SetsETagToNull_IfRequestIsNull() { // Arrange SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object, new Mock<IEdmStructuralProperty>().Object } }; ODataProperty[] properties = new[] { new ODataProperty(), new ODataProperty() }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext)) .Returns(properties[0]); serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(1), _entityInstanceContext)) .Returns(properties[1]); // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext); // Assert Assert.Null(entry.ETag); }
public void CreateEntry_Calls_CreateStructuralProperty_ForEachSelectedStructuralProperty() { // Arrange SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object, new Mock<IEdmStructuralProperty>().Object } }; ODataProperty[] properties = new ODataProperty[] { new ODataProperty(), new ODataProperty() }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext)) .Returns(properties[0]) .Verifiable(); serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(1), _entityInstanceContext)) .Returns(properties[1]) .Verifiable(); // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext); // Assert serializer.Verify(); Assert.Equal(properties, entry.Properties); }
public void WriteObjectInline_CanWriteExpandedNavigationProperty_DerivedExpandedSingleValuedNavigationPropertyIsNull() { // Arrange IEdmEntityType specialOrderType = (IEdmEntityType)_specialOrderType.Definition; IEdmNavigationProperty customerProperty = specialOrderType.NavigationProperties().Single(p => p.Name == "SpecialCustomer"); Mock<IEdmEntityObject> order = new Mock<IEdmEntityObject>(); object customerValue = null; order.Setup(c => c.TryGetPropertyValue("SpecialCustomer", out customerValue)).Returns(true); order.Setup(c => c.GetEdmType()).Returns(_specialOrderType); IEdmEntityType orderType = (IEdmEntityType)_orderType.Definition; ODataQueryOptionParser parser = new ODataQueryOptionParser( _model, orderType, _orderSet, new Dictionary<string, string> { { "$select", "Default.SpecialOrder/SpecialCustomer" }, { "$expand", "Default.SpecialOrder/SpecialCustomer" } }); SelectExpandClause selectExpandClause = parser.ParseSelectAndExpand(); SelectExpandNode selectExpandNode = new SelectExpandNode(); selectExpandNode.ExpandedNavigationProperties[customerProperty] = selectExpandClause.SelectedItems.OfType<ExpandedNavigationSelectItem>().Single().SelectAndExpand; Mock<ODataWriter> writer = new Mock<ODataWriter>(); writer.Setup(w => w.WriteStart(null as ODataEntry)).Verifiable(); Mock<ODataEdmTypeSerializer> ordersSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Entry); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); serializerProvider.Setup(p => p.GetEdmTypeSerializer(customerProperty.Type)) .Returns(ordersSerializer.Object); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(serializerProvider.Object); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer.CallBase = true; // Act serializer.Object.WriteObjectInline(order.Object, _orderType, writer.Object, _writeContext); // Assert writer.Verify(); }
public void CreateEntry_Works_ToAppendDynamicProperties_ForOpenEntityType() { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmEntitySet customers = model.EntityContainer.FindEntitySet("Customers"); IEdmEntityType customerType = model.FindDeclaredType("Default.Customer") as IEdmEntityType; Type simpleOpenCustomer = typeof(SimpleOpenCustomer); model.SetAnnotationValue(customerType, new ClrTypeAnnotation(simpleOpenCustomer)); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(customerType, new DynamicPropertyDictionaryAnnotation( simpleOpenCustomer.GetProperty("CustomerProperties"))); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataEntityTypeSerializer serializer = new ODataEntityTypeSerializer(serializerProvider); SelectExpandNode selectExpandNode = new SelectExpandNode(null, customerType, model); ODataSerializerContext writeContext = new ODataSerializerContext { Model = model, Path = new ODataPath(new EntitySetPathSegment(customers)) }; SimpleOpenCustomer customer = new SimpleOpenCustomer() { CustomerId = 991, Name = "Name #991", Address = new SimpleOpenAddress { City = "a city", Street = "a street", Properties = new Dictionary<string, object> { {"ArrayProperty", new[] { "15", "14", "13" } } } }, CustomerProperties = new Dictionary<string, object>() }; customer.CustomerProperties.Add("EnumProperty", SimpleEnum.Fourth); customer.CustomerProperties.Add("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); customer.CustomerProperties.Add("ListProperty", new List<int>{5,4,3,2,1}); customer.CustomerProperties.Add("DateTimeProperty", new DateTime(2014, 10, 24)); EntityInstanceContext entityInstanceContext = new EntityInstanceContext(writeContext, customerType.ToEdmTypeReference(false) as IEdmEntityTypeReference, customer); // Act ODataEntry entry = serializer.CreateEntry(selectExpandNode, entityInstanceContext); // Assert Assert.Equal(entry.TypeName, "Default.Customer"); Assert.Equal(7, entry.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(entry.Properties.Where(p => p.Name == "CustomerId")); Assert.Equal(991, street.Value); ODataProperty city = Assert.Single(entry.Properties.Where(p => p.Name == "Name")); Assert.Equal("Name #991", city.Value); // Verify the nested open complex property ODataProperty address = Assert.Single(entry.Properties.Where(p => p.Name == "Address")); ODataComplexValue addressComplexValue = Assert.IsType<ODataComplexValue>(address.Value); ODataProperty addressDynamicProperty = Assert.Single(addressComplexValue.Properties.Where(p => p.Name == "ArrayProperty")); ODataCollectionValue addressCollectionValue = Assert.IsType<ODataCollectionValue>(addressDynamicProperty.Value); Assert.Equal(new[] { "15", "14", "13" }, addressCollectionValue.Items.OfType<string>().ToList()); Assert.Equal("Collection(Edm.String)", addressCollectionValue.TypeName); // Verify the dynamic properties ODataProperty enumProperty = Assert.Single(entry.Properties.Where(p => p.Name == "EnumProperty")); ODataEnumValue enumValue = Assert.IsType<ODataEnumValue>(enumProperty.Value); Assert.Equal("Fourth", enumValue.Value); Assert.Equal("Default.SimpleEnum", enumValue.TypeName); ODataProperty guidProperty = Assert.Single(entry.Properties.Where(p => p.Name == "GuidProperty")); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), guidProperty.Value); ODataProperty listProperty = Assert.Single(entry.Properties.Where(p => p.Name == "ListProperty")); ODataCollectionValue collectionValue = Assert.IsType<ODataCollectionValue>(listProperty.Value); Assert.Equal(new List<int>{5,4,3,2,1}, collectionValue.Items.OfType<int>().ToList()); Assert.Equal("Collection(Edm.Int32)", collectionValue.TypeName); ODataProperty dateTimeProperty = Assert.Single(entry.Properties.Where(p => p.Name == "DateTimeProperty")); Assert.Equal(new DateTimeOffset(new DateTime(2014, 10, 24)), dateTimeProperty.Value); }
[InlineData(null, "NS.SpecialCustomer/SpecialOrders", true, "SpecialOrders")] // expand derived navigation property on derived type -> expand requested public void GetNavigationPropertiesToBeExpanded_Expands_ExpectedProperties( string select, string expand, bool specialCustomer, string navigationPropertiesToExpand) { // Arrange SelectExpandClause selectExpandClause = new ODataUriParser(_model.Model, serviceRoot: null).ParseSelectAndExpand(select, expand, _model.Customer, _model.Customers); IEdmEntityType entityType = specialCustomer ? _model.SpecialCustomer : _model.Customer; // Act SelectExpandNode selectExpandNode = new SelectExpandNode(selectExpandClause, entityType, _model.Model); var result = selectExpandNode.ExpandedNavigationProperties.Keys; // Assert Assert.Equal(navigationPropertiesToExpand, String.Join(",", result.Select(p => p.Name).OrderBy(n => n))); }
public void CreateEntry_UsesCorrectTypeName() { EntityInstanceContext instanceContext = new EntityInstanceContext { EntityType = _customerType.EntityDefinition(), SerializerContext = _writeContext }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; SelectExpandNode selectExpandNode = new SelectExpandNode(); // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, instanceContext); // Assert Assert.Equal("Default.Customer", entry.TypeName); }
public void CreateEntry_IgnoresProperty_IfCreateStructuralPropertyReturnsNull() { // Arrange SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object } }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext)) .Returns<ODataProperty>(null); // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext); // Assert serializer.Verify(); Assert.Empty(entry.Properties); }
public void WriteObjectInline_WritesNavigationLinksReturnedBy_CreateNavigationLink_ForEachSelectedNavigationProperty() { // Arrange SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedNavigationProperties = { new Mock<IEdmNavigationProperty>().Object, new Mock<IEdmNavigationProperty>().Object } }; ODataNavigationLink[] navigationLinks = new[] { new ODataNavigationLink(), new ODataNavigationLink() }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer .Setup(s => s.CreateNavigationLink(selectExpandNode.SelectedNavigationProperties.ElementAt(0), It.IsAny<EntityInstanceContext>())) .Returns(navigationLinks[0]); serializer .Setup(s => s.CreateNavigationLink(selectExpandNode.SelectedNavigationProperties.ElementAt(1), It.IsAny<EntityInstanceContext>())) .Returns(navigationLinks[1]); serializer.CallBase = true; Mock<ODataWriter> writer = new Mock<ODataWriter>(); writer.Setup(w => w.WriteStart(navigationLinks[0])).Verifiable(); writer.Setup(w => w.WriteStart(navigationLinks[1])).Verifiable(); // Act serializer.Object.WriteObjectInline(_customer, _customerType, writer.Object, _writeContext); // Assert writer.Verify(); }
public void WriteObjectInline_Calls_CreateEntry() { // Arrange SelectExpandNode selectExpandNode = new SelectExpandNode(); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); ODataWriter writer = new Mock<ODataWriter>().Object; serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer.Setup(s => s.CreateEntry(selectExpandNode, It.Is<EntityInstanceContext>(e => Verify(e, _customer, _writeContext)))).Verifiable(); serializer.CallBase = true; // Act serializer.Object.WriteObjectInline(_customer, _customerType, writer, _writeContext); // Assert serializer.Verify(); }
public void WriteObjectInline_SetsParentContext_ForExpandedNavigationProperties() { // Arrange ODataWriter mockWriter = new Mock<ODataWriter>().Object; IEdmNavigationProperty ordersProperty = _customerSet.EntityType().DeclaredNavigationProperties().Single(); Mock<ODataEdmTypeSerializer> expandedItemSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Feed); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); serializerProvider.Setup(p => p.GetEdmTypeSerializer(ordersProperty.Type)) .Returns(expandedItemSerializer.Object); SelectExpandNode selectExpandNode = new SelectExpandNode { ExpandedNavigationProperties = { { ordersProperty, new SelectExpandClause(new SelectItem[0], allSelected: true) } } }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(serializerProvider.Object); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer.Setup(s => s.CreateEntry(selectExpandNode, _entityInstanceContext)).Returns(new ODataEntry()); serializer.CallBase = true; // Act serializer.Object.WriteObjectInline(_customer, _customerType, mockWriter, _writeContext); // Assert expandedItemSerializer.Verify( s => s.WriteObjectInline(It.IsAny<object>(), ordersProperty.Type, mockWriter, It.Is<ODataSerializerContext>(c => c.ExpandedEntity.SerializerContext == _writeContext))); }
/// <summary> /// Creates the <see cref="ODataEntry"/> to be written while writing this entity. /// </summary> /// <param name="selectExpandNode">The <see cref="SelectExpandNode"/> describing the response graph.</param> /// <param name="entityInstanceContext">The context for the entity instance being written.</param> /// <returns>The created <see cref="ODataEntry"/>.</returns> public virtual ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext) { if (selectExpandNode == null) { throw Error.ArgumentNull("selectExpandNode"); } if (entityInstanceContext == null) { throw Error.ArgumentNull("entityInstanceContext"); } string typeName = entityInstanceContext.EntityType.FullName(); ODataEntry entry = new ODataEntry { TypeName = typeName, Properties = CreateStructuralPropertyBag(selectExpandNode.SelectedStructuralProperties, entityInstanceContext), Actions = CreateODataActions(selectExpandNode.SelectedActions, entityInstanceContext) }; IEdmEntityType pathType = GetODataPathType(entityInstanceContext.SerializerContext); AddTypeNameAnnotationAsNeeded(entry, pathType, entityInstanceContext.SerializerContext.MetadataLevel); if (entityInstanceContext.EntitySet != null) { IEdmModel model = entityInstanceContext.SerializerContext.Model; EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(entityInstanceContext.EntitySet); EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityInstanceContext, entityInstanceContext.SerializerContext.MetadataLevel); if (selfLinks.IdLink != null) { entry.Id = new Uri(selfLinks.IdLink); } if (selfLinks.ReadLink != null) { entry.ReadLink = selfLinks.ReadLink; } if (selfLinks.EditLink != null) { entry.EditLink = selfLinks.EditLink; } string etag = CreateETag(entityInstanceContext); if (etag != null) { entry.ETag = etag; } } return entry; }
public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext) { ODataEntry entry = base.CreateEntry(selectExpandNode, entityInstanceContext); // instance annotation on entry ODataPrimitiveValue guidValue = new ODataPrimitiveValue(new Guid("A6E07EAC-AD49-4BF7-A06E-203FF4D4B0D8")); entry.InstanceAnnotations.Add(new ODataInstanceAnnotation("Entry.GuidAnnotation", guidValue)); ODataPrimitiveValue strValue = new ODataPrimitiveValue("Hello World."); entry.InstanceAnnotations.Add(new ODataInstanceAnnotation("Hello.World", strValue)); return entry; }
public void CreateEntry_SetsETagToNull_IfModelDontHaveConcurrencyProperty() { // Arrange IEdmEntitySet orderSet = _model.EntityContainer.FindEntitySet("Orders"); Order order = new Order() { Name = "Foo", Shipment = "Bar", ID = 10, }; _writeContext.NavigationSource = orderSet; _entityInstanceContext = new EntityInstanceContext(_writeContext, orderSet.EntityType().AsReference(), order); SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object, new Mock<IEdmStructuralProperty>().Object } }; ODataProperty[] properties = new[] { new ODataProperty(), new ODataProperty() }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext)) .Returns(properties[0]); serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(1), _entityInstanceContext)) .Returns(properties[1]); MockHttpRequestMessage request = new MockHttpRequestMessage(); request.SetConfiguration(new HttpConfiguration()); _entityInstanceContext.Request = request; // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext); // Assert Assert.Null(entry.ETag); }
public void WriteObjectInline_Calls_CreateNavigationLink_ForEachExpandedNavigationProperty() { // Arrange SelectExpandNode selectExpandNode = new SelectExpandNode { ExpandedNavigationProperties = { { new Mock<IEdmNavigationProperty>().Object, null }, { new Mock<IEdmNavigationProperty>().Object, null } } }; Mock<ODataWriter> writer = new Mock<ODataWriter>(); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); var expandedNavigationProperties = selectExpandNode.ExpandedNavigationProperties.ToList(); serializer.Setup(s => s.CreateNavigationLink(expandedNavigationProperties[0].Key, It.IsAny<EntityInstanceContext>())).Verifiable(); serializer.Setup(s => s.CreateNavigationLink(expandedNavigationProperties[1].Key, It.IsAny<EntityInstanceContext>())).Verifiable(); serializer.CallBase = true; // Act serializer.Object.WriteObjectInline(_customer, _customerType, writer.Object, _writeContext); // Assert serializer.Verify(); }
public void CreateEntry_SetsEtagToNotNull_IfWithConcurrencyProperty() { // Arrange Mock<IEdmStructuralProperty> mockConcurrencyProperty = new Mock<IEdmStructuralProperty>(); mockConcurrencyProperty.SetupGet(s => s.ConcurrencyMode).Returns(EdmConcurrencyMode.Fixed); mockConcurrencyProperty.SetupGet(s => s.Name).Returns("City"); SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedStructuralProperties = { new Mock<IEdmStructuralProperty>().Object, mockConcurrencyProperty.Object } }; ODataProperty[] properties = new[] { new ODataProperty(), new ODataProperty() }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(0), _entityInstanceContext)) .Returns(properties[0]); serializer .Setup(s => s.CreateStructuralProperty(selectExpandNode.SelectedStructuralProperties.ElementAt(1), _entityInstanceContext)) .Returns(properties[1]); MockHttpRequestMessage request = new MockHttpRequestMessage(); HttpConfiguration configuration = new HttpConfiguration(); Mock<IETagHandler> mockETagHandler = new Mock<IETagHandler>(); string tag = "\"'anycity'\""; EntityTagHeaderValue etagHeaderValue = new EntityTagHeaderValue(tag, isWeak: true); mockETagHandler.Setup(e => e.CreateETag(It.IsAny<IDictionary<string, object>>())).Returns(etagHeaderValue); configuration.SetETagHandler(mockETagHandler.Object); request.SetConfiguration(configuration); _entityInstanceContext.Request = request; // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext); // Assert Assert.Equal(etagHeaderValue.ToString(), entry.ETag); }
public void WriteObjectInline_ExpandsUsingInnerSerializerUsingRightContext_ExpandedNavigationProperties() { // Arrange IEdmEntityType customerType = _customerSet.EntityType(); IEdmNavigationProperty ordersProperty = customerType.NavigationProperties().Single(p => p.Name == "Orders"); ODataQueryOptionParser parser = new ODataQueryOptionParser(_model, customerType, _customerSet, new Dictionary<string, string> { { "$select", "Orders" }, { "$expand", "Orders" } }); SelectExpandClause selectExpandClause = parser.ParseSelectAndExpand(); SelectExpandNode selectExpandNode = new SelectExpandNode { ExpandedNavigationProperties = { { ordersProperty, selectExpandClause.SelectedItems.OfType<ExpandedNavigationSelectItem>().Single().SelectAndExpand } } }; Mock<ODataWriter> writer = new Mock<ODataWriter>(); Mock<ODataEdmTypeSerializer> innerSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Entry); innerSerializer .Setup(s => s.WriteObjectInline(_customer.Orders, ordersProperty.Type, writer.Object, It.IsAny<ODataSerializerContext>())) .Callback((object o, IEdmTypeReference t, ODataWriter w, ODataSerializerContext context) => { Assert.Same(context.NavigationSource.Name, "Orders"); Assert.Same(context.SelectExpandClause, selectExpandNode.ExpandedNavigationProperties.Single().Value); }) .Verifiable(); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); serializerProvider.Setup(p => p.GetEdmTypeSerializer(ordersProperty.Type)) .Returns(innerSerializer.Object); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(serializerProvider.Object); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer.CallBase = true; _writeContext.SelectExpandClause = selectExpandClause; // Act serializer.Object.WriteObjectInline(_customer, _customerType, writer.Object, _writeContext); // Assert innerSerializer.Verify(); // check that the context is rolled back Assert.Same(_writeContext.NavigationSource.Name, "Customers"); Assert.Same(_writeContext.SelectExpandClause, selectExpandClause); }
public void CreateEntry_Calls_CreateODataAction_ForEachSelectAction() { // Arrange ODataAction[] actions = new ODataAction[] { new ODataAction(), new ODataAction() }; SelectExpandNode selectExpandNode = new SelectExpandNode { SelectedActions = { new Mock<IEdmAction>().Object, new Mock<IEdmAction>().Object } }; Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(_serializerProvider); serializer.CallBase = true; serializer.Setup(s => s.CreateODataAction(selectExpandNode.SelectedActions.ElementAt(0), _entityInstanceContext)).Returns(actions[0]).Verifiable(); serializer.Setup(s => s.CreateODataAction(selectExpandNode.SelectedActions.ElementAt(1), _entityInstanceContext)).Returns(actions[1]).Verifiable(); // Act ODataEntry entry = serializer.Object.CreateEntry(selectExpandNode, _entityInstanceContext); // Assert Assert.Equal(actions, entry.Actions); serializer.Verify(); }
/// <summary> /// Creates the <see cref="SelectExpandNode"/> that describes the set of properties and actions to select and expand while writing this entity. /// </summary> /// <param name="entityInstanceContext">Contains the entity instance being written and the context.</param> /// <returns> /// The <see cref="SelectExpandNode"/> that describes the set of properties and actions to select and expand while writing this entity. /// </returns> public virtual SelectExpandNode CreateSelectExpandNode(EntityInstanceContext entityInstanceContext) { if (entityInstanceContext == null) { throw Error.ArgumentNull("entityInstanceContext"); } ODataSerializerContext writeContext = entityInstanceContext.SerializerContext; IEdmEntityType entityType = entityInstanceContext.EntityType; object selectExpandNode; Tuple<SelectExpandClause, IEdmEntityType> key = Tuple.Create(writeContext.SelectExpandClause, entityType); if (!writeContext.Items.TryGetValue(key, out selectExpandNode)) { // cache the selectExpandNode so that if we are writing a feed we don't have to construct it again. selectExpandNode = new SelectExpandNode(entityType, writeContext); writeContext.Items[key] = selectExpandNode; } return selectExpandNode as SelectExpandNode; }
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); }
/// <summary> /// Creates the <see cref="ODataEntry"/> to be written while writing this entity. /// </summary> /// <param name="selectExpandNode">The <see cref="SelectExpandNode"/> describing the response graph.</param> /// <param name="entityInstanceContext">The context for the entity instance being written.</param> /// <returns>The created <see cref="ODataEntry"/>.</returns> public virtual ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext) { if (selectExpandNode == null) { throw Error.ArgumentNull("selectExpandNode"); } if (entityInstanceContext == null) { throw Error.ArgumentNull("entityInstanceContext"); } string typeName = entityInstanceContext.EntityType.FullName(); ODataEntry entry = new ODataEntry { TypeName = typeName, Properties = CreateStructuralPropertyBag(selectExpandNode.SelectedStructuralProperties, entityInstanceContext), }; // Try to add the dynamic properties if the entity type is open. if ((entityInstanceContext.EntityType.IsOpen && selectExpandNode.SelectAllDynamicProperties) || (entityInstanceContext.EntityType.IsOpen && selectExpandNode.SelectedDynamicProperties.Any())) { IEdmTypeReference entityTypeReference = entityInstanceContext.EntityType.ToEdmTypeReference(isNullable: false); List <ODataProperty> dynamicProperties = AppendDynamicProperties(entityInstanceContext.EdmObject, (IEdmStructuredTypeReference)entityTypeReference, entityInstanceContext.SerializerContext, entry.Properties.ToList(), selectExpandNode.SelectedDynamicProperties.ToArray()); if (dynamicProperties != null) { entry.Properties = entry.Properties.Concat(dynamicProperties); } } IEnumerable <ODataAction> actions = CreateODataActions(selectExpandNode.SelectedActions, entityInstanceContext); foreach (ODataAction action in actions) { entry.AddAction(action); } IEdmEntityType pathType = GetODataPathType(entityInstanceContext.SerializerContext); AddTypeNameAnnotationAsNeeded(entry, pathType, entityInstanceContext.SerializerContext.MetadataLevel); if (entityInstanceContext.NavigationSource != null) { if (!(entityInstanceContext.NavigationSource is IEdmContainedEntitySet)) { IEdmModel model = entityInstanceContext.SerializerContext.Model; NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(entityInstanceContext.NavigationSource); EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityInstanceContext, entityInstanceContext.SerializerContext.MetadataLevel); if (selfLinks.IdLink != null) { entry.Id = selfLinks.IdLink; } if (selfLinks.ReadLink != null) { entry.ReadLink = selfLinks.ReadLink; } if (selfLinks.EditLink != null) { entry.EditLink = selfLinks.EditLink; } } string etag = CreateETag(entityInstanceContext); if (etag != null) { entry.ETag = etag; } } return(entry); }
public void WriteObjectInline_CanExpandNavigationProperty_ContainingEdmObject() { // Arrange IEdmEntityType customerType = _customerSet.EntityType(); IEdmNavigationProperty ordersProperty = customerType.NavigationProperties().Single(p => p.Name == "Orders"); Mock<IEdmObject> orders = new Mock<IEdmObject>(); orders.Setup(o => o.GetEdmType()).Returns(ordersProperty.Type); object ordersValue = orders.Object; Mock<IEdmEntityObject> customer = new Mock<IEdmEntityObject>(); customer.Setup(c => c.TryGetPropertyValue("Orders", out ordersValue)).Returns(true); customer.Setup(c => c.GetEdmType()).Returns(customerType.AsReference()); ODataQueryOptionParser parser = new ODataQueryOptionParser(_model, customerType, _customerSet, new Dictionary<string, string> { { "$select", "Orders" }, { "$expand", "Orders" } }); SelectExpandClause selectExpandClause = parser.ParseSelectAndExpand(); SelectExpandNode selectExpandNode = new SelectExpandNode(); selectExpandNode.ExpandedNavigationProperties[ordersProperty] = selectExpandClause.SelectedItems.OfType<ExpandedNavigationSelectItem>().Single().SelectAndExpand; Mock<ODataWriter> writer = new Mock<ODataWriter>(); Mock<ODataEdmTypeSerializer> ordersSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Entry); ordersSerializer.Setup(s => s.WriteObjectInline(ordersValue, ordersProperty.Type, writer.Object, It.IsAny<ODataSerializerContext>())).Verifiable(); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); serializerProvider.Setup(p => p.GetEdmTypeSerializer(ordersProperty.Type)).Returns(ordersSerializer.Object); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(serializerProvider.Object); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer.CallBase = true; // Act serializer.Object.WriteObjectInline(customer.Object, _customerType, writer.Object, _writeContext); //Assert ordersSerializer.Verify(); }
public void CreateEntry_Works_ToAppendNullDynamicProperties_ForOpenEntityType(bool enableNullDynamicProperty, int count) { // Arrange IEdmModel model = SerializationTestsHelpers.SimpleOpenTypeModel(); IEdmEntitySet customers = model.EntityContainer.FindEntitySet("Customers"); IEdmEntityType customerType = model.FindDeclaredType("Default.Customer") as IEdmEntityType; Type simpleOpenCustomer = typeof(SimpleOpenCustomer); model.SetAnnotationValue(customerType, new ClrTypeAnnotation(simpleOpenCustomer)); IEdmComplexType addressType = model.FindDeclaredType("Default.Address") as IEdmComplexType; Type simpleOpenAddress = typeof(SimpleOpenAddress); model.SetAnnotationValue(addressType, new ClrTypeAnnotation(simpleOpenAddress)); IEdmEnumType enumType = model.FindDeclaredType("Default.SimpleEnum") as IEdmEnumType; Type simpleEnumType = typeof(SimpleEnum); model.SetAnnotationValue(enumType, new ClrTypeAnnotation(simpleEnumType)); model.SetAnnotationValue(customerType, new DynamicPropertyDictionaryAnnotation( simpleOpenCustomer.GetProperty("CustomerProperties"))); model.SetAnnotationValue(addressType, new DynamicPropertyDictionaryAnnotation( simpleOpenAddress.GetProperty("Properties"))); ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); ODataEntityTypeSerializer serializer = new ODataEntityTypeSerializer(serializerProvider); HttpConfiguration config = new HttpConfiguration(); config.SetSerializeNullDynamicProperty(enableNullDynamicProperty); HttpRequestMessage request = new HttpRequestMessage(); request.SetConfiguration(config); SelectExpandNode selectExpandNode = new SelectExpandNode(null, customerType, model); ODataSerializerContext writeContext = new ODataSerializerContext { Model = model, Path = new ODataPath(new EntitySetPathSegment(customers)), Request = request }; SimpleOpenCustomer customer = new SimpleOpenCustomer() { CustomerId = 991, Name = "Name #991", Address = new SimpleOpenAddress { City = "a city", Street = "a street", Properties = new Dictionary<string, object> { { "ArrayProperty", new[] { "15", "14", "13" } } } }, CustomerProperties = new Dictionary<string, object>() }; customer.CustomerProperties.Add("GuidProperty", new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA")); customer.CustomerProperties.Add("NullProperty", null); EntityInstanceContext entityInstanceContext = new EntityInstanceContext(writeContext, customerType.ToEdmTypeReference(false) as IEdmEntityTypeReference, customer); // Act ODataEntry entry = serializer.CreateEntry(selectExpandNode, entityInstanceContext); // Assert Assert.Equal(entry.TypeName, "Default.Customer"); Assert.Equal(count, entry.Properties.Count()); // Verify the declared properties ODataProperty street = Assert.Single(entry.Properties.Where(p => p.Name == "CustomerId")); Assert.Equal(991, street.Value); ODataProperty city = Assert.Single(entry.Properties.Where(p => p.Name == "Name")); Assert.Equal("Name #991", city.Value); // Verify the nested open complex property ODataProperty address = Assert.Single(entry.Properties.Where(p => p.Name == "Address")); ODataComplexValue addressComplexValue = Assert.IsType<ODataComplexValue>(address.Value); ODataProperty addressDynamicProperty = Assert.Single(addressComplexValue.Properties.Where(p => p.Name == "ArrayProperty")); ODataCollectionValue addressCollectionValue = Assert.IsType<ODataCollectionValue>(addressDynamicProperty.Value); Assert.Equal(new[] { "15", "14", "13" }, addressCollectionValue.Items.OfType<string>().ToList()); Assert.Equal("Collection(Edm.String)", addressCollectionValue.TypeName); // Verify the dynamic properties ODataProperty guidProperty = Assert.Single(entry.Properties.Where(p => p.Name == "GuidProperty")); Assert.Equal(new Guid("181D3A20-B41A-489F-9F15-F91F0F6C9ECA"), guidProperty.Value); ODataProperty nullProperty = entry.Properties.SingleOrDefault(p => p.Name == "NullProperty"); if (enableNullDynamicProperty) { Assert.NotNull(nullProperty); Assert.Null(nullProperty.Value); } else { Assert.Null(nullProperty); } }
/// <summary> /// Creates the <see cref="ODataEntry"/> to be written while writing this entity. /// </summary> /// <param name="selectExpandNode">The <see cref="SelectExpandNode"/> describing the response graph.</param> /// <param name="entityInstanceContext">The context for the entity instance being written.</param> /// <returns>The created <see cref="ODataEntry"/>.</returns> public virtual ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext) { if (selectExpandNode == null) { throw Error.ArgumentNull("selectExpandNode"); } if (entityInstanceContext == null) { throw Error.ArgumentNull("entityInstanceContext"); } string typeName = entityInstanceContext.EntityType.FullName(); ODataEntry entry = new ODataEntry { TypeName = typeName, Properties = CreateStructuralPropertyBag(selectExpandNode.SelectedStructuralProperties, entityInstanceContext), }; // Try to add the dynamic properties if the entity type is open. if ((entityInstanceContext.EntityType.IsOpen && selectExpandNode.SelectAllDynamicProperties) || (entityInstanceContext.EntityType.IsOpen && selectExpandNode.SelectedDynamicProperties.Any())) { IEdmTypeReference entityTypeReference = entityInstanceContext.EntityType.ToEdmTypeReference(isNullable: false); List<ODataProperty> dynamicProperties = AppendDynamicProperties(entityInstanceContext.EdmObject, (IEdmStructuredTypeReference)entityTypeReference, entityInstanceContext.SerializerContext, entry.Properties.ToList(), selectExpandNode.SelectedDynamicProperties.ToArray()); if (dynamicProperties != null) { entry.Properties = entry.Properties.Concat(dynamicProperties); } } IEnumerable<ODataAction> actions = CreateODataActions(selectExpandNode.SelectedActions, entityInstanceContext); foreach (ODataAction action in actions) { entry.AddAction(action); } IEdmEntityType pathType = GetODataPathType(entityInstanceContext.SerializerContext); AddTypeNameAnnotationAsNeeded(entry, pathType, entityInstanceContext.SerializerContext.MetadataLevel); if (entityInstanceContext.NavigationSource != null) { if (!(entityInstanceContext.NavigationSource is IEdmContainedEntitySet)) { IEdmModel model = entityInstanceContext.SerializerContext.Model; NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(entityInstanceContext.NavigationSource); EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityInstanceContext, entityInstanceContext.SerializerContext.MetadataLevel); if (selfLinks.IdLink != null) { entry.Id = selfLinks.IdLink; } if (selfLinks.ReadLink != null) { entry.ReadLink = selfLinks.ReadLink; } if (selfLinks.EditLink != null) { entry.EditLink = selfLinks.EditLink; } } string etag = CreateETag(entityInstanceContext); if (etag != null) { entry.ETag = etag; } } return entry; }
public void WriteObjectInline_CanWriteExpandedNavigationProperty_DerivedExpandedCollectionValuedNavigationPropertyIsNull() { // Arrange IEdmEntityType specialCustomerType = (IEdmEntityType)_specialCustomerType.Definition; IEdmNavigationProperty specialOrdersProperty = specialCustomerType.NavigationProperties().Single(p => p.Name == "SpecialOrders"); Mock<IEdmEntityObject> customer = new Mock<IEdmEntityObject>(); object specialOrdersValue = null; customer.Setup(c => c.TryGetPropertyValue("SpecialOrders", out specialOrdersValue)).Returns(true); customer.Setup(c => c.GetEdmType()).Returns(_specialCustomerType); IEdmEntityType customerType = _customerSet.EntityType(); ODataQueryOptionParser parser = new ODataQueryOptionParser( _model, customerType, _customerSet, new Dictionary<string, string> { { "$select", "Default.SpecialCustomer/SpecialOrders" }, { "$expand", "Default.SpecialCustomer/SpecialOrders" } }); SelectExpandClause selectExpandClause = parser.ParseSelectAndExpand(); SelectExpandNode selectExpandNode = new SelectExpandNode(); selectExpandNode.ExpandedNavigationProperties[specialOrdersProperty] = selectExpandClause.SelectedItems.OfType<ExpandedNavigationSelectItem>().Single().SelectAndExpand; Mock<ODataWriter> writer = new Mock<ODataWriter>(); writer.Setup(w => w.WriteStart(It.IsAny<ODataFeed>())).Callback( (ODataFeed feed) => { Assert.Null(feed.Count); Assert.Null(feed.DeltaLink); Assert.Null(feed.Id); Assert.Empty(feed.InstanceAnnotations); Assert.Null(feed.NextPageLink); }).Verifiable(); Mock<ODataEdmTypeSerializer> ordersSerializer = new Mock<ODataEdmTypeSerializer>(ODataPayloadKind.Entry); Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>(); serializerProvider.Setup(p => p.GetEdmTypeSerializer(specialOrdersProperty.Type)) .Returns(ordersSerializer.Object); Mock<ODataEntityTypeSerializer> serializer = new Mock<ODataEntityTypeSerializer>(serializerProvider.Object); serializer.Setup(s => s.CreateSelectExpandNode(It.IsAny<EntityInstanceContext>())).Returns(selectExpandNode); serializer.CallBase = true; // Act serializer.Object.WriteObjectInline(customer.Object, _customerType, writer.Object, _writeContext); // Assert writer.Verify(); }