Modify Batch, Top, Skip, IndexableByKey support restriction and Sort Restriction, add unit tests

This commit is contained in:
Sam Xu 2018-01-23 16:00:15 -08:00
parent b00cd0ee26
commit f7f9f98f8f
9 changed files with 668 additions and 63 deletions

View file

@ -4,7 +4,6 @@
// ------------------------------------------------------------
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Vocabularies;
namespace Microsoft.OpenApi.OData.Capabilities
{
@ -22,8 +21,8 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// Initializes a new instance of <see cref="BatchSupported"/> class.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="target">The annotation target.</param>
public BatchSupported(IEdmModel model, IEdmVocabularyAnnotatable target)
/// <param name="target">The annotation target <see cref="IEdmEntityContainer"/>.</param>
public BatchSupported(IEdmModel model, IEdmEntityContainer target)
: base(model, target)
{
}

View file

@ -15,11 +15,6 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// </summary>
internal class SortRestrictions : CapabilitiesRestrictions
{
private bool _sortable = true;
private IList<string> _ascendingOnlyProperties = new List<string>();
private IList<string> _descendingOnlyProperties = new List<string>();
private IList<string> _nonSortableProperties = new List<string>();
/// <summary>
/// The Term type name.
/// </summary>
@ -28,50 +23,22 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <summary>
/// Gets the Sortable value.
/// </summary>
public bool Sortable
{
get
{
Initialize();
return _sortable;
}
}
public bool? Sortable { get; private set; }
/// <summary>
/// Gets the properties which can only be used for sorting in Ascending order.
/// </summary>
public IList<string> AscendingOnlyProperties
{
get
{
Initialize();
return _ascendingOnlyProperties;
}
}
public IList<string> AscendingOnlyProperties { get; private set; }
/// <summary>
/// Gets the properties which can only be used for sorting in Descending order.
/// </summary>
public IList<string> DescendingOnlyProperties
{
get
{
Initialize();
return _descendingOnlyProperties;
}
}
public IList<string> DescendingOnlyProperties { get; private set; }
/// <summary>
/// Gets the properties which cannot be used in $orderby expressions.
/// </summary>
public IList<string> NonSortableProperties
{
get
{
Initialize();
return _nonSortableProperties;
}
}
public IList<string> NonSortableProperties { get; private set; }
/// <summary>
/// Initializes a new instance of <see cref="SortRestrictions"/> class.
@ -90,7 +57,7 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <returns>True/False.</returns>
public bool IsAscendingOnlyProperty(IEdmProperty property)
{
return AscendingOnlyProperties.Any(a => a == property.Name);
return AscendingOnlyProperties != null ? AscendingOnlyProperties.Any(a => a == property.Name) : false;
}
/// <summary>
@ -100,7 +67,7 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <returns>True/False.</returns>
public bool IsDescendingOnlyProperty(IEdmProperty property)
{
return DescendingOnlyProperties.Any(a => a == property.Name);
return DescendingOnlyProperties != null ? DescendingOnlyProperties.Any(a => a == property.Name) : false;
}
/// <summary>
@ -108,9 +75,9 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// </summary>
/// <param name="property">The input property.</param>
/// <returns>True/False.</returns>
public bool IsNonsortableProperty(IEdmProperty property)
public bool IsNonSortableProperty(IEdmProperty property)
{
return NonSortableProperties.Any(a => a == property.Name);
return NonSortableProperties != null ? NonSortableProperties.Any(a => a == property.Name) : false;
}
protected override void Initialize(IEdmVocabularyAnnotation annotation)
@ -124,13 +91,17 @@ namespace Microsoft.OpenApi.OData.Capabilities
IEdmRecordExpression record = (IEdmRecordExpression)annotation.Value;
_sortable = SetBoolProperty(record, "Sortable", true);
// Sortable
Sortable = record.GetBoolean("Sortable");
_ascendingOnlyProperties = GetCollectProperty(record, "AscendingOnlyProperties");
// AscendingOnlyProperties
AscendingOnlyProperties = record.GetCollectionPropertyPath("AscendingOnlyProperties");
_descendingOnlyProperties = GetCollectProperty(record, "DescendingOnlyProperties");
// DescendingOnlyProperties
DescendingOnlyProperties = record.GetCollectionPropertyPath("DescendingOnlyProperties");
_nonSortableProperties = GetCollectProperty(record, "NonSortablePropeties");
// NonSortablePropeties
NonSortableProperties = record.GetCollectionPropertyPath("NonSortableProperties");
}
}
}

