diff --git a/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs b/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs index 296068914..30a47bdd7 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs @@ -47,11 +47,16 @@ public class OpenApiUrlTreeNode public string Segment { get; private set; } /// - /// Flag indicating whether the node's PathItems has operations. + /// Flag indicating whether the node's PathItems dictionary has operations + /// under a given label. /// + /// The name of the key for the target operations + /// in the node's PathItems dictionary. /// true or false. public bool HasOperations(string label) { + Utils.CheckArgumentNullOrEmpty(label, nameof(label)); + if (!(PathItems?.ContainsKey(label) ?? false)) { return false; @@ -139,6 +144,8 @@ public OpenApiUrlTreeNode Attach(string path, string label) { Utils.CheckArgumentNullOrEmpty(label, nameof(label)); + Utils.CheckArgumentNullOrEmpty(path, nameof(path)); + Utils.CheckArgumentNull(pathItem, nameof(pathItem)); if (path.StartsWith(RootPathSegment)) { diff --git a/test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs b/test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs index a246c66ff..944e6c830 100644 --- a/test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs +++ b/test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs @@ -11,6 +11,26 @@ namespace Microsoft.OpenApi.Tests.Services { public class OpenApiUrlTreeNodeTests { + private OpenApiDocument OpenApiDocumentSample_1 => new OpenApiDocument() + { + Paths = new OpenApiPaths() + { + ["/"] = new OpenApiPathItem(), + ["/houses"] = new OpenApiPathItem(), + ["/cars"] = new OpenApiPathItem() + } + }; + + private OpenApiDocument OpenApiDocumentSample_2 => new OpenApiDocument() + { + Paths = new OpenApiPaths() + { + ["/"] = new OpenApiPathItem(), + ["/hotels"] = new OpenApiPathItem(), + ["/offices"] = new OpenApiPathItem() + } + }; + [Fact] public void CreateUrlSpaceWithoutOpenApiDocument() { @@ -64,15 +84,7 @@ public void CreatePathWithoutRootWorks() [Fact] public void CreateMultiplePathsWorks() { - var doc = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; + var doc = OpenApiDocumentSample_1; string label = "assets"; var rootNode = OpenApiUrlTreeNode.Create(doc, label); @@ -89,25 +101,9 @@ public void CreateMultiplePathsWorks() [Fact] public void AttachDocumentWorks() { - var doc1 = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; + var doc1 = OpenApiDocumentSample_1; - var doc2 = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/hotels"] = new OpenApiPathItem(), - ["/offices"] = new OpenApiPathItem() - } - }; + var doc2 = OpenApiDocumentSample_2; var label1 = "personal"; var label2 = "business"; @@ -123,15 +119,7 @@ public void AttachDocumentWorks() [Fact] public void AttachPathWorks() { - var doc = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; + var doc = OpenApiDocumentSample_1; var label1 = "personal"; var rootNode = OpenApiUrlTreeNode.Create(doc, label1); @@ -335,96 +323,10 @@ public void SegmentIsParameterWorks() Assert.Equal("{apartment-id}", rootNode.Children["houses"].Children["apartments"].Children["{apartment-id}"].Segment); } - [Fact] - public void ThrowsArgumentExceptionForDuplicateLabels() - { - var doc1 = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; - - var doc2 = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/hotels"] = new OpenApiPathItem(), - ["/offices"] = new OpenApiPathItem() - } - }; - - var label1 = "personal"; - var rootNode = OpenApiUrlTreeNode.Create(doc1, label1); - - Assert.Throws(() => rootNode.Attach(doc2, label1)); - } - - [Fact] - public void ThrowsArgumentNullExceptionForNullArgumentsInCreateMethod() - { - var doc = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; - - Assert.Throws(() => OpenApiUrlTreeNode.Create(doc, "")); - Assert.Throws(() => OpenApiUrlTreeNode.Create(doc, null)); - Assert.Throws(() => OpenApiUrlTreeNode.Create(null, "beta")); - } - - [Fact] - public void ThrowsArgumentNullExceptionForNullArgumentsInAttachMethod() - { - var doc1 = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; - - var doc2 = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/hotels"] = new OpenApiPathItem(), - ["/offices"] = new OpenApiPathItem() - } - }; - - var label1 = "personal"; - var rootNode = OpenApiUrlTreeNode.Create(doc1, label1); - - Assert.Throws(() => rootNode.Attach(doc2, "")); - Assert.Throws(() => rootNode.Attach(doc2, null)); - Assert.Throws(() => rootNode.Attach(null, "beta")); - } - [Fact] public void AdditionalDataWorks() { - var doc = new OpenApiDocument() - { - Paths = new OpenApiPaths() - { - ["/"] = new OpenApiPathItem(), - ["/houses"] = new OpenApiPathItem(), - ["/cars"] = new OpenApiPathItem() - } - }; + var doc = OpenApiDocumentSample_1; var label = "personal"; var rootNode = OpenApiUrlTreeNode.Create(doc, label); @@ -476,5 +378,70 @@ public void AdditionalDataWorks() Assert.Equal("Convertible", item); }); } + + [Fact] + public void ThrowsArgumentExceptionForDuplicateLabels() + { + var doc1 = OpenApiDocumentSample_1; + + var doc2 = OpenApiDocumentSample_2; + + var label1 = "personal"; + var rootNode = OpenApiUrlTreeNode.Create(doc1, label1); + + Assert.Throws(() => rootNode.Attach(doc2, label1)); + } + + [Fact] + public void ThrowsArgumentNullExceptionForNullOrEmptyArgumentsInCreateMethod() + { + var doc = OpenApiDocumentSample_1; + + Assert.Throws(() => OpenApiUrlTreeNode.Create(doc, "")); + Assert.Throws(() => OpenApiUrlTreeNode.Create(doc, null)); + Assert.Throws(() => OpenApiUrlTreeNode.Create(null, "beta")); + Assert.Throws(() => OpenApiUrlTreeNode.Create(null, null)); + Assert.Throws(() => OpenApiUrlTreeNode.Create(null, "")); + } + + [Fact] + public void ThrowsArgumentNullExceptionForNullOrEmptyArgumentsInAttachMethod() + { + var doc1 = OpenApiDocumentSample_1; + + var doc2 = OpenApiDocumentSample_2; + + var label1 = "personal"; + var rootNode = OpenApiUrlTreeNode.Create(doc1, label1); + + Assert.Throws(() => rootNode.Attach(doc2, "")); + Assert.Throws(() => rootNode.Attach(doc2, null)); + Assert.Throws(() => rootNode.Attach(null, "beta")); + Assert.Throws(() => rootNode.Attach(null, null)); + Assert.Throws(() => rootNode.Attach(null, "")); + } + + [Fact] + public void ThrowsArgumentNullExceptionForNullOrEmptyArgumentInHasOperationsMethod() + { + var doc = OpenApiDocumentSample_1; + + var label = "personal"; + var rootNode = OpenApiUrlTreeNode.Create(doc, label); + + Assert.Throws(() => rootNode.HasOperations(null)); + Assert.Throws(() => rootNode.HasOperations("")); + } + + [Fact] + public void ThrowsArgumentNullExceptionForNullArgumentInAddAdditionalDataMethod() + { + var doc = OpenApiDocumentSample_1; + + var label = "personal"; + var rootNode = OpenApiUrlTreeNode.Create(doc, label); + + Assert.Throws(() => rootNode.AddAdditionalData(null)); + } } }