// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OData.Edm.Validation;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
using Xunit;
namespace Microsoft.OpenApi.OData.Edm.Tests
{
public class EdmVocabularyAnnotationExtensionsTests
{
[Fact]
public void GetStringWorksForString()
{
// Arrange
string qualifiedName = "Org.OData.Core.V1.ODataVersions";
string annotation = $@"";
IEdmModel model = GetEdmModel(annotation);
Assert.NotNull(model); // guard
// Act
string versions1 = model.GetString(model.EntityContainer, qualifiedName);
string versions2 = model.GetString(model.EntityContainer, qualifiedName);
// Assert
Assert.NotNull(versions1);
Assert.NotNull(versions2);
Assert.Same(versions1, versions2);
Assert.Equal("9.99,4.01", versions1);
Assert.Equal("9.99,4.01", versions2);
}
[Fact]
public void GetStringWorksForMutlipleModels()
{
// Arrange
string qualifiedName = "Org.OData.Core.V1.ODataVersions";
string annotation = $@"";
IEdmModel model = GetEdmModel(annotation);
Assert.NotNull(model); // guard
IEdmModel coreModel = EdmCoreModel.Instance;
// Act & Assert
string versions1 = model.GetString(model.EntityContainer, qualifiedName);
Assert.NotNull(versions1);
string versions2 = coreModel.GetString(model.EntityContainer, qualifiedName);
Assert.Null(versions2);
string versions3 = model.GetString(model.EntityContainer, qualifiedName);
Assert.NotNull(versions3);
Assert.Equal(versions1, versions3);
}
[Fact]
public void GetRecordWorksForRecord()
{
// Arrange
string qualifiedName = "Org.OData.Capabilities.V1.CountRestrictions";
string annotation = $@"
Emails
mij
RelatedEvents
abc
";
IEdmModel model = GetEdmModel(annotation);
Assert.NotNull(model); // guard
// Act
CountRestrictionsType count1 = model.GetRecord(model.EntityContainer, qualifiedName);
CountRestrictionsType count2 = model.GetRecord(model.EntityContainer, qualifiedName);
// Assert
Assert.NotNull(count1);
Assert.NotNull(count2);
System.Action verifyCount = count =>
{
// Countable
Assert.NotNull(count.Countable);
Assert.True(count.Countable.Value);
// NonCountableProperties
Assert.NotNull(count.NonCountableProperties);
Assert.Equal(new[] { "Emails", "mij" }, count.NonCountableProperties);
// NonCountableNavigationProperties
Assert.NotNull(count.NonCountableNavigationProperties);
Assert.Equal(new[] { "RelatedEvents", "abc" }, count.NonCountableNavigationProperties);
};
verifyCount(count1);
verifyCount(count2);
}
[Fact]
public void GetCollectionWorksForCollectionOfString()
{
// Arrange
string qualifiedName = "Org.OData.Capabilities.V1.SupportedFormats";
string annotation = $@"
abc
xyz
";
IEdmModel model = GetEdmModel(annotation);
Assert.NotNull(model); // guard
// Act
IEnumerable supportedFormats1 = model.GetCollection(model.EntityContainer, qualifiedName);
IEnumerable supportedFormats2 = model.GetCollection(model.EntityContainer, qualifiedName);
// Assert
Assert.NotNull(supportedFormats1);
Assert.NotNull(supportedFormats2);
Assert.Equal(supportedFormats1, supportedFormats2);
Assert.Equal(2, supportedFormats1.Count());
Assert.Equal(new[] { "abc", "xyz" }, supportedFormats1);
}
[Fact]
public void GetGenericCollectionWorksForCollectionOfGenericRecord()
{
// Arrange
string qualifiedName = "NS.MyCollectionCountRestrictions";
string annotation = $@"
123
abc
234
xyz
567
mij
789
rst
";
IEdmModel model = GetEdmModel(annotation);
Assert.NotNull(model); // guard
// Act
IEnumerable counts1 = model.GetCollection(model.EntityContainer, qualifiedName);
IEnumerable counts2 = model.GetCollection(model.EntityContainer, qualifiedName);
// Assert
Assert.NotNull(counts1);
Assert.NotNull(counts2);
Assert.Equal(2, counts1.Count());
foreach (var countItem in new[] { counts1, counts2 })
{
CountRestrictionsType count = countItem.First();
// Countable
Assert.NotNull(count.Countable);
Assert.True(count.Countable.Value);
// NonCountableProperties
Assert.NotNull(count.NonCountableProperties);
Assert.Equal(new[] { "123", "abc" }, count.NonCountableProperties);
// NonCountableNavigationProperties
Assert.NotNull(count.NonCountableNavigationProperties);
Assert.Equal(new[] { "234", "xyz" }, count.NonCountableNavigationProperties);
// #2
count = countItem.Last();
// Countable
Assert.NotNull(count.Countable);
Assert.False(count.Countable.Value);
// NonCountableProperties
Assert.NotNull(count.NonCountableProperties);
Assert.Equal(new[] { "567", "mij" }, count.NonCountableProperties);
// NonCountableNavigationProperties
Assert.NotNull(count.NonCountableNavigationProperties);
Assert.Equal(new[] { "789", "rst" }, count.NonCountableNavigationProperties);
}
}
private IEdmModel GetEdmModel(string annotation)
{
const string template = @"
{0}
";
string modelText = string.Format(template, annotation);
IEdmModel model;
IEnumerable errors;
bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out errors);
Assert.True(result);
return model;
}
}
}