View file

@ -13,19 +13,10 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// </summary>
internal abstract class SupportedRestrictions : CapabilitiesRestrictions
{
private bool _supported = true;
/// <summary>
/// Get the Supported boolean value.
/// </summary>
public bool Supported
{
get
{
Initialize();
return _supported;
}
}
public bool? Supported { get; private set; }
/// <summary>
/// Initializes a new instance of <see cref="SupportedRestrictions"/> class.
@ -46,10 +37,11 @@ namespace Microsoft.OpenApi.OData.Capabilities
return;
}
// supported
IEdmBooleanConstantExpression boolConstant = (IEdmBooleanConstantExpression)annotation.Value;
if (boolConstant != null)
{
_supported = boolConstant.Value;
Supported = boolConstant.Value;
}
}
}

View file

@ -171,7 +171,7 @@ namespace Microsoft.OpenApi.OData.Generator
Utils.CheckArgumentNull(target, nameof(target));
TopSupported top = new TopSupported(context.Model, target);
if (top.Supported)
if (top.Supported == null || top.Supported.Value)
{
return new OpenApiParameter
{
@ -194,7 +194,7 @@ namespace Microsoft.OpenApi.OData.Generator
Utils.CheckArgumentNull(target, nameof(target));
SkipSupported skip = new SkipSupported(context.Model, target);
if (skip.Supported)
if (skip.Supported == null || skip.Supported.Value)
{
return new OpenApiParameter
{
@ -311,7 +311,7 @@ namespace Microsoft.OpenApi.OData.Generator
Utils.CheckArgumentNull(entityType, nameof(entityType));
SortRestrictions sort = new SortRestrictions(context.Model, target);
if (!sort.Sortable)
if (sort.Sortable != null && sort.Sortable.Value == false)
{
return null;
}
@ -319,7 +319,7 @@ namespace Microsoft.OpenApi.OData.Generator
IList<IOpenApiAny> orderByItems = new List<IOpenApiAny>();
foreach (var property in entityType.StructuralProperties())
{
if (sort.IsNonsortableProperty(property))
if (sort.IsNonSortableProperty(property))
{
continue;
}

View file

@ -0,0 +1,64 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OpenApi.OData.Capabilities;
using Xunit;
namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests
{
public class BatchSupportedTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultBatchSupportedValues()
{
// Arrange
EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
// Act
BatchSupported batch = new BatchSupported(EdmCoreModel.Instance, container);
// Assert
Assert.Equal(CapabilitiesConstants.BatchSupported, batch.QualifiedName);
Assert.Null(batch.Supported);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline, true)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline, false)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine, true)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine, false)]
public void EntitySetContainerReturnsCorrectBatchSupportedValue(EdmVocabularyAnnotationSerializationLocation location, bool support)
{
// Arrange
IEdmModel model = GetEdmModel(location, support);
Assert.NotNull(model); // guard
// Act
BatchSupported batch = new BatchSupported(model, model.EntityContainer);
// Assert
Assert.NotNull(batch.Supported);
Assert.Equal(support, batch.Supported.Value);
}
private static IEdmModel GetEdmModel(EdmVocabularyAnnotationSerializationLocation location, bool supported)
{
EdmModel model = new EdmModel();
EdmEntityContainer container = new EdmEntityContainer("NS", "Default");
model.AddElement(container);
IEdmTerm term = model.FindTerm(CapabilitiesConstants.BatchSupported);
Assert.NotNull(term);
IEdmBooleanConstantExpression boolean = new EdmBooleanConstant(supported);
EdmVocabularyAnnotation annotation = new EdmVocabularyAnnotation(container, term, boolean);
annotation.SetSerializationLocation(model, location);
model.SetVocabularyAnnotation(annotation);
return model;
}
}
}

View file

