Skip to content

Commit

Permalink
TrackedResource Serialization (#19855)
Browse files Browse the repository at this point in the history
* Resource Serialization

* Divide Resource class

* Update writer

* TrackedResource Serialization

* Update Deserialization

* Update Test

* Update Tests

* Update Tracked Resource

* Fix PR

* Remove readonly
  • Loading branch information
HarveyLink authored Apr 2, 2021
1 parent 8724f9d commit d8b8880
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using Azure.Core;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// Generic representation of a tracked resource. All tracked resources should extend this class
/// </summary>
public abstract partial class TrackedResource<TIdentifier> : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input TrackedResource object.
/// </summary>
/// <param name="writer"> Input Json writer. </param>
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

writer.WriteStartObject();
if (Optional.IsDefined(Location))
{
writer.WritePropertyName("location");
writer.WriteStringValue(Location.Name);
}
if (Optional.IsCollectionDefined(Tags))
{
writer.WritePropertyName("tags");
writer.WriteStartObject();
foreach (var item in Tags)
{
writer.WritePropertyName(item.Key);
writer.WriteStringValue(item.Value);
}
writer.WriteEndObject();
}
writer.WriteEndObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Azure.ResourceManager.Core
/// Generic representation of a tracked resource. All tracked resources should extend this class
/// </summary>
[ReferenceType]
public abstract class TrackedResource<TIdentifier> : Resource<TIdentifier> where TIdentifier : TenantResourceIdentifier
public abstract partial class TrackedResource<TIdentifier> : Resource<TIdentifier> where TIdentifier : TenantResourceIdentifier
{
private IDictionary<string, string> _tag = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
/// <summary>
/// Gets the tags.
/// </summary>
public virtual IDictionary<string, string> Tags =>
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
public virtual IDictionary<string, string> Tags => _tag;

/// <summary>
/// Gets or sets the location the resource is in.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Azure.ResourceManager.Core.Tests
{
public class TestTrackedResource<TIdentifier> : TrackedResource<TIdentifier> where TIdentifier : TenantResourceIdentifier
{
public TestTrackedResource(string id) : this(id, LocationData.Default)
{
Id = ResourceIdentifier.Create(id) as TIdentifier;
}

public TestTrackedResource(string id, string location)
{
Id = ResourceIdentifier.Create(id) as TIdentifier;
Location = location;
}

public override TIdentifier Id { get; protected set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Core;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
{
[Parallelizable]
public class TrackedResourceTests
{
[Test]
public void SerializationTest()
{
string expected = "{\"properties\":{\"location\":\"eastus\",\"tags\":{\"key1\":\"value1\",\"key2\":\"value2\"}}}";
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());
Assert.IsTrue(expected.Equals(json));
}

[Test]
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());
Assert.IsTrue(expected.Equals(json));
}
}
}

0 comments on commit d8b8880

Please sign in to comment.