Modify Insert & Update restrictions and add unit tests

This commit is contained in:
Sam Xu 2018-01-23 14:30:26 -08:00
parent 2f1ed28186
commit 29b8a910ed
4 changed files with 380 additions and 44 deletions

View file

@ -15,9 +15,6 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// </summary>
internal class InsertRestrictions : CapabilitiesRestrictions
{
private bool _insertable = true;
private IList<string> _nonInsertableNavigationProperties = new List<string>();
/// <summary>
/// The Term type name.
/// </summary>
@ -26,26 +23,12 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <summary>
/// Gets the Insertable value.
/// </summary>
public bool Insertable
{
get
{
Initialize();
return _insertable;
}
}
public bool? Insertable { get; private set; }
/// <summary>
/// Gets the navigation properties which do not allow deep inserts.
/// </summary>
public IList<string> NonInsertableNavigationProperties
{
get
{
Initialize();
return _nonInsertableNavigationProperties;
}
}
public IList<string> NonInsertableNavigationProperties { get; private set; }
/// <summary>
/// Initializes a new instance of <see cref="InsertRestrictions"/> class.
@ -64,7 +47,9 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <returns>True/False.</returns>
public bool IsNonINsertableNavigationProperty(IEdmNavigationProperty property)
{
return NonInsertableNavigationProperties.Any(a => a == property.Name);
return NonInsertableNavigationProperties != null ?
NonInsertableNavigationProperties.Any(a => a == property.Name) :
false;
}
protected override void Initialize(IEdmVocabularyAnnotation annotation)
@ -78,9 +63,11 @@ namespace Microsoft.OpenApi.OData.Capabilities
IEdmRecordExpression record = (IEdmRecordExpression)annotation.Value;
_insertable = SetBoolProperty(record, "Insertable", true);
// Insertable
Insertable = record.GetBoolean("Insertable");
_nonInsertableNavigationProperties = GetCollectNavigationProperty(record, "NonInsertableNavigationProperties");
// NonInsertableNavigationProperties
NonInsertableNavigationProperties = record.GetCollectionPropertyPath("NonInsertableNavigationProperties");
}
}
}

View file

@ -15,9 +15,6 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// </summary>
internal class UpdateRestrictions : CapabilitiesRestrictions
{
private bool _updatable = true;
private IList<string> _nonUpdatableNavigationProperties = new List<string>();
/// <summary>
/// The Term type name.
/// </summary>
@ -26,26 +23,12 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <summary>
/// Gets the Updatable value.
/// </summary>
public bool Updatable
{
get
{
Initialize();
return _updatable;
}
}
public bool? Updatable { get; private set; }
/// <summary>
/// Gets the navigation properties which do not allow rebinding.
/// </summary>
public IList<string> NonUpdatableNavigationProperties
{
get
{
Initialize();
return _nonUpdatableNavigationProperties;
}
}
public IList<string> NonUpdatableNavigationProperties { get; private set; }
/// <summary>
/// Initializes a new instance of <see cref="UpdateRestrictions"/> class.
@ -64,7 +47,9 @@ namespace Microsoft.OpenApi.OData.Capabilities
/// <returns>True/False.</returns>
public bool IsNonUpdatableNavigationProperty(IEdmNavigationProperty property)
{
return NonUpdatableNavigationProperties.Any(a => a == property.Name);
return NonUpdatableNavigationProperties != null ?
NonUpdatableNavigationProperties.Any(a => a == property.Name) :
false;
}
protected override void Initialize(IEdmVocabularyAnnotation annotation)
@ -78,9 +63,11 @@ namespace Microsoft.OpenApi.OData.Capabilities
IEdmRecordExpression record = (IEdmRecordExpression)annotation.Value;
_updatable = SetBoolProperty(record, "Updatable", true);
// Updatable
Updatable = record.GetBoolean("Updatable");
_nonUpdatableNavigationProperties = GetCollectNavigationProperty(record, "NonUpdatableNavigationProperties");
// NonUpdatableNavigationProperties
NonUpdatableNavigationProperties = record.GetCollectionPropertyPath("NonUpdatableNavigationProperties");
}
}
}

View file

