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

Add WritableSubResource tests #22618

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 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
737171b
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 2, 2021
4f1408d
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jun 4, 2021
c5d5790
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 7, 2021
0bcb9e4
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jun 7, 2021
2a78d80
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jun 11, 2021
23eca40
Merge branch 'mgmt-track2' of https://github.com/AME-Redmond/azure-sd…
m-nash Jun 16, 2021
2bddb0e
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 16, 2021
26e907c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 23, 2021
3786863
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jun 23, 2021
96fef0c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
m-nash Jul 6, 2021
1d1ebdc
Add SubscriptionContainer tests (#22348)
YalinLi0312 Jul 6, 2021
77abf4b
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jul 6, 2021
aed679c
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jul 9, 2021
db62698
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jul 9, 2021
e9c51b6
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
YalinLi0312 Jul 9, 2021
9e56349
Change SubResource constructors to protected
YalinLi0312 Jul 13, 2021
2ed3057
Fix a bug in ReferenceType tests
YalinLi0312 Jul 13, 2021
d142c44
Add WritableSubResource tests
YalinLi0312 Jul 13, 2021
4a77028
Delete not in use class UpdateResourceGroupOperation
YalinLi0312 Jul 13, 2021
ddacbf3
Update unit test
YalinLi0312 Jul 13, 2021
9122b17
Update reference type tests
YalinLi0312 Jul 13, 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing the a sub resource of ResourceIdentifier.
/// A class representing the sub resource of a ResourceIdentifier.
/// </summary>
[ReferenceType]
public class SubResource : SubResource<ResourceIdentifier>
Expand All @@ -15,7 +13,7 @@ public class SubResource : SubResource<ResourceIdentifier>
/// Initializes an empty instance of <see cref="SubResource"/> for mocking.
/// </summary>
[InitializationConstructor]
public SubResource()
protected SubResource()
{
}

Expand All @@ -26,7 +24,7 @@ protected internal SubResource(string id) : base(id) { }
}

/// <summary>
/// A class representing a sub-resource that contains only the ID.
/// A class representing a sub-resource that contains only the read-only ID.
/// </summary>
[ReferenceType(typeof(ResourceIdentifier))]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Types differ by type argument only")]
Expand All @@ -37,7 +35,7 @@ public partial class SubResource<TIdentifier>
/// Initializes an empty instance of <see cref="SubResource{TIdentifier}"/> for mocking.
/// </summary>
[InitializationConstructor]
public SubResource()
protected SubResource()
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
}

writer.WriteStartObject();
if (Optional.IsDefined(Id))
{
writer.WritePropertyName("id");
writer.WriteStringValue(Id);
}
writer.WriteEndObject();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing the a writable sub resource of ResourceIdentifier.
/// A class representing the writable sub resource of a ResourceIdentifier.
/// </summary>
[ReferenceType]
public class WritableSubResource : WritableSubResource<ResourceIdentifier>
Expand All @@ -27,11 +24,11 @@ protected internal WritableSubResource(string id) : base(id) { }
}

/// <summary>
/// A class representing a sub-resource that contains only the read-only ID.
/// A class representing a sub-resource that contains only the ID.
/// </summary>
[ReferenceType(typeof(ResourceIdentifier))]
[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>
public partial class WritableSubResource<TIdentifier>
where TIdentifier : ResourceIdentifier
{
/// <summary>
Expand All @@ -47,7 +44,7 @@ public WritableSubResource()
[SerializationConstructor]
protected internal WritableSubResource(string id)
{
Id = (TIdentifier)id;
Id = (TIdentifier)id;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void ValidateInitializationConstructor()
var initializationCtor = refType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(c => HasAttribute(c.GetCustomAttributes<Attribute>(false), InitializationConstructor)).FirstOrDefault();
Assert.IsNotNull(initializationCtor);
Assert.IsTrue(refType.IsAbstract == initializationCtor.IsFamily, $"If {refType.Name} is abstract then its initialization ctor should be protected");
Assert.IsTrue(refType.IsAbstract != initializationCtor.IsPublic, $"If {refType.Name} is abstract then its initialization ctor should be public");
Assert.IsTrue((refType.IsAbstract || AllNonSetterProperties(refType)) == initializationCtor.IsFamily, $"If {refType.Name} is abstract then its initialization ctor should be protected");
Assert.IsTrue((refType.IsAbstract || AllNonSetterProperties(refType)) != initializationCtor.IsPublic, $"If {refType.Name} is abstract then its initialization ctor should not be public");
Assert.IsFalse(initializationCtor.IsAssembly, $"Initialization ctor for {refType.Name} should not be internal");
}
}
Expand All @@ -44,5 +44,18 @@ public bool HasAttribute(IEnumerable<Attribute> list, Type attributeType)
{
return list.FirstOrDefault(a => a.GetType() == attributeType) is not null;
}

public bool AllNonSetterProperties(Type referenceType)
{
List<PropertyInfo> properties = referenceType.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
foreach (var property in properties)
{
if (property.CanWrite)
{
return false;
}
}
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text.Json;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
{
[Parallelizable]
public class WritableSubResourceTests
{
[Test]
public void Deserialization()
{
var id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1";
var expected = "{\"id\":\"" + id + "\"}";
var resource1 = new WritableSubResource(id);
var jsonString = JsonHelper.SerializeToString(resource1);
var json = JsonDocument.Parse(jsonString).RootElement;
var resource2 = WritableSubResource.DeserializeWritableSubResource(json);
Assert.AreEqual(expected, jsonString);
Assert.AreEqual(jsonString, JsonHelper.SerializeToString(resource2));

var resource3 = new WritableSubResource();
resource3.Id = id;
Assert.AreEqual(jsonString, JsonHelper.SerializeToString(resource3));
}
}
}