diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs index 764903ec8171..962adbabbb4a 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs @@ -10,7 +10,7 @@ namespace Azure.ResourceManager.Core /// /// A class representing a sub-resource that contains only the ID. /// - public partial class SubResource : IUtf8JsonSerializable + public partial class SubResource : IUtf8JsonSerializable { /// /// Serialize the input SubResource object. @@ -37,9 +37,9 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) /// /// The JSON element to be deserialized. /// Deserialized SubResource object. - internal static SubResource DeserializeSubResource(JsonElement element) + internal static SubResource DeserializeSubResource(JsonElement element) { - Optional id = default; + string id = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("id")) @@ -48,7 +48,7 @@ internal static SubResource DeserializeSubResource(JsonElement element) continue; } } - return new SubResource(id.Value); + return new SubResource(id); } } } diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs index 49a2b46b604b..1a68edf72652 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs @@ -1,34 +1,89 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; + namespace Azure.ResourceManager.Core { /// - /// A class representing a sub-resource that contains only the ID. + /// A class representing the a sub resource of ResourceIdentifier. /// [ReferenceType] - public partial class SubResource + public class SubResource : SubResource { /// /// Initializes an empty instance of for mocking. /// [InitializationConstructor] - public SubResource() + public SubResource() { } - /// Initializes a new instance of SubResource. + /// Initializes a new instance of . + /// ARM resource Id. + [SerializationConstructor] + protected internal SubResource(string id) : base(id) { } + } + + /// + /// A class representing a sub-resource that contains only the ID. + /// + [ReferenceType] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Types differ by type argument only")] + public partial class SubResource : IEquatable>, IEquatable, + IComparable>, IComparable + where TIdentifier : ResourceIdentifier + { + /// + /// Initializes an empty instance of for mocking. + /// + [InitializationConstructor] + public SubResource() + { + } + + /// Initializes a new instance of . /// ARM resource Id. [SerializationConstructor] protected internal SubResource(string id) { - Id = id; + Id = (TIdentifier)id; } /// /// Gets the ARM resource identifier. /// /// - public virtual ResourceIdentifier Id { get; } + public virtual TIdentifier Id { get; } + + /// + public int CompareTo(string other) + { + return string.Compare(Id, other, StringComparison.InvariantCultureIgnoreCase); + } + + /// + public int CompareTo(SubResource other) + { + if (other is null) + return 1; + + if (ReferenceEquals(this, other)) + return 0; + + return Id.CompareTo(other.Id); + } + + /// + public bool Equals(SubResource other) + { + return Id.Equals(other?.Id); + } + + /// + public bool Equals(string other) + { + return Id.Equals(other); + } } } diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.Serialization.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.Serialization.cs index 7e742920b67d..5978b65b4b43 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.Serialization.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.Serialization.cs @@ -10,10 +10,10 @@ namespace Azure.ResourceManager.Core /// /// A class representing a sub-resource that contains only the ID. /// - public partial class WritableSubResource : IUtf8JsonSerializable + public partial class WritableSubResource : IUtf8JsonSerializable { /// - /// Serialize the input SubResourceReadOnly object. + /// Serialize the input WritableSubResource object. /// /// Input Json writer. void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) @@ -28,13 +28,13 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) } /// - /// Deserialize the input JSON element to a SubResourceReadOnly object. + /// Deserialize the input JSON element to a WritableSubResource object. /// /// The JSON element to be deserialized. - /// Deserialized SubResourceReadOnly object. - internal static WritableSubResource DeserializeSubResourceReadOnly(JsonElement element) + /// Deserialized WritableSubResource object. + internal static WritableSubResource DeserializeWritableSubResource(JsonElement element) { - Optional id = default; + string id = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("id")) @@ -43,7 +43,7 @@ internal static WritableSubResource DeserializeSubResourceReadOnly(JsonElement e continue; } } - return new WritableSubResource(id.Value); + return new WritableSubResource(id); } } } diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.cs index b18a480d3bd2..ff38cd31f7cb 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/WritableSubResource.cs @@ -1,32 +1,88 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; + namespace Azure.ResourceManager.Core { /// - /// A class representing a sub-resource that contains only the read-only ID. + /// A class representing the a writable sub resource of ResourceIdentifier. /// [ReferenceType] - public partial class WritableSubResource + public class WritableSubResource : WritableSubResource { - /// Initializes a new instance of SubResourceReadOnly. + /// + /// Initializes an empty instance of for mocking. + /// [InitializationConstructor] - public WritableSubResource() + public WritableSubResource() { } - /// Initializes a new instance of SubResourceReadOnly. + /// Initializes a new instance of . + /// ARM resource Id. + [SerializationConstructor] + protected internal WritableSubResource(string id) : base(id) { } + } + + /// + /// A class representing a sub-resource that contains only the read-only ID. + /// + [ReferenceType] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Types differ by type argument only")] + public partial class WritableSubResource : IEquatable>, IEquatable, + IComparable>, IComparable + where TIdentifier : ResourceIdentifier + { + /// + /// Initializes an empty instance of for mocking. + /// + [InitializationConstructor] + public WritableSubResource() + { + } + + /// Initializes a new instance of . /// ARM resource Id. [SerializationConstructor] protected internal WritableSubResource(string id) { - Id = id; + Id = (TIdentifier)id; } /// /// Gets or sets the ARM resource identifier. /// /// - public virtual ResourceIdentifier Id { get; set; } + public virtual TIdentifier Id { get; set; } + + /// + public int CompareTo(string other) + { + return string.Compare(Id, other, StringComparison.InvariantCultureIgnoreCase); + } + + /// + public int CompareTo(WritableSubResource other) + { + if (other is null) + return 1; + + if (ReferenceEquals(this, other)) + return 0; + return Id.CompareTo(other.Id); + } + + /// + public bool Equals(WritableSubResource other) + { + return Id.Equals(other?.Id); + } + + /// + public bool Equals(string other) + { + return Id.Equals(other); + } } } diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/SubResourceTests.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/SubResourceTests.cs new file mode 100644 index 000000000000..14dc2a8f9902 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/SubResourceTests.cs @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using NUnit.Framework; + +namespace Azure.ResourceManager.Core.Tests +{ + [Parallelizable] + public class SubResourceTests + { + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.classicStorage/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account2")] + [TestCase(1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/${?>._`", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + public void CompareToObject(int expected, string id1, string id2) + { + SubResource resource1 = new SubResource(id1); + SubResource resource2 = new SubResource(id2); + Assert.AreEqual(expected, resource1.CompareTo(resource2)); + } + + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.classicStorage/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account2")] + [TestCase(1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/${?>._`", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + public void CompareToString(int expected, string id1, string id2) + { + SubResource resource1 = new SubResource(id1); + Assert.AreEqual(expected, resource1.CompareTo(id2)); + } + + [Test] + public void CompareToNull() + { + var resource1 = new SubResource("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1"); + SubResource resource2 = null; + Assert.AreEqual(1, resource1.CompareTo(resource2)); + Assert.AreEqual(1, resource1.CompareTo((string)null)); + } + + [Test] + public void CompareToSame() + { + var resource1 = new SubResource("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1"); + var resource2 = resource1; + Assert.AreEqual(0, resource1.CompareTo(resource2)); + } + } +} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/WritableSubResourceTests.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/WritableSubResourceTests.cs new file mode 100644 index 000000000000..a5141148820f --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/WritableSubResourceTests.cs @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using NUnit.Framework; + +namespace Azure.ResourceManager.Core.Tests +{ + [Parallelizable] + public class WritableSubResourceTests + { + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.classicStorage/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account2")] + [TestCase(1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/${?>._`", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + public void CompareToObject(int expected, string id1, string id2) + { + WritableSubResource resource1 = new WritableSubResource(id1); + WritableSubResource resource2 = new WritableSubResource(id2); + Assert.AreEqual(expected, resource1.CompareTo(resource2)); + } + + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.classicStorage/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account2")] + [TestCase(1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.DiffSpace/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + [TestCase(0, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.${?>._`/storageAccounts/account1")] + [TestCase(-1, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/${?>._`", + "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account2")] + public void CompareToString(int expected, string id1, string id2) + { + WritableSubResource resource1 = new WritableSubResource(id1); + Assert.AreEqual(expected, resource1.CompareTo(id2)); + } + + [Test] + public void CompareToNull() + { + var resource1 = new WritableSubResource("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1"); + WritableSubResource resource2 = null; + Assert.AreEqual(1, resource1.CompareTo(resource2)); + Assert.AreEqual(1, resource1.CompareTo((string)null)); + } + + [Test] + public void CompareToSame() + { + var resource1 = new WritableSubResource("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1"); + var resource2 = resource1; + Assert.AreEqual(0, resource1.CompareTo(resource2)); + } + } +}