@ -0,0 +1,130 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi.OData.Capabilities;
using Xunit;
namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests
{
public class IndexableByKeyTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultIndexableByKeyValues()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Entity");
// Act
IndexableByKey index = new IndexableByKey(EdmCoreModel.Instance, entityType);
// Assert
Assert.Equal(CapabilitiesConstants.IndexableByKey, index.QualifiedName);
Assert.Null(index.Supported);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntityTypeReturnsCorrectIndexableByKeyValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
// Act
IndexableByKey index = new IndexableByKey(model, calendar);
// Assert
Assert.NotNull(index.Supported);
Assert.False(index.Supported.Value);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntitySetReturnsCorrectIndexableByKeyValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Default/Calendars"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars");
Assert.NotNull(calendars); // guard
// Act
IndexableByKey index = new IndexableByKey(model, calendars);
// Assert
Assert.NotNull(index.Supported);
Assert.False(index.Supported.Value);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnNavigationPropertyReturnsCorrectIndexableByKeyValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar/RelatedEvents"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location, true);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
IEdmNavigationProperty navigationProperty = calendar.DeclaredNavigationProperties().First(c => c.Name == "RelatedEvents");
Assert.NotNull(navigationProperty); // guard
// Act
IndexableByKey index = new IndexableByKey(model, navigationProperty);
// Assert
Assert.NotNull(index.Supported);
Assert.False(index.Supported.Value);
}
private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location, bool navInLine = false)
{
string countAnnotation = @"<Annotation Term=""Org.OData.Capabilities.V1.IndexableByKey"" Bool=""false"" />";
if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine)
{
countAnnotation = string.Format(template, countAnnotation);
return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation);
}
else
{
if (navInLine)
{
return CapabilitiesModelHelper.GetEdmModelNavInline(countAnnotation);
}
else
{
return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation);
}
}
}
}
}

View file

@ -3,9 +3,129 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi.OData.Capabilities;
using Xunit;
namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests
{
public class SkipSupportedTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultSkipSupportedValues()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Entity");
// Act
SkipSupported skip = new SkipSupported(EdmCoreModel.Instance, entityType);
// Assert
Assert.Equal(CapabilitiesConstants.SkipSupported, skip.QualifiedName);
Assert.Null(skip.Supported);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntityTypeReturnsCorrectSkipSupportedValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
// Act
SkipSupported skip = new SkipSupported(model, calendar);
// Assert
Assert.NotNull(skip.Supported);
Assert.False(skip.Supported.Value);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntitySetReturnsCorrectSkipSupportedValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Default/Calendars"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars");
Assert.NotNull(calendars); // guard
// Act
SkipSupported skip = new SkipSupported(model, calendars);
// Assert
Assert.NotNull(skip.Supported);
Assert.False(skip.Supported.Value);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnNavigationPropertyReturnsCorrectSkipSupportedValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar/RelatedEvents"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location, true);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
IEdmNavigationProperty navigationProperty = calendar.DeclaredNavigationProperties().First(c => c.Name == "RelatedEvents");
Assert.NotNull(navigationProperty); // guard
// Act
SkipSupported skip = new SkipSupported(model, navigationProperty);
// Assert
Assert.NotNull(skip.Supported);
Assert.False(skip.Supported.Value);
}
private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location, bool navInLine = false)
{
string countAnnotation = @"<Annotation Term=""Org.OData.Capabilities.V1.SkipSupported"" Bool=""false"" />";
if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine)
{
countAnnotation = string.Format(template, countAnnotation);
return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation);
}
else
{
if (navInLine)
{
return CapabilitiesModelHelper.GetEdmModelNavInline(countAnnotation);
}
else
{
return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation);
}
}
}
}
}

View file