@ -0,0 +1,181 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
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 InsertRestrictionsTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultInsertRestrictionsValues()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Entity");
// Act
InsertRestrictions insert = new InsertRestrictions(EdmCoreModel.Instance, entityType);
// Assert
Assert.Equal(CapabilitiesConstants.InsertRestrictions, insert.QualifiedName);
Assert.Null(insert.Insertable);
Assert.Null(insert.NonInsertableNavigationProperties);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntityTypeReturnsCorrectInsertRestrictionsValue(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
InsertRestrictions insert = new InsertRestrictions(model, calendar);
// Assert
VerifyInsertRestrictions(insert);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntitySetReturnsCorrectInsertRestrictionsValue(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
InsertRestrictions insert = new InsertRestrictions(model, calendars);
// Assert
VerifyInsertRestrictions(insert);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnNavigationPropertyReturnsCorrectInsertRestrictionsValue(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
InsertRestrictions insert = new InsertRestrictions(model, navigationProperty);
// Assert
VerifyInsertRestrictions(insert);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void IsNonINsertableNavigationPropertyReturnsCorrectForProperty(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
IEdmNavigationProperty navigationProperty = calendar.DeclaredNavigationProperties().First(c => c.Name == "RelatedEvents");
Assert.NotNull(navigationProperty); // Guard
// Act
InsertRestrictions insert = new InsertRestrictions(model, calendar);
// Assert
Assert.NotNull(insert.Insertable);
Assert.False(insert.Insertable.Value);
Assert.True(insert.IsNonINsertableNavigationProperty(navigationProperty));
}
private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location, bool navInLine = false)
{
string countAnnotation = @"
<Annotation Term=""Org.OData.Capabilities.V1.InsertRestrictions"" >
<Record>
<PropertyValue Property=""Insertable"" Bool=""false"" />
<PropertyValue Property=""NonInsertableNavigationProperties"" >
<Collection>
<NavigationPropertyPath>abc</NavigationPropertyPath>
<NavigationPropertyPath>RelatedEvents</NavigationPropertyPath>
</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 VerifyInsertRestrictions(InsertRestrictions insert)
{
Assert.NotNull(insert);
Assert.NotNull(insert.Insertable);
Assert.False(insert.Insertable.Value);
Assert.NotNull(insert.NonInsertableNavigationProperties);
Assert.Equal(2, insert.NonInsertableNavigationProperties.Count);
Assert.Equal("abc|RelatedEvents", String.Join("|", insert.NonInsertableNavigationProperties));
}
}
}

View file

@ -0,0 +1,181 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
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 UpdateRestrictionsTests
{
[Fact]
public void UnknownAnnotatableTargetReturnsDefaultUpdateRestrictionsValues()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Entity");
// Act
UpdateRestrictions update = new UpdateRestrictions(EdmCoreModel.Instance, entityType);
// Assert
Assert.Equal(CapabilitiesConstants.UpdateRestrictions, update.QualifiedName);
Assert.Null(update.Updatable);
Assert.Null(update.NonUpdatableNavigationProperties);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntityTypeReturnsCorrectUpdateRestrictionsValue(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
UpdateRestrictions update = new UpdateRestrictions(model, calendar);
// Assert
VerifyUpdateRestrictions(update);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnEntitySetReturnsCorrectUpdateRestrictionsValue(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
UpdateRestrictions update = new UpdateRestrictions(model, calendars);
// Assert
VerifyUpdateRestrictions(update);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void TargetOnNavigationPropertyReturnsCorrectUpdateRestrictionsValue(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
UpdateRestrictions update = new UpdateRestrictions(model, navigationProperty);
// Assert
VerifyUpdateRestrictions(update);
}
[Theory]
[InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)]
[InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)]
public void IsNonUpdatableNavigationPropertyReturnsCorrectForProperty(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
IEdmNavigationProperty navigationProperty = calendar.DeclaredNavigationProperties().First(c => c.Name == "RelatedEvents");
Assert.NotNull(navigationProperty); // Guard
// Act
UpdateRestrictions update = new UpdateRestrictions(model, calendar);
// Assert
Assert.NotNull(update.Updatable);
Assert.False(update.Updatable.Value);
Assert.True(update.IsNonUpdatableNavigationProperty(navigationProperty));
}
private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location, bool navInLine = false)
{
string countAnnotation = @"
<Annotation Term=""Org.OData.Capabilities.V1.UpdateRestrictions"" >
<Record>
<PropertyValue Property=""Updatable"" Bool=""false"" />
<PropertyValue Property=""NonUpdatableNavigationProperties"" >
<Collection>
<NavigationPropertyPath>abc</NavigationPropertyPath>
<NavigationPropertyPath>RelatedEvents</NavigationPropertyPath>
</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 VerifyUpdateRestrictions(UpdateRestrictions update)
{
Assert.NotNull(update);
Assert.NotNull(update.Updatable);
Assert.False(update.Updatable.Value);
Assert.NotNull(update.NonUpdatableNavigationProperties);
Assert.Equal(2, update.NonUpdatableNavigationProperties.Count);
Assert.Equal("abc|RelatedEvents", String.Join("|", update.NonUpdatableNavigationProperties));
}
}
}