Skip to content

Commit

Permalink
Add WritableSubResource tests (#22618)
Browse files Browse the repository at this point in the history
* Change the accessbility to virtual for Resource.Id

* Add SubscriptionContainer tests (#22348)

* Change SubResource constructors to protected

* Fix a bug in ReferenceType tests

* Add WritableSubResource tests

* Delete not in use class UpdateResourceGroupOperation

* Update unit test

* Update reference type tests

Co-authored-by: m-nash <prognash@microsoft.com>
Co-authored-by: m-nash <prognash@gmail.com>
  • Loading branch information
3 people authored Jul 13, 2021
1 parent a07759e commit 61189f1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 77 deletions.

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));
}
}
}

0 comments on commit 61189f1

Please sign in to comment.