Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templetize SubResource and WritableSubResource classes, and added unit tests #21397

Merged
merged 16 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
10f6ad0
Change the accessbility to virtual for Resource.Id
YalinLi0312 Mar 24, 2021
ccc17c7
merge from usptream
Apr 5, 2021
5060f5c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Apr 12, 2021
9a9a651
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Apr 21, 2021
7764cb5
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Apr 23, 2021
832483a
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Apr 28, 2021
3066cd2
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 May 6, 2021
9597dc7
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 May 6, 2021
86547b0
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 10, 2021
4fa650c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 10, 2021
6f858c5
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 17, 2021
fb80156
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash May 25, 2021
17415b6
Sub resource template and tests
gearama May 27, 2021
81b73ad
public class , public constructor
gearama May 27, 2021
81f8d73
some PR comments
gearama May 27, 2021
28ca370
PR comments and phonecall around silent errors and casting
gearama May 27, 2021
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 @@ -10,7 +10,7 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// </summary>
public partial class SubResource : IUtf8JsonSerializable
public partial class SubResource<TIdentifier> : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input SubResource object.
Expand All @@ -37,9 +37,9 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
/// </summary>
/// <param name="element">The JSON element to be deserialized.</param>
/// <returns>Deserialized SubResource object.</returns>
internal static SubResource DeserializeSubResource(JsonElement element)
internal static SubResource<TIdentifier> DeserializeSubResource(JsonElement element)
{
Optional<string> id = default;
string id = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("id"))
Expand All @@ -48,7 +48,7 @@ internal static SubResource DeserializeSubResource(JsonElement element)
continue;
}
}
return new SubResource(id.Value);
return new SubResource<TIdentifier>(id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,89 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// A class representing the a sub resource of ResourceIdentifier.
/// </summary>
[ReferenceType]
public partial class SubResource
public class SubResource : SubResource<ResourceIdentifier>
gearama marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Initializes an empty instance of <see cref="SubResource"/> for mocking.
/// </summary>
[InitializationConstructor]
public SubResource()
public SubResource()
{
}

/// <summary> Initializes a new instance of SubResource. </summary>
/// <summary> Initializes a new instance of <see cref="SubResource"/>. </summary>
/// <param name="id"> ARM resource Id. </param>
[SerializationConstructor]
protected internal SubResource(string id) : base(id) { }
}

/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// </summary>
[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<TIdentifier> : IEquatable<SubResource<TIdentifier>>, IEquatable<string>,
IComparable<SubResource<TIdentifier>>, IComparable<string>
where TIdentifier : ResourceIdentifier
{
/// <summary>
/// Initializes an empty instance of <see cref="SubResource{TIdentifier}"/> for mocking.
/// </summary>
[InitializationConstructor]
public SubResource()
{
}

/// <summary> Initializes a new instance of <see cref="SubResource{TIdentifier}"/>. </summary>
/// <param name="id"> ARM resource Id. </param>
[SerializationConstructor]
protected internal SubResource(string id)
{
Id = id;
Id = (TIdentifier)id;
}

/// <summary>
/// Gets the ARM resource identifier.
/// </summary>
/// <value></value>
public virtual ResourceIdentifier Id { get; }
public virtual TIdentifier Id { get; }

/// <inheritdoc/>
public int CompareTo(string other)
{
return string.Compare(Id, other, StringComparison.InvariantCultureIgnoreCase);
}

/// <inheritdoc/>
public int CompareTo(SubResource<TIdentifier> other)
{
if (other is null)
return 1;

if (ReferenceEquals(this, other))
return 0;

return Id.CompareTo(other.Id);
}

/// <inheritdoc/>
public bool Equals(SubResource<TIdentifier> other)
{
return Id.Equals(other?.Id);
}

/// <inheritdoc/>
public bool Equals(string other)
{
return Id.Equals(other);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// </summary>
public partial class WritableSubResource : IUtf8JsonSerializable
public partial class WritableSubResource<TIdentifier> : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input SubResourceReadOnly object.
/// Serialize the input WritableSubResource object.
/// </summary>
/// <param name="writer"> Input Json writer. </param>
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
Expand All @@ -28,13 +28,13 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
}

/// <summary>
/// Deserialize the input JSON element to a SubResourceReadOnly object.
/// Deserialize the input JSON element to a WritableSubResource object.
/// </summary>
/// <param name="element">The JSON element to be deserialized.</param>
/// <returns>Deserialized SubResourceReadOnly object.</returns>
internal static WritableSubResource DeserializeSubResourceReadOnly(JsonElement element)
/// <returns>Deserialized WritableSubResource object.</returns>
internal static WritableSubResource<TIdentifier> DeserializeWritableSubResource(JsonElement element)
{
Optional<string> id = default;
string id = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("id"))
Expand All @@ -43,7 +43,7 @@ internal static WritableSubResource DeserializeSubResourceReadOnly(JsonElement e
continue;
}
}
return new WritableSubResource(id.Value);
return new WritableSubResource<TIdentifier>(id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing a sub-resource that contains only the read-only ID.
/// A class representing the a writable sub resource of ResourceIdentifier.
/// </summary>
[ReferenceType]
public partial class WritableSubResource
public class WritableSubResource : WritableSubResource<ResourceIdentifier>
gearama marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary> Initializes a new instance of SubResourceReadOnly. </summary>
/// <summary>
/// Initializes an empty instance of <see cref="WritableSubResource"/> for mocking.
/// </summary>
[InitializationConstructor]
public WritableSubResource()
public WritableSubResource()
{
}

/// <summary> Initializes a new instance of SubResourceReadOnly. </summary>
/// <summary> Initializes a new instance of <see cref="WritableSubResource"/>. </summary>
/// <param name="id"> ARM resource Id. </param>
[SerializationConstructor]
protected internal WritableSubResource(string id) : base(id) { }
}

/// <summary>
/// A class representing a sub-resource that contains only the read-only ID.
/// </summary>
[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 <TIdentifier> : IEquatable<WritableSubResource<TIdentifier>>, IEquatable<string>,
IComparable<WritableSubResource<TIdentifier>>, IComparable<string>
where TIdentifier : ResourceIdentifier
{
/// <summary>
/// Initializes an empty instance of <see cref="WritableSubResource{TIdentifier}"/> for mocking.
/// </summary>
[InitializationConstructor]
public WritableSubResource()
{
}

/// <summary> Initializes a new instance of <see cref="WritableSubResource{TIdentifier}"/>. </summary>
/// <param name="id"> ARM resource Id. </param>
[SerializationConstructor]
protected internal WritableSubResource(string id)
{
Id = id;
Id = (TIdentifier)id;
}

/// <summary>
/// Gets or sets the ARM resource identifier.
/// </summary>
/// <value></value>
public virtual ResourceIdentifier Id { get; set; }
public virtual TIdentifier Id { get; set; }

/// <inheritdoc/>
public int CompareTo(string other)
{
return string.Compare(Id, other, StringComparison.InvariantCultureIgnoreCase);
}

/// <inheritdoc/>
public int CompareTo(WritableSubResource<TIdentifier> other)
{
if (other is null)
return 1;

if (ReferenceEquals(this, other))
return 0;
return Id.CompareTo(other.Id);
}

/// <inheritdoc/>
public bool Equals(WritableSubResource<TIdentifier> other)
{
return Id.Equals(other?.Id);
}

/// <inheritdoc/>
public bool Equals(string other)
{
return Id.Equals(other);
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}