|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Azure.Generator.Management.Models; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace Azure.Generator.Management.Tests |
| 8 | +{ |
| 9 | + public class RequestPathPatternTests |
| 10 | + { |
| 11 | + [TestCase("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}", true)] |
| 12 | + [TestCase("/subscriptions/{subscriptionId}", "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", true)] |
| 13 | + [TestCase("/subscriptions/{subscriptionId}", "/providers/Microsoft.Management/managementGroups/{managementGroupId}", false)] |
| 14 | + public void IsAncestorOf_BasicCases(string ancestor, string descendant, bool expected) |
| 15 | + { |
| 16 | + var ancestorPattern = new RequestPathPattern(ancestor); |
| 17 | + var descendantPattern = new RequestPathPattern(descendant); |
| 18 | + Assert.AreEqual(expected, ancestorPattern.IsAncestorOf(descendantPattern)); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void IsAncestorOf_AncestorMustBeShorterThanDescendant() |
| 23 | + { |
| 24 | + var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"); |
| 25 | + var childPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"); |
| 26 | + // Ancestor and child are the same length, should return false |
| 27 | + Assert.IsFalse(ancestorPattern.IsAncestorOf(childPattern)); |
| 28 | + |
| 29 | + var longerAncestor = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage"); |
| 30 | + var shorterChild = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"); |
| 31 | + // Ancestor is longer than child, should return false |
| 32 | + Assert.IsFalse(longerAncestor.IsAncestorOf(shorterChild)); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public void IsAncestorOf_VariableSegmentCheck() |
| 37 | + { |
| 38 | + var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"); |
| 39 | + var descendantPattern = new RequestPathPattern("/subscriptions/{otherSub}/resourceGroups/{otherGroup}/providers/Microsoft.Storage/storageAccounts/{accountName}"); |
| 40 | + Assert.IsTrue(ancestorPattern.IsAncestorOf(descendantPattern)); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void IsAncestorOf_ConstantSegmentMismatch() |
| 45 | + { |
| 46 | + var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"); |
| 47 | + var descendantPattern = new RequestPathPattern("/tenants/{tenantId}/resourceGroups/{resourceGroupName}"); |
| 48 | + Assert.IsFalse(ancestorPattern.IsAncestorOf(descendantPattern)); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments