public void ShouldCreateSubclassWithoutModelWhenSerializationInfoIsGiven() { var typeContext = ODataResourceTypeContext.Create(SerializationInfo, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: true); Assert.NotNull(typeContext); Assert.True(typeContext.GetType().Name.EndsWith("WithoutModel")); }
static ODataResourceTypeContextTests() { Model = new EdmModel(); EntitySetElementType = new EdmEntityType("ns", "Customer"); ExpectedEntityType = new EdmEntityType("ns", "VipCustomer", EntitySetElementType); ActualEntityType = new EdmEntityType("ns", "DerivedVipCustomer", ExpectedEntityType); ComplexType = new EdmComplexType("ns", "Address"); EdmEntityContainer defaultContainer = new EdmEntityContainer("ns", "DefaultContainer"); Model.AddElement(defaultContainer); EntitySet = new EdmEntitySet(defaultContainer, "Customers", EntitySetElementType); Model.AddElement(EntitySetElementType); Model.AddElement(ExpectedEntityType); Model.AddElement(ActualEntityType); defaultContainer.AddElement(EntitySet); SerializationInfo = new ODataResourceSerializationInfo { NavigationSourceName = "MyCustomers", NavigationSourceEntityTypeName = "ns.MyCustomer", ExpectedTypeName = "ns.MyVipCustomer" }; SerializationInfoWithEdmUnknowEntitySet = new ODataResourceSerializationInfo() { NavigationSourceName = null, NavigationSourceEntityTypeName = "ns.MyCustomer", ExpectedTypeName = "ns.MyVipCustomer", NavigationSourceKind = EdmNavigationSourceKind.UnknownEntitySet }; TypeContextWithoutModel = ODataResourceTypeContext.Create(SerializationInfo, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: true); TypeContextWithModel = ODataResourceTypeContext.Create(/*serializationInfo*/ null, EntitySet, EntitySetElementType, ExpectedEntityType, throwIfMissingTypeInfo: true); TypeContextWithEdmUnknowEntitySet = ODataResourceTypeContext.Create(SerializationInfoWithEdmUnknowEntitySet, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: true); BaseTypeContextThatThrows = ODataResourceTypeContext.Create(serializationInfo: null, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: true); BaseTypeContextThatWillNotThrow = ODataResourceTypeContext.Create(serializationInfo: null, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: false); }
public void ShouldCreateSubclassWithoutModelWhenBothSerializationInfoAndMetadataAreGiven() { var typeContext = ODataResourceTypeContext.Create(SerializationInfo, EntitySet, EntitySetElementType, ExpectedEntityType, throwIfMissingTypeInfo: true); Assert.NotNull(typeContext); Assert.True(typeContext.GetType().Name.EndsWith("WithoutModel")); }
public async Task WriteNestedResourceInfoContextUrlAsync_WritesNestedResourceInfoContextUrl() { var nestedResourceInfo = new ODataNestedResourceInfo { Name = "BestSeller", Url = new Uri($"{ServiceUri}/Categories(1)/BestSeller"), IsCollection = false, AssociationLinkUrl = new Uri($"{ServiceUri}/Categories(1)/associationLink") }; var typeContext = ODataResourceTypeContext.Create( CreateCategorySerializationInfo(), /* navigationSource */ null, /* navigationSourceEntityType */ null, /* expectedResourceType */ null, /* throwIfMissingTypeInfo */ true); var contextUrlInfo = ODataContextUrlInfo.Create( typeContext, this.messageWriterSettings.Version ?? ODataVersion.V4, ODataDeltaKind.Resource, this.messageWriterSettings.ODataUri); var result = await SetupJsonLightResourceSerializerAndRunTestAsync( (jsonLightResourceSerializer) => { return(jsonLightResourceSerializer.WriteNestedResourceInfoContextUrlAsync( nestedResourceInfo, contextUrlInfo)); }); Assert.Equal("{\"[email protected]\":\"http://tempuri.org/$metadata#Categories/$entity\"", result); }
public void ShouldCreateSubclassWithModelWhenMetadataIsGiven() { var typeContext = ODataResourceTypeContext.Create(/*serializationInfo*/ null, EntitySet, EntitySetElementType, ExpectedEntityType, throwIfMissingTypeInfo: true); Assert.NotNull(typeContext); Assert.EndsWith("WithModel", typeContext.GetType().Name); }
public ODataStreamReferenceValueTests() { this.testSubject = new ODataStreamReferenceValue(); var entry = new ODataResource { TypeName = "ns.DerivedType", Properties = new[] { new ODataProperty { Name = "Id", Value = 1, SerializationInfo = new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Key } }, new ODataProperty { Name = "Name", Value = "Bob", SerializationInfo = new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.ETag } } } }; var serializationInfo = new ODataResourceSerializationInfo { NavigationSourceName = "Set", NavigationSourceEntityTypeName = "ns.BaseType", ExpectedTypeName = "ns.BaseType" }; var typeContext = ODataResourceTypeContext.Create(serializationInfo, null, null, null, true); var metadataContext = new TestMetadataContext(); var entryMetadataContext = ODataResourceMetadataContext.Create(entry, typeContext, serializationInfo, null, metadataContext, new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree)); var fullMetadataBuilder = new ODataConventionalEntityMetadataBuilder(entryMetadataContext, metadataContext, new ODataConventionalUriBuilder(ServiceUri, ODataUrlKeyDelimiter.Parentheses)); this.streamWithFullBuilder = new ODataStreamReferenceValue(); this.streamWithFullBuilder.SetMetadataBuilder(fullMetadataBuilder, "Stream"); }
public void ShouldNotIncludeFragmentItemSelectorOnSingleton() { foreach (ODataVersion version in Versions) { var singletonTypeContextWithModel = ODataResourceTypeContext.Create(/*serializationInfo*/ null, this.singletonCity, this.cityType, this.cityType, throwIfMissingTypeInfo: true); this.CreateEntryContextUri(singletonTypeContextWithModel, version).OriginalString.Should().Be(BuildExpectedContextUri("#SingletonCity", false)); } }
private Uri CreateDeltaResponseContextUri(ODataResourceTypeContext typeContext, ODataDeltaKind kind, ODataVersion version) { ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, version, kind); Uri contextUrl = this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.Delta, info); contextUrl.Should().NotBeNull(); return(contextUrl); }
public void ShouldCreateSubclassWithModelWhenExpectedTypeisGiven() { var typeContext = ODataResourceTypeContext.Create(null, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: ComplexType, throwIfMissingTypeInfo: false); Assert.NotNull(typeContext); Assert.True(typeContext.GetType().Name.EndsWith("WithModel")); }
private Uri CreateFeedContextUri(ODataResourceTypeContext typeContext, bool isResponse = true) { ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, false); Uri contextUrl = isResponse ? this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.ResourceSet, info) : this.requestContextUriBuilder.BuildContextUri(ODataPayloadKind.ResourceSet, info); return(contextUrl); }
public void EntryContextUriShouldNotBeWrittenIfNotProvided() { var serializationInfo = new ODataResourceSerializationInfo { NavigationSourceName = "MyContainer.MyCities", NavigationSourceEntityTypeName = "TestModel.MyCity", ExpectedTypeName = "TestModel.MyCity" }; var typeContext = ODataResourceTypeContext.Create(serializationInfo, null, null, null, true); this.builderWithNoMetadataDocumentUri.BuildContextUri(ODataPayloadKind.Resource, ODataContextUrlInfo.Create(typeContext, true)).Should().BeNull(); }
private Uri CreateEntryContextUri(ODataResourceTypeContext typeContext, ODataVersion version, bool isResponse = true) { ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, version, true); Uri contextUrl = isResponse ? this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.Resource, info) : this.requestContextUriBuilder.BuildContextUri(ODataPayloadKind.Resource, info); return(contextUrl); }
public void ShouldNotIncludeEntityOnSingletonWithoutModel() { ODataResourceSerializationInfo serializationInfo = new ODataResourceSerializationInfo() { ExpectedTypeName = "People", NavigationSourceEntityTypeName = "People", NavigationSourceName = "Boss", NavigationSourceKind = EdmNavigationSourceKind.Singleton, }; var requestSingletonTypeContextWithoutModel = ODataResourceTypeContext.Create(serializationInfo, /*navigationSource*/ null, /*navigationSourceEntityType*/ null, /*expectedEntityType*/ null, true); this.CreateEntryContextUri(requestSingletonTypeContextWithoutModel).OriginalString.Should().Be(BuildExpectedContextUri("#Boss", false)); }
public void BuildResourceContextUriForComplexResource() { var typeContext = ODataResourceTypeContext.Create(/*serializationInfo*/ null, null, null, this.addressType, throwIfMissingTypeInfo: false); ODataResource value = new ODataResource { TypeName = "TestModel.Address" }; var contextUri = this.CreateEntryContextUri(typeContext, true); contextUri.OriginalString.Should().Be(BuildExpectedContextUri("#TestModel.Address")); }
private Uri CreateFeedContextUri(string applyClauseString, ODataVersion version) { ApplyClause applyClause = new ODataQueryOptionParser(edmModel, this.cityType, this.citySet, new Dictionary <string, string> { { "$apply", applyClauseString } }).ParseApply(); ODataResourceTypeContext typeContext = ODataResourceTypeContext.Create(/*serializationInfo*/ null, this.citySet, this.cityType, this.cityType, true); ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, version, false, new ODataUri() { Apply = applyClause }); Uri contextUrl = this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.ResourceSet, info); return(contextUrl); }
private Uri CreateEntryContextUri(string selectClause, string expandClause, ODataVersion version) { SelectExpandClause selectExpandClause = new ODataQueryOptionParser(edmModel, this.cityType, this.citySet, new Dictionary <string, string> { { "$expand", expandClause }, { "$select", selectClause } }).ParseSelectAndExpand(); ODataResourceTypeContext typeContext = ODataResourceTypeContext.Create(/*serializationInfo*/ null, this.citySet, this.cityType, this.cityType, true); ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, version, true, new ODataUri() { SelectAndExpand = selectExpandClause }); Uri contextUrl = this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.ResourceSet, info); return(contextUrl); }
public void BuildResourceContextUriForComplexWithNullAnnotation() { var typeContext = ODataResourceTypeContext.Create(/*serializationInfo*/ null, null, null, this.addressType, throwIfMissingTypeInfo: true); ODataResource value = new ODataResource { TypeName = "TestModel.Address" }; value.TypeAnnotation = new ODataTypeAnnotation(); foreach (ODataVersion version in Versions) { var contextUri = this.CreateEntryContextUri(typeContext, version, true); contextUri.OriginalString.Should().Be(BuildExpectedContextUri("#TestModel.Address")); } }
public async Task WriteResourceSetContextUriAsync_WritesResourceSetContextUri() { var typeContext = ODataResourceTypeContext.Create( CreateCategorySerializationInfo(), /* navigationSource */ null, /* navigationSourceEntityType */ null, /* expectedResourceType */ null, /* throwIfMissingTypeInfo */ true); var result = await SetupJsonLightResourceSerializerAndRunTestAsync( (jsonLightResourceSerializer) => { return(jsonLightResourceSerializer.WriteResourceSetContextUriAsync(typeContext)); }); Assert.Equal("{\"@odata.context\":\"http://tempuri.org/$metadata#Categories\"", result); }
public void ShouldWriteIfSerializationInfoWithoutNavigationSourceButUnknownSetOnFeedResponse() { this.CreateFeedContextUri(ODataResourceTypeContext.Create( serializationInfo: new ODataResourceSerializationInfo() { ExpectedTypeName = "NS.Type", IsFromCollection = true, NavigationSourceEntityTypeName = "NS.Type", NavigationSourceKind = EdmNavigationSourceKind.UnknownEntitySet, NavigationSourceName = null }, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: true), isResponse: true); }
public ODataNavigationLinkTests() { this.navigationLink = new ODataNestedResourceInfo(); var entry = new ODataResource { TypeName = "ns.DerivedType", Properties = new[] { new ODataProperty { Name = "Id", Value = 1, SerializationInfo = new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Key } }, new ODataProperty { Name = "Name", Value = "Bob", SerializationInfo = new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.ETag } } } }; var serializationInfo = new ODataResourceSerializationInfo { NavigationSourceName = "Set", NavigationSourceEntityTypeName = "ns.BaseType", ExpectedTypeName = "ns.BaseType" }; var typeContext = ODataResourceTypeContext.Create(serializationInfo, null, null, null, true); var metadataContext = new TestMetadataContext(); var entryMetadataContext = ODataResourceMetadataContext.Create(entry, typeContext, serializationInfo, null, metadataContext, new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree)); var metadataBuilder = new ODataConventionalEntityMetadataBuilder(entryMetadataContext, metadataContext, new ODataConventionalUriBuilder(ServiceUri, ODataUrlKeyDelimiter.Parentheses)); this.navigationLinkWithFullBuilder = new ODataNestedResourceInfo { Name = "NavProp" }; this.navigationLinkWithFullBuilder.MetadataBuilder = metadataBuilder; this.navigationLinkWithNoOpBuilder = new ODataNestedResourceInfo { Name = "NavProp" }; this.navigationLinkWithNoOpBuilder.MetadataBuilder = new NoOpResourceMetadataBuilder(entry); this.navigationLinkWithNullBuilder = new ODataNestedResourceInfo { Name = "NavProp" }; this.navigationLinkWithNullBuilder.MetadataBuilder = ODataResourceMetadataBuilder.Null; }
public async Task WriteDeltaContextUriAsync_WritesDeltaContextUri() { var typeContext = ODataResourceTypeContext.Create( /* serializationInfo */ null, this.categoriesEntitySet, this.categoryEntityType, this.categoryEntityType, /* throwIfMissingTypeInfo */ true); var result = await SetupJsonLightResourceSerializerAndRunTestAsync( (jsonLightResourceSerializer) => { return(jsonLightResourceSerializer.WriteDeltaContextUriAsync( typeContext, ODataDeltaKind.Resource, /* parentContextUrlInfo */ null)); }); Assert.Equal("{\"@odata.context\":\"http://tempuri.org/$metadata#Categories/$entity\"", result); }
public ODataResourceTests() { this.odataEntry = new ODataResource(); this.odataEntryWithFullBuilder = new ODataResource { TypeName = "ns.DerivedType", Properties = new[] { new ODataProperty { Name = "Id", Value = 1, SerializationInfo = new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.Key } }, new ODataProperty { Name = "Name", Value = "Bob", SerializationInfo = new ODataPropertySerializationInfo { PropertyKind = ODataPropertyKind.ETag } } } }; var serializationInfo = new ODataResourceSerializationInfo { NavigationSourceName = "Set", NavigationSourceEntityTypeName = "ns.BaseType", ExpectedTypeName = "ns.BaseType" }; var typeContext = ODataResourceTypeContext.Create(serializationInfo, null, null, null, true); var metadataContext = new TestMetadataContext(); var entryMetadataContext = ODataResourceMetadataContext.Create(this.odataEntryWithFullBuilder, typeContext, serializationInfo, null, metadataContext, new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), null); this.odataEntryWithFullBuilder.MetadataBuilder = new ODataConventionalEntityMetadataBuilder(entryMetadataContext, metadataContext, new ODataConventionalUriBuilder(new Uri("http://service/", UriKind.Absolute), ODataUrlKeyDelimiter.Parentheses)); this.odataEntryWithNullBuilder = new ODataResource { MetadataBuilder = ODataResourceMetadataBuilder.Null }; }
/// <summary> /// Gets a resource metadata builder for the given resource. /// </summary> /// <param name="resourceState">Resource state to use as reference for information needed by the builder.</param> /// <param name="useKeyAsSegment">true if keys should go in separate segments in auto-generated URIs, false if they should go in parentheses.</param> /// <param name="isDelta">true if the payload being read is a delta payload</param> /// <returns>A resource metadata builder.</returns> public ODataResourceMetadataBuilder GetResourceMetadataBuilderForReader(IODataJsonLightReaderResourceState resourceState, bool useKeyAsSegment, bool isDelta = false) { Debug.Assert(resourceState != null, "resource != null"); // Only apply the conventional template builder on response. On a request we would only report what's on the wire. if (resourceState.MetadataBuilder == null) { ODataResourceBase resource = resourceState.Resource; if (this.isResponse && !isDelta) { ODataTypeAnnotation typeAnnotation = resource.TypeAnnotation; IEdmStructuredType structuredType = null; if (typeAnnotation != null) { if (typeAnnotation.Type != null) { // First try ODataTypeAnnotation.Type (for perf improvement) structuredType = typeAnnotation.Type as IEdmStructuredType; } else if (typeAnnotation.TypeName != null) { // Then try ODataTypeAnnotation.TypeName structuredType = this.model.FindType(typeAnnotation.TypeName) as IEdmStructuredType; } } if (structuredType == null) { // No type name read from the payload. Use resource type from model. structuredType = resourceState.ResourceType; } IEdmNavigationSource navigationSource = resourceState.NavigationSource; IEdmEntityType navigationSourceElementType = this.edmTypeResolver.GetElementType(navigationSource); IODataResourceTypeContext typeContext = ODataResourceTypeContext.Create( /*serializationInfo*/ null, navigationSource, navigationSourceElementType, resourceState.ResourceTypeFromMetadata ?? resourceState.ResourceType, /*throwIfMissingTypeInfo*/ true); IODataResourceMetadataContext resourceMetadataContext = ODataResourceMetadataContext.Create(resource, typeContext, /*serializationInfo*/ null, structuredType, this, resourceState.SelectedProperties); ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(this.ServiceBaseUri, useKeyAsSegment ? ODataUrlKeyDelimiter.Slash : ODataUrlKeyDelimiter.Parentheses); if (structuredType.IsODataEntityTypeKind()) { resourceState.MetadataBuilder = new ODataConventionalEntityMetadataBuilder(resourceMetadataContext, this, uriBuilder); } else { resourceState.MetadataBuilder = new ODataConventionalResourceMetadataBuilder(resourceMetadataContext, this, uriBuilder); } } else { resourceState.MetadataBuilder = new NoOpResourceMetadataBuilder(resource); } } return(resourceState.MetadataBuilder); }
private void InitializeTypeContext() { this.responseCityTypeContextWithoutSerializationInfo = ODataResourceTypeContext.Create(/*serializationInfo*/ null, this.citySet, this.cityType, this.cityType, throwIfMissingTypeInfo: true); this.responseCapitolCityTypeContextWithoutSerializationInfo = ODataResourceTypeContext.Create(/*serializationInfo*/ null, this.citySet, this.cityType, this.capitolCityType, throwIfMissingTypeInfo: true); }
public void ShouldCreateBaseClassWhenSerializationInfoAndUserModelAreBothMissingAndThrowIfMissingTypeInfoIsFalse() { var typeContext = ODataResourceTypeContext.Create(serializationInfo: null, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: false); Assert.IsType <ODataResourceTypeContext>(typeContext); }
/// <summary> /// Tries to writes the context URI property for a resource set into the payload if one is available. /// </summary> /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource set.</param> /// <returns>The contextUrlInfo, if the context URI was successfully written.</returns> internal ODataContextUrlInfo WriteResourceSetContextUri(ODataResourceTypeContext typeContext) { ODataUri odataUri = this.JsonLightOutputContext.MessageWriterSettings.ODataUri; return(this.WriteContextUriProperty(ODataPayloadKind.ResourceSet, () => ODataContextUrlInfo.Create(typeContext, this.MessageWriterSettings.Version ?? ODataVersion.V4, /* isSingle */ false, odataUri))); }
/// <summary> /// Tries to writes the context URI property for delta resource/resource set/link into the payload if one is available. /// </summary> /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource.</param> /// <param name="kind">The delta kind to write.</param> /// <param name="parentContextUrlInfo">The parent contextUrlInfo.</param> /// <returns>The created context uri info.</returns> internal ODataContextUrlInfo WriteDeltaContextUri(ODataResourceTypeContext typeContext, ODataDeltaKind kind, ODataContextUrlInfo parentContextUrlInfo = null) { ODataUri odataUri = this.JsonLightOutputContext.MessageWriterSettings.ODataUri; return(this.WriteContextUriProperty(ODataPayloadKind.Delta, () => ODataContextUrlInfo.Create(typeContext, this.MessageWriterSettings.Version ?? ODataVersion.V4, kind, odataUri), parentContextUrlInfo)); }
/// <summary> /// Tries to writes the context URI property for a resource into the payload if one is available. /// </summary> /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource.</param> /// <param name="parentContextUrlInfo">The parent contextUrlInfo.</param> /// <returns>The created context uri info.</returns> internal ODataContextUrlInfo WriteResourceContextUri(ODataResourceTypeContext typeContext, ODataContextUrlInfo parentContextUrlInfo = null) { ODataUri odataUri = this.JsonLightOutputContext.MessageWriterSettings.ODataUri; return(this.WriteContextUriProperty(ODataPayloadKind.Resource, () => ODataContextUrlInfo.Create(typeContext, /* isSingle */ true, odataUri), parentContextUrlInfo)); }
public void ShouldCreateBaseClassWhenSerializationInfoAndUserModelAreBothMissingAndThrowIfMissingTypeInfoIsTrue() { var typeContext = ODataResourceTypeContext.Create(serializationInfo: null, navigationSource: null, navigationSourceEntityType: null, expectedResourceType: null, throwIfMissingTypeInfo: true); typeContext.Should().BeOfType <ODataResourceTypeContext>(); }