Skip to content

Commit

Permalink
Fixing failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Dec 2, 2024
1 parent a9d5fc7 commit 2f72e89
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<PackageVersion Include="MySql.Data" Version="8.0.32.1" />
<PackageVersion Include="MySql.Data.EntityFrameworkCore" Version="8.0.22" />
<PackageVersion Include="NEST" Version="7.17.5" />
<PackageVersion Include="NJsonSchema" Version="10.2.2" />
<PackageVersion Include="NJsonSchema" Version="11.1.0" />
<PackageVersion Include="NLog" Version="4.6.8" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Nullean.VsTest.Pretty.TestLogger" Version="0.4.0" PrivateAssets="All" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<ItemGroup>
<PackageReference Include="NJsonSchema"/>
<PackageReference Include="SharpZipLib"/>
<PackageReference Include="Newtonsoft.Json"/>
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Apm/Api/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Service
{
private Service() { }

[JsonConstructor]
internal Service(string name, string version) => (Name, Version) = (name, version);

public AgentC Agent { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal PayloadItemSerializer() =>
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false,
Converters = { new JsonConverterDouble(), new JsonConverterDecimal() },
Converters = { new JsonConverterDouble(), new JsonConverterDecimal(), new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) },
TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver
{
Modifiers = { j =>
Expand Down
3 changes: 2 additions & 1 deletion test/Elastic.Apm.Tests.MockApmServer/ContextDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Text.Json.Serialization;
using Elastic.Apm.Api;
using Elastic.Apm.Helpers;

using Elastic.Apm.Model;
using Elastic.Apm.Report.Serialization;

// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable MemberCanBePrivate.Global
Expand All @@ -16,6 +16,7 @@ namespace Elastic.Apm.Tests.MockApmServer
internal class ContextDto : IDto
{
[JsonPropertyName("tags")]
[JsonConverter(typeof(LabelsJsonConverter))]
public LabelsDictionary Labels { get; set; }

public Request Request { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
using Elastic.Apm.Specification;
using Elastic.Apm.Tests.Utilities;
using FluentAssertions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Namotion.Reflection;
using NJsonSchema;
using Xunit.Sdk;
using JsonSerializerOptions = System.Text.Json.JsonSerializerOptions;
Expand All @@ -31,8 +33,13 @@ namespace Elastic.Apm.Tests.MockApmServer.Controllers
[ApiController]
public class IntakeV2EventsController : ControllerBase
{
private static readonly ConcurrentDictionary<Type, Lazy<Task<JsonSchema>>> Schemata =
new ConcurrentDictionary<Type, Lazy<Task<JsonSchema>>>();
private static readonly ConcurrentDictionary<Type, Lazy<Task<JsonSchema>>> Schemata = new();

private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

private const string ExpectedContentType = "application/x-ndjson; charset=utf-8";
private const string ThisClassName = nameof(IntakeV2EventsController);
Expand Down Expand Up @@ -100,12 +107,8 @@ private async Task<int> ParsePayload()
private async Task ParsePayloadLineAndAddToReceivedData(string line)
{
var foundDto = false;
var payload = JsonSerializer.Deserialize<PayloadLineDto>(
line,
new JsonSerializerOptions
{
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
}) ?? throw new ArgumentException("Deserialization failed");
var payload = JsonSerializer
.Deserialize<PayloadLineDto>(line, JsonSerializerOptions) ?? throw new ArgumentException("Deserialization failed");

await HandleParsed(nameof(payload.Error), payload.Error, _mockApmServer.ReceivedData.Errors, _mockApmServer.AddError);
await HandleParsed(nameof(payload.Metadata), payload.Metadata, _mockApmServer.ReceivedData.Metadata, _mockApmServer.AddMetadata);
Expand Down Expand Up @@ -136,11 +139,12 @@ async Task HandleParsed<TDto>(string dtoType, TDto dto, ImmutableList<TDto> accu
}, _validator)
.Value;

var jObject = JsonNode.Parse(line) as JsonObject;
var value = jObject!.TryGetPropertyValue(dtoType, out var dtoValue) ? dtoValue.GetValue<string>() : null;
using var jsonDocument = JsonDocument.Parse(line);

var validationErrors = schema.Validate(value);
var root = jsonDocument.RootElement;
var property = root.GetProperty(dtoType.ToLower());

var validationErrors = schema.Validate(property.ToString());
validationErrors.Should().BeEmpty();
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion test/Elastic.Apm.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public void MetricSet_Serializes_And_Deserializes()
{
var samples = new List<MetricSample>

Check failure on line 412 in test/Elastic.Apm.Tests/SerializationTests.cs

View workflow job for this annotation

GitHub Actions / tests

Elastic.Apm.Tests.SerializationTests.MetricSet_Serializes_And_Deserializes

Expected json to be "{"samples":{"sample_1":{"value":1},"sample__2":{"value":2.1}},"timestamp":1603343944891}" with a length of 88, but "{"samples":{"sample_1":{"value":1},"sample__2":{"value":2.1000000000000001}},"timestamp":1603343944891}" has a length of 103, differs near "000" (index 59).
{
new MetricSample("sample_1", 1), new MetricSample("sample*\"2", 2.1), new MetricSample("sample_1", 3)
new("sample_1", 1), new("sample*\"2", 2.1), new("sample_1", 3)
};

var metricSet = new Elastic.Apm.Metrics.MetricSet(1603343944891, samples);
Expand Down

0 comments on commit 2f72e89

Please sign in to comment.