-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TrackedResource Serialization (#19855)
* 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
1 parent
8724f9d
commit d8b8880
Showing
4 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...resourcemanager/Azure.ResourceManager.Core/src/Resources/TrackedResource.Serialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/Resource/TestTrackedResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/TrackedResourceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |