Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,23 @@ private static IReadOnlyList<RequestPathSegment> ParseSegments(string path)
/// <returns></returns>
public bool IsAncestorOf(RequestPathPattern other)
{
// To be the parent of other, you must at least be shorter than other.
// Ancestor detection: compare only constant segments, skip variable segments.
if (other.Count <= Count)
return false;
for (int i = 0; i < Count; i++)
{
// we need the segment to be identical when strict is true (which is the default value)
// when strict is false, we also need the segment to be identical if it is constant.
// but if it is a reference, we only require they have the same type, do not require they have the same variable name.
// This case happens a lot during the management group parent detection - different RP calls this different things
if (!this[i].Equals(other[i]))
if (this[i].IsConstant)
{
if (!this[i].Equals(other[i]))
return false;
}
else // variable segment
{
if (!other[i].IsConstant)
continue;
// If this[i] is variable, other[i] must also be variable
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Generator.Management.Models;
using NUnit.Framework;

namespace Azure.Generator.Management.Tests
{
public class RequestPathPatternTests
{
[TestCase("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}", true)]
[TestCase("/subscriptions/{subscriptionId}", "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", true)]
[TestCase("/subscriptions/{subscriptionId}", "/providers/Microsoft.Management/managementGroups/{managementGroupId}", false)]
public void IsAncestorOf_BasicCases(string ancestor, string descendant, bool expected)
{
var ancestorPattern = new RequestPathPattern(ancestor);
var descendantPattern = new RequestPathPattern(descendant);
Assert.AreEqual(expected, ancestorPattern.IsAncestorOf(descendantPattern));
}

[Test]
public void IsAncestorOf_AncestorMustBeShorterThanDescendant()
{
var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
var childPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
// Ancestor and child are the same length, should return false
Assert.IsFalse(ancestorPattern.IsAncestorOf(childPattern));

var longerAncestor = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage");
var shorterChild = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
// Ancestor is longer than child, should return false
Assert.IsFalse(longerAncestor.IsAncestorOf(shorterChild));
}

[Test]
public void IsAncestorOf_VariableSegmentCheck()
{
var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
var descendantPattern = new RequestPathPattern("/subscriptions/{otherSub}/resourceGroups/{otherGroup}/providers/Microsoft.Storage/storageAccounts/{accountName}");
Assert.IsTrue(ancestorPattern.IsAncestorOf(descendantPattern));
}

[Test]
public void IsAncestorOf_ConstantSegmentMismatch()
{
var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
var descendantPattern = new RequestPathPattern("/tenants/{tenantId}/resourceGroups/{resourceGroupName}");
Assert.IsFalse(ancestorPattern.IsAncestorOf(descendantPattern));
}
}
}
Loading