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

Adding tests for SubResource. CC@94%. #22117

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 50 additions & 0 deletions common/ManagementTestShared/Redesign/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.IO;
using System.Text;
using System.Text.Json;
using Azure.Core;
using NUnit.Framework;

namespace Azure.ResourceManager.TestFramework
{
internal static class JsonHelper
{
private static readonly JsonWriterOptions PrettyJsonOptions = new JsonWriterOptions() { Indented = true };
private static readonly JsonWriterOptions CompactJsonOptions = new JsonWriterOptions();

/// <summary>
/// This methods serialize the complete resource data to string.
/// </summary>
/// <param name="data"> Resource data that implements <see cref="IUtf8JsonSerializable"/>. </param>
/// <returns> Json string represent the data. </returns>
public static string SerializeToString(IUtf8JsonSerializable data, bool indented = false)
{
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, indented ? PrettyJsonOptions : CompactJsonOptions);
writer.WriteObjectValue(data);
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}

/// <summary>
/// This methods serialize the data properties to string under "properties" tag.
/// Please note if data has top level properties needed that goes outside of "properties",
/// use <see cref="JsonHelper.SerializeToString"/> instead.
/// </summary>
/// <param name="data"> data that implements <see cref="IUtf8JsonSerializable"/>. </param>
/// <returns> Json string represent the data object's properties. </returns>
public static string SerializePropertiesToString(IUtf8JsonSerializable data, bool indented = false)
{
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, indented ? PrettyJsonOptions : CompactJsonOptions);
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(data);
writer.WriteEndObject();
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Text.Json;
using Azure.Core;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
Expand All @@ -20,16 +21,8 @@ public void SerializationTestType1()
Plan plan = new Plan("NameForPlan", "PublisherForPlan", "ProductForPlan", "PromotionCodeForPlan", "VersionForPlan");
Sku sku = new Sku("NameForSku", "TierForSku", "FamilyForSku", "SizeForSku", 15464547);
GenericResourceData data = new GenericResourceData(id, id.Name, id.ResourceType, LocationData.EastUS, null, plan, null, "KindForResource", "ManagedByForResource", sku, null);
var stream = new MemoryStream();
var options = new JsonWriterOptions();
options.Indented = true;
Utf8JsonWriter writer = new(stream, options);
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(data);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray()) + Environment.NewLine;

var json = JsonHelper.SerializePropertiesToString(data, indented: true) + Environment.NewLine;
Assert.AreEqual(expected, json);
}

Expand All @@ -45,16 +38,8 @@ public void SerializationTestType2()
GenericResourceData genericResource = new GenericResourceData(id, id.Name, id.ResourceType, LocationData.EastUS, null, plan, null, kind, managedBy, sku, null);
genericResource.Tags.Add("key1", "value1");
genericResource.Tags.Add("key2", "value2");
var stream = new MemoryStream();
var options = new JsonWriterOptions();
options.Indented = true;
Utf8JsonWriter writer = new(stream, options);
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(genericResource);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray()) + Environment.NewLine;

var json = JsonHelper.SerializePropertiesToString(genericResource, indented: true) + Environment.NewLine;
Assert.AreEqual(expected, json);
}

Expand All @@ -65,14 +50,7 @@ public void InvalidSerializationTest()
ResourceGroupResourceIdentifier id = Id;
GenericResourceData data = new GenericResourceData(id, id.Name, id.ResourceType, LocationData.EastUS, null, null, null, null, null, null, null);

var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(data);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(data);
Assert.AreEqual(expected, json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using System.Text.Json;
using Azure.Core;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
Expand Down Expand Up @@ -239,29 +240,15 @@ public void SerializationTest()
{
string expected = "{\"properties\":{\"name\":\"NameForPlan\",\"publisher\":\"PublisherForPlan\",\"product\":\"ProductForPlan\",\"promotionCode\":\"PromotionCodeForPlan\",\"version\":\"VersionForPlan\"}}";
Plan plan = new("NameForPlan", "PublisherForPlan", "ProductForPlan", "PromotionCodeForPlan", "VersionForPlan");
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(plan);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(plan);
Assert.IsTrue(expected.Equals(json));
}

[Test]
public void InvalidSerializationTest()
{
Plan plan = new(null, null, null, null, null);
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(plan);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(plan);
Assert.IsTrue(json.Equals("{\"properties\":{}}"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using System.Text.Json;
using Azure.Core;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
Expand Down Expand Up @@ -235,29 +236,15 @@ public void SerializationTest()
{
string expected = "{\"properties\":{\"name\":\"NameForSku\",\"tier\":\"TierForSku\",\"size\":\"SizeForSku\",\"family\":\"FamilyForSku\",\"capacity\":123456789}}";
Sku sku = new("NameForSku", "TierForSku", "FamilyForSku", "SizeForSku", 123456789);
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(sku);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(sku);
Assert.IsTrue(expected.Equals(json));
}

[Test]
public void InvalidSerializationTest()
{
Sku sku = new(null, null, null, null);
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(sku);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(sku);
Assert.IsTrue(json.Equals("{\"properties\":{}}"));
}

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

using System.IO;
using System.Text;
using System.Text.Json;
using Azure.Core;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
Expand Down Expand Up @@ -50,7 +55,9 @@ public void CompareToNull()
{
var resource1 = new SubResource("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1");
SubResource resource2 = null;
var resource3 = new SubResource();
Assert.AreEqual(1, resource1.CompareTo(resource2));
Assert.AreEqual(1, resource1.CompareTo(resource3));
Assert.AreEqual(1, resource1.CompareTo((string)null));
}

Expand All @@ -61,5 +68,19 @@ public void CompareToSame()
var resource2 = resource1;
Assert.AreEqual(0, resource1.CompareTo(resource2));
}

[Test]
public void Deserialization()
{
var id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1";
var resource1 = new SubResource(id);
var jsonString = JsonHelper.SerializeToString(resource1);
var json = JsonDocument.Parse(jsonString).RootElement;
var resource2 = SubResource.DeserializeSubResource(json);
Assert.AreEqual(0, resource1.CompareTo(resource2));
Assert.IsTrue(resource1.Equals(resource2));
Assert.IsTrue(resource1.Equals(id));
Assert.IsFalse(resource1.Equals(id + "1"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Core;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
Expand All @@ -23,14 +24,7 @@ public void SerializationTest()
TestTrackedResource<ResourceGroupResourceIdentifier> data = new("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1", LocationData.EastUS);
data.Tags.Add("key1", "value1");
data.Tags.Add("key2", "value2");
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(data);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(data);
Assert.IsTrue(expected.Equals(json));
}

Expand All @@ -39,14 +33,7 @@ public void InvalidSerializationTest()
{
string expected = "{\"properties\":{\"location\":\"westus\",\"tags\":{}}}";
TestTrackedResource<ResourceGroupResourceIdentifier> data = new("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo");
var stream = new MemoryStream();
Utf8JsonWriter writer = new(stream, new JsonWriterOptions());
writer.WriteStartObject();
writer.WritePropertyName("properties");
writer.WriteObjectValue(data);
writer.WriteEndObject();
writer.Flush();
string json = Encoding.UTF8.GetString(stream.ToArray());
var json = JsonHelper.SerializePropertiesToString(data);
Assert.IsTrue(expected.Equals(json));
}
}
Expand Down