Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ public class OpenApiUrlTreeNode
public string Segment { get; private set; }

/// <summary>
/// Flag indicating whether the node's PathItems has operations.
/// Flag indicating whether the node's PathItems dictionary has operations
/// under a given label.
/// </summary>
/// <param name="label">The name of the key for the target operations
/// in the node's PathItems dictionary.</param>
/// <returns>true or false.</returns>
public bool HasOperations(string label)
{
Utils.CheckArgumentNullOrEmpty(label, nameof(label));

if (!(PathItems?.ContainsKey(label) ?? false))
{
return false;
Expand Down Expand Up @@ -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))
{
Expand Down
213 changes: 90 additions & 123 deletions test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand All @@ -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";
Expand All @@ -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);
Expand Down Expand Up @@ -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<ArgumentException>(() => 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<ArgumentNullException>(() => OpenApiUrlTreeNode.Create(doc, ""));
Assert.Throws<ArgumentNullException>(() => OpenApiUrlTreeNode.Create(doc, null));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentNullException>(() => rootNode.Attach(doc2, ""));
Assert.Throws<ArgumentNullException>(() => rootNode.Attach(doc2, null));
Assert.Throws<ArgumentNullException>(() => 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);
Expand Down Expand Up @@ -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<ArgumentException>(() => rootNode.Attach(doc2, label1));
}

[Fact]
public void ThrowsArgumentNullExceptionForNullOrEmptyArgumentsInCreateMethod()
{
var doc = OpenApiDocumentSample_1;

Assert.Throws<ArgumentNullException>(() => OpenApiUrlTreeNode.Create(doc, ""));
Assert.Throws<ArgumentNullException>(() => OpenApiUrlTreeNode.Create(doc, null));
Assert.Throws<ArgumentNullException>(() => OpenApiUrlTreeNode.Create(null, "beta"));
Assert.Throws<ArgumentNullException>(() => OpenApiUrlTreeNode.Create(null, null));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentNullException>(() => rootNode.Attach(doc2, ""));
Assert.Throws<ArgumentNullException>(() => rootNode.Attach(doc2, null));
Assert.Throws<ArgumentNullException>(() => rootNode.Attach(null, "beta"));
Assert.Throws<ArgumentNullException>(() => rootNode.Attach(null, null));
Assert.Throws<ArgumentNullException>(() => rootNode.Attach(null, ""));
}

[Fact]
public void ThrowsArgumentNullExceptionForNullOrEmptyArgumentInHasOperationsMethod()
{
var doc = OpenApiDocumentSample_1;

var label = "personal";
var rootNode = OpenApiUrlTreeNode.Create(doc, label);

Assert.Throws<ArgumentNullException>(() => rootNode.HasOperations(null));
Assert.Throws<ArgumentNullException>(() => rootNode.HasOperations(""));
}

[Fact]
public void ThrowsArgumentNullExceptionForNullArgumentInAddAdditionalDataMethod()
{
var doc = OpenApiDocumentSample_1;

var label = "personal";
var rootNode = OpenApiUrlTreeNode.Create(doc, label);

Assert.Throws<ArgumentNullException>(() => rootNode.AddAdditionalData(null));
}
}
}