Skip to content

Commit

Permalink
Plan Serialization (#19820)
Browse files Browse the repository at this point in the history
* Plan Serialization

* Divide Plan class

* Update Plan
  • Loading branch information
HarveyLink authored Mar 30, 2021
1 parent 77d9e2d commit 0f24d6b
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Azure.ResourceManager.Core
{
/// <summary>
/// Representation of a publisher plan for marketplace RPs.
/// </summary>
public sealed partial class Plan : IUtf8JsonSerializable
{
/// <summary>
/// Serialize the input Plan 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(Name))
{
writer.WritePropertyName("name");
writer.WriteStringValue(Name);
}
if (Optional.IsDefined(Publisher))
{
writer.WritePropertyName("publisher");
writer.WriteStringValue(Publisher);
}
if (Optional.IsDefined(Product))
{
writer.WritePropertyName("product");
writer.WriteStringValue(Product);
}
if (Optional.IsDefined(PromotionCode))
{
writer.WritePropertyName("promotionCode");
writer.WriteStringValue(PromotionCode);
}
if (Optional.IsDefined(Version))
{
writer.WritePropertyName("version");
writer.WriteStringValue(Version);
}
writer.WriteEndObject();
}

/// <summary>
/// Deserialize the input Json object.
/// </summary>
/// <param name="element"> The Json object need to be deserialized. </param>
internal static Plan DeserializePlan(JsonElement element)
{
Optional<string> name = default;
Optional<string> publisher = default;
Optional<string> product = default;
Optional<string> promotionCode = default;
Optional<string> version = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("name"))
{
name = property.Value.GetString();
continue;
}
if (property.NameEquals("publisher"))
{
publisher = property.Value.GetString();
continue;
}
if (property.NameEquals("product"))
{
product = property.Value.GetString();
continue;
}
if (property.NameEquals("promotionCode"))
{
promotionCode = property.Value.GetString();
continue;
}
if (property.NameEquals("version"))
{
version = property.Value.GetString();
continue;
}
}
return new Plan(name.Value, publisher.Value, product.Value, promotionCode.Value, version.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// Representation of a publisher plan for marketplace RPs.
/// </summary>
public sealed class Plan : IEquatable<Plan>, IComparable<Plan>
public sealed partial class Plan : IEquatable<Plan>, IComparable<Plan>
{
/// <summary>
/// Initializes a new instance of the <see cref="Plan"/> class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using NUnit.Framework;
using System.IO;
using System.Text;
using System.Text.Json;
using Azure.Core;
using NUnit.Framework;

namespace Azure.ResourceManager.Core.Tests
{
Expand Down Expand Up @@ -229,5 +233,56 @@ public void EqualsToSamePlans()
Plan plan2 = plan1;
Assert.IsTrue(plan1.Equals(plan2));
}

[Test]
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());
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());
Assert.IsTrue(json.Equals("{\"properties\":{}}"));
}

[Test]
public void DeserializationTest()
{
string json = "{\"name\":\"NameForPlan\",\"publisher\":\"PublisherForPlan\",\"product\":\"ProductForPlan\",\"promotionCode\":\"PromotionCodeForPlan\",\"version\":\"VersionForPlan\"}";
JsonElement element = JsonDocument.Parse(json).RootElement;
Plan plan = Plan.DeserializePlan(element);
Assert.IsTrue(plan.Name.Equals("NameForPlan"));
Assert.IsTrue(plan.PromotionCode.Equals("PromotionCodeForPlan"));
}

[Test]
public void InvalidDeserializationTest()
{
string json = "{\"name\":\"NameForPlan\",\"notPublisher\":\"PublisherForPlan\",\"product\":\"ProductForPlan\",\"version\":\"VersionForPlan\"}";
JsonElement element = JsonDocument.Parse(json).RootElement;
Plan plan = Plan.DeserializePlan(element);
Assert.IsTrue(plan.Publisher == null);
Assert.IsTrue(plan.PromotionCode == null);
}
}
}

0 comments on commit 0f24d6b

Please sign in to comment.