/// <summary> /// Deserializes the given <paramref name="link"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="link">The given delta link.</param> /// <param name="elementType">The element type.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The created object.</returns> public virtual object ReadDeltaLink(ODataDeltaLinkWrapper link, IEdmStructuredTypeReference elementType, ODataDeserializerContext readContext) { if (link == null) { throw Error.ArgumentNull(nameof(link)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } if (readContext.IsNoClrType) { EdmDeltaLink edmDeltaLink = new EdmDeltaLink(elementType?.Definition as IEdmEntityType); edmDeltaLink.Source = link.DeltaLink.Source; edmDeltaLink.Target = link.DeltaLink.Target; edmDeltaLink.Relationship = link.DeltaLink.Relationship; return(edmDeltaLink); } else { // refactor how to get the CLR type Type elementClrType = readContext.Model.GetClrType(elementType); Type deltaLinkType = typeof(DeltaLink <>).MakeGenericType(elementClrType); IDeltaLink deltaLink = Activator.CreateInstance(deltaLinkType) as IDeltaLink; deltaLink.Source = link.DeltaLink.Source; deltaLink.Target = link.DeltaLink.Target; deltaLink.Relationship = link.DeltaLink.Relationship; return(deltaLink); } }
public void ReadDeltaLink_ThrowsArgumentNull_ForInputParameters() { // Arrange & Act & Assert Mock <IODataDeserializerProvider> deserializerProvider = new Mock <IODataDeserializerProvider>(); ODataDeltaResourceSetDeserializer deserializer = new ODataDeltaResourceSetDeserializer(deserializerProvider.Object); ExceptionAssert.ThrowsArgumentNull(() => deserializer.ReadDeltaLink(null, null, null), "link"); // Arrange & Act & Assert ODataDeltaLinkWrapper wrapper = new ODataDeltaLinkWrapper( new ODataDeltaLink(new Uri("http://localhost"), new Uri("http://localhost"), "delete")); ExceptionAssert.ThrowsArgumentNull(() => deserializer.ReadDeltaLink(wrapper, null, null), "readContext"); }
public void ReadDeltaLink_Returns_DeltaLink(bool typed) { // Arrange Mock <IODataDeserializerProvider> deserializerProvider = new Mock <IODataDeserializerProvider>(); ODataDeltaResourceSetDeserializer deserializer = new ODataDeltaResourceSetDeserializer(deserializerProvider.Object); Uri source = new Uri("Customers(8)", UriKind.RelativeOrAbsolute); Uri target = new Uri("Orders(10645)", UriKind.RelativeOrAbsolute); ODataDeltaLink link = new ODataDeltaLink(source, target, "Orders"); ODataDeltaLinkWrapper wrapper = new ODataDeltaLinkWrapper(link); IEdmModel model = GetEdmModel(); IEdmEntityType customer = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer"); IEdmStructuredTypeReference elementType = new EdmEntityTypeReference(customer, true); ODataDeserializerContext context = new ODataDeserializerContext { Model = model, ResourceType = typeof(DeltaSet <>) }; if (typed) { context.ResourceType = typeof(DeltaSet <>); } else { context.ResourceType = typeof(EdmChangedObjectCollection); } // Act object deltaLinkObject = deserializer.ReadDeltaLink(wrapper, elementType, context); // Assert if (typed) { DeltaLink <Customer> deltaLink = Assert.IsType <DeltaLink <Customer> >(deltaLinkObject); Assert.Equal(source, deltaLink.Source); Assert.Equal(target, deltaLink.Target); Assert.Equal("Orders", deltaLink.Relationship); } else { EdmDeltaLink deltaLink = Assert.IsType <EdmDeltaLink>(deltaLinkObject); Assert.Equal(source, deltaLink.Source); Assert.Equal(target, deltaLink.Target); Assert.Equal("Orders", deltaLink.Relationship); } }
/// <summary> /// Deserializes the given <paramref name="deltaResourceSet"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="deltaResourceSet">The delta resource set to deserialize.</param> /// <param name="elementType">The element type.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The deserialized resource set object.</returns> public virtual IEnumerable ReadDeltaResourceSet(ODataDeltaResourceSetWrapper deltaResourceSet, IEdmStructuredTypeReference elementType, ODataDeserializerContext readContext) { if (deltaResourceSet == null) { throw Error.ArgumentNull(nameof(deltaResourceSet)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } // Delta Items foreach (ODataItemWrapper itemWrapper in deltaResourceSet.DeltaItems) { // Deleted Link ODataDeltaDeletedLinkWrapper deletedLinkWrapper = itemWrapper as ODataDeltaDeletedLinkWrapper; if (deletedLinkWrapper != null) { yield return(ReadDeltaDeletedLink(deletedLinkWrapper, elementType, readContext)); } // Added Link ODataDeltaLinkWrapper deltaLinkWrapper = itemWrapper as ODataDeltaLinkWrapper; if (deltaLinkWrapper != null) { yield return(ReadDeltaLink(deltaLinkWrapper, elementType, readContext)); } // Added resource, updated resource and deleted resource ODataResourceWrapper resourceWrapper = itemWrapper as ODataResourceWrapper; if (resourceWrapper != null) { yield return(ReadDeltaResource(resourceWrapper, elementType, readContext)); } } }
/// <summary> /// Deserializes the given <paramref name="link"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="link">The given delta link.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The created <see cref="IEdmDeltaLink"/>.</returns> public virtual IEdmDeltaLink ReadDeltaLink(ODataDeltaLinkWrapper link, ODataDeserializerContext readContext) { // TODO: return(null); }
public async Task ReadDeletedLinkInDeltaResourceSetWorksAsExpected() { // Arrange string payload = "{" + "\"@odata.context\":\"http://localhost/$metadata#Customers/$delta\"," + "\"@odata.count\":5," + "\"value\":[" + "{" + "\"@odata.id\":\"Customers(42)\"," + "\"Name\":\"Sammy\"" + "}," + "{" + "\"@odata.context\":\"http://localhost/$metadata#Customers/$deletedLink\"," + "\"source\":\"Customers(39)\"," + "\"relationship\":\"Orders\"," + "\"target\":\"Orders(10643)\"" + "}," + "{" + "\"@odata.context\":\"http://localhost/$metadata#Customers/$link\"," + "\"source\":\"Customers(32)\"," + "\"relationship\":\"Orders\"," + "\"target\":\"Orders(10645)\"" + "}," + "{" + "\"@odata.context\":\"http://localhost/$metadata#Orders/$entity\"," + "\"@odata.id\":\"Orders(10643)\"," + "\"Price\": 82" + "}," + "{" + "\"@odata.context\":\"http://localhost/$metadata#Customers/$deletedEntity\"," + "\"id\":\"Customers(21)\"," + "\"reason\":\"deleted\"" + "}" + "]," + "\"@odata.deltaLink\":\"Customers?$expand=Orders&$deltatoken=8015\"" + "}"; IEdmEntitySet customers = Model.EntityContainer.FindEntitySet("Customers"); Assert.NotNull(customers); // Guard // Act Func <ODataMessageReader, Task <ODataReader> > func = mr => mr.CreateODataDeltaResourceSetReaderAsync(customers, customers.EntityType()); ODataItemWrapper item = await ReadPayloadAsync(payload, Model, func); // Assert Assert.NotNull(item); // --- DeltaResourceSet // |--- Resource (1) // |--- Resource (2) // |--- DeletedResource (1) // | // |--- DeltaDeletedLink // |--- DeltaLink ODataDeltaResourceSetWrapper deltaResourceSet = Assert.IsType <ODataDeltaResourceSetWrapper>(item); // Resources Assert.Equal(3, deltaResourceSet.ResourceBases.Count); Assert.Collection(deltaResourceSet.ResourceBases, e => { ODataResourceWrapper resource1 = Assert.IsType <ODataResourceWrapper>(e); Assert.Equal("Customers(42)", resource1.Resource.Id.OriginalString); Assert.Equal("Sammy", resource1.Resource.Properties.First(p => p.Name == "Name").Value); }, e => { ODataResourceWrapper resource2 = Assert.IsType <ODataResourceWrapper>(e); Assert.Equal("Orders(10643)", resource2.Resource.Id.OriginalString); Assert.Equal(82, resource2.Resource.Properties.First(p => p.Name == "Price").Value); }, e => { ODataDeletedResourceWrapper deletedResource = Assert.IsType <ODataDeletedResourceWrapper>(e); Assert.Equal("Customers(21)", deletedResource.DeletedResource.Id.OriginalString); Assert.Equal(DeltaDeletedEntryReason.Deleted, deletedResource.DeletedResource.Reason); }); // DeltaLinks Assert.Equal(2, deltaResourceSet.DeltaLinks.Count); Assert.Collection(deltaResourceSet.DeltaLinks, e => { ODataDeltaDeletedLinkWrapper deletedLinkWrapper = Assert.IsType <ODataDeltaDeletedLinkWrapper>(e); Assert.Equal("Customers(39)", deletedLinkWrapper.DeltaDeletedLink.Source.OriginalString); Assert.Equal("Orders(10643)", deletedLinkWrapper.DeltaDeletedLink.Target.OriginalString); Assert.Equal("Orders", deletedLinkWrapper.DeltaDeletedLink.Relationship); }, e => { ODataDeltaLinkWrapper linkWrapper = Assert.IsType <ODataDeltaLinkWrapper>(e); Assert.Equal("Customers(32)", linkWrapper.DeltaLink.Source.OriginalString); Assert.Equal("Orders(10645)", linkWrapper.DeltaLink.Target.OriginalString); Assert.Equal("Orders", linkWrapper.DeltaLink.Relationship); }); }
/// <summary> /// Deserializes the given <paramref name="deltaResourceSet"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="deltaResourceSet">The delta resource set to deserialize.</param> /// <param name="elementType">The element type.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The deserialized resource set object.</returns> public virtual IEnumerable ReadDeltaResourceSet(ODataDeltaResourceSetWrapper deltaResourceSet, IEdmStructuredTypeReference elementType, ODataDeserializerContext readContext) { if (deltaResourceSet == null) { throw Error.ArgumentNull(nameof(deltaResourceSet)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } // Delta Items foreach (ODataItemWrapper itemWrapper in deltaResourceSet.DeltaItems) { // Deleted Link ODataDeltaDeletedLinkWrapper deletedLinkWrapper = itemWrapper as ODataDeltaDeletedLinkWrapper; if (deletedLinkWrapper != null) { yield return(ReadDeltaDeletedLink(deletedLinkWrapper, elementType, readContext)); } // Added Link ODataDeltaLinkWrapper deltaLinkWrapper = itemWrapper as ODataDeltaLinkWrapper; if (deltaLinkWrapper != null) { yield return(ReadDeltaLink(deltaLinkWrapper, elementType, readContext)); } // DeletedResource ODataDeletedResourceWrapper deletedResourceWrapper = itemWrapper as ODataDeletedResourceWrapper; if (deletedResourceWrapper != null) { // TODO: deleted resource yield return(null); } // Added resource or updated resource ODataResourceWrapper resourceWrapper = itemWrapper as ODataResourceWrapper; if (resourceWrapper != null) { IEdmModel model = readContext.Model; if (model == null) { throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext); } IEdmStructuredType actualType = model.FindType(resourceWrapper.Resource.TypeName) as IEdmStructuredType; if (actualType == null) { throw new ODataException(Error.Format(SRResources.ResourceTypeNotInModel, resourceWrapper.Resource.TypeName)); } IEdmTypeReference edmTypeReference = actualType.ToEdmTypeReference(true); ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(edmTypeReference); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, edmTypeReference.FullName())); } // TODO: normal resource // yield return deserializer.ReadInline(resourceWrapper, edmTypeReference, readContext); if (readContext.IsDeltaOfT) { Type elementClrType = readContext.Model.GetClrType(elementType); Type deltaType = typeof(Deltas.Delta <>).MakeGenericType(elementClrType); Deltas.IDelta delta = Activator.CreateInstance(deltaType) as Deltas.IDelta; yield return(delta); } else { yield return(new EdmDeltaResourceObject(actualType as IEdmEntityType)); } continue; } } }