From bc405aa8acbfef2ab34a089083264b8808bd9325 Mon Sep 17 00:00:00 2001 From: Sam Xu Date: Thu, 27 Jun 2019 17:06:02 -0700 Subject: [PATCH] Make all test passed --- .../Common/Utils.cs | 18 ++ .../Edm/EdmAnnotationExtensions.cs | 21 +- .../Edm/RecordExpressionExtensions.cs | 1 - .../OpenApiSecuritySchemeGenerator.cs | 5 + .../Operation/EdmOperationOperationHandler.cs | 8 +- .../PathItem/EntityPathItemHandler.cs | 6 +- .../PathItem/EntitySetPathItemHandler.cs | 4 +- .../PathItem/SingletonPathItemHandler.cs | 4 +- .../Capabilities/CapabilitiesConstants.cs | 28 +- .../Capabilities/CapabilitiesTermKind.cs | 163 ---------- .../CollectionPropertyRestrictionsType.cs | 36 ++- .../Capabilities/CustomParameter.cs | 2 + .../Capabilities/DeepInsertSupportType.cs | 1 + .../Capabilities/DeepUpdateSupportType.cs | 1 + .../Capabilities/DeleteRestrictionsType.cs | 15 +- .../Capabilities/ExpandRestrictionsType.cs | 7 +- .../FilterExpressionRestrictionType.cs | 44 +++ .../Capabilities/FilterRestrictionsType.cs | 17 + .../ModificationQueryOptionsType.cs | 1 + .../NavigationRestrictionsType.cs | 70 +++-- ...ictions.cs => OperationRestrictionType.cs} | 6 +- .../Capabilities/ReadRestrictionsType.cs | 16 + .../Capabilities/SelectSupportType.cs | 28 ++ .../Vocabulary/TermAttribute.cs | 3 +- .../EdmVocabularyAnnotationExtensionsTests.cs | 48 +-- .../EdmActionOperationHandlerTests.cs | 2 +- .../EdmFunctionOperationHandlerTests.cs | 2 +- .../PathItem/EntityPathItemHandlerTests.cs | 24 +- .../PathItem/EntitySetPathItemHandlerTests.cs | 12 +- .../PathItem/SingletonPathItemHandlerTests.cs | 23 +- .../Vocabulary/Authorization/ApiKeyTests.cs | 11 + .../Authorization/AuthorizationTests.cs | 126 +++----- .../AuthorizationVocabularyTests.cs | 2 +- .../Vocabulary/Authorization/HttpTests.cs | 111 +++++++ .../Authorization/OAuth2AuthCodeTests.cs | 110 +++++++ .../OAuth2ClientCredentialsTests.cs | 105 +++++++ .../Authorization/OAuth2ImplicitTests.cs | 105 +++++++ .../Authorization/OAuth2PasswordTests.cs | 110 +++++++ .../Authorization/OpenIDConnectTests.cs | 102 ++++++ .../Authorization/SecuritySchemeTests.cs | 3 +- ...CollectionPropertyRestrictionsTypeTests.cs | 78 +++++ .../CountRestrictionsTypeTests.cs | 111 ++----- .../Capabilities/CustomParameterTests.cs | 126 ++++++++ .../DeepInsertSupportTypeTests.cs | 79 +++++ .../DeepUpdateSupportTypeTests.cs | 79 +++++ .../DeleteRestrictionsTypeTests.cs | 75 ++++- .../ExpandRestrictionsTypeTests.cs | 30 +- .../FilterRestrictionsTypeTests.cs | 46 ++- .../Capabilities/IndexableByKeyTests.cs | 111 ------- .../InsertRestrictionsTypeTests.cs | 294 +++++------------- .../ModificationQueryOptionsTypeTests.cs | 114 +++++++ .../NavigationRestrictionsTypeTests.cs | 180 ++++++----- .../OperationRestrictionTypeTests.cs | 123 ++++++++ .../Capabilities/PermissionTypeTests.cs | 123 ++++++++ .../Capabilities/ReadRestrictionsTypeTests.cs | 173 +++++++++++ .../SearchRestrictionsTypeTests.cs | 32 +- .../Capabilities/SelectSupportTypeTests.cs | 113 +++++++ .../Capabilities/SkipSupportedTests.cs | 113 ------- .../Capabilities/SortRestrictionsTypeTests.cs | 11 + .../Capabilities/TopSupportedTests.cs | 114 ------- .../UpdateRestrictionsTypeTests.cs | 7 +- 61 files changed, 2369 insertions(+), 1164 deletions(-) delete mode 100644 src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesTermKind.cs create mode 100644 src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterExpressionRestrictionType.cs rename src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/{OperationRestrictions.cs => OperationRestrictionType.cs} (87%) create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/HttpTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2AuthCodeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ClientCredentialsTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ImplicitTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2PasswordTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OpenIDConnectTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CollectionPropertyRestrictionsTypeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepInsertSupportTypeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepUpdateSupportTypeTests.cs delete mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/IndexableByKeyTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ModificationQueryOptionsTypeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/OperationRestrictionTypeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/PermissionTypeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ReadRestrictionsTypeTests.cs create mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SelectSupportTypeTests.cs delete mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SkipSupportedTests.cs delete mode 100644 test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/TopSupportedTests.cs diff --git a/src/Microsoft.OpenApi.OData.Reader/Common/Utils.cs b/src/Microsoft.OpenApi.OData.Reader/Common/Utils.cs index 5e1d049..87230c8 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Common/Utils.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Common/Utils.cs @@ -4,6 +4,7 @@ // ------------------------------------------------------------ using System; +using Microsoft.OpenApi.OData.Vocabulary; namespace Microsoft.OpenApi.OData.Common { @@ -12,6 +13,23 @@ namespace Microsoft.OpenApi.OData.Common /// public static class Utils { + /// + /// Get the term qualified name when using the type of + /// + /// The type of the term. + /// The qualified name. + public static string GetTermQualifiedName() + { + object[] attributes = typeof(T).GetCustomAttributes(typeof(TermAttribute), false); + if (attributes == null && attributes.Length == 0) + { + return null; + } + + TermAttribute term = (TermAttribute)attributes[0]; + return term.QualifiedName; + } + /// /// Upper the first character of the string. /// diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/EdmAnnotationExtensions.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/EdmAnnotationExtensions.cs index 537dcf2..c2a7c4c 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/EdmAnnotationExtensions.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/EdmAnnotationExtensions.cs @@ -24,20 +24,6 @@ namespace Microsoft.OpenApi.OData.Edm private static IEdmModel _savedModel = null; // if diffenent model, the cache will be cleaned. private static object _objectLock = new object(); - /// - /// Get the term qualified name when using the type of - /// - /// The type of the term. - /// The qualified name. - public static string GetTermQualifiedName() - { - object[] attributes = typeof(T).GetCustomAttributes(typeof(TermAttribute), false); - Debug.Assert(attributes != null && attributes.Length == 1); - - TermAttribute term = (TermAttribute)attributes[0]; - return term.QualifiedName; - } - /// /// Gets the boolean term value for the given . /// @@ -129,7 +115,7 @@ namespace Microsoft.OpenApi.OData.Edm public static T GetRecord(this IEdmModel model, IEdmVocabularyAnnotatable target) where T : IRecord, new() { - string qualifiedName = GetTermQualifiedName(); + string qualifiedName = Utils.GetTermQualifiedName(); return model.GetRecord(target, qualifiedName); } @@ -226,7 +212,7 @@ namespace Microsoft.OpenApi.OData.Edm Utils.CheckArgumentNull(model, nameof(model)); Utils.CheckArgumentNull(target, nameof(target)); - string qualifiedName = GetTermQualifiedName(); + string qualifiedName = Utils.GetTermQualifiedName(); return GetCollection(model, target, qualifiedName); } @@ -386,7 +372,7 @@ namespace Microsoft.OpenApi.OData.Edm Debug.Assert(term != null); IEdmVocabularyAnnotation annotation = model.FindVocabularyAnnotations(target, term).FirstOrDefault(); - if (annotation != null && annotation.Value != null && annotation.Value.ExpressionKind == EdmExpressionKind.StringConstant) + if (annotation != null && annotation.Value != null && annotation.Value.ExpressionKind == EdmExpressionKind.BooleanConstant) { IEdmBooleanConstantExpression boolConstant = (IEdmBooleanConstantExpression)annotation.Value; if (boolConstant != null) @@ -481,7 +467,6 @@ namespace Microsoft.OpenApi.OData.Edm } - private static Type GetTypeInfo(string fullTypeName) where T : IRecord { object[] attributes = typeof(T).GetCustomAttributes(typeof(SubTypeAttribute), false); diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/RecordExpressionExtensions.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/RecordExpressionExtensions.cs index d56e76e..f104fff 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/RecordExpressionExtensions.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/RecordExpressionExtensions.cs @@ -4,7 +4,6 @@ // ------------------------------------------------------------ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using Microsoft.OData.Edm; diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSecuritySchemeGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSecuritySchemeGenerator.cs index 32a87f3..f0cfb92 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSecuritySchemeGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSecuritySchemeGenerator.cs @@ -36,6 +36,11 @@ namespace Microsoft.OpenApi.OData.Generator IDictionary securitySchemes = new Dictionary(); var authorizations = context.Model.GetAuthorizations(context.EntityContainer); + if (authorizations == null) + { + return securitySchemes; + } + foreach (var authorization in authorizations) { OpenApiSecurityScheme scheme = new OpenApiSecurityScheme diff --git a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs index c06e8df..952d6ba 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Operation/EdmOperationOperationHandler.cs @@ -154,14 +154,14 @@ namespace Microsoft.OpenApi.OData.Operation /// protected override void SetSecurity(OpenApiOperation operation) { - IEnumerable restrictions = Context.Model.GetCollection(EdmOperation, CapabilitiesConstants.OperationRestrictions); + IEnumerable restrictions = Context.Model.GetCollection(EdmOperation, CapabilitiesConstants.OperationRestrictions); if (restrictions == null || !restrictions.Any()) { return; } // TODO: how to use the collection? - OperationRestriction operationRestriction = restrictions.First(); + OperationRestrictionType operationRestriction = restrictions.First(); // the Permission should be collection, however current ODL supports the single permission. // Will update after ODL change. @@ -171,14 +171,14 @@ namespace Microsoft.OpenApi.OData.Operation /// protected override void AppendCustomParameters(OpenApiOperation operation) { - IEnumerable restrictions = Context.Model.GetCollection(EdmOperation, CapabilitiesConstants.OperationRestrictions); + IEnumerable restrictions = Context.Model.GetCollection(EdmOperation, CapabilitiesConstants.OperationRestrictions); if (restrictions == null || !restrictions.Any()) { return; } // TODO: how to use the collection? - OperationRestriction operationRestriction = restrictions.First(); + OperationRestrictionType operationRestriction = restrictions.First(); if (operationRestriction.CustomHeaders != null) { diff --git a/src/Microsoft.OpenApi.OData.Reader/PathItem/EntityPathItemHandler.cs b/src/Microsoft.OpenApi.OData.Reader/PathItem/EntityPathItemHandler.cs index 6f30fd8..3ea823e 100644 --- a/src/Microsoft.OpenApi.OData.Reader/PathItem/EntityPathItemHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/PathItem/EntityPathItemHandler.cs @@ -20,7 +20,7 @@ namespace Microsoft.OpenApi.OData.PathItem /// protected override void SetOperations(OpenApiPathItem item) { - ReadRestrictionsType read = Context.Model.GetRecord(EntitySet, CapabilitiesConstants.ReadRestrictions); + ReadRestrictionsType read = Context.Model.GetRecord(EntitySet); if (read == null || (read.ReadByKeyRestrictions == null && read.IsReadable) || (read.ReadByKeyRestrictions != null && read.ReadByKeyRestrictions.IsReadable)) @@ -29,13 +29,13 @@ namespace Microsoft.OpenApi.OData.PathItem AddOperation(item, OperationType.Get); } - UpdateRestrictionsType update = Context.Model.GetRecord(EntitySet, CapabilitiesConstants.UpdateRestrictions); + UpdateRestrictionsType update = Context.Model.GetRecord(EntitySet); if (update == null || update.IsUpdatable) { AddOperation(item, OperationType.Patch); } - DeleteRestrictionsType delete = Context.Model.GetRecord(EntitySet, CapabilitiesConstants.DeleteRestrictions); + DeleteRestrictionsType delete = Context.Model.GetRecord(EntitySet); if (delete == null || delete.IsDeletable) { AddOperation(item, OperationType.Delete); diff --git a/src/Microsoft.OpenApi.OData.Reader/PathItem/EntitySetPathItemHandler.cs b/src/Microsoft.OpenApi.OData.Reader/PathItem/EntitySetPathItemHandler.cs index 9b6fbdf..662e2b0 100644 --- a/src/Microsoft.OpenApi.OData.Reader/PathItem/EntitySetPathItemHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/PathItem/EntitySetPathItemHandler.cs @@ -26,13 +26,13 @@ namespace Microsoft.OpenApi.OData.PathItem /// protected override void SetOperations(OpenApiPathItem item) { - ReadRestrictionsType read = Context.Model.GetRecord(EntitySet, CapabilitiesConstants.ReadRestrictions); + ReadRestrictionsType read = Context.Model.GetRecord(EntitySet); if (read == null || read.IsReadable) { AddOperation(item, OperationType.Get); } - InsertRestrictionsType insert = Context.Model.GetRecord(EntitySet, CapabilitiesConstants.ReadRestrictions); + InsertRestrictionsType insert = Context.Model.GetRecord(EntitySet); if (insert == null || insert.IsInsertable) { AddOperation(item, OperationType.Post); diff --git a/src/Microsoft.OpenApi.OData.Reader/PathItem/SingletonPathItemHandler.cs b/src/Microsoft.OpenApi.OData.Reader/PathItem/SingletonPathItemHandler.cs index ed5eca6..fe447b4 100644 --- a/src/Microsoft.OpenApi.OData.Reader/PathItem/SingletonPathItemHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/PathItem/SingletonPathItemHandler.cs @@ -27,14 +27,14 @@ namespace Microsoft.OpenApi.OData.PathItem protected override void SetOperations(OpenApiPathItem item) { // Retrieve a singleton. - ReadRestrictionsType read = Context.Model.GetRecord(Singleton, CapabilitiesConstants.ReadRestrictions); + ReadRestrictionsType read = Context.Model.GetRecord(Singleton); if (read == null || read.IsReadable) { AddOperation(item, OperationType.Get); } // Update a singleton - UpdateRestrictionsType update = Context.Model.GetRecord(Singleton, CapabilitiesConstants.UpdateRestrictions); + UpdateRestrictionsType update = Context.Model.GetRecord(Singleton); if (update == null || update.IsUpdatable) { AddOperation(item, OperationType.Patch); diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesConstants.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesConstants.cs index b58bcca..11d5d49 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesConstants.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesConstants.cs @@ -23,62 +23,62 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Org.OData.Capabilities.V1.FilterRestrictions /// - public const string FilterRestrictions = "Org.OData.Capabilities.V1..FilterRestrictions"; + public const string FilterRestrictions = "Org.OData.Capabilities.V1.FilterRestrictions"; /// /// Org.OData.Capabilities.V1.SortRestrictions /// - public const string SortRestrictions = "Org.OData.Capabilities.V1..SortRestrictions"; + public const string SortRestrictions = "Org.OData.Capabilities.V1.SortRestrictions"; /// /// Org.OData.Capabilities.V1.NavigationRestrictions /// - public const string NavigationRestrictions = "Org.OData.Capabilities.V1..NavigationRestrictions"; + public const string NavigationRestrictions = "Org.OData.Capabilities.V1.NavigationRestrictions"; /// /// Org.OData.Capabilities.V1.SearchRestrictions /// - public const string SearchRestrictions = "Org.OData.Capabilities.V1..SearchRestrictions"; + public const string SearchRestrictions = "Org.OData.Capabilities.V1.SearchRestrictions"; /// /// Org.OData.Capabilities.V1.CountRestrictions /// - public const string CountRestrictions = "Org.OData.Capabilities.V1..CountRestrictions"; + public const string CountRestrictions = "Org.OData.Capabilities.V1.CountRestrictions"; /// /// Org.OData.Capabilities.V1.ExpandRestrictions /// - public const string ExpandRestrictions = "Org.OData.Capabilities.V1..ExpandRestrictions"; + public const string ExpandRestrictions = "Org.OData.Capabilities.V1.ExpandRestrictions"; /// /// Org.OData.Capabilities.V1.DeleteRestrictions /// - public const string DeleteRestrictions = "Org.OData.Capabilities.V1..DeleteRestrictions"; + public const string DeleteRestrictions = "Org.OData.Capabilities.V1.DeleteRestrictions"; /// /// Org.OData.Capabilities.V1.DeleteRestrictions /// - public const string UpdateRestrictions = "Org.OData.Capabilities.V1..UpdateRestrictions"; + public const string UpdateRestrictions = "Org.OData.Capabilities.V1.UpdateRestrictions"; /// /// Org.OData.Capabilities.V1.InsertRestrictions /// - public const string InsertRestrictions = "Org.OData.Capabilities.V1..InsertRestrictions"; + public const string InsertRestrictions = "Org.OData.Capabilities.V1.InsertRestrictions"; /// /// Org.OData.Capabilities.V1.TopSupported /// - public const string TopSupported = "Org.OData.Capabilities.V1..TopSupported"; + public const string TopSupported = "Org.OData.Capabilities.V1.TopSupported"; /// /// Org.OData.Capabilities.V1.SkipSupported /// - public const string SkipSupported = "Org.OData.Capabilities.V1..SkipSupported"; + public const string SkipSupported = "Org.OData.Capabilities.V1.SkipSupported"; /// /// Term: Org.OData.Capabilities.V1.BatchSupported /// - public const string BatchSupported = "Org.OData.Capabilities.V1..BatchSupported"; + public const string BatchSupported = "Org.OData.Capabilities.V1.BatchSupported"; /// /// Term: rg.OData.Capabilities.V1.OperationRestrictions @@ -88,11 +88,11 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Org.OData.Capabilities.V1.IndexableByKey /// - public const string IndexableByKey = "Org.OData.Capabilities.V1..IndexableByKey"; + public const string IndexableByKey = "Org.OData.Capabilities.V1.IndexableByKey"; /// /// Org.OData.Capabilities.V1.KeyAsSegmentSupported /// - public const string KeyAsSegmentSupported = "Org.OData.Capabilities.V1..KeyAsSegmentSupported"; + public const string KeyAsSegmentSupported = "Org.OData.Capabilities.V1.KeyAsSegmentSupported"; } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesTermKind.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesTermKind.cs deleted file mode 100644 index 6295010..0000000 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CapabilitiesTermKind.cs +++ /dev/null @@ -1,163 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. -// ------------------------------------------------------------ - -namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities -{ - /// - /// The Enum kind for Capabiliites annotation - /// - internal enum CapabilitesTermKind - { - /// - /// Media types of supported formats, including format parameters - /// - SupportedFormats, - - /// - /// List of acceptable compression methods for ($batch) requests, e.g. gzip - /// - AcceptableEncodings, - - /// - /// Supports key values according to OData URL conventions - /// - IndexableByKey, - - /// - /// Supported odata.isolation levels - /// - Isolation, - - /// - /// Supports key as segment - /// - KeyAsSegmentSupported, - - /// - /// Supports $top - /// - TopSupported, - - /// - /// Supports $skip - /// - SkipSupported, - - /// - /// Service supports the asynchronous request preference - /// - AsynchronousRequestsSupported, - - /// - /// Supports $batch requests - /// - BatchSupported, - - /// - /// Service supports the continue on error preference - /// - BatchContinueOnErrorSupported, - - /// - /// List of functions supported in $filter - /// - FilterFunctions, - - /// - /// Supports callbacks for the specified protocols - /// - CallbackSupported, - - /// - /// Supports cross joins for the entity sets in this container - /// - CrossJoinSupported, - - /// - /// Change tracking capabilities of this service or entity set - /// - ChangeTracking, - - /// - /// Restrictions on /$count path suffix and $count=true system query option - /// - CountRestrictions, - - /// - /// Restrictions on navigating properties according to OData URL conventions - /// - NavigationRestrictions, - - /// - /// Restrictions on $filter expressions - /// - FilterRestrictions, - - /// - /// Restrictions on $orderby expressions - /// - SortRestrictions, - - /// - /// Restrictions on $expand expressions - /// - ExpandRestrictions, - - /// - /// Restrictions on $search expressions - /// - SearchRestrictions, - - /// - /// Restrictions on insert operations - /// - InsertRestrictions, - - /// - /// Deep Insert Support of the annotated resource (the whole service, an entity set, or a collection-valued resource - /// - DeepInsertSupport, - - /// - /// Restrictions on update operations - /// - UpdateRestrictions, - - /// - /// Deep Update Support of the annotated resource (the whole service, an entity set, or a collection-valued resource) - /// - DeepUpdateSupported, - - /// - /// Restrictions on delete operations - /// - DeleteRestrictions, - - /// - /// Describes restrictions on operations applied to collection-valued structural properties. - /// - CollectionPropertyRestrictions, - - /// - /// Restrictions for function or action operation. - /// - OperationRestrictions, - - /// - /// Restrictions for retrieving a collection of entities, retrieving a singleton instance, invoking a function - /// - ReadRestrictions, - - /// - /// Custom headers that are supported/required for the annotated resource - /// - CustomHeaders, - - /// - /// Custom query options that are supported/required for the annotated resource - /// - CustomQueryOptions - } -} diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CollectionPropertyRestrictionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CollectionPropertyRestrictionsType.cs index e2958e1..d125e00 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CollectionPropertyRestrictionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CollectionPropertyRestrictionsType.cs @@ -13,6 +13,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Complex Type: Org.OData.Capabilities.V1.CollectionPropertyRestrictionsType /// + [Term("Org.OData.Capabilities.V1.CollectionPropertyRestrictions")] internal class CollectionPropertyRestrictionsType : IRecord { /// @@ -70,13 +71,46 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// public bool? Deletable { get; private set; } + /// + /// Init the . + /// + /// The input record. public virtual void Initialize(IEdmRecordExpression record) { Utils.CheckArgumentNull(record, nameof(record)); + // CollectionProperty + CollectionProperty = record.GetPropertyPath("CollectionProperty"); + + // FilterFunctions + FilterFunctions = record.GetCollection("FilterFunctions"); + + // FilterRestrictions + FilterRestrictions = record.GetRecord("FilterRestrictions"); + + // SearchRestrictions + SearchRestrictions = record.GetRecord("SearchRestrictions"); + + // SortRestrictions + SortRestrictions = record.GetRecord("SortRestrictions"); + + // TopSupported + TopSupported = record.GetBoolean("TopSupported"); + + // SkipSupported + SkipSupported = record.GetBoolean("SkipSupported"); + + // SelectSupport + SelectSupport = record.GetRecord("SelectSupport"); + + // Insertable + Insertable = record.GetBoolean("Insertable"); + + // Updatable + Updatable = record.GetBoolean("Updatable"); + // Deletable Deletable = record.GetBoolean("Deletable"); - } } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CustomParameter.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CustomParameter.cs index 31bcbae..d424326 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CustomParameter.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/CustomParameter.cs @@ -14,6 +14,8 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Complex type: Org.OData.Capabilities.V1.CustomParameter /// + // [Term("Org.OData.Capabilities.V1.CustomHeaders")] + // [Term("Org.OData.Capabilities.V1.CustomQueryOptions")] internal class CustomParameter : IRecord { /// diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepInsertSupportType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepInsertSupportType.cs index 4af1790..3bb6f80 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepInsertSupportType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepInsertSupportType.cs @@ -12,6 +12,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Complex Type: Org.OData.Capabilities.V1.DeepInsertSupport /// + [Term("Org.OData.Capabilities.V1.DeepInsertSupport")] internal class DeepInsertSupportType : IRecord { /// diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepUpdateSupportType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepUpdateSupportType.cs index 33c474e..b152ece 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepUpdateSupportType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeepUpdateSupportType.cs @@ -12,6 +12,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Complex Type: Org.OData.Capabilities.V1.DeepUpdateSupportType /// + [Term("Org.OData.Capabilities.V1.DeepUpdateSupport")] internal class DeepUpdateSupportType : IRecord { /// diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeleteRestrictionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeleteRestrictionsType.cs index d3d7f09..30618dc 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeleteRestrictionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/DeleteRestrictionsType.cs @@ -43,7 +43,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities public IList CustomHeaders { get; private set; } /// - /// Gets the Supported or required custom headers. + /// Gets the Supported or required custom query options. /// public IList CustomQueryOptions { get; private set; } @@ -76,11 +76,20 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities // Deletable Deletable = record.GetBoolean("Deletable"); + // NonDeletableNavigationProperties + NonDeletableNavigationProperties = record.GetCollectionPropertyPath("NonDeletableNavigationProperties"); + // MaxLevels MaxLevels = (int?)record.GetInteger("MaxLevels"); - // NonDeletableNavigationProperties - NonDeletableNavigationProperties = record.GetCollectionPropertyPath("NonDeletableNavigationProperties"); + // Permission + Permission = record.GetRecord("Permission"); + + // CustomHeaders + CustomHeaders = record.GetCollection("CustomHeaders"); + + // CustomQueryOptions + CustomQueryOptions = record.GetCollection("CustomQueryOptions"); } } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ExpandRestrictionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ExpandRestrictionsType.cs index 1f326e6..caf5ca5 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ExpandRestrictionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ExpandRestrictionsType.cs @@ -14,7 +14,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Org.OData.Capabilities.V1.ExpandRestrictions /// - [Term("Org.OData.Capabilities.V1.DeleteRestrictions")] + [Term("Org.OData.Capabilities.V1.ExpandRestrictions")] internal class ExpandRestrictionsType : IRecord { /// @@ -30,7 +30,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Gets the maximum number of levels that can be expanded in a expand expression. /// - public int? MaxLevles { get; private set; } + public long? MaxLevels { get; private set; } /// /// Test the target supports $expand. @@ -57,6 +57,9 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities // NonExpandableProperties NonExpandableProperties = record.GetCollectionPropertyPath("NonExpandableProperties"); + + // MaxLevels + MaxLevels = record.GetInteger("MaxLevels"); } } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterExpressionRestrictionType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterExpressionRestrictionType.cs new file mode 100644 index 0000000..a7b8ab5 --- /dev/null +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterExpressionRestrictionType.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------ +// 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.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; + +namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities +{ + /// + /// Complex Type: Org.OData.Capabilities.V1.FilterExpressionRestrictionType + /// + internal class FilterExpressionRestrictionType : IRecord + { + /// + /// Gets the Path to the restricted property. + /// + public string Property { get; private set; } + + /// + /// Gets the RequiresFilter value. + /// + /// + /// + public string AllowedExpressions { get; private set; } + + /// + /// Init the . + /// + /// The input record. + public void Initialize(IEdmRecordExpression record) + { + Utils.CheckArgumentNull(record, nameof(record)); + + // Property + Property = record.GetPropertyPath("Property"); + + // AllowedExpressions + AllowedExpressions = record.GetString("AllowedExpressions"); + } + } +} diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterRestrictionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterRestrictionsType.cs index 947b447..7cd05d4 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterRestrictionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/FilterRestrictionsType.cs @@ -37,6 +37,17 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// public IList NonFilterableProperties { get; private set; } + /// + /// Gets The maximum number of levels (including recursion) that can be traversed in a filter expression. A value of -1 indicates there is no restriction. + /// + public long? MaxLevels { get; private set; } + + /// + /// Gets These properties only allow a subset of filter expressions. + /// A valid filter expression for a single property can be enclosed in parentheses and combined by `and` with valid expressions for other properties. + /// + public IList FilterExpressionRestrictions { get; private set; } + /// /// Test the target supports filter. /// @@ -82,6 +93,12 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities // NonFilterableProperties NonFilterableProperties = record.GetCollectionPropertyPath("NonFilterableProperties"); + + // MaxLevels + MaxLevels = record.GetInteger("MaxLevels"); + + // FilterExpressionRestrictions + FilterExpressionRestrictions = record.GetCollection("FilterExpressionRestrictions"); } } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ModificationQueryOptionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ModificationQueryOptionsType.cs index 74b54a3..51aa8c2 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ModificationQueryOptionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ModificationQueryOptionsType.cs @@ -12,6 +12,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// /// Complex type: Org.OData.Capabilities.V1.ModificationQueryOptionsType /// + [Term("Org.OData.Capabilities.V1.ModificationQueryOptions")] internal class ModificationQueryOptionsType : IRecord { /// diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/NavigationRestrictionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/NavigationRestrictionsType.cs index fe8631d..32ca4e7 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/NavigationRestrictionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/NavigationRestrictionsType.cs @@ -143,7 +143,43 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities FilterRestrictions = record.GetRecord("FilterRestrictions"); // SearchRestrictions - //SearchRestrictions = record.GetRecord("SearchRestrictions", (r, t) => r.Init(t)); + SearchRestrictions = record.GetRecord("SearchRestrictions"); + + // SortRestrictions + SortRestrictions = record.GetRecord("SortRestrictions"); + + // TopSupported + TopSupported = record.GetBoolean("TopSupported"); + + // SkipSupported + SkipSupported = record.GetBoolean("SkipSupported"); + + // SelectSupport + SelectSupport = record.GetRecord("SelectSupport"); + + // IndexableByKey + IndexableByKey = record.GetBoolean("IndexableByKey"); + + // InsertRestrictions + InsertRestrictions = record.GetRecord("InsertRestrictions"); + + // DeepInsertSupport + DeepInsertSupport = record.GetRecord("DeepInsertSupport"); + + // UpdateRestrictions + UpdateRestrictions = record.GetRecord("UpdateRestrictions"); + + // DeepUpdateSupport + DeepUpdateSupport = record.GetRecord("DeepUpdateSupport"); + + // DeleteRestrictions + DeleteRestrictions = record.GetRecord("DeleteRestrictions"); + + // IndexableByKey + OptimisticConcurrencyControl = record.GetBoolean("OptimisticConcurrencyControl"); + + // ReadRestrictions + ReadRestrictions = record.GetRecord("ReadRestrictions"); } } @@ -182,7 +218,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities } /// - /// Init the . + /// Init the . /// /// The input record. public void Initialize(IEdmRecordExpression record) @@ -195,35 +231,5 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities // RestrictedProperties RestrictedProperties = record.GetCollection("RestrictedProperties"); } - - private static IList GetRestrictedProperties(IEdmRecordExpression record) - { - if (record != null && record.Properties != null) - { - IEdmPropertyConstructor property = record.Properties.FirstOrDefault(p => p.Name == "RestrictedProperties"); - if (property != null) - { - IEdmCollectionExpression value = property.Value as IEdmCollectionExpression; - if (value != null && value.Elements != null) - { - IList restrictedProperties = new List(); - foreach (var item in value.Elements.OfType()) - { - NavigationPropertyRestriction restriction = new NavigationPropertyRestriction(); - restriction.Navigability = item.GetEnum("Navigability"); - restriction.NavigationProperty = item.GetPropertyPath("NavigationProperty"); - restrictedProperties.Add(restriction); - } - - if (restrictedProperties.Any()) - { - return restrictedProperties; - } - } - } - } - - return null; - } } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/OperationRestrictions.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/OperationRestrictionType.cs similarity index 87% rename from src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/OperationRestrictions.cs rename to src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/OperationRestrictionType.cs index 0599538..22a155b 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/OperationRestrictions.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/OperationRestrictionType.cs @@ -11,9 +11,11 @@ using Microsoft.OpenApi.OData.Edm; namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities { /// - /// Complex Type: Org.OData.Capabilities.V1.OperationRestriction + /// Complex Type: Org.OData.Capabilities.V1.OperationRestrictionType + /// Be note: in ODL 7.6, it's named as "OperationRestriction", after that, it will be changed as "OperationRestrictionType" /// - internal class OperationRestriction : IRecord + [Term("Org.OData.Capabilities.V1.OperationRestrictions")] + internal class OperationRestrictionType : IRecord { /// /// Gets the List of required scopes to invoke an action or function. diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ReadRestrictionsType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ReadRestrictionsType.cs index 557c8d1..65aec22 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ReadRestrictionsType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/ReadRestrictionsType.cs @@ -10,6 +10,9 @@ using Microsoft.OpenApi.OData.Edm; namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities { + /// + /// Complex Type: Org.OData.Capabilities.V1.ReadRestrictionsBase + /// internal abstract class ReadRestrictionsBase : IRecord { /// @@ -38,6 +41,10 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities /// True/false. public bool IsReadable => Readable == null || Readable.Value; + /// + /// Init the . + /// + /// The input record. public virtual void Initialize(IEdmRecordExpression record) { Utils.CheckArgumentNull(record, nameof(record)); @@ -57,10 +64,12 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities } /// + /// Complex Type: Org.OData.Capabilities.V1.ReadByKeyRestrictionsType /// Restrictions for retrieving an entity by key /// internal class ReadByKeyRestrictions : ReadRestrictionsBase { + // nothing here } /// @@ -69,8 +78,15 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities [Term("Org.OData.Capabilities.V1.ReadRestrictions")] internal class ReadRestrictionsType : ReadRestrictionsBase { + /// + /// Gets the Restrictions for retrieving an entity by key + /// public ReadByKeyRestrictions ReadByKeyRestrictions { get; set; } + /// + /// Init the . + /// + /// The input record. public override void Initialize(IEdmRecordExpression record) { // Load base diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/SelectSupportType.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/SelectSupportType.cs index 68e6f78..2deff9d 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/SelectSupportType.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/Capabilities/SelectSupportType.cs @@ -5,12 +5,14 @@ using Microsoft.OData.Edm.Vocabularies; using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities { /// /// Complex type: Org.OData.Capabilities.V1.SelectSupportType /// + [Term("Org.OData.Capabilities.V1.SelectSupport")] internal class SelectSupportType : IRecord { /// @@ -66,6 +68,32 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities { Utils.CheckArgumentNull(record, nameof(record)); + // Supported + Supported = record.GetBoolean("Supported"); + + // Expandable + Expandable = record.GetBoolean("Expandable"); + + // Filterable + Filterable = record.GetBoolean("Filterable"); + + // Searchable + Searchable = record.GetBoolean("Searchable"); + + // TopSupported + TopSupported = record.GetBoolean("TopSupported"); + + // SkipSupported + SkipSupported = record.GetBoolean("SkipSupported"); + + // ComputeSupported + ComputeSupported = record.GetBoolean("ComputeSupported"); + + // Countable + Countable = record.GetBoolean("Countable"); + + // Sortable + Sortable = record.GetBoolean("Sortable"); } } } diff --git a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/TermAttribute.cs b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/TermAttribute.cs index f42566b..2d5feb8 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Vocabulary/TermAttribute.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Vocabulary/TermAttribute.cs @@ -3,7 +3,6 @@ // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------ -using Microsoft.OData.Edm.Vocabularies; using System; using System.Collections.Generic; @@ -12,7 +11,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary /// /// /// - [AttributeUsage(AttributeTargets.Class)] + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] internal class TermAttribute : Attribute { public TermAttribute(string qualifiedName) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/EdmVocabularyAnnotationExtensionsTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/EdmVocabularyAnnotationExtensionsTests.cs index f45d220..7d44b7b 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/EdmVocabularyAnnotationExtensionsTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Edm/EdmVocabularyAnnotationExtensionsTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Xml.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; @@ -192,39 +193,40 @@ namespace Microsoft.OpenApi.OData.Edm.Tests // Assert Assert.NotNull(counts1); Assert.NotNull(counts2); - Assert.Same(counts1, counts2); Assert.Equal(2, counts1.Count()); - // #1 - CountRestrictionsType count = counts1.First(); + foreach (var countItem in new[] { counts1, counts2 }) + { + CountRestrictionsType count = countItem.First(); - // Countable - Assert.NotNull(count.Countable); - Assert.True(count.Countable.Value); + // Countable + Assert.NotNull(count.Countable); + Assert.True(count.Countable.Value); - // NonCountableProperties - Assert.NotNull(count.NonCountableProperties); - Assert.Equal(new[] { "123", "abc" }, count.NonCountableProperties); + // NonCountableProperties + Assert.NotNull(count.NonCountableProperties); + Assert.Equal(new[] { "123", "abc" }, count.NonCountableProperties); - // NonCountableNavigationProperties - Assert.NotNull(count.NonCountableNavigationProperties); - Assert.Equal(new[] { "234", "xyz" }, count.NonCountableNavigationProperties); + // NonCountableNavigationProperties + Assert.NotNull(count.NonCountableNavigationProperties); + Assert.Equal(new[] { "234", "xyz" }, count.NonCountableNavigationProperties); - // #2 - count = counts1.Last(); + // #2 + count = countItem.Last(); - // Countable - Assert.NotNull(count.Countable); - Assert.False(count.Countable.Value); + // Countable + Assert.NotNull(count.Countable); + Assert.False(count.Countable.Value); - // NonCountableProperties - Assert.NotNull(count.NonCountableProperties); - Assert.Equal(new[] { "567", "mij" }, count.NonCountableProperties); + // NonCountableProperties + Assert.NotNull(count.NonCountableProperties); + Assert.Equal(new[] { "567", "mij" }, count.NonCountableProperties); - // NonCountableNavigationProperties - Assert.NotNull(count.NonCountableNavigationProperties); - Assert.Equal(new[] { "789", "rst" }, count.NonCountableNavigationProperties); + // NonCountableNavigationProperties + Assert.NotNull(count.NonCountableNavigationProperties); + Assert.Equal(new[] { "789", "rst" }, count.NonCountableNavigationProperties); + } } private IEdmModel GetEdmModel(string annotation) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs index 7d25cc0..101e4b4 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmActionOperationHandlerTests.cs @@ -132,7 +132,7 @@ namespace Microsoft.OpenApi.OData.Operation.Tests if (enableOperationId) { - Assert.Equal("Customers.VipCustomer.MyAction", operation.OperationId); + Assert.Equal("Customers.NS.VipCustomer.MyAction", operation.OperationId); } else { diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs index 03f81b0..da8243a 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Operation/EdmFunctionOperationHandlerTests.cs @@ -136,7 +136,7 @@ namespace Microsoft.OpenApi.OData.Operation.Tests if (enableOperationId) { - Assert.Equal("Customers.VipCustomer.MyFunction.1e00", operation.OperationId); + Assert.Equal("Customers.NS.VipCustomer.MyFunction.1e00", operation.OperationId); } else { diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntityPathItemHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntityPathItemHandlerTests.cs index bd94c22..9534afc 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntityPathItemHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntityPathItemHandlerTests.cs @@ -81,16 +81,16 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateEntityPathItemWorksForReadByKeyRestrictionsCapablities(bool readable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", readable); +"; // Assert VerifyPathItemOperations(annotation, expected); @@ -102,12 +102,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateEntityPathItemWorksForReadRestrictionsCapablities(bool readable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", readable); +"; // Assert VerifyPathItemOperations(annotation, expected); @@ -119,12 +119,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateEntityPathItemWorksForUpdateRestrictionsCapablities(bool updatable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", updatable); +"; // Assert VerifyPathItemOperations(annotation, expected); @@ -136,12 +136,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateEntityPathItemWorksForDeleteRestrictionsCapablities(bool deletable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", deletable); +"; // Assert VerifyPathItemOperations(annotation, expected); diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntitySetPathItemHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntitySetPathItemHandlerTests.cs index af43abb..e3ca808 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntitySetPathItemHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/EntitySetPathItemHandlerTests.cs @@ -85,12 +85,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateEntitySetPathItemWorksForReadRestrictionsCapablities(bool readable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", readable); +"; // Assert VerifyPathItemOperations(annotation, expected); @@ -102,12 +102,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateEntitySetPathItemWorksForInsertRestrictionsCapablities(bool insertable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", insertable); +"; // Assert VerifyPathItemOperations(annotation, expected); diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/SingletonPathItemHandlerTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/SingletonPathItemHandlerTests.cs index a05fae8..b00aa16 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/SingletonPathItemHandlerTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/PathItem/SingletonPathItemHandlerTests.cs @@ -85,12 +85,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateSingletonPathItemWorksForReadRestrictionsCapablities(bool readable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", readable); +"; // Assert VerifyPathItemOperations(annotation, expected); @@ -102,12 +102,12 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests public void CreateSingletonPathItemWorksForUpdateRestrictionsCapablities(bool updatable, OperationType[] expected) { // Arrange - string annotation = String.Format(@" + string annotation = $@" - + -", updatable); +"; // Assert VerifyPathItemOperations(annotation, expected); @@ -148,16 +148,7 @@ namespace Microsoft.OpenApi.OData.PathItem.Tests - - - - - - - - - - + {0} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/ApiKeyTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/ApiKeyTests.cs index 199bb8f..e4e1c9c 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/ApiKeyTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/ApiKeyTests.cs @@ -9,6 +9,7 @@ using System.Xml.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Authorization; using Xunit; @@ -17,6 +18,16 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests { public class ApiKeyTests { + [Fact] + public void SchemeTypeKindSetCorrectly() + { + // Arrange + ApiKey apiKey = new ApiKey(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.ApiKey, apiKey.SchemeType); + } + [Fact] public void InitializeThrowArgumentNullRecord() { diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationTests.cs index 2e7646d..22a103b 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationTests.cs @@ -3,121 +3,77 @@ // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------ -using System.Xml.Linq; +using System; using Microsoft.OData.Edm; -using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Exceptions; +using Microsoft.OpenApi.OData.Properties; using Microsoft.OpenApi.OData.Vocabulary.Authorization; using Xunit; namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests { - public class Authorization1Tests + public class AuthorizationTests { [Fact] - public void TermAttributeAttachedOnSecurityScheme() + public void CreateAuthorizationReturnsNullWithNullRecord() { - OData.Vocabulary.Authorization.Authorization a = new Http(); - // Arrange & Act - string qualifiedName = EdmVocabularyAnnotationExtensions.GetTermQualifiedName(); + var authorization = OData.Vocabulary.Authorization.Authorization.CreateAuthorization(record: null); // Assert - Assert.Equal("Org.OData.Authorization.V1.SecuritySchemes", qualifiedName); + Assert.Null(authorization); } [Fact] - public void InitializeSecuritySchemeWithRecordSuccess() + public void CreateAuthorizationThrowsForOAuthAuthorizationRecord() { - // Arrange - IEdmRecordExpression record = new EdmRecordExpression( - new EdmPropertyConstructor("Authorization", new EdmStringConstant("DelegatedWork")), - new EdmPropertyConstructor("RequiredScopes", new EdmCollectionExpression( - new EdmStringConstant("User.ReadAll"), - new EdmStringConstant("User.WriteAll")))); + // Arrange & Act + IEdmStructuredTypeReference structuredTypeRef = GetType("Org.OData.Authorization.V1.OAuthAuthorization"); + IEdmRecordExpression record = new EdmRecordExpression(structuredTypeRef, + new EdmPropertyConstructor("Name", new EdmStringConstant("temp"))); - SecurityScheme securityScheme = new SecurityScheme(); - Assert.Null(securityScheme.Authorization); - Assert.Null(securityScheme.RequiredScopes); - - // Act - securityScheme.Initialize(record); + Action test = () => OData.Vocabulary.Authorization.Authorization.CreateAuthorization(record); // Assert - Assert.NotNull(securityScheme.Authorization); - Assert.Equal("DelegatedWork", securityScheme.Authorization); - - Assert.NotNull(securityScheme.RequiredScopes); - Assert.Equal(2, securityScheme.RequiredScopes.Count); - Assert.Equal(new[] { "User.ReadAll", "User.WriteAll" }, securityScheme.RequiredScopes); + OpenApiException exception = Assert.Throws(test); + Assert.Equal(String.Format(SRResource.AuthorizationRecordTypeNameNotCorrect, structuredTypeRef.FullName()), exception.Message); } - [Fact] - public void InitializeSecuritySchemeWorksWithCsdl() + [Theory] + [InlineData(typeof(OpenIDConnect))] + [InlineData(typeof(Http))] + [InlineData(typeof(ApiKey))] + [InlineData(typeof(OAuth2ClientCredentials))] + [InlineData(typeof(OAuth2Implicit))] + [InlineData(typeof(OAuth2Password))] + [InlineData(typeof(OAuth2AuthCode))] + public void CreateAuthorizationReturnsOpenIDConnect(Type type) { - // Arrange - string annotation = @" - - - - - - User.ReadAll - User.WriteAll - - - - - - - - Directory.ReadAll - Directory.WriteAll - - - - - "; - - IEdmModel model = GetEdmModel(annotation); - Assert.NotNull(model); // guard - Assert.NotNull(model.EntityContainer); - - // Act - SecurityScheme[] schemes = model.GetCollection(model.EntityContainer).ToArray(); + // Arrange & Act + string qualifiedName = AuthorizationConstants.Namespace + "." + type.Name; + IEdmRecordExpression record = new EdmRecordExpression(GetType(qualifiedName), + new EdmPropertyConstructor("Name", new EdmStringConstant("temp"))); // Assert - Assert.NotNull(schemes); - Assert.Equal(2, schemes.Length); + var authorization = OData.Vocabulary.Authorization.Authorization.CreateAuthorization(record); + Assert.NotNull(authorization); + Assert.Equal(type, authorization.GetType()); - // #1 - Assert.Equal("DelegatedWork", schemes[0].Authorization); - Assert.Equal(2, schemes[0].RequiredScopes.Count); - Assert.Equal(new[] { "User.ReadAll", "User.WriteAll" }, schemes[0].RequiredScopes); - - // #2 - Assert.Equal("DelegatedPersonal", schemes[1].Authorization); - Assert.Equal(2, schemes[1].RequiredScopes.Count); - Assert.Equal(new[] { "Directory.ReadAll", "Directory.WriteAll" }, schemes[1].RequiredScopes); + Assert.Equal("temp", authorization.Name); + Assert.Null(authorization.Description); } - private IEdmModel GetEdmModel(string annotation) + private static IEdmStructuredTypeReference GetType(string qualifiedName) { - const string template = @" - - - - {0} - - - -"; - string modelText = string.Format(template, annotation); + EdmModel model = new EdmModel(); + IEdmType edmType = model.FindType(qualifiedName); + Assert.NotNull(edmType); - IEdmModel model; + IEdmComplexType complexType = edmType as IEdmComplexType; + Assert.NotNull(complexType); - bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); - Assert.True(result); - return model; + return new EdmComplexTypeReference(complexType, true); } } } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationVocabularyTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationVocabularyTests.cs index bd1cd88..848a466 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationVocabularyTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/AuthorizationVocabularyTests.cs @@ -24,7 +24,7 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests EdmEntityContainer container = new EdmEntityContainer("NS", "Container"); model.AddElement(container); - // Act & Assert + // Act var authorizations = model.GetAuthorizations(container); // Assert diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/HttpTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/HttpTests.cs new file mode 100644 index 0000000..f775dc5 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/HttpTests.cs @@ -0,0 +1,111 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Authorization; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests +{ + public class HttpTests + { + [Fact] + public void SchemeTypeKindSetCorrectly() + { + // Arrange + Http http = new Http(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.Http, http.SchemeType); + } + + [Fact] + public void InitializeThrowArgumentNullRecord() + { + // Arrange & Act & Assert + Assert.Throws("record", () => new Http().Initialize(record: null)); + } + + [Fact] + public void InitializeHttpWithRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Name", new EdmStringConstant("HttpWork")), + new EdmPropertyConstructor("Description", new EdmStringConstant("Description of the scheme")), + new EdmPropertyConstructor("Scheme", new EdmStringConstant("Authorization scheme")), + new EdmPropertyConstructor("BearerFormat", new EdmStringConstant("Format of the bearer token"))); + + Http http = new Http(); + Assert.Null(http.Name); + Assert.Null(http.Description); + Assert.Null(http.Scheme); + Assert.Null(http.BearerFormat); + + // Act + http.Initialize(record); + + // Assert + Assert.Equal("HttpWork", http.Name); + Assert.Equal("Description of the scheme", http.Description); + Assert.Equal("Authorization scheme", http.Scheme); + Assert.Equal("Format of the bearer token", http.BearerFormat); + } + + [Fact] + public void InitializeHttpWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + Http http = model.GetRecord(model.EntityContainer, "NS.MyHttp"); + + // Assert + Assert.NotNull(http); + Assert.Equal("HttpWork", http.Name); + Assert.Null(http.Description); + Assert.Equal("Authorization scheme", http.Scheme); + Assert.Equal("Format of the bearer token", http.BearerFormat); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2AuthCodeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2AuthCodeTests.cs new file mode 100644 index 0000000..420098b --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2AuthCodeTests.cs @@ -0,0 +1,110 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Authorization; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests +{ + public class OAuth2AuthCodeTests + { + [Fact] + public void SchemeTypeKindAndOAuthTypeSetCorrectly() + { + // Arrange + OAuth2AuthCode authCode = new OAuth2AuthCode(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.OAuth2, authCode.SchemeType); + Assert.Equal(OAuth2Type.AuthCode, authCode.OAuth2Type); + } + + [Fact] + public void InitializeThrowArgumentNullRecord() + { + // Arrange & Act & Assert + Assert.Throws("record", () => new OAuth2AuthCode().Initialize(record: null)); + } + + [Fact] + public void InitializeOAuth2AuthCodeWithRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("TokenUrl", new EdmStringConstant("http://tokenUrl")), + new EdmPropertyConstructor("AuthorizationUrl", new EdmStringConstant("http://authorizationUrl"))); + + OAuth2AuthCode authCode = new OAuth2AuthCode(); + Assert.Null(authCode.Name); + Assert.Null(authCode.Description); + Assert.Null(authCode.Scopes); + Assert.Null(authCode.AuthorizationUrl); + Assert.Null(authCode.TokenUrl); + + // Act + authCode.Initialize(record); + + // Assert + Assert.Null(authCode.Name); + Assert.Null(authCode.Description); + Assert.Null(authCode.Scopes); + Assert.Equal("http://authorizationUrl", authCode.AuthorizationUrl); + Assert.Equal("http://tokenUrl", authCode.TokenUrl); + } + + [Fact] + public void InitializeOAuth2AuthCodeWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + OAuth2AuthCode authoCode = model.GetRecord(model.EntityContainer, "NS.MyOAuth2AuthCode"); + + // Assert + Assert.Null(authoCode.Name); + Assert.Null(authoCode.Description); + Assert.Null(authoCode.Scopes); + Assert.Equal("http://tokenUrl", authoCode.TokenUrl); + Assert.Equal("http://authorizationUrl", authoCode.AuthorizationUrl); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ClientCredentialsTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ClientCredentialsTests.cs new file mode 100644 index 0000000..898cd41 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ClientCredentialsTests.cs @@ -0,0 +1,105 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Authorization; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests +{ + public class OAuth2ClientCredentialsTests + { + [Fact] + public void SchemeTypeKindAndOAuthTypeSetCorrectly() + { + // Arrange + OAuth2ClientCredentials credentials = new OAuth2ClientCredentials(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.OAuth2, credentials.SchemeType); + Assert.Equal(OAuth2Type.ClientCredentials, credentials.OAuth2Type); + } + + [Fact] + public void InitializeThrowArgumentNullRecord() + { + // Arrange & Act & Assert + Assert.Throws("record", () => new OAuth2ClientCredentials().Initialize(record: null)); + } + + [Fact] + public void InitializeOAuth2ClientCredentialsWithRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("TokenUrl", new EdmStringConstant("http://tokenUrl"))); + + OAuth2ClientCredentials credentials = new OAuth2ClientCredentials(); + Assert.Null(credentials.Name); + Assert.Null(credentials.Description); + Assert.Null(credentials.Scopes); + Assert.Null(credentials.TokenUrl); + + // Act + credentials.Initialize(record); + + // Assert + Assert.Null(credentials.Name); + Assert.Null(credentials.Description); + Assert.Null(credentials.Scopes); + Assert.Equal("http://tokenUrl", credentials.TokenUrl); + } + + [Fact] + public void InitializeOAuth2ClientCredentialsWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + OAuth2ClientCredentials credentials = model.GetRecord(model.EntityContainer, "NS.MyOAuth2ClientCredentials"); + + // Assert + Assert.Null(credentials.Name); + Assert.Null(credentials.Description); + Assert.Null(credentials.Scopes); + Assert.Equal("http://tokenUrl", credentials.TokenUrl); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ImplicitTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ImplicitTests.cs new file mode 100644 index 0000000..51ac1b6 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2ImplicitTests.cs @@ -0,0 +1,105 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Authorization; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests +{ + public class OAuth2ImplicitTests + { + [Fact] + public void SchemeTypeKindAndOAuthTypeSetCorrectly() + { + // Arrange + OAuth2Implicit oAuthImplicit = new OAuth2Implicit(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.OAuth2, oAuthImplicit.SchemeType); + Assert.Equal(OAuth2Type.Implicit, oAuthImplicit.OAuth2Type); + } + + [Fact] + public void InitializeThrowArgumentNullRecord() + { + // Arrange & Act & Assert + Assert.Throws("record", () => new OAuth2Implicit().Initialize(record: null)); + } + + [Fact] + public void InitializeOAuth2ImplicitWithRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("AuthorizationUrl", new EdmStringConstant("http://authorizationUrl"))); + + OAuth2Implicit oAuthImplicit = new OAuth2Implicit(); + Assert.Null(oAuthImplicit.Name); + Assert.Null(oAuthImplicit.Description); + Assert.Null(oAuthImplicit.Scopes); + Assert.Null(oAuthImplicit.AuthorizationUrl); + + // Act + oAuthImplicit.Initialize(record); + + // Assert + Assert.Null(oAuthImplicit.Name); + Assert.Null(oAuthImplicit.Description); + Assert.Null(oAuthImplicit.Scopes); + Assert.Equal("http://authorizationUrl", oAuthImplicit.AuthorizationUrl); + } + + [Fact] + public void InitializeOAuth2ImplicitWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + OAuth2Implicit oAuthImplicit = model.GetRecord(model.EntityContainer, "NS.MyOAuth2Implicit"); + + // Assert + Assert.Null(oAuthImplicit.Name); + Assert.Null(oAuthImplicit.Description); + Assert.Null(oAuthImplicit.Scopes); + Assert.Equal("http://authorizationUrl", oAuthImplicit.AuthorizationUrl); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2PasswordTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2PasswordTests.cs new file mode 100644 index 0000000..d193f07 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OAuth2PasswordTests.cs @@ -0,0 +1,110 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Authorization; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests +{ + public class OAuth2PasswordTests + { + [Fact] + public void SchemeTypeKindAndOAuthTypeSetCorrectly() + { + // Arrange + OAuth2Password password = new OAuth2Password(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.OAuth2, password.SchemeType); + Assert.Equal(OAuth2Type.Pasword, password.OAuth2Type); + } + + [Fact] + public void InitializeThrowArgumentNullRecord() + { + // Arrange & Act & Assert + Assert.Throws("record", () => new OAuth2Password().Initialize(record: null)); + } + + [Fact] + public void InitializeOAuth2PasswordWithRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("RefreshUrl", new EdmStringConstant("http://refreshUrl")), + new EdmPropertyConstructor("TokenUrl", new EdmStringConstant("http://tokenUrl"))); + + OAuth2Password password = new OAuth2Password(); + Assert.Null(password.Name); + Assert.Null(password.Description); + Assert.Null(password.Scopes); + Assert.Null(password.RefreshUrl); + Assert.Null(password.TokenUrl); + + // Act + password.Initialize(record); + + // Assert + Assert.Null(password.Name); + Assert.Null(password.Description); + Assert.Null(password.Scopes); + Assert.Equal("http://refreshUrl", password.RefreshUrl); + Assert.Equal("http://tokenUrl", password.TokenUrl); + } + + [Fact] + public void InitializeOAuth2PasswordWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + OAuth2Password password = model.GetRecord(model.EntityContainer, "NS.MyOAuth2Password"); + + // Assert + Assert.Null(password.Name); + Assert.Null(password.Description); + Assert.Null(password.Scopes); + Assert.Equal("http://refreshUrl", password.RefreshUrl); + Assert.Equal("http://tokenUrl", password.TokenUrl); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OpenIDConnectTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OpenIDConnectTests.cs new file mode 100644 index 0000000..f97f97b --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/OpenIDConnectTests.cs @@ -0,0 +1,102 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Authorization; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests +{ + public class OpenIDConnectTests + { + [Fact] + public void SchemeTypeKindSetCorrectly() + { + // Arrange + OpenIDConnect openIDConnect = new OpenIDConnect(); + + // Act & Assert + Assert.Equal(SecuritySchemeType.OpenIdConnect, openIDConnect.SchemeType); + } + + [Fact] + public void InitializeThrowArgumentNullRecord() + { + // Arrange & Act & Assert + Assert.Throws("record", () => new OpenIDConnect().Initialize(record: null)); + } + + [Fact] + public void InitializeOpenIDConnectWithRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Name", new EdmStringConstant("OpenIDConnectWork")), + new EdmPropertyConstructor("IssuerUrl", new EdmStringConstant("http://any"))); + + OpenIDConnect idConnection = new OpenIDConnect(); + Assert.Null(idConnection.Name); + Assert.Null(idConnection.Description); + Assert.Null(idConnection.IssuerUrl); + + // Act + idConnection.Initialize(record); + + // Assert + Assert.Equal("OpenIDConnectWork", idConnection.Name); + Assert.Null(idConnection.Description); + Assert.Equal("http://any", idConnection.IssuerUrl); + } + + [Fact] + public void InitializeOpenIDConnectWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + OpenIDConnect idConnection = model.GetRecord(model.EntityContainer, "NS.MyOpenIDConnect"); + + // Assert + Assert.Null(idConnection.Name); + Assert.Null(idConnection.Description); + Assert.Equal("http://any", idConnection.IssuerUrl); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/SecuritySchemeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/SecuritySchemeTests.cs index 9ad1a2d..0616a57 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/SecuritySchemeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Authorization/SecuritySchemeTests.cs @@ -9,6 +9,7 @@ using System.Xml.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Authorization; using Xunit; @@ -28,7 +29,7 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Authorization.Tests public void TermAttributeAttachedOnSecurityScheme() { // Arrange & Act - string qualifiedName = EdmVocabularyAnnotationExtensions.GetTermQualifiedName(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert Assert.Equal("Org.OData.Authorization.V1.SecuritySchemes", qualifiedName); diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CollectionPropertyRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CollectionPropertyRestrictionsTypeTests.cs new file mode 100644 index 0000000..e65ce6f --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CollectionPropertyRestrictionsTypeTests.cs @@ -0,0 +1,78 @@ +// ------------------------------------------------------------ +// 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.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class CollectionPropertyRestrictionsTypeTests + { + [Fact] + public void TermAttributeAttachedOnCollectionPropertyRestrictionsType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.CollectionPropertyRestrictions", qualifiedName); + } + + [Fact] + public void InitializeCollectionPropertyRestrictionsTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("CollectionProperty", new EdmPropertyPathExpression("abc/xyz")), + new EdmPropertyConstructor("FilterFunctions", new EdmCollectionExpression(new EdmStringConstant("div"))), + new EdmPropertyConstructor("FilterRestrictions", new EdmRecordExpression(new EdmPropertyConstructor("Filterable", new EdmBooleanConstant(true)))), + new EdmPropertyConstructor("SearchRestrictions", new EdmRecordExpression(new EdmPropertyConstructor("Searchable", new EdmBooleanConstant(false)))), + new EdmPropertyConstructor("SortRestrictions", new EdmRecordExpression(new EdmPropertyConstructor("Sortable", new EdmBooleanConstant(false)))), + new EdmPropertyConstructor("TopSupported", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("Deletable", new EdmBooleanConstant(false)) + // SkipSupported + // SelectSupport + // Insertable + // Updatable + ); + + // Act + CollectionPropertyRestrictionsType collectionPropertyRestrictions = new CollectionPropertyRestrictionsType(); + collectionPropertyRestrictions.Initialize(record); + + // Assert + Assert.Null(collectionPropertyRestrictions.SkipSupported); + Assert.Null(collectionPropertyRestrictions.SelectSupport); + Assert.Null(collectionPropertyRestrictions.Insertable); + Assert.Null(collectionPropertyRestrictions.Updatable); + + Assert.Equal("abc/xyz", collectionPropertyRestrictions.CollectionProperty); + + Assert.NotNull(collectionPropertyRestrictions.FilterFunctions); + string function = Assert.Single(collectionPropertyRestrictions.FilterFunctions); + Assert.Equal("div", function); + + Assert.NotNull(collectionPropertyRestrictions.FilterRestrictions); + Assert.NotNull(collectionPropertyRestrictions.FilterRestrictions.Filterable); + Assert.True(collectionPropertyRestrictions.FilterRestrictions.Filterable.Value); + + Assert.NotNull(collectionPropertyRestrictions.SearchRestrictions); + Assert.NotNull(collectionPropertyRestrictions.SearchRestrictions.Searchable); + Assert.False(collectionPropertyRestrictions.SearchRestrictions.Searchable.Value); + + Assert.NotNull(collectionPropertyRestrictions.SortRestrictions); + Assert.NotNull(collectionPropertyRestrictions.SortRestrictions.Sortable); + Assert.False(collectionPropertyRestrictions.SortRestrictions.Sortable.Value); + + Assert.NotNull(collectionPropertyRestrictions.TopSupported); + Assert.True(collectionPropertyRestrictions.TopSupported.Value); + + Assert.NotNull(collectionPropertyRestrictions.Deletable); + Assert.False(collectionPropertyRestrictions.Deletable.Value); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CountRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CountRestrictionsTypeTests.cs index ad68c6b..1ca2e19 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CountRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CountRestrictionsTypeTests.cs @@ -5,7 +5,8 @@ using System; using Microsoft.OData.Edm; -using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -15,87 +16,41 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class CountRestrictionsTypeTests { [Fact] - public void CountRestrictionsTypeAsTermQualifiedName() + public void TermAttributeAttachedOnCountRestrictionsType() { // Arrange & Act - + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.CountRestrictions", qualifiedName); } [Fact] - public void PropertiesAsNullForDefaultCountRestrictionsType() + public void InitializeCountRestrictionsTypeWithRecordSuccess() { - // Arrange & Act + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Countable", new EdmBooleanConstant(false) ), + new EdmPropertyConstructor("NonCountableProperties", new EdmCollectionExpression( + new EdmPropertyPathExpression("Emails"), + new EdmPropertyPathExpression("mij"))), + new EdmPropertyConstructor("NonCountableNavigationProperties", new EdmCollectionExpression( + new EdmNavigationPropertyPathExpression("RelatedEvents"), + new EdmNavigationPropertyPathExpression("abc"))) + ); + + // Act CountRestrictionsType count = new CountRestrictionsType(); + count.Initialize(record); // Assert - Assert.Null(count.Countable); - Assert.Null(count.NonCountableProperties); - Assert.Null(count.NonCountableNavigationProperties); + VerifyCountRestrictions(count); } [Fact] - public void UnknownAnnotatableTargetReturnsDefaultPropertyValues() - { - // Arrange & Act - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); - - // Act - CountRestrictionsType count = EdmCoreModel.Instance.GetRecord(entityType); - - // Assert - Assert.Null(count); - } - - [Theory] - [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] - [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] - public void TargetOnEntityTypeReturnsCorrectCountRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) + public void InitializeCountRestrictionsWorksWithCsdl() { // Arrange - const string template = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); - Assert.NotNull(calendars); // guard - - // Act - CountRestrictionsType count = model.GetRecord(calendars); - - // Assert - VerifyCountRestrictions(count); - } - - [Theory] - [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] - [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] - public void TargetOnEntitySetReturnsCorrectCountRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) - { - // Arrange - const string template = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); - Assert.NotNull(calendars); // guard - - // Act - CountRestrictionsType count = model.GetRecord(calendars); - - // Assert - VerifyCountRestrictions(count); - } - - private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) - { string countAnnotation = @" @@ -115,15 +70,17 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests "; - if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) - { - countAnnotation = string.Format(template, countAnnotation); - return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation); - } - else - { - return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation); - } + IEdmModel model = CapabilitiesModelHelper.GetEdmModelSetInline(countAnnotation); + Assert.NotNull(model); // guard + + IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); + Assert.NotNull(calendars); // guard + + // Act + CountRestrictionsType count = model.GetRecord(calendars); + + // Assert + VerifyCountRestrictions(count); } private static void VerifyCountRestrictions(CountRestrictionsType count) diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs new file mode 100644 index 0000000..b37f694 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/CustomParameterTests.cs @@ -0,0 +1,126 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. +// ------------------------------------------------------------ + +using System.IO; +using System.Xml; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Microsoft.OpenApi.OData.Vocabulary.Core; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class CustomParameterTests + { + [Fact] + public void InitializeCustomParameterWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Name", new EdmStringConstant("odata-debug")), + new EdmPropertyConstructor("Description", new EdmStringConstant("Debug support for OData services")), + new EdmPropertyConstructor("DocumentationURL", new EdmStringConstant("https://debug.html")), + new EdmPropertyConstructor("Required", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("ExampleValues", + new EdmCollectionExpression( + new EdmRecordExpression(new EdmPropertyConstructor("Value", new EdmStringConstant("html"))), + new EdmRecordExpression(new EdmPropertyConstructor("Value", new EdmTimeOfDayConstant(new TimeOfDay(3, 4, 5, 6))))))); + + // Act + CustomParameter parameter = new CustomParameter(); + parameter.Initialize(record); + + // Assert + VerifyCustomParameter(parameter); + } + + [Fact] + public void InitializeCountRestrictionsWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + + + + + + + + + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + + // Act + CustomParameter count = model.GetRecord(model.EntityContainer, "NS.MyCustomParameter"); + + // Assert + VerifyCustomParameter(count); + } + + private static void VerifyCustomParameter(CustomParameter parameter) + { + Assert.NotNull(parameter); + + Assert.NotNull(parameter.Name); + Assert.Equal("odata-debug", parameter.Name); + + Assert.NotNull(parameter.Description); + Assert.Equal("Debug support for OData services", parameter.Description); + + Assert.NotNull(parameter.DocumentationURL); + Assert.Equal("https://debug.html", parameter.DocumentationURL); + + Assert.NotNull(parameter.Required); + Assert.False(parameter.Required.Value); + + Assert.NotNull(parameter.ExampleValues); + Assert.Equal(2, parameter.ExampleValues.Count); + + // #1 + PrimitiveExampleValue value = parameter.ExampleValues[0]; + Assert.Null(value.Description); + Assert.NotNull(value.Value); + Assert.Equal("html", value.Value.Value); + + // #2 + value = parameter.ExampleValues[1]; + Assert.Null(value.Description); + Assert.NotNull(value.Value); + Assert.Equal(new TimeOfDay(3, 4, 5, 6), value.Value.Value); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + {0} + + + +"; + string schema = string.Format(template, annotation); + + IEdmModel model; + bool result = SchemaReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(schema)) }, out model, out _); + Assert.True(result); + return model; + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepInsertSupportTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepInsertSupportTypeTests.cs new file mode 100644 index 0000000..b9e8fc5 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepInsertSupportTypeTests.cs @@ -0,0 +1,79 @@ +// ------------------------------------------------------------ +// 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.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class DeepInsertSupportTypeTests + { + [Fact] + public void TermAttributeAttachedOnDeepInsertSupportType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.DeepInsertSupport", qualifiedName); + } + + [Fact] + public void InitializeDeepInsertSupportTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Supported", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("ContentIDSupported", new EdmBooleanConstant(true))); + + // Act + DeepInsertSupportType deepInsert = new DeepInsertSupportType(); + deepInsert.Initialize(record); + + // Assert + VerifyDeepInsertSupportType(deepInsert); + } + + [Fact] + public void InitializeDeepInsertSupportTypeWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + "; + + IEdmModel model = CapabilitiesModelHelper.GetEdmModelSetInline(annotation); + Assert.NotNull(model); // guard + + IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); + Assert.NotNull(calendars); // guard + + // Act + DeepInsertSupportType deepInsert = model.GetRecord(calendars); + + // Assert + VerifyDeepInsertSupportType(deepInsert); + } + + private static void VerifyDeepInsertSupportType(DeepInsertSupportType deepInsert) + { + Assert.NotNull(deepInsert); + + Assert.NotNull(deepInsert.Supported); + Assert.False(deepInsert.Supported.Value); + + Assert.NotNull(deepInsert.ContentIDSupported); + Assert.True(deepInsert.ContentIDSupported.Value); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepUpdateSupportTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepUpdateSupportTypeTests.cs new file mode 100644 index 0000000..e1aba36 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeepUpdateSupportTypeTests.cs @@ -0,0 +1,79 @@ +// ------------------------------------------------------------ +// 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.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class DeepUpdateSupportTypeTests + { + [Fact] + public void TermAttributeAttachedOnDeepUpdateSupportType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.DeepUpdateSupport", qualifiedName); + } + + [Fact] + public void InitializeDeepUpdateSupportTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Supported", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("ContentIDSupported", new EdmBooleanConstant(true))); + + // Act + DeepUpdateSupportType deepUpdate = new DeepUpdateSupportType(); + deepUpdate.Initialize(record); + + // Assert + VerifyDeepUpdateSupportType(deepUpdate); + } + + [Fact] + public void InitializeDeepUpdateSupportTypeWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + "; + + IEdmModel model = CapabilitiesModelHelper.GetEdmModelSetInline(annotation); + Assert.NotNull(model); // guard + + IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); + Assert.NotNull(calendars); // guard + + // Act + DeepUpdateSupportType deepUpdate = model.GetRecord(calendars); + + // Assert + VerifyDeepUpdateSupportType(deepUpdate); + } + + private static void VerifyDeepUpdateSupportType(DeepUpdateSupportType deepUpdate) + { + Assert.NotNull(deepUpdate); + + Assert.NotNull(deepUpdate.Supported); + Assert.False(deepUpdate.Supported.Value); + + Assert.NotNull(deepUpdate.ContentIDSupported); + Assert.True(deepUpdate.ContentIDSupported.Value); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeleteRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeleteRestrictionsTypeTests.cs index 99606e1..916e97b 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeleteRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/DeleteRestrictionsTypeTests.cs @@ -6,6 +6,8 @@ using System; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -15,26 +17,39 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class DeleteRestrictionsTypeTests { [Fact] - public void KindPropertyReturnsDeleteRestrictionsEnumMember() + public void TermAttributeAttachedOnDeleteRestrictionsType() { // Arrange & Act - DeleteRestrictionsType delete = new DeleteRestrictionsType(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert - // Assert.Equal(CapabilitesTermKind.DeleteRestrictions, delete.Kind); + Assert.Equal("Org.OData.Capabilities.V1.DeleteRestrictions", qualifiedName); } [Fact] - public void UnknownAnnotatableTargetReturnsDefaultDeleteRestrictionsValues() + public void InitializeDeleteRestrictionsTypeWithRecordSuccess() { - // Arrange - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Deletable", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("NonDeletableNavigationProperties", + new EdmCollectionExpression(new EdmNavigationPropertyPathExpression("abc"), new EdmNavigationPropertyPathExpression("RelatedEvents"))), + new EdmPropertyConstructor("MaxLevels", new EdmIntegerConstant(42)), + new EdmPropertyConstructor("Permission", new EdmRecordExpression( + new EdmPropertyConstructor("Scheme", new EdmRecordExpression(new EdmPropertyConstructor("Authorization", new EdmStringConstant("schemeName")))))), + new EdmPropertyConstructor("CustomQueryOptions", new EdmCollectionExpression( + new EdmRecordExpression( + new EdmPropertyConstructor("Name", new EdmStringConstant("odata-debug")), + new EdmPropertyConstructor("DocumentationURL", new EdmStringConstant("https://debug.html"))))) + // CustomHeaders + ); - // Act - DeleteRestrictionsType delete = EdmCoreModel.Instance.GetRecord(entityType); + // Act + DeleteRestrictionsType delete = new DeleteRestrictionsType(); + delete.Initialize(record); // Assert - Assert.Null(delete); + VerifyDeleteRestrictionsType(delete); } [Theory] @@ -58,7 +73,7 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests DeleteRestrictionsType delete = model.GetRecord(calendars); // Assert - VerifyDeleteRestrictions(delete); + VerifyDeleteRestrictionsType(delete); } [Theory] @@ -82,12 +97,12 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests DeleteRestrictionsType delete = model.GetRecord(calendars); // Assert - VerifyDeleteRestrictions(delete); + VerifyDeleteRestrictionsType(delete); } private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) { - string countAnnotation = @" + string annotation = @" @@ -97,21 +112,39 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests RelatedEvents + + + + + + + + + + + + + + + + + + "; if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) { - countAnnotation = string.Format(template, countAnnotation); - return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation); + annotation = string.Format(template, annotation); + return CapabilitiesModelHelper.GetEdmModelOutline(annotation); } else { - return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation); + return CapabilitiesModelHelper.GetEdmModelTypeInline(annotation); } } - private static void VerifyDeleteRestrictions(DeleteRestrictionsType delete) + private static void VerifyDeleteRestrictionsType(DeleteRestrictionsType delete) { Assert.NotNull(delete); @@ -123,6 +156,16 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Assert.Equal("abc|RelatedEvents", String.Join("|", delete.NonDeletableNavigationProperties)); Assert.True(delete.IsNonDeletableNavigationProperty("RelatedEvents")); + + Assert.NotNull(delete.Permission); + Assert.Equal("schemeName", delete.Permission.Scheme.Authorization); + + Assert.Null(delete.CustomHeaders); + + Assert.NotNull(delete.CustomQueryOptions); + CustomParameter parameter = Assert.Single(delete.CustomQueryOptions); + Assert.Equal("odata-debug", parameter.Name); + Assert.Equal("https://debug.html", parameter.DocumentationURL); } } } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ExpandRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ExpandRestrictionsTypeTests.cs index 3d69e2e..039b9e0 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ExpandRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ExpandRestrictionsTypeTests.cs @@ -6,6 +6,8 @@ using System; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -15,26 +17,32 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class ExpandRestrictionsTypeTests { [Fact] - public void KindPropertyReturnsExpandRestrictionsEnumMember() + public void TermAttributeAttachedOnExpandRestrictionsType() { // Arrange & Act - ExpandRestrictionsType expand = new ExpandRestrictionsType(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert - // Assert.Equal(CapabilitesTermKind.ExpandRestrictions, expand.Kind); + Assert.Equal("Org.OData.Capabilities.V1.ExpandRestrictions", qualifiedName); } [Fact] - public void UnknownAnnotatableTargetReturnsDefaultExpandRestrictionsValues() + public void InitializeExpandRestrictionsTypeWithRecordSuccess() { - // Arrange - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Expandable", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("NonExpandableProperties", + new EdmCollectionExpression(new EdmNavigationPropertyPathExpression("abc"), new EdmNavigationPropertyPathExpression("RelatedEvents"))), + new EdmPropertyConstructor("MaxLevels", new EdmIntegerConstant(42)) + ); - // Act - ExpandRestrictionsType expand = EdmCoreModel.Instance.GetRecord(entityType); + // Act + ExpandRestrictionsType expand = new ExpandRestrictionsType(); + expand.Initialize(record); // Assert - Assert.Null(expand); + VerifyExpandRestrictions(expand); } [Theory] @@ -91,6 +99,7 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests + abc @@ -118,6 +127,9 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Assert.NotNull(expand.Expandable); Assert.False(expand.Expandable.Value); + Assert.NotNull(expand.MaxLevels); + Assert.Equal(42, expand.MaxLevels.Value); + Assert.NotNull(expand.NonExpandableProperties); Assert.Equal(2, expand.NonExpandableProperties.Count); Assert.Equal("abc|RelatedEvents", String.Join("|", expand.NonExpandableProperties)); diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/FilterRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/FilterRestrictionsTypeTests.cs index 4082193..a55d2fe 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/FilterRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/FilterRestrictionsTypeTests.cs @@ -6,6 +6,8 @@ using System.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -15,32 +17,41 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class FilterRestrictionsTypeTests { [Fact] - public void KindPropertyReturnsFilterRestrictionsEnumMember() + public void TermAttributeAttachedOnFilterRestrictionsType() { // Arrange & Act - FilterRestrictionsType filter = new FilterRestrictionsType(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert - // Assert.Equal(CapabilitesTermKind.FilterRestrictions, filter.Kind); + Assert.Equal("Org.OData.Capabilities.V1.FilterRestrictions", qualifiedName); } [Fact] - public void UnknownAnnotatableTargetReturnsDefaultFilterRestrictionsValues() + public void InitializFilterRestrictionsTypeWithRecordSuccess() { - // Arrange - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Filterable", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("RequiresFilter", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("RequiredProperties", new EdmCollectionExpression(new EdmPropertyPathExpression("Id"))), + new EdmPropertyConstructor("NonFilterableProperties", new EdmCollectionExpression(new EdmPropertyPathExpression("Emails"))), + new EdmPropertyConstructor("FilterExpressionRestrictions", new EdmCollectionExpression( + new EdmRecordExpression(new EdmPropertyConstructor("Property", new EdmPropertyPathExpression("RelatedProperty"))))), + new EdmPropertyConstructor("MaxLevels", new EdmIntegerConstant(42)) + ); - // Act - FilterRestrictionsType filter = EdmCoreModel.Instance.GetRecord(entityType); + // Act + FilterRestrictionsType filter = new FilterRestrictionsType(); + filter.Initialize(record); // Assert - Assert.Null(filter); + VerifyFilterRestrictions(filter); } [Theory] [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] - public void TargetOnEntityTypeReturnsCorrectfilterRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) + public void TargetOnEntityTypeReturnsCorrectFilterRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) { // Arrange const string template = @" @@ -92,6 +103,7 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests + Id @@ -102,6 +114,13 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Emails + + + + + + + "; @@ -134,6 +153,13 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Assert.Single(filter.NonFilterableProperties); Assert.Equal("Emails", filter.NonFilterableProperties.First()); + Assert.NotNull(filter.MaxLevels); + Assert.Equal(42, filter.MaxLevels.Value); + + Assert.NotNull(filter.FilterExpressionRestrictions); + FilterExpressionRestrictionType express = Assert.Single(filter.FilterExpressionRestrictions); + Assert.Equal("RelatedProperty", express.Property); + Assert.True(filter.IsRequiredProperty("Id")); Assert.False(filter.IsRequiredProperty("ID")); diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/IndexableByKeyTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/IndexableByKeyTests.cs deleted file mode 100644 index de83303..0000000 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/IndexableByKeyTests.cs +++ /dev/null @@ -1,111 +0,0 @@ -// ------------------------------------------------------------ -// 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 Xunit; - -namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests -{ -#if false - public class IndexableByKeyTests - { - [Fact] - public void KindPropertyReturnsIndexableByKeyEnumMember() - { - // Arrange & Act - IndexableByKey index = new IndexableByKey(); - - // Assert - Assert.Equal(CapabilitesTermKind.IndexableByKey, index.Kind); - } - - [Fact] - public void UnknownAnnotatableTargetReturnsDefaultIndexableByKeyValues() - { - // Arrange - IndexableByKey index = new IndexableByKey(); - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); - - // Act - bool result = index.Load(EdmCoreModel.Instance, entityType); - - // Assert - Assert.False(result); - Assert.True(index.IsSupported); - Assert.Null(index.Supported); - } - - [Theory] - [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] - [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] - public void TargetOnEntityTypeReturnsCorrectIndexableByKeyValue(EdmVocabularyAnnotationSerializationLocation location) - { - // Arrange - const string template = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); - Assert.NotNull(calendars); // guard - - // Act - IndexableByKey index = new IndexableByKey(); - bool result = index.Load(model, calendars); - - // Assert - Assert.True(result); - 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 = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); - Assert.NotNull(calendars); // guard - - // Act - IndexableByKey index = new IndexableByKey(); - bool result = index.Load(model, calendars); - - // Assert - Assert.True(result); - Assert.NotNull(index.Supported); - Assert.False(index.Supported.Value); - } - - private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) - { - string countAnnotation = @""; - - if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) - { - countAnnotation = string.Format(template, countAnnotation); - return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation); - } - else - { - return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation); - } - } - } -#endif -} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/InsertRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/InsertRestrictionsTypeTests.cs index 3cb364b..c16818c 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/InsertRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/InsertRestrictionsTypeTests.cs @@ -7,9 +7,10 @@ using System; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; -using Microsoft.OpenApi.OData.Tests; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Microsoft.OpenApi.OData.Vocabulary.Core; using Xunit; namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests @@ -17,140 +18,46 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class InsertRestrictionsTypeTests { [Fact] - public void OkEdmModel() + public void TermAttributeAttachedOnInsertRestrictionsType() { // Arrange & Act - EdmModel model = new EdmModel(); - EdmEntityType entity = new EdmEntityType("NS", "Entity"); - entity.AddKeys(entity.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); - model.AddElement(entity); + string qualifiedName = Utils.GetTermQualifiedName(); - EdmEntityContainer container = new EdmEntityContainer("NS", "Default"); - EdmEntitySet set = container.AddEntitySet("Entities", entity); - model.AddElement(container); - - IEdmComplexType securitySchemeType = model.FindType("Org.OData.Authorization.V1.SecurityScheme") as IEdmComplexType; - IEdmRecordExpression securityScheme = new EdmRecordExpression( - new EdmComplexTypeReference(securitySchemeType, false), - new EdmPropertyConstructor("Authorization", new EdmStringConstant("authorizationName")), - new EdmPropertyConstructor("RequiredScopes", new EdmCollectionExpression(new EdmStringConstant("RequiredScopes1"), new EdmStringConstant("RequiredScopes2")))); - - IEdmComplexType scopeType = model.FindType("Org.OData.Capabilities.V1.ScopeType") as IEdmComplexType; - IEdmRecordExpression scope1 = new EdmRecordExpression( - new EdmComplexTypeReference(scopeType, false), - new EdmPropertyConstructor("Scope", new EdmStringConstant("scopeName1")), - new EdmPropertyConstructor("RestrictedProperties", new EdmStringConstant("p1,p2"))); - - IEdmRecordExpression scope2 = new EdmRecordExpression( - new EdmComplexTypeReference(scopeType, false), - new EdmPropertyConstructor("Scope", new EdmStringConstant("scopeName2")), - new EdmPropertyConstructor("RestrictedProperties", new EdmStringConstant("p3,p4"))); - - IEdmComplexType permissionType = model.FindType("Org.OData.Capabilities.V1.PermissionType") as IEdmComplexType; - IEdmRecordExpression permission = new EdmRecordExpression( - new EdmComplexTypeReference(permissionType, false), - new EdmPropertyConstructor("Scheme", securityScheme), - new EdmPropertyConstructor("Scopes", new EdmCollectionExpression(scope1, scope2))); - - IEdmComplexType complex = model.FindType("Org.OData.Capabilities.V1.InsertRestrictionsType") as IEdmComplexType; - - IEdmCollectionExpression nonInsertableProperties = new EdmCollectionExpression - ( - new EdmPathExpression("abc"), - new EdmPathExpression("xyz") - ); - - IEdmCollectionExpression nonInsertableNavigationProperties = new EdmCollectionExpression - ( - new EdmNavigationPropertyPathExpression("nvabc"), - new EdmNavigationPropertyPathExpression("nvxyz") - ); - - IEdmRecordExpression queryOptions = new EdmRecordExpression( - new EdmPropertyConstructor("ExpandSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("SelectSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("ComputeSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("FilterSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("SearchSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("SortSupported", new EdmBooleanConstant(false)), - new EdmPropertyConstructor("SortSupported", new EdmBooleanConstant(false))); + // Assert + Assert.Equal("Org.OData.Capabilities.V1.InsertRestrictions", qualifiedName); + } + [Fact] + public void InitializInsertRestrictionsTypeWithRecordSuccess() + { + // Assert IEdmRecordExpression primitiveExampleValue = new EdmRecordExpression( - new EdmPropertyConstructor("Description", new EdmStringConstant("Description23")), - new EdmPropertyConstructor("Value", new EdmStringConstant("Value1"))); + new EdmPropertyConstructor("Description", new EdmStringConstant("example desc")), + new EdmPropertyConstructor("Value", new EdmStringConstant("example value"))); - IEdmRecordExpression customHeaders1 = new EdmRecordExpression( - new EdmPropertyConstructor("Name", new EdmStringConstant("HeadName1")), - new EdmPropertyConstructor("Description", new EdmStringConstant("Description1")), - new EdmPropertyConstructor("ComputeSupported", new EdmStringConstant("http://any")), - new EdmPropertyConstructor("Required", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("ExampleValues", new EdmCollectionExpression(primitiveExampleValue))); - - IEdmRecordExpression customHeaders2 = new EdmRecordExpression( - new EdmPropertyConstructor("Name", new EdmStringConstant("HeadName2")), - new EdmPropertyConstructor("Description", new EdmStringConstant("Description2")), - new EdmPropertyConstructor("ComputeSupported", new EdmStringConstant("http://any")), - new EdmPropertyConstructor("Required", new EdmBooleanConstant(false)), - new EdmPropertyConstructor("ExampleValues", new EdmCollectionExpression(primitiveExampleValue))); - - IEdmRecordExpression insertRecord = new EdmRecordExpression( - new EdmComplexTypeReference(complex, false), + IEdmRecordExpression record = new EdmRecordExpression( new EdmPropertyConstructor("Insertable", new EdmBooleanConstant(false)), - new EdmPropertyConstructor("NonInsertableProperties", nonInsertableProperties), - new EdmPropertyConstructor("NonInsertableNavigationProperties", nonInsertableNavigationProperties), + new EdmPropertyConstructor("NonInsertableProperties", new EdmCollectionExpression(new EdmPathExpression("abc/xyz"))), + new EdmPropertyConstructor("NonInsertableNavigationProperties", new EdmCollectionExpression(new EdmNavigationPropertyPathExpression("abc"), new EdmNavigationPropertyPathExpression("RelatedEvents"))), new EdmPropertyConstructor("MaxLevels", new EdmIntegerConstant(8)), - new EdmPropertyConstructor("Permission", permission), - new EdmPropertyConstructor("QueryOptions", queryOptions), - new EdmPropertyConstructor("CustomHeaders", new EdmCollectionExpression(customHeaders1, customHeaders2)), - new EdmPropertyConstructor("CustomQueryOptions", new EdmCollectionExpression(customHeaders1, customHeaders2)) + new EdmPropertyConstructor("CustomQueryOptions", new EdmCollectionExpression( + new EdmRecordExpression( + new EdmPropertyConstructor("Name", new EdmStringConstant("primitive name")), + new EdmPropertyConstructor("Description", new EdmStringConstant("primitive desc")), + new EdmPropertyConstructor("DocumentationURL", new EdmStringConstant("http://any3")), + new EdmPropertyConstructor("Required", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("ExampleValues", new EdmCollectionExpression(primitiveExampleValue))))) + // QueryOptions + // Permission + // CustomHeaders ); - IEdmTerm term = model.FindTerm("Org.OData.Capabilities.V1.InsertRestrictions"); - var annotation = new EdmVocabularyAnnotation(set, term, insertRecord); - - annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); - model.SetVocabularyAnnotation(annotation); - - // Assert - string csdl = EdmModelHelper.GetCsdl(model); - Assert.Equal("", csdl); - } - - [Fact] - public void KindPropertyReturnsInsertRestrictionsEnumMember() - { - // Arrange & Act - InsertRestrictionsType insert = new InsertRestrictionsType(); - - // Assert - // Assert.Equal(CapabilitesTermKind.InsertRestrictions, insert.Kind); - } - - [Fact] - public void KindPropertyReturnsInsertRestrictionsEnumMember2() - { - // Arrange - IEdmModel model = GetModelWithInsertRestrictions(); - IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Entities"); - // Act - InsertRestrictionsType insert = model.GetRecord(entitySet); + InsertRestrictionsType insert = new InsertRestrictionsType(); + insert.Initialize(record); // Assert - //Assert.Equal(CapabilitesTermKind.InsertRestrictions, insert.Kind); - } - - [Fact] - public void UnknownAnnotatableTargetReturnsDefaultInsertRestrictionsValues() - { - // Arrange - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); - - // Act - InsertRestrictionsType insert = EdmCoreModel.Instance.GetRecord(entityType); - - // Assert - Assert.Null(insert); + VerifyInsertRestrictions(insert); } [Theory] @@ -207,13 +114,36 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests + + + abc/xyz + + abc RelatedEvents - + + + + + + + + + + + + + + + + + + + "; @@ -228,107 +158,6 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests } } - public IEdmModel GetModelWithInsertRestrictions() - { - EdmModel model = new EdmModel(); - - IEdmRecordExpression securityScheme = new EdmRecordExpression( - new EdmPropertyConstructor("Authorization", new EdmStringConstant("authorizationName")), - new EdmPropertyConstructor("RequiredScopes", new EdmCollectionExpression(new EdmStringConstant("RequiredScopes1"), new EdmStringConstant("RequiredScopes2")))); - - IEdmRecordExpression scope1 = new EdmRecordExpression( - new EdmPropertyConstructor("Scope", new EdmStringConstant("scopeName1")), - new EdmPropertyConstructor("RestrictedProperties", new EdmStringConstant("p1,p2"))); - - IEdmRecordExpression scope2 = new EdmRecordExpression( - new EdmPropertyConstructor("Scope", new EdmStringConstant("scopeName2")), - new EdmPropertyConstructor("RestrictedProperties", new EdmStringConstant("p3,p4"))); - - IEdmRecordExpression permission = new EdmRecordExpression( - new EdmPropertyConstructor("Scheme", securityScheme), - new EdmPropertyConstructor("Scopes", new EdmCollectionExpression(scope1, scope2))); - - IEdmCollectionExpression nonInsertableProperties = new EdmCollectionExpression - ( - new EdmPathExpression("abc"), - new EdmPathExpression("xyz") - ); - - IEdmCollectionExpression nonInsertableNavigationProperties = new EdmCollectionExpression - ( - new EdmNavigationPropertyPathExpression("nvabc"), - new EdmNavigationPropertyPathExpression("nvxyz") - ); - - IEdmRecordExpression queryOptions = new EdmRecordExpression( - new EdmPropertyConstructor("ExpandSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("SelectSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("ComputeSupported", new EdmBooleanConstant(false)), - new EdmPropertyConstructor("FilterSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("SearchSupported", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("SortSupported", new EdmBooleanConstant(false))); - - IEdmRecordExpression primitiveExampleValue = new EdmRecordExpression( - new EdmPropertyConstructor("Description", new EdmStringConstant("Description23")), - new EdmPropertyConstructor("Value", new EdmStringConstant("Value1"))); - - IEdmRecordExpression customHeaders1 = new EdmRecordExpression( - new EdmPropertyConstructor("Name", new EdmStringConstant("HeadName1")), - new EdmPropertyConstructor("Description", new EdmStringConstant("Description1")), - new EdmPropertyConstructor("ComputeSupported", new EdmStringConstant("http://any")), - new EdmPropertyConstructor("Required", new EdmBooleanConstant(true)), - new EdmPropertyConstructor("ExampleValues", new EdmCollectionExpression(primitiveExampleValue))); - - IEdmRecordExpression customHeaders2 = new EdmRecordExpression( - new EdmPropertyConstructor("Name", new EdmStringConstant("HeadName2")), - new EdmPropertyConstructor("Description", new EdmStringConstant("Description2")), - new EdmPropertyConstructor("ComputeSupported", new EdmStringConstant("http://any")), - new EdmPropertyConstructor("Required", new EdmBooleanConstant(false)), - new EdmPropertyConstructor("ExampleValues", new EdmCollectionExpression(primitiveExampleValue))); - - /* - - - - - - - - - - - */ - IEdmComplexType complex = model.FindType("Org.OData.Capabilities.V1.InsertRestrictionsType") as IEdmComplexType; - Assert.NotNull(complex); - - EdmRecordExpression record = new EdmRecordExpression( - new EdmComplexTypeReference(complex, false), - new EdmPropertyConstructor("Insertable", new EdmBooleanConstant(false)), - new EdmPropertyConstructor("NonInsertableProperties", nonInsertableProperties), - new EdmPropertyConstructor("NonInsertableNavigationProperties", nonInsertableNavigationProperties), - new EdmPropertyConstructor("MaxLevels", new EdmIntegerConstant(8)), - new EdmPropertyConstructor("Permission", permission), - new EdmPropertyConstructor("QueryOptions", queryOptions), - new EdmPropertyConstructor("CustomHeaders", new EdmCollectionExpression(customHeaders1, customHeaders2)), - new EdmPropertyConstructor("CustomQueryOptions", new EdmCollectionExpression(customHeaders2))); - - EdmEntityType entity = new EdmEntityType("NS", "Entity"); - entity.AddKeys(entity.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); - model.AddElement(entity); - EdmEntityContainer container = new EdmEntityContainer("NS", "Default"); - EdmEntitySet entities = container.AddEntitySet("Entities", entity); - model.AddElement(container); - - IEdmTerm term = model.FindTerm("Org.OData.Capabilities.V1.InsertRestrictions"); - Assert.NotNull(term); - - var annotation = new EdmVocabularyAnnotation(entities, term, record); - annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline); - model.SetVocabularyAnnotation(annotation); - - return model; - } - private static void VerifyInsertRestrictions(InsertRestrictionsType insert) { Assert.NotNull(insert); @@ -342,6 +171,23 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Assert.True(insert.IsNonInsertableNavigationProperty("RelatedEvents")); Assert.False(insert.IsNonInsertableNavigationProperty("MyUnknownNavigationProperty")); + + Assert.Null(insert.QueryOptions); + Assert.Null(insert.Permission); + Assert.Null(insert.CustomHeaders); + + Assert.NotNull(insert.MaxLevels); + Assert.Equal(8, insert.MaxLevels.Value); + + Assert.NotNull(insert.CustomQueryOptions); + CustomParameter parameter = Assert.Single(insert.CustomQueryOptions); + Assert.Equal("primitive name", parameter.Name); + Assert.Equal("http://any3", parameter.DocumentationURL); + + Assert.NotNull(parameter.ExampleValues); + PrimitiveExampleValue example = Assert.Single(parameter.ExampleValues); + Assert.Equal("example desc", example.Description); + Assert.Equal("example value", example.Value.Value); } } } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ModificationQueryOptionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ModificationQueryOptionsTypeTests.cs new file mode 100644 index 0000000..3c1ce16 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ModificationQueryOptionsTypeTests.cs @@ -0,0 +1,114 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. +// ------------------------------------------------------------ + +using System.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class ModificationQueryOptionsTypeTests + { + [Fact] + public void TermAttributeAttachedOnModificationQueryOptionsType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.ModificationQueryOptions", qualifiedName); + } + + [Fact] + public void InitializInsertRestrictionsTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("ExpandSupported", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("SelectSupported", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("ComputeSupported", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("FilterSupported", new EdmBooleanConstant(true)) + // SearchSupported + // SortSupported + ); + + // Act + ModificationQueryOptionsType query = new ModificationQueryOptionsType(); + query.Initialize(record); + + // Assert + VerifyModificationQueryOptions(query); + } + + [Fact] + public void InitializeModificationQueryOptionsTypeWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + ModificationQueryOptionsType query = model.GetRecord(model.EntityContainer); + + // Assert + VerifyModificationQueryOptions(query); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + + private static void VerifyModificationQueryOptions(ModificationQueryOptionsType query) + { + Assert.NotNull(query); + + Assert.Null(query.SearchSupported); + Assert.Null(query.SortSupported); + + Assert.NotNull(query.ExpandSupported); + Assert.True(query.ExpandSupported); + + Assert.NotNull(query.SelectSupported); + Assert.True(query.SelectSupported); + + Assert.NotNull(query.ComputeSupported); + Assert.False(query.ComputeSupported); // false + + Assert.NotNull(query.FilterSupported); + Assert.True(query.FilterSupported); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/NavigationRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/NavigationRestrictionsTypeTests.cs index 1ec5129..a16ac5f 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/NavigationRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/NavigationRestrictionsTypeTests.cs @@ -6,6 +6,8 @@ using System.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -15,26 +17,44 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class NavigationRestrictionsTypeTests { [Fact] - public void KindPropertyReturnsNavigationRestrictionsEnumMember() + public void TermAttributeAttachedOnNavigationRestrictionsType() { // Arrange & Act - NavigationRestrictionsType navigation = new NavigationRestrictionsType(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert - // Assert.Equal(CapabilitesTermKind.NavigationRestrictions, navigation.Kind); + Assert.Equal("Org.OData.Capabilities.V1.NavigationRestrictions", qualifiedName); } [Fact] - public void UnknownAnnotatableTargetReturnsDefaultPropertyValues() + public void InitializNavigationRestrictionsTypeWithRecordSuccess() { - // Arrange - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); + // Assert + EdmModel model = new EdmModel(); + IEdmEnumType navigationType = model.FindType("Org.OData.Capabilities.V1.NavigationType") as IEdmEnumType; + Assert.NotNull(navigationType); + + IEdmRecordExpression restriction1 = new EdmRecordExpression( + new EdmPropertyConstructor("NavigationProperty", new EdmNavigationPropertyPathExpression("abc")), + new EdmPropertyConstructor("Navigability", new EdmEnumMemberExpression(navigationType.Members.First(c => c.Name == "Single"))), + new EdmPropertyConstructor("SkipSupported", new EdmBooleanConstant(false))); + + IEdmRecordExpression restriction2 = new EdmRecordExpression( + new EdmPropertyConstructor("NavigationProperty", new EdmNavigationPropertyPathExpression("xyz")), + new EdmPropertyConstructor("Navigability", new EdmEnumMemberExpression(navigationType.Members.First(c => c.Name == "None"))), + new EdmPropertyConstructor("OptimisticConcurrencyControl", new EdmBooleanConstant(true))); + + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Navigability", new EdmEnumMemberExpression(navigationType.Members.First(c => c.Name == "Recursive"))), + new EdmPropertyConstructor("RestrictedProperties", new EdmCollectionExpression(restriction1, restriction2)) + ); // Act - NavigationRestrictionsType navigation = EdmCoreModel.Instance.GetRecord(entityType); + NavigationRestrictionsType navigation = new NavigationRestrictionsType(); + navigation.Initialize(record); // Assert - Assert.Null(navigation); + VerifyNavigationRestrictions(navigation); } [Theory] @@ -43,40 +63,13 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public void TargetOnEntityTypeReturnsCorrectNavigationRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) { // Arrange - const string outOfLineTemplate = @" + const string template = @" {0} "; - string navigationAnnotation = @" - - - - Org.OData.Capabilities.V1.NavigationType/Recursive - - - - - - Org.OData.Capabilities.V1.NavigationType/Single - - - - - - - "; - - IEdmModel model; - if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) - { - navigationAnnotation = string.Format(outOfLineTemplate, navigationAnnotation); - model = CapabilitiesModelHelper.GetEdmModelOutline(navigationAnnotation); - } - else - { - model = CapabilitiesModelHelper.GetEdmModelTypeInline(navigationAnnotation); - } + IEdmModel model = GetEdmModel(template, location); + Assert.NotNull(model); // guard IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); @@ -84,14 +77,7 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests NavigationRestrictionsType navigation = model.GetRecord(calendars); // Assert - Assert.NotNull(navigation.Navigability); - Assert.Equal(NavigationType.Recursive, navigation.Navigability.Value); - - Assert.NotNull(navigation.RestrictedProperties); - NavigationPropertyRestriction navRestriction = Assert.Single(navigation.RestrictedProperties); - Assert.NotNull(navRestriction.Navigability); - Assert.Equal(NavigationType.Single, navRestriction.Navigability.Value); - Assert.Equal("abc", navRestriction.NavigationProperty); + VerifyNavigationRestrictions(navigation); } [Theory] @@ -100,46 +86,13 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public void TargetOnEntitySetReturnsCorrectNavigationRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) { // Arrange - const string outOfLineTemplate = @" + const string template = @" {0} "; - string navigationAnnotation = @" - - - - Org.OData.Capabilities.V1.NavigationType/Recursive - - - - - - Org.OData.Capabilities.V1.NavigationType/Single - - - - - - Org.OData.Capabilities.V1.NavigationType/None - - - - - - - "; - - IEdmModel model; - if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) - { - navigationAnnotation = string.Format(outOfLineTemplate, navigationAnnotation); - model = CapabilitiesModelHelper.GetEdmModelOutline(navigationAnnotation); - } - else - { - model = CapabilitiesModelHelper.GetEdmModelTypeInline(navigationAnnotation); - } + IEdmModel model = GetEdmModel(template, location); + Assert.NotNull(model); // guard IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); @@ -156,6 +109,46 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Assert.True(navigation.IsRestrictedProperty("xyz")); } + private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) + { + string navigationAnnotation = @" + + + + Org.OData.Capabilities.V1.NavigationType/Recursive + + + + + + Org.OData.Capabilities.V1.NavigationType/Single + + + + + + + Org.OData.Capabilities.V1.NavigationType/None + + + + + + + + "; + + if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) + { + navigationAnnotation = string.Format(template, navigationAnnotation); + return CapabilitiesModelHelper.GetEdmModelOutline(navigationAnnotation); + } + else + { + return CapabilitiesModelHelper.GetEdmModelTypeInline(navigationAnnotation); + } + } + [Fact] public void TargetWithUnknownEnumMemberDoesnotReturnsNavigationRestrictionsValue() { @@ -193,11 +186,32 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests Assert.NotNull(navigation.RestrictedProperties); Assert.Equal(2, navigation.RestrictedProperties.Count); + Assert.False(navigation.IsRestrictedProperty("abc")); + Assert.True(navigation.IsRestrictedProperty("xyz")); + + // #1 NavigationPropertyRestriction navRestriction = navigation.RestrictedProperties.First(); Assert.NotNull(navRestriction.Navigability); Assert.Equal(NavigationType.Single, navRestriction.Navigability.Value); Assert.Equal("abc", navRestriction.NavigationProperty); - Assert.False(navigation.IsRestrictedProperty("abc")); + + Assert.NotNull(navRestriction.SkipSupported); + Assert.False(navRestriction.SkipSupported.Value); + + Assert.Null(navRestriction.TopSupported); + Assert.Null(navRestriction.OptimisticConcurrencyControl); + + // #2 + navRestriction = navigation.RestrictedProperties.Last(); + Assert.NotNull(navRestriction.Navigability); + Assert.Equal(NavigationType.None, navRestriction.Navigability.Value); + Assert.Equal("xyz", navRestriction.NavigationProperty); + + Assert.Null(navRestriction.SkipSupported); + Assert.Null(navRestriction.TopSupported); + + Assert.NotNull(navRestriction.OptimisticConcurrencyControl); + Assert.True(navRestriction.OptimisticConcurrencyControl.Value); } } } diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/OperationRestrictionTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/OperationRestrictionTypeTests.cs new file mode 100644 index 0000000..80f42f8 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/OperationRestrictionTypeTests.cs @@ -0,0 +1,123 @@ +// ------------------------------------------------------------ +// 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 System.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class OperationRestrictionTypeTests + { + [Fact] + public void TermAttributeAttachedOnOperationRestrictionType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.OperationRestrictions", qualifiedName); + } + + [Fact] + public void InitializOperationRestrictionTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("CustomHeaders", new EdmCollectionExpression( + new EdmRecordExpression( + new EdmPropertyConstructor("Name", new EdmStringConstant("head name")), + new EdmPropertyConstructor("Description", new EdmStringConstant("head desc")), + new EdmPropertyConstructor("DocumentationURL", new EdmStringConstant("http://any3")), + new EdmPropertyConstructor("Required", new EdmBooleanConstant(true))))) + // Permission + // CustomQueryOptions + ); + + // Act + OperationRestrictionType operation = new OperationRestrictionType(); + operation.Initialize(record); + + // Assert + VerifyOperationRestrictions(operation); + } + + [Fact] + public void InitializeModificationQueryOptionsTypeWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + + + + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + IEdmOperation edmOperation = model.SchemaElements.OfType().First(); + Assert.NotNull(edmOperation); + + // Act + OperationRestrictionType operation = model.GetRecord(edmOperation, "NS.MyOperationRestriction"); + + // Assert + VerifyOperationRestrictions(operation); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + + + {0} + + + + +"; + + string modelText = string.Format(template, annotation); + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + + private static void VerifyOperationRestrictions(OperationRestrictionType operation) + { + Assert.NotNull(operation); + + Assert.Null(operation.Permission); + Assert.Null(operation.CustomQueryOptions); + + Assert.NotNull(operation.CustomHeaders); + + CustomParameter parameter = Assert.Single(operation.CustomHeaders); + Assert.Equal("head name", parameter.Name); + Assert.Equal("http://any3", parameter.DocumentationURL); + Assert.Equal("head desc", parameter.Description); + Assert.True(parameter.Required.Value); + Assert.Null(parameter.ExampleValues); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/PermissionTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/PermissionTypeTests.cs new file mode 100644 index 0000000..f4751a1 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/PermissionTypeTests.cs @@ -0,0 +1,123 @@ +// ------------------------------------------------------------ +// 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.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class PermissionTypeTests + { + [Fact] + public void DefaultPropertyAsNull() + { + // Arrange & Act + PermissionType permission = new PermissionType(); + + // Assert + Assert.Null(permission.Scheme); + Assert.Null(permission.Scopes); + } + + [Fact] + public void InitializeWithNullRecordThrows() + { + // Arrange & Act + PermissionType permission = new PermissionType(); + + // Assert + Assert.Throws("record", () => permission.Initialize(record: null)); + } + + [Fact] + public void InitializeWithPermissionTypeRecordSuccess() + { + // Arrange + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Scheme", new EdmRecordExpression( + new EdmPropertyConstructor("Authorization", new EdmStringConstant("scheme name")))), + new EdmPropertyConstructor("Scopes", new EdmCollectionExpression(new EdmRecordExpression( + new EdmPropertyConstructor("Scope", new EdmStringConstant("scope name")))))); + + // Act + PermissionType permission = new PermissionType(); + permission.Initialize(record); + + // Assert + VerifyPermissionType(permission); + } + + [Fact] + public void ScopeTypeTermValueInitializeWorksForScopeType() + { + // Arrange + string annotation = @" + + + + + + + + + + + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + + // Act + PermissionType permission = model.GetRecord(model.EntityContainer, "NS.MyTerm"); + + // Assert + VerifyPermissionType(permission); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + + private static void VerifyPermissionType(PermissionType permission) + { + Assert.NotNull(permission); + + Assert.NotNull(permission.Scheme); + Assert.Equal("scheme name", permission.Scheme.Authorization); + Assert.Null(permission.Scheme.RequiredScopes); + + Assert.NotNull(permission.Scopes); + ScopeType scope = Assert.Single(permission.Scopes); + Assert.Equal("scope name", scope.Scope); + Assert.Null(scope.RestrictedProperties); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ReadRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ReadRestrictionsTypeTests.cs new file mode 100644 index 0000000..32dd06f --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/ReadRestrictionsTypeTests.cs @@ -0,0 +1,173 @@ +// ------------------------------------------------------------ +// 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.Common; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class ReadRestrictionsTypeTests + { + [Fact] + public void TermAttributeAttachedOnReadRestrictionsType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.ReadRestrictions", qualifiedName); + } + + [Fact] + public void InitializReadRestrictionsTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Readable", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("CustomQueryOptions", new EdmCollectionExpression( + new EdmRecordExpression(new EdmPropertyConstructor("Name", new EdmStringConstant("root query name"))))), + // Root Permission + // Root CustomHeaders + new EdmPropertyConstructor("ReadByKeyRestrictions", new EdmRecordExpression( + new EdmPropertyConstructor("Readable", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("CustomHeaders", new EdmCollectionExpression( + new EdmRecordExpression(new EdmPropertyConstructor("Name", new EdmStringConstant("by key head name"))))) + // ByKey Permission + // ByKey CustomQueryOptions + )) + ); + + // Act + ReadRestrictionsType read = new ReadRestrictionsType(); + read.Initialize(record); + + // Assert + VerifyReadRestrictions(read); + } + + [Theory] + [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] + [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] + public void TargetOnEntityTypeReturnsCorrectReadRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) + { + // Arrange + const string template = @" + + {0} + "; + + IEdmModel model = GetEdmModel(template, location); + Assert.NotNull(model); // guard + + IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); + Assert.NotNull(calendars); // guard + + // Act + ReadRestrictionsType read = model.GetRecord(calendars); + + // Assert + VerifyReadRestrictions(read); + } + + [Theory] + [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] + [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] + public void TargetOnEntitySetReturnsCorrectReadRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location) + { + // Arrange + const string template = @" + + {0} + "; + + IEdmModel model = GetEdmModel(template, location); + Assert.NotNull(model); // guard + + IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); + Assert.NotNull(calendars); // guard + + // Act + ReadRestrictionsType read = model.GetRecord(calendars); + + // Assert + VerifyReadRestrictions(read); + } + + private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) + { + string countAnnotation = @" + + + + + + + + + + + + + + + + + + + + + + + + "; + + if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) + { + countAnnotation = string.Format(template, countAnnotation); + return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation); + } + else + { + return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation); + } + } + + private static void VerifyReadRestrictions(ReadRestrictionsType read) + { + Assert.NotNull(read); + + Assert.NotNull(read.Readable); + Assert.False(read.Readable.Value); + + Assert.Null(read.Permission); + Assert.Null(read.CustomHeaders); + + Assert.NotNull(read.CustomQueryOptions); + CustomParameter parameter = Assert.Single(read.CustomQueryOptions); + Assert.Equal("root query name", parameter.Name); + Assert.Null(parameter.DocumentationURL); + Assert.Null(parameter.ExampleValues); + + // ReadByKeyRestrictions + Assert.NotNull(read.ReadByKeyRestrictions); + Assert.NotNull(read.ReadByKeyRestrictions.Readable); + Assert.True(read.ReadByKeyRestrictions.Readable.Value); + + Assert.Null(read.ReadByKeyRestrictions.Permission); + Assert.Null(read.ReadByKeyRestrictions.CustomQueryOptions); + + Assert.NotNull(read.ReadByKeyRestrictions.CustomHeaders); + parameter = Assert.Single(read.ReadByKeyRestrictions.CustomHeaders); + Assert.Equal("by key head name", parameter.Name); + Assert.Null(parameter.DocumentationURL); + Assert.Null(parameter.ExampleValues); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SearchRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SearchRestrictionsTypeTests.cs index 6bcde20..30f5eab 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SearchRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SearchRestrictionsTypeTests.cs @@ -5,6 +5,8 @@ using System.Linq; using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -14,26 +16,38 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class SearchRestrictionsTypeTests { [Fact] - public void KindPropertyReturnsSearchRestrictionsEnumMember() + public void TermAttributeAttachedOnSearchRestrictionsType() { // Arrange & Act - SearchRestrictionsType search = new SearchRestrictionsType(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert - // Assert.Equal(CapabilitesTermKind.SearchRestrictions, search.Kind); + Assert.Equal("Org.OData.Capabilities.V1.SearchRestrictions", qualifiedName); } [Fact] - public void UnknownAnnotatableTargetReturnsDefaultPropertyValues() + public void InitializSearchRestrictionsTypeWithRecordSuccess() { - // Arrange - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); + // Assert + EdmModel model = new EdmModel(); + IEdmEnumType searchExpressions = model.FindType("Org.OData.Capabilities.V1.SearchExpressions") as IEdmEnumType; + Assert.NotNull(searchExpressions); - // Act - SearchRestrictionsType search = EdmCoreModel.Instance.GetRecord(entityType); + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Searchable", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("UnsupportedExpressions", new EdmEnumMemberExpression( + searchExpressions.Members.First(c => c.Name == "AND"), + searchExpressions.Members.First(c => c.Name == "OR"))) + ); + + // Act + SearchRestrictionsType search = new SearchRestrictionsType(); + search.Initialize(record); // Assert - Assert.Null(search); + Assert.False(search.Searchable); + Assert.NotNull(search.UnsupportedExpressions); + Assert.Equal(SearchExpressions.AND | SearchExpressions.OR, search.UnsupportedExpressions.Value); } [Fact] diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SelectSupportTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SelectSupportTypeTests.cs new file mode 100644 index 0000000..c007edc --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SelectSupportTypeTests.cs @@ -0,0 +1,113 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. +// ------------------------------------------------------------ + +using System.Xml.Linq; +using Microsoft.OData.Edm; +using Microsoft.OData.Edm.Csdl; +using Microsoft.OData.Edm.Vocabularies; +using Microsoft.OpenApi.OData.Common; +using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.Vocabulary.Capabilities; +using Xunit; + +namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests +{ + public class SelectSupportTypeTests + { + [Fact] + public void TermAttributeAttachedOnSelectSupportType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.SelectSupport", qualifiedName); + } + + [Fact] + public void InitializSelectSupportTypeWithRecordSuccess() + { + // Assert + IEdmRecordExpression record = new EdmRecordExpression( + new EdmPropertyConstructor("Supported", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("Filterable", new EdmBooleanConstant(true)), + new EdmPropertyConstructor("Expandable", new EdmBooleanConstant(false)), + new EdmPropertyConstructor("Searchable", new EdmBooleanConstant(true)) + // TopSupported + // SkipSupported + // ComputeSupported + // Countable + // Sortable + ); + + // Act + SelectSupportType select = new SelectSupportType(); + select.Initialize(record); + + // Assert + VerifySelectSupportType(select); + } + + [Fact] + public void InitializeSelectSupportTypeTypeWorksWithCsdl() + { + // Arrange + string annotation = @" + + + + + + + "; + + IEdmModel model = GetEdmModel(annotation); + Assert.NotNull(model); // guard + Assert.NotNull(model.EntityContainer); + + // Act + SelectSupportType select = model.GetRecord(model.EntityContainer); + + // Assert + VerifySelectSupportType(select); + } + + private IEdmModel GetEdmModel(string annotation) + { + const string template = @" + + + + {0} + + + +"; + string modelText = string.Format(template, annotation); + + IEdmModel model; + + bool result = CsdlReader.TryParse(XElement.Parse(modelText).CreateReader(), out model, out _); + Assert.True(result); + return model; + } + + private static void VerifySelectSupportType(SelectSupportType select) + { + Assert.NotNull(select); + + Assert.Null(select.TopSupported); + Assert.Null(select.SkipSupported); + Assert.Null(select.ComputeSupported); + Assert.Null(select.Countable); + Assert.Null(select.Sortable); + + Assert.True(select.Supported); + Assert.False(select.Expandable); + Assert.True(select.Filterable); + Assert.True(select.Searchable); + } + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SkipSupportedTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SkipSupportedTests.cs deleted file mode 100644 index 62179d2..0000000 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SkipSupportedTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -// ------------------------------------------------------------ -// 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 Xunit; - -namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests -{ -#if false - public class SkipSupportedTests - { - [Fact] - public void KindPropertyReturnsSkipSupportedEnumMember() - { - // Arrange & Act - SkipSupported skip = new SkipSupported(); - - // Assert - Assert.Equal(CapabilitesTermKind.SkipSupported, skip.Kind); - } - - [Fact] - public void UnknownAnnotatableTargetReturnsDefaultSkipSupportedValues() - { - // Arrange - SkipSupported skip = new SkipSupported(); - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); - - // Act - bool result = skip.Load(EdmCoreModel.Instance, entityType); - - // Assert - Assert.False(result); - Assert.True(skip.IsSupported); - Assert.Null(skip.Supported); - } - - [Theory] - [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] - [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] - public void TargetOnEntityTypeReturnsCorrectSkipSupportedValue(EdmVocabularyAnnotationSerializationLocation location) - { - // Arrange - const string template = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntityType calendar = model.SchemaElements.OfType().First(c => c.Name == "Calendar"); - Assert.NotNull(calendar); // guard - - // Act - SkipSupported skip = new SkipSupported(); - bool result = skip.Load(model, calendar); - - // Assert - Assert.True(result); - Assert.False(skip.IsSupported); - 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 = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); - Assert.NotNull(calendars); // guard - - // Act - SkipSupported skip = new SkipSupported(); - bool result = skip.Load(model, calendars); - - // Assert - Assert.True(result); - Assert.NotNull(skip.Supported); - Assert.False(skip.Supported.Value); - } - - private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) - { - string countAnnotation = @""; - - if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) - { - countAnnotation = string.Format(template, countAnnotation); - return CapabilitiesModelHelper.GetEdmModelOutline(countAnnotation); - } - else - { - return CapabilitiesModelHelper.GetEdmModelTypeInline(countAnnotation); - } - } - } -#endif -} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SortRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SortRestrictionsTypeTests.cs index 6aac3b7..e52711f 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SortRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/SortRestrictionsTypeTests.cs @@ -6,6 +6,7 @@ using System.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -14,6 +15,16 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests { public class SortRestrictionsTypeTests { + [Fact] + public void TermAttributeAttachedOnSortRestrictionsType() + { + // Arrange & Act + string qualifiedName = Utils.GetTermQualifiedName(); + + // Assert + Assert.Equal("Org.OData.Capabilities.V1.SortRestrictions", qualifiedName); + } + [Fact] public void UnknownAnnotatableTargetReturnsDefaultSortRestrictionsValues() { diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/TopSupportedTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/TopSupportedTests.cs deleted file mode 100644 index fecfcfc..0000000 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/TopSupportedTests.cs +++ /dev/null @@ -1,114 +0,0 @@ -// ------------------------------------------------------------ -// 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 Xunit; - -namespace Microsoft.OpenApi.OData.Reader.Capabilities.Tests -{ -#if false - public class TopSupportedTests - { - [Fact] - public void KindPropertyReturnsTopSupportedEnumMember() - { - // Arrange & Act - TopSupported top = new TopSupported(); - - // Assert - Assert.Equal(CapabilitesTermKind.TopSupported, top.Kind); - } - - [Fact] - public void UnknownAnnotatableTargetReturnsDefaultTopSupportedValues() - { - // Arrange - TopSupported top = new TopSupported(); - EdmEntityType entityType = new EdmEntityType("NS", "Entity"); - - // Act - bool result = top.Load(EdmCoreModel.Instance, entityType); - - // Assert - Assert.False(result); - Assert.True(top.IsSupported); - Assert.Null(top.Supported); - } - - [Theory] - [InlineData(EdmVocabularyAnnotationSerializationLocation.Inline)] - [InlineData(EdmVocabularyAnnotationSerializationLocation.OutOfLine)] - public void TargetOnEntityTypeReturnsCorrectTopSupportedValue(EdmVocabularyAnnotationSerializationLocation location) - { - // Arrange - const string template = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntityType calendar = model.SchemaElements.OfType().First(c => c.Name == "Calendar"); - Assert.NotNull(calendar); // guard - - // Act - TopSupported top = new TopSupported(); - bool result = top.Load(model, calendar); - - // Assert - Assert.True(result); - Assert.False(top.IsSupported); - 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 = @" - - {0} - "; - - IEdmModel model = GetEdmModel(template, location); - Assert.NotNull(model); // guard - - IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars"); - Assert.NotNull(calendars); // guard - - // Act - TopSupported top = new TopSupported(); - bool result = top.Load(model, calendars); - - // Assert - Assert.True(result); - Assert.False(top.IsSupported); - Assert.NotNull(top.Supported); - Assert.False(top.Supported.Value); - } - - private static IEdmModel GetEdmModel(string template, EdmVocabularyAnnotationSerializationLocation location) - { - string topAnnotation = @""; - - if (location == EdmVocabularyAnnotationSerializationLocation.OutOfLine) - { - topAnnotation = string.Format(template, topAnnotation); - return CapabilitiesModelHelper.GetEdmModelOutline(topAnnotation); - } - else - { - return CapabilitiesModelHelper.GetEdmModelTypeInline(topAnnotation); - } - } - } -#endif -} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/UpdateRestrictionsTypeTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/UpdateRestrictionsTypeTests.cs index 36082e3..d9222c8 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/UpdateRestrictionsTypeTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Vocabulary/Capabilities/UpdateRestrictionsTypeTests.cs @@ -7,6 +7,7 @@ using System; using System.Linq; using Microsoft.OData.Edm; using Microsoft.OData.Edm.Csdl; +using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; using Xunit; @@ -16,13 +17,13 @@ namespace Microsoft.OpenApi.OData.Reader.Vocabulary.Capabilities.Tests public class UpdateRestrictionsTypeTests { [Fact] - public void KindPropertyReturnsUpdateRestrictionsEnumMember() + public void TermAttributeAttachedOnUpdateRestrictionsType() { // Arrange & Act - UpdateRestrictionsType update = new UpdateRestrictionsType(); + string qualifiedName = Utils.GetTermQualifiedName(); // Assert - // Assert.Equal(CapabilitesTermKind.UpdateRestrictions, update.Kind); + Assert.Equal("Org.OData.Capabilities.V1.UpdateRestrictions", qualifiedName); } [Fact]