@ -0,0 +1,199 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi.OData.Capabilities;
using Xunit;
namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests
{
public class SortRestrictionsTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultSortRestrictionsValues()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Entity");
// Act
SortRestrictions sort = new SortRestrictions(EdmCoreModel.Instance, entityType);
// Assert
Assert.Equal(CapabilitiesConstants.SortRestrictions, sort.QualifiedName);
Assert.Null(sort.Sortable);
Assert.Null(sort.AscendingOnlyProperties);
Assert.Null(sort.DescendingOnlyProperties);
Assert.Null(sort.NonSortableProperties);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntityTypeReturnsCorrectSortRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
// Act
SortRestrictions sort = new SortRestrictions(model, calendar);
// Assert
VerifySortRestrictions(sort);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntitySetReturnsCorrectSortRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Default/Calendars"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars");
Assert.NotNull(calendars); // guard
// Act
SortRestrictions sort = new SortRestrictions(model, calendars);
// Assert
VerifySortRestrictions(sort);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnNavigationPropertyReturnsCorrectSortRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar/RelatedEvents"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location, true);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
IEdmNavigationProperty navigationProperty = calendar.DeclaredNavigationProperties().First(c => c.Name == "RelatedEvents");
Assert.NotNull(navigationProperty); // guard
// Act
SortRestrictions sort = new SortRestrictions(model, navigationProperty);
// Assert
VerifySortRestrictions(sort);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void IsNonSortablePropertiesReturnsCorrectForProperty(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // Guard
IEdmProperty emails = calendar.DeclaredStructuralProperties().First(c => c.Name == "Emails");
Assert.NotNull(emails); // Guard
// Act
SortRestrictions sort = new SortRestrictions(model, calendar);
// Assert
Assert.NotNull(sort.Sortable);
Assert.False(sort.Sortable.Value);
Assert.True(sort.IsNonSortableProperty(emails));
}
private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location, bool navInLine = false)
{
string countAnnotation = @"
<Annotation Term=""Org.OData.Capabilities.V1.SortRestrictions"" >
<Record>
<PropertyValue Property=""Sortable"" Bool=""false"" />
<PropertyValue Property=""AscendingOnlyProperties"" >
<Collection>
<PropertyPath>abc</PropertyPath>
</Collection>
</PropertyValue>
<PropertyValue Property=""DescendingOnlyProperties"" >
<Collection>
<PropertyPath>rst</PropertyPath>
</Collection>
</PropertyValue>
<PropertyValue Property=""NonSortableProperties"" >
<Collection>
<PropertyPath>Emails</PropertyPath>
</Collection>
</PropertyValue>
</Record>
</Annotation>";
if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine)
{
countAnnotation = string.Format(template, countAnnotation);
return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation);
}
else
{
if (navInLine)
{
return CapabilitiesModelHelper.GetEdmModelNavInline(countAnnotation);
}
else
{
return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation);
}
}
}
private static void VerifySortRestrictions(SortRestrictions sort)
{
Assert.NotNull(sort);
Assert.NotNull(sort.Sortable);
Assert.False(sort.Sortable.Value);
Assert.NotNull(sort.AscendingOnlyProperties);
Assert.Single(sort.AscendingOnlyProperties);
Assert.Equal("abc", sort.AscendingOnlyProperties.First());
Assert.NotNull(sort.DescendingOnlyProperties);
Assert.Single(sort.DescendingOnlyProperties);
Assert.Equal("rst", sort.DescendingOnlyProperties.First());
Assert.NotNull(sort.NonSortableProperties);
Assert.Single(sort.NonSortableProperties);
Assert.Equal("Emails", sort.NonSortableProperties.First());
}
}
}

View file

@ -0,0 +1,130 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi.OData.Capabilities;
using Xunit;
namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests
{
public class TopSupportedTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultTopSupportedValues()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Entity");
// Act
TopSupported top = new TopSupported(EdmCoreModel.Instance, entityType);
// Assert
Assert.Equal(CapabilitiesConstants.TopSupported, top.QualifiedName);
Assert.Null(top.Supported);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntityTypeReturnsCorrectTopSupportedValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
// Act
TopSupported top = new TopSupported(model, calendar);
// Assert
Assert.NotNull(top.Supported);
Assert.False(top.Supported.Value);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntitySetReturnsCorrectTopSupportedValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Default/Calendars"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location);
Assert.NotNull(model); // guard
IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars");
Assert.NotNull(calendars); // guard
// Act
TopSupported top = new TopSupported(model, calendars);
// Assert
Assert.NotNull(top.Supported);
Assert.False(top.Supported.Value);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnNavigationPropertyReturnsCorrectTopSupportedValue(EdmVocabularyAnnotationSerializationLocation location)
{
// Arrange
const string template = @"
<Annotations Target=""NS.Calendar/RelatedEvents"">
{0}
</Annotations>";
IEdmModel model = GetEdmModel(template, location, true);
Assert.NotNull(model); // guard
IEdmEntityType calendar = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Calendar");
Assert.NotNull(calendar); // guard
IEdmNavigationProperty navigationProperty = calendar.DeclaredNavigationProperties().First(c => c.Name == "RelatedEvents");
Assert.NotNull(navigationProperty); // guard
// Act
TopSupported top = new TopSupported(model, navigationProperty);
// Assert
Assert.NotNull(top.Supported);
Assert.False(top.Supported.Value);
}
private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location, bool navInLine = false)
{
string countAnnotation = @"<Annotation Term=""Org.OData.Capabilities.V1.TopSupported"" Bool=""false"" />";
if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine)
{
countAnnotation = string.Format(template, countAnnotation);
return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation);
}
else
{
if (navInLine)
{
return CapabilitiesModelHelper.GetEdmModelNavInline(countAnnotation);
}
else
{
return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation);
}
}
}
}
}