Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Performance.Toolkit.Plugins.Core.Metadata;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Fixture = AutoFixture.Fixture;

namespace Microsoft.Performance.Toolkit.Plugins.Core.Tests;

[TestClass]
public sealed class PluginMetadataSerializationTests
{
private Fixture? fixture = null;

[TestInitialize]
public void Setup()
{
this.fixture = new Fixture();
}

[TestMethod]
public void PluginMetadata_Serialization_RoundTrip()
{
SerializationTestsHelper.RunSerializationTest<PluginMetadata>(this.fixture!);
}

[TestMethod]
public void PluginContentsMetadata_Serialization_RoundTrip()
{
SerializationTestsHelper.RunSerializationTest<PluginContentsMetadata>(this.fixture!);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using AutoFixture;
using Microsoft.Performance.Toolkit.Plugins.Core.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Performance.Toolkit.Plugins.Core.Tests;

public class SerializationTestsHelper
{
public static void RunSerializationTest<T>(Fixture fixture) where T : class
{
ISerializer<T> serializer = SerializationUtils.GetJsonSerializerWithDefaultOptions<T>();

T original = fixture.Create<T>();

using var stream = new MemoryStream();
serializer.Serialize(stream, original);

stream.Position = 0;

T deserialized = serializer.Deserialize(stream);

Assert.IsNotNull(deserialized);
Assert.IsTrue(original.Equals(deserialized));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Performance.Toolkit.Plugins.Core
/// <summary>
/// Provides extension methods for <see cref="IEnumerable{T}"/>.
/// </summary>
internal static class EnumerableExtensions
public static class EnumerableExtensions
{
/// <summary>
/// Determines whether two sequences are equal by comparing their elements by using the default equality comparer for their type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.Performance.SDK;
using Microsoft.Performance.Toolkit.Plugins.Core.Serialization;
using NuGet.Versioning;

namespace Microsoft.Performance.Toolkit.Plugins.Core.Metadata
Expand Down Expand Up @@ -87,7 +86,6 @@ public PluginMetadata(
/// <summary>
/// Gets the version of the performance SDK which this plugin depends upon.
/// </summary>
[JsonConverter(typeof(SemanticVersionJsonConverter))]
public SemanticVersion SdkVersion { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public PluginIdentity(string id, PluginVersion version)
}

/// <summary>
/// Gets the identifer of this plugin.
/// Gets the identifier of this plugin.
/// </summary>
public string Id { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

namespace Microsoft.Performance.Toolkit.Plugins.Core.Serialization
{
/// <summary>
/// Converts a <see cref="PluginVersion"/> to and from JSON.
/// </summary>
public class PluginVersionJsonConverter : JsonConverter<PluginVersion>
{
/// <inheritdoc/>
public override PluginVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var versionString = reader.GetString();
return versionString is null ? null : PluginVersion.Parse(versionString);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, PluginVersion value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@

namespace Microsoft.Performance.Toolkit.Plugins.Core.Serialization
{
/// <summary>
/// Converts a <see cref="SemanticVersion"/> to and from JSON.
/// </summary>
public class SemanticVersionJsonConverter : JsonConverter<SemanticVersion>
{
/// <inheritdoc/>
public override SemanticVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var versionString = reader.GetString();
return versionString is null ? null : SemanticVersion.Parse(versionString);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, SemanticVersion value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,9 @@ public static ISerializer<T> GetJsonSerializer<T>(JsonSerializerOptions serializ
Converters =
{
new JsonStringEnumConverter(),
new VersionStringConverter(),
new PluginVersionJsonConverter(),
new SemanticVersionJsonConverter(),
}
};

internal class VersionStringConverter
: JsonConverter<Version>
{
public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string stringValue = reader.GetString();
return new Version(stringValue);
}

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Microsoft.Performance.SDK\Microsoft.Performance.SDK.csproj" />
<ProjectReference Include="..\..\Microsoft.Performance.Toolkit.Plugins.Core.Tests\Microsoft.Performance.Toolkit.Plugins.Core.Tests.csproj" />
<ProjectReference Include="..\..\Microsoft.Performance.Toolkit.Plugins.Core\Microsoft.Performance.Toolkit.Plugins.Core.csproj" />
<ProjectReference Include="..\Microsoft.Performance.Toolkit.Plugins.Cli\Microsoft.Performance.Toolkit.Plugins.Cli.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Performance.Toolkit.Plugins.Cli.Manifest;
using Microsoft.Performance.Toolkit.Plugins.Core.Tests;
using Fixture = AutoFixture.Fixture;

namespace Microsoft.Performance.Toolkit.Plugins.Cli.Tests;

[TestClass]
public sealed class PluginManifestSerializationTest
{
private Fixture? fixture = null;

[TestInitialize]
public void Setup()
{
this.fixture = new Fixture();
}

[TestMethod]
public void PluginManifest_Serialization_RoundTrip()
{
SerializationTestsHelper.RunSerializationTest<PluginManifest>(this.fixture!);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Performance.SDK;
using Microsoft.Performance.Toolkit.Plugins.Core;
using Microsoft.Performance.Toolkit.Plugins.Core.Serialization;
using System.Text.Json.Serialization;

namespace Microsoft.Performance.Toolkit.Plugins.Cli.Manifest
{
/// <summary>
/// Represents the identity of a plugin in a manifest.
/// </summary>
public sealed class PluginIdentityManifest
: IEquatable<PluginIdentityManifest>
{
/// <summary>
/// Initializes a new instance of the <see cref="PluginIdentityManifest"/>
Expand All @@ -25,6 +25,9 @@ public PluginIdentityManifest(
string id,
PluginVersion version)
{
Guard.NotNull(id, nameof(id));
Guard.NotNull(version, nameof(version));

this.Id = id;
this.Version = version;
}
Expand All @@ -37,7 +40,55 @@ public PluginIdentityManifest(
/// <summary>
/// Gets or sets the version of this plugin.
/// </summary>
[JsonConverter(typeof(PluginVersionJsonConverter))]
public PluginVersion Version { get; }

/// <inheritdoc />
public override bool Equals(object? obj)
{
return Equals(obj as PluginIdentityManifest);
}

/// <inheritdoc />
public bool Equals(PluginIdentityManifest? other)
{
return Equals(this, other);
}

/// <summary>
/// Determines whether the specified <see cref="PluginIdentityManifest"/> instances are considered equal.
/// </summary>
/// <param name="a">
/// The first plugin identity to compare.
/// </param>
/// <param name="b">
/// The second plugin identity to compare.
/// </param>
/// <returns>
/// <c>true</c> if a and b are considered equal; <c>false</c> otherwise.
/// If a or b is null, the method returns <c>false</c>.
/// </returns>
public static bool Equals(PluginIdentityManifest? a, PluginIdentityManifest? b)
{
if (ReferenceEquals(a, b))
{
return true;
}

if (a is null || b is null)
{
return false;
}

return string.Equals(a.Id, b.Id, StringComparison.Ordinal) &&
PluginVersion.Equals(a.Version, b.Version);
}

/// <inheritdoc />
public override int GetHashCode()
{
return HashCodeUtils.CombineHashCodeValues(
this.Id.GetHashCode(),
this.Version.GetHashCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Performance.Toolkit.Plugins.Core;
using Microsoft.Performance.SDK;
using System.Text.Json.Serialization;

namespace Microsoft.Performance.Toolkit.Plugins.Cli.Manifest
{
/// <summary>
/// Represents the manifest for a plugin.
/// </summary>
public sealed class PluginManifest
: IEquatable<PluginManifest>
{
/// <summary>
/// Initializes a new instance of the <see cref="PluginManifest"/>s
Expand All @@ -26,6 +31,7 @@ public sealed class PluginManifest
/// <param name="projectUrl">
/// The URL of the project that owns this plugin.
/// </param>
[JsonConstructor]
public PluginManifest(
PluginIdentityManifest identity,
string displayName,
Expand Down Expand Up @@ -65,5 +71,51 @@ public PluginManifest(
/// Gets the URL of the project that owns this plugin.
/// </summary>
public Uri ProjectUrl { get; }

/// <inheritdoc />
public override bool Equals(object? obj)
{
return Equals(obj as PluginManifest);
}

/// <inheritdoc />
public bool Equals(PluginManifest? other)
{
if (other is null)
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return this.Identity.Equals(other.Identity)
&& this.DisplayName == other.DisplayName
&& this.Description == other.Description
&& this.Owners.EnumerableEqual(other.Owners)
&& this.ProjectUrl == other.ProjectUrl;
}

/// <inheritdoc />
public override int GetHashCode()
{
int result = HashCodeUtils.CombineHashCodeValues(
this.Identity?.GetHashCode() ?? 0,
this.DisplayName?.GetHashCode() ?? 0,
this.Description?.GetHashCode() ?? 0,
this.ProjectUrl?.GetHashCode() ?? 0);

if (this.Owners != null)
{
foreach (PluginOwnerInfoManifest owner in this.Owners)
{
result = HashCodeUtils.CombineHashCodeValues(result, owner?.GetHashCode() ?? 0);
}
}

return result;
}
}
}
Loading
Loading