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

Migrate System.Text.Json to Newtone.Json #57

Merged
4 commits merged into from
Jul 5, 2024
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
10 changes: 5 additions & 5 deletions DotnetAdapters.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TmsRunner", "TmsRunner\TmsR
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tms.Adapter", "Tms.Adapter\Tms.Adapter.csproj", "{B5482D1B-736D-4E8D-88D9-5242922B58D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TmsRunnerTests", "TmsRunnerTests\TmsRunnerTests.csproj", "{5ACF7A63-C444-43D5-87A9-90D43801D28A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TmsRunnerTests", "TmsRunnerTests\TmsRunnerTests.csproj", "{5ACF7A63-C444-43D5-87A9-90D43801D28A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tms.Adapter.Core", "Tms.Adapter.Core\Tms.Adapter.Core.csproj", "{671EC822-3ED1-4797-850E-CA9952B8E5B3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tms.Adapter.Core", "Tms.Adapter.Core\Tms.Adapter.Core.csproj", "{671EC822-3ED1-4797-850E-CA9952B8E5B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tms.Adapter.CoreTests", "Tms.Adapter.CoreTests\Tms.Adapter.CoreTests.csproj", "{11F581C6-4B0F-424C-8160-6F0C4055C4F2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tms.Adapter.CoreTests", "Tms.Adapter.CoreTests\Tms.Adapter.CoreTests.csproj", "{11F581C6-4B0F-424C-8160-6F0C4055C4F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tms.Adapter.XUnit", "Tms.Adapter.XUnit\Tms.Adapter.XUnit.csproj", "{B5612734-4F6B-4801-9D17-5A1195C8D2A0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tms.Adapter.XUnit", "Tms.Adapter.XUnit\Tms.Adapter.XUnit.csproj", "{B5612734-4F6B-4801-9D17-5A1195C8D2A0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tms.Adapter.SpecFlowPlugin", "Tms.Adapter.SpecFlowPlugin\Tms.Adapter.SpecFlowPlugin.csproj", "{47C8A533-91F4-43E9-B75B-5DA46A9A05AD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tms.Adapter.SpecFlowPlugin", "Tms.Adapter.SpecFlowPlugin\Tms.Adapter.SpecFlowPlugin.csproj", "{47C8A533-91F4-43E9-B75B-5DA46A9A05AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
6 changes: 3 additions & 3 deletions Tms.Adapter.Core/Configurator/Configurator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Configuration;
using System.Text.Json;

using Newtonsoft.Json;

namespace Tms.Adapter.Core.Configurator;

Expand All @@ -16,7 +17,6 @@ public static class Configurator
private const string TmsAutomaticUpdationLinksToTestCases = "TMS_AUTOMATIC_UPDATION_LINKS_TO_TEST_CASES";
private const string TmsCertValidation = "TMS_CERT_VALIDATION";
private const string ConfigFile = "TMS_CONFIG_FILE";
private static readonly JsonSerializerOptions SerializerOptions = new() { PropertyNameCaseInsensitive = true };

public static TmsSettings GetConfig()
{
Expand All @@ -31,7 +31,7 @@ public static TmsSettings GetConfig()

if (File.Exists(defaultJsonConfigPath))
{
var fileConfig = JsonSerializer.Deserialize<TmsSettings>(File.ReadAllText(defaultJsonConfigPath), SerializerOptions);
var fileConfig = JsonConvert.DeserializeObject<TmsSettings>(File.ReadAllText(defaultJsonConfigPath));

if (fileConfig != null)
{
Expand Down
4 changes: 2 additions & 2 deletions Tms.Adapter.Core/Tms.Adapter.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down Expand Up @@ -42,12 +42,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
<PackageReference Include="TestIt.ApiClient" Version="3.0.0" />
</ItemGroup>

Expand Down
29 changes: 26 additions & 3 deletions Tms.Adapter.CoreTests/Client/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,39 @@ await Assert
}

[TestMethod]
public async Task TryLinkAutoTestToWorkItems()
public async Task GetWorkItemsLinkedToAutoTest()
{
// Arrange
var client = new TmsClient(_logger, _settings);
var id = Guid.NewGuid().ToString();
var workItemIds = Array.Empty<string>();

// Act & Assert
await Assert.ThrowsExceptionAsync<ApiException>(async () =>
await client.TryLinkAutoTestToWorkItems(id, workItemIds).ConfigureAwait(false)).ConfigureAwait(false);
await client.GetWorkItemsLinkedToAutoTest(id).ConfigureAwait(false)).ConfigureAwait(false);
}

[TestMethod]
public async Task DeleteAutoTestLinkFromWorkItem()
{
// Arrange
var client = new TmsClient(_logger, _settings);
var id = Guid.NewGuid().ToString();
const string workItemId = "";

// Act & Assert
await client.DeleteAutoTestLinkFromWorkItem(id, workItemId).ConfigureAwait(false);
}

[TestMethod]
public async Task LinkAutoTestToWorkItems()
{
// Arrange
var client = new TmsClient(_logger, _settings);
var id = Guid.NewGuid().ToString();
var workItemIds = Array.Empty<string>();

// Act & Assert
await client.LinkAutoTestToWorkItems(id, workItemIds).ConfigureAwait(false);
}

[TestMethod]
Expand Down
4 changes: 3 additions & 1 deletion Tms.Adapter.CoreTests/Writer/WriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using NSubstitute;
using Tms.Adapter.Core.Client;
using Tms.Adapter.Core.Configurator;
using Tms.Adapter.Core.Models;

namespace Tms.Adapter.CoreTests.Writer;
Expand All @@ -11,13 +12,14 @@ public class WriterTests
private readonly ClassContainer _classContainer = Substitute.For<ClassContainer>();
private readonly ITmsClient _client = Substitute.For<ITmsClient>();
private readonly ILogger<Core.Writer.Writer> _logger = Substitute.For<ILogger<Core.Writer.Writer>>();
private readonly TmsSettings _settings = Substitute.For<TmsSettings>();
private readonly TestContainer _testContainer = Substitute.For<TestContainer>();

[TestMethod]
public async Task WriteResults()
{
// Arrange
var writer = new Core.Writer.Writer(_logger, _client);
var writer = new Core.Writer.Writer(_logger, _client, _settings);

// Act & Assert
await writer.Write(_testContainer, _classContainer).ConfigureAwait(false);
Expand Down
3 changes: 2 additions & 1 deletion Tms.Adapter.SpecFlowPlugin/Tms.Adapter.SpecFlowPlugin.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down Expand Up @@ -38,6 +38,7 @@

<ItemGroup>
<PackageReference Include="SpecFlow" Version="3.9.74" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 7 additions & 6 deletions Tms.Adapter.SpecFlowPlugin/TmsTagParser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Text.Json;
using System.Text.Json.Nodes;

using Newtonsoft.Json;

using TechTalk.SpecFlow;
using Tms.Adapter.Core.Models;
using Tms.Adapter.Core.Utils;

using JsonException = System.Text.Json.JsonException;

namespace Tms.Adapter.SpecFlowPlugin;

public static class TmsTagParser
Expand All @@ -18,10 +23,6 @@ public static class TmsTagParser
private const string Links = "LINKS";
private const string WorkItemIds = "WORKITEMIDS";

private static readonly JsonSerializerOptions Options = new JsonSerializerOptions
{ PropertyNameCaseInsensitive = true };


public static TestContainer AddTags(TestContainer testContainer, FeatureInfo featureInfo,
ScenarioInfo scenarioInfo, Dictionary<string, string> parameters)
{
Expand Down Expand Up @@ -145,11 +146,11 @@ private static bool IsJsonArray(this string? source)

private static LinkItem? GetLink(string source)
{
return JsonSerializer.Deserialize<LinkItem>(source, Options);
return JsonConvert.DeserializeObject<LinkItem>(source);
}

private static List<LinkItem?>? GetLinks(string source)
{
return JsonSerializer.Deserialize<List<LinkItem?>>(source, Options);
return JsonConvert.DeserializeObject<List<LinkItem?>>(source);
}
}
12 changes: 7 additions & 5 deletions Tms.Adapter/Adapter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Diagnostics;
using System.Text.Json;

using Newtonsoft.Json;

using Tms.Adapter.Models;

namespace Tms.Adapter;
Expand All @@ -8,19 +10,19 @@
{
public static void AddLinks(params Link[] links)
{
Console.WriteLine($"{MessageType.TmsStepLinks}: " + JsonSerializer.Serialize(links));
Console.WriteLine($"{MessageType.TmsStepLinks}: " + JsonConvert.SerializeObject(links));
}

public static void AddLinks(string url, string? title = null, string? description = null,
LinkType? type = null)
{
Console.WriteLine($"{MessageType.TmsStepLinks}: " + JsonSerializer.Serialize(new List<Link>
Console.WriteLine($"{MessageType.TmsStepLinks}: " + JsonConvert.SerializeObject(new List<Link>
{
new Link
{
Title = title,

Check warning on line 23 in Tms.Adapter/Adapter.cs

View workflow job for this annotation

GitHub Actions / validate (./TmsRunner)

Possible null reference assignment.

Check warning on line 23 in Tms.Adapter/Adapter.cs

View workflow job for this annotation

GitHub Actions / validate (./Tms.Adapter)

Possible null reference assignment.

Check warning on line 23 in Tms.Adapter/Adapter.cs

View workflow job for this annotation

GitHub Actions / test (./TmsRunnerTests)

Possible null reference assignment.
Url = url,
Description = description,

Check warning on line 25 in Tms.Adapter/Adapter.cs

View workflow job for this annotation

GitHub Actions / validate (./TmsRunner)

Possible null reference assignment.

Check warning on line 25 in Tms.Adapter/Adapter.cs

View workflow job for this annotation

GitHub Actions / validate (./Tms.Adapter)

Possible null reference assignment.

Check warning on line 25 in Tms.Adapter/Adapter.cs

View workflow job for this annotation

GitHub Actions / test (./TmsRunnerTests)

Possible null reference assignment.
Type = type
}
}));
Expand All @@ -36,7 +38,7 @@
var fullPath = Path.GetFullPath(pathToFile);

Console.WriteLine($"{MessageType.TmsStepAttachment}: " +
JsonSerializer.Serialize(new Models.File
JsonConvert.SerializeObject(new Models.File
{ PathToFile = fullPath, CallerMemberName = memberName }));
}

Expand All @@ -56,7 +58,7 @@
.Replace("$_executor_", string.Empty);

Console.WriteLine($"{MessageType.TmsStepAttachmentAsText}: " +
JsonSerializer.Serialize(new Models.File
JsonConvert.SerializeObject(new Models.File
{ Name = name, Content = content, CallerMemberName = memberName }));
}

Expand Down
8 changes: 5 additions & 3 deletions Tms.Adapter/Attributes/ParameterizedAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using MethodBoundaryAspect.Fody.Attributes;
using MethodBoundaryAspect.Fody.Attributes;

using Newtonsoft.Json;

using Tms.Adapter.Models;

namespace Tms.Adapter.Attributes;
Expand All @@ -14,7 +16,7 @@ public override void OnEntry(MethodExecutionArgs arg)
.Zip(arguments, (k, v) => new { k, v })
.ToDictionary(x => x.k, x => x.v);

Console.WriteLine($"{MessageType.TmsParameters}: " + JsonSerializer.Serialize(args));
Console.WriteLine($"{MessageType.TmsParameters}: " + JsonConvert.SerializeObject(args));
}
}

Expand Down
8 changes: 5 additions & 3 deletions Tms.Adapter/Attributes/StepAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using MethodBoundaryAspect.Fody.Attributes;

using Newtonsoft.Json;

using Tms.Adapter.Models;
using Tms.Adapter.Utils;

Expand Down Expand Up @@ -128,7 +130,7 @@
CurrentMethodType = _currentMethodType
};

Console.WriteLine($"{MessageType.TmsStep}: " + JsonSerializer.Serialize(step));
Console.WriteLine($"{MessageType.TmsStep}: " + JsonConvert.SerializeObject(step));
}

public override void OnExit(MethodExecutionArgs arg)
Expand All @@ -141,7 +143,7 @@
WriteData("Failed");
}

private void WriteData(string outcome, object result = null)

Check warning on line 146 in Tms.Adapter/Attributes/StepAttribute.cs

View workflow job for this annotation

GitHub Actions / validate (./TmsRunner)

Cannot convert null literal to non-nullable reference type.

Check warning on line 146 in Tms.Adapter/Attributes/StepAttribute.cs

View workflow job for this annotation

GitHub Actions / validate (./Tms.Adapter)

Cannot convert null literal to non-nullable reference type.

Check warning on line 146 in Tms.Adapter/Attributes/StepAttribute.cs

View workflow job for this annotation

GitHub Actions / test (./TmsRunnerTests)

Cannot convert null literal to non-nullable reference type.
{
_completedAt = DateTime.UtcNow;

Expand All @@ -154,6 +156,6 @@
Outcome = outcome
};

Console.WriteLine($"{MessageType.TmsStepResult}: " + JsonSerializer.Serialize(stepResult));
Console.WriteLine($"{MessageType.TmsStepResult}: " + JsonConvert.SerializeObject(stepResult));
}
}
4 changes: 2 additions & 2 deletions Tms.Adapter/Tms.Adapter.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
Expand Down Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="Fody" Version="6.6.4" />
<PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.148" />
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.9.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
12 changes: 7 additions & 5 deletions TmsRunner/Services/ProcessorService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

using Newtonsoft.Json;

using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Tms.Adapter.Models;
using TmsRunner.Entities;
Expand Down Expand Up @@ -31,7 +33,7 @@ private async Task<List<Step>> GetStepsWithAttachmentsAsync(string? traceJson, L
{
case MessageType.TmsStep:
{
var step = JsonSerializer.Deserialize<Step>(message.Value ?? string.Empty);
var step = JsonConvert.DeserializeObject<Step>(message.Value ?? string.Empty);
if (step == null)
{
logger.LogWarning("Can not deserialize step: {Step}", message.Value);
Expand Down Expand Up @@ -77,7 +79,7 @@ private async Task<List<Step>> GetStepsWithAttachmentsAsync(string? traceJson, L
}
case MessageType.TmsStepResult:
{
var stepResult = JsonSerializer.Deserialize<StepResult>(message.Value ?? string.Empty);
var stepResult = JsonConvert.DeserializeObject<StepResult>(message.Value ?? string.Empty);

if (stepResult == null)
{
Expand All @@ -96,7 +98,7 @@ private async Task<List<Step>> GetStepsWithAttachmentsAsync(string? traceJson, L
}
case MessageType.TmsStepAttachmentAsText:
{
var attachment = JsonSerializer.Deserialize<File>(message.Value ?? string.Empty);
var attachment = JsonConvert.DeserializeObject<File>(message.Value ?? string.Empty);
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(attachment!.Content));
var createdAttachment =
await apiClient.UploadAttachmentAsync(Path.GetFileName(attachment.Name), ms).ConfigureAwait(false);
Expand All @@ -117,7 +119,7 @@ private async Task<List<Step>> GetStepsWithAttachmentsAsync(string? traceJson, L
}
case MessageType.TmsStepAttachment:
{
var file = JsonSerializer.Deserialize<File>(message.Value ?? string.Empty);
var file = JsonConvert.DeserializeObject<File>(message.Value ?? string.Empty);

if (System.IO.File.Exists(file!.PathToFile))
{
Expand Down
2 changes: 1 addition & 1 deletion TmsRunner/TmsRunner.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion TmsRunner/Utils/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static AutoTestResultsForTestRunModel ConvertResultToModel(AutoTestResult
l.Title,
l.Url,
l.Description,
Enum.Parse<LinkType>(l.Type.ToString()!))
Enum.TryParse<LinkType>(l.Type?.ToString(), true, out var result) ? result : null)
).ToList();

return new AutoTestResultsForTestRunModel(
Expand Down
8 changes: 5 additions & 3 deletions TmsRunner/Utils/LogParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using System.Text.Json;

using Newtonsoft.Json;

using System.Text.RegularExpressions;
using Tms.Adapter.Attributes;
using Tms.Adapter.Models;
Expand All @@ -19,7 +21,7 @@ public sealed class LogParser(Replacer replacer)
var match = regex.Match(traceJson);
var json = match.Groups[1].Value;

return string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize<Dictionary<string, string>>(json);
return string.IsNullOrEmpty(json) ? null : JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
}

public static string GetMessage(string traceJson)
Expand All @@ -45,7 +47,7 @@ public static string GetMessage(string traceJson)

var linksJsonArray = $"[ {string.Join(", ", matches.Select(m => m.Groups[1].Value))} ]";

return JsonSerializer.Deserialize<List<Link>>(linksJsonArray);
return JsonConvert.DeserializeObject<List<Link>>(linksJsonArray);
}

// TODO: write unit tests
Expand Down
Loading