diff --git a/source/B2BApi/IncomingMessages/IncomingMessageReceiver.cs b/source/B2BApi/IncomingMessages/IncomingMessageReceiver.cs index 280294db3c..e66577c53f 100644 --- a/source/B2BApi/IncomingMessages/IncomingMessageReceiver.cs +++ b/source/B2BApi/IncomingMessages/IncomingMessageReceiver.cs @@ -121,9 +121,7 @@ private static async Task CreateResponseAsync( var entityTypeMapping = new Dictionary { { IncomingDocumentType.RequestAggregatedMeasureData, AuditLogEntityType.RequestAggregatedMeasureData }, - { IncomingDocumentType.B2CRequestAggregatedMeasureData, AuditLogEntityType.RequestAggregatedMeasureData }, { IncomingDocumentType.RequestWholesaleSettlement, AuditLogEntityType.RequestWholesaleServices }, - { IncomingDocumentType.B2CRequestWholesaleSettlement, AuditLogEntityType.RequestWholesaleServices }, }; entityTypeMapping.TryGetValue(incomingDocumentType, out var affectedEntityType); diff --git a/source/IncomingMessages.Application/Extensions/DependencyInjection/IncomingMessagesExtensions.cs b/source/IncomingMessages.Application/Extensions/DependencyInjection/IncomingMessagesExtensions.cs index b985dae3af..a4744b45b4 100644 --- a/source/IncomingMessages.Application/Extensions/DependencyInjection/IncomingMessagesExtensions.cs +++ b/source/IncomingMessages.Application/Extensions/DependencyInjection/IncomingMessagesExtensions.cs @@ -63,10 +63,10 @@ public static IServiceCollection AddIncomingMessagesModule(this IServiceCollecti .AddScoped() .AddScoped() .AddScoped() - .AddScoped() + .AddScoped() .AddScoped() .AddScoped() - .AddScoped() + .AddScoped() .AddScoped() .AddScoped() .AddScoped() @@ -142,9 +142,11 @@ public static IServiceCollection AddIncomingMessagesModule(this IServiceCollecti services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); return services; } diff --git a/source/IncomingMessages.Infrastructure/IncomingMessages.Infrastructure.csproj b/source/IncomingMessages.Infrastructure/IncomingMessages.Infrastructure.csproj index 4a0a90ec18..7347e173ce 100644 --- a/source/IncomingMessages.Infrastructure/IncomingMessages.Infrastructure.csproj +++ b/source/IncomingMessages.Infrastructure/IncomingMessages.Infrastructure.csproj @@ -26,7 +26,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataB2CJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataB2CJsonMessageParserBase.cs similarity index 51% rename from source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataB2CJsonMessageParser.cs rename to source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataB2CJsonMessageParserBase.cs index f78148dcc5..473197710e 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataB2CJsonMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataB2CJsonMessageParserBase.cs @@ -15,34 +15,21 @@ using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models; using Energinet.DataHub.EDI.BuildingBlocks.Interfaces; using Energinet.DataHub.EDI.IncomingMessages.Domain; +using Energinet.DataHub.EDI.IncomingMessages.Domain.Abstractions; using Energinet.DataHub.EDI.IncomingMessages.Interfaces.Models; namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.AggregatedMeasureDataRequestMessageParsers; -public class AggregatedMeasureDataB2CJsonMessageParser : IMarketMessageParser +public class AggregatedMeasureDataB2CJsonMessageParserBase(ISerializer serializer) + : B2CJsonMessageParserBase(serializer) { - private readonly ISerializer _serializer; + public override IncomingDocumentType DocumentType => IncomingDocumentType.B2CRequestAggregatedMeasureData; - public AggregatedMeasureDataB2CJsonMessageParser(ISerializer serializer) - { - _serializer = serializer; - } - - public DocumentFormat HandledFormat => DocumentFormat.Json; + public override DocumentFormat DocumentFormat => DocumentFormat.Json; - public IncomingDocumentType DocumentType => IncomingDocumentType.B2CRequestAggregatedMeasureData; - - public async Task ParseAsync( - IIncomingMarketMessageStream incomingMarketMessageStream, - CancellationToken cancellationToken) + protected override IIncomingMessage MapIncomingMessage(RequestAggregatedMeasureDataDto incomingMessageDto) { - ArgumentNullException.ThrowIfNull(incomingMarketMessageStream); - - var requestAggregatedMeasureDataDto = await _serializer - .DeserializeAsync(incomingMarketMessageStream.Stream, cancellationToken) - .ConfigureAwait(false); - - var series = requestAggregatedMeasureDataDto.Serie + var series = incomingMessageDto.Serie .Select( serie => new RequestAggregatedMeasureDataMessageSeries( serie.Id, @@ -56,17 +43,17 @@ public async Task ParseAsync( serie.SettlementVersion)).ToList().AsReadOnly(); var requestAggregatedMeasureData = new RequestAggregatedMeasureDataMessage( - requestAggregatedMeasureDataDto.SenderNumber, - requestAggregatedMeasureDataDto.SenderRoleCode, - requestAggregatedMeasureDataDto.ReceiverNumber, - requestAggregatedMeasureDataDto.ReceiverRoleCode, - requestAggregatedMeasureDataDto.BusinessReason, - requestAggregatedMeasureDataDto.MessageType, - requestAggregatedMeasureDataDto.MessageId, - requestAggregatedMeasureDataDto.CreatedAt, - requestAggregatedMeasureDataDto.BusinessType, + incomingMessageDto.SenderNumber, + incomingMessageDto.SenderRoleCode, + incomingMessageDto.ReceiverNumber, + incomingMessageDto.ReceiverRoleCode, + incomingMessageDto.BusinessReason, + incomingMessageDto.MessageType, + incomingMessageDto.MessageId, + incomingMessageDto.CreatedAt, + incomingMessageDto.BusinessType, series); - return new IncomingMarketMessageParserResult(requestAggregatedMeasureData); + return requestAggregatedMeasureData; } } diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/OldAggregatedMeasureDataB2CJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/OldAggregatedMeasureDataB2CJsonMessageParser.cs new file mode 100644 index 0000000000..cfcdfc25e9 --- /dev/null +++ b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/OldAggregatedMeasureDataB2CJsonMessageParser.cs @@ -0,0 +1,33 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models; + +namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.AggregatedMeasureDataRequestMessageParsers; + +public class OldAggregatedMeasureDataB2CJsonMessageParser(IEnumerable messageParsers) : IMarketMessageParser +{ + private readonly IEnumerable _messageParsers = messageParsers; + + public DocumentFormat HandledFormat => DocumentFormat.Json; + + public IncomingDocumentType DocumentType => IncomingDocumentType.B2CRequestAggregatedMeasureData; + + public async Task ParseAsync(IIncomingMarketMessageStream incomingMarketMessageStream, CancellationToken cancellationToken) + { + var messageParser = _messageParsers + .Single(x => x.DocumentFormat == HandledFormat && x.DocumentType == DocumentType); + return await messageParser.ParseAsync(incomingMarketMessageStream, cancellationToken).ConfigureAwait(false); + } +} diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/B2CJsonMessageParserBase.cs b/source/IncomingMessages.Infrastructure/MessageParsers/B2CJsonMessageParserBase.cs new file mode 100644 index 0000000000..16ed88e678 --- /dev/null +++ b/source/IncomingMessages.Infrastructure/MessageParsers/B2CJsonMessageParserBase.cs @@ -0,0 +1,123 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Text.Json; +using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models; +using Energinet.DataHub.EDI.BuildingBlocks.Interfaces; +using Energinet.DataHub.EDI.IncomingMessages.Domain.Abstractions; +using Energinet.DataHub.EDI.IncomingMessages.Domain.Validation.ValidationErrors; +using Json.Schema; +using NJsonSchema; +using NJsonSchema.Generation; +using JsonSchema = Json.Schema.JsonSchema; + +namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers; + +/// +/// This class is responsible for parsing a B2C JSON message into a specific type. +/// +/// +/// The type to parse the B2C JSON message into. +/// +public abstract class B2CJsonMessageParserBase(ISerializer serializer) : MessageParserBase() +{ + private readonly ISerializer _serializer = serializer; + + protected override Task<(JsonSchema? Schema, ValidationError? ValidationError)> GetSchemaAsync(IIncomingMarketMessageStream marketMessage, CancellationToken cancellationToken) + { + var schema = NJsonSchema.JsonSchema.FromType( + new SystemTextJsonSchemaGeneratorSettings() + { + DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull, + SchemaType = SchemaType.JsonSchema, + }); + + // Serialize the NJsonSchema.JsonSchema to a JSON string + var jsonString = schema.ToJson(); + + // Add the $schema property to the JSON string + var jsonObject = JsonDocument.Parse(jsonString).RootElement.Clone(); + var jsonSchemaString = jsonObject.GetRawText().Replace("http://json-schema.org/draft-04/schema#", "http://json-schema.org/draft-07/schema"); + + // Deserialize the JSON string to a Json.Schema.JsonSchema + var jsonSchema = JsonSerializer.Deserialize(jsonSchemaString); + return Task.FromResult((jsonSchema, (ValidationError?)null)); + } + + protected override async Task ParseMessageAsync( + IIncomingMarketMessageStream marketMessage, + JsonSchema schemaResult, + CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(marketMessage); + + var isSchemaValidAsync = await IsSchemaValidAsync(marketMessage.Stream, schemaResult, cancellationToken) + .ConfigureAwait(false); + + if (isSchemaValidAsync.Any()) + { + return new IncomingMarketMessageParserResult(isSchemaValidAsync); + } + + var requestWholesaleSettlementDto = await _serializer + .DeserializeAsync(marketMessage.Stream, cancellationToken) + .ConfigureAwait(false); + + var incomingMessage = MapIncomingMessage(requestWholesaleSettlementDto); + return new IncomingMarketMessageParserResult(incomingMessage); + } + + protected abstract IIncomingMessage MapIncomingMessage(T incomingMessageDto); + + private async Task IsSchemaValidAsync(Stream marketMessageStream, JsonSchema jsonSchema, CancellationToken cancellationToken) + { + var document = await JsonDocument.ParseAsync(marketMessageStream, cancellationToken: cancellationToken).ConfigureAwait(false); + var validationResult = jsonSchema.Evaluate(document, new EvaluationOptions { OutputFormat = OutputFormat.Hierarchical }); + + if (!validationResult.IsValid) + { + return validationResult.Details + .Select(FindErrorsForInvalidEvaluation) + .SelectMany(detail => detail) + .ToArray(); + } + + // Reset the stream position to the beginning for deserialization + marketMessageStream.Position = 0; + + return []; + } + + private ValidationError[] FindErrorsForInvalidEvaluation(EvaluationResults result) + { + if (!result.IsValid) + { + foreach (var detail in result.Details) + { + FindErrorsForInvalidEvaluation(detail); + } + } + + if (!result.HasErrors || result.Errors == null) return []; + + var validationErrors = new List(); + var propertyName = result.InstanceLocation.ToString(); + foreach (var error in result.Errors) + { + validationErrors.Add(InvalidMessageStructure.From($"{propertyName}: {error}")); + } + + return validationErrors.ToArray(); + } +} diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/OldWholesaleSettlementB2CJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/OldWholesaleSettlementB2CJsonMessageParser.cs new file mode 100644 index 0000000000..89ed6469d2 --- /dev/null +++ b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/OldWholesaleSettlementB2CJsonMessageParser.cs @@ -0,0 +1,33 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models; + +namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.WholesaleSettlementMessageParsers; + +public class OldWholesaleSettlementB2CJsonMessageParser(IEnumerable messageParsers) : IMarketMessageParser +{ + private readonly IEnumerable _messageParsers = messageParsers; + + public DocumentFormat HandledFormat => DocumentFormat.Json; + + public IncomingDocumentType DocumentType => IncomingDocumentType.B2CRequestWholesaleSettlement; + + public async Task ParseAsync(IIncomingMarketMessageStream incomingMarketMessageStream, CancellationToken cancellationToken) + { + var messageParser = _messageParsers + .Single(x => x.DocumentFormat == HandledFormat && x.DocumentType == DocumentType); + return await messageParser.ParseAsync(incomingMarketMessageStream, cancellationToken).ConfigureAwait(false); + } +} diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementB2CJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementB2CJsonMessageParserBase.cs similarity index 53% rename from source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementB2CJsonMessageParser.cs rename to source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementB2CJsonMessageParserBase.cs index 788702d52e..bd3e80e02e 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementB2CJsonMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementB2CJsonMessageParserBase.cs @@ -15,35 +15,23 @@ using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models; using Energinet.DataHub.EDI.BuildingBlocks.Interfaces; using Energinet.DataHub.EDI.IncomingMessages.Domain; +using Energinet.DataHub.EDI.IncomingMessages.Domain.Abstractions; +using Energinet.DataHub.EDI.IncomingMessages.Domain.Validation.ValidationErrors; using Energinet.DataHub.EDI.IncomingMessages.Interfaces.Models; +using Json.Schema; namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.WholesaleSettlementMessageParsers; -public class WholesaleSettlementB2CJsonMessageParser : IMarketMessageParser +public class WholesaleSettlementB2CJsonMessageParserBase(ISerializer serializer) + : B2CJsonMessageParserBase(serializer) { - private readonly ISerializer _serializer; + public override IncomingDocumentType DocumentType => IncomingDocumentType.B2CRequestWholesaleSettlement; - public WholesaleSettlementB2CJsonMessageParser( - ISerializer serializer) - { - _serializer = serializer; - } - - public DocumentFormat HandledFormat => DocumentFormat.Json; + public override DocumentFormat DocumentFormat => DocumentFormat.Json; - public IncomingDocumentType DocumentType => IncomingDocumentType.B2CRequestWholesaleSettlement; - - public async Task ParseAsync( - IIncomingMarketMessageStream incomingMarketMessageStream, - CancellationToken cancellationToken) + protected override IIncomingMessage MapIncomingMessage(RequestWholesaleSettlementDto incomingMessageDto) { - ArgumentNullException.ThrowIfNull(incomingMarketMessageStream); - - var requestWholesaleSettlementDto = await _serializer - .DeserializeAsync(incomingMarketMessageStream.Stream, cancellationToken) - .ConfigureAwait(false); - - var seriesCollection = requestWholesaleSettlementDto.Series + var seriesCollection = incomingMessageDto.Series .Select( series => new RequestWholesaleServicesSeries( series.Id, @@ -61,17 +49,17 @@ public async Task ParseAsync( .AsReadOnly(); var requestWholesaleServicesMessage = new RequestWholesaleServicesMessage( - requestWholesaleSettlementDto.SenderNumber, - requestWholesaleSettlementDto.SenderRoleCode, - requestWholesaleSettlementDto.ReceiverNumber, - requestWholesaleSettlementDto.ReceiverRoleCode, - requestWholesaleSettlementDto.BusinessReason, - requestWholesaleSettlementDto.MessageType, - requestWholesaleSettlementDto.MessageId, - requestWholesaleSettlementDto.CreatedAt, - requestWholesaleSettlementDto.BusinessType, + incomingMessageDto.SenderNumber, + incomingMessageDto.SenderRoleCode, + incomingMessageDto.ReceiverNumber, + incomingMessageDto.ReceiverRoleCode, + incomingMessageDto.BusinessReason, + incomingMessageDto.MessageType, + incomingMessageDto.MessageId, + incomingMessageDto.CreatedAt, + incomingMessageDto.BusinessType, seriesCollection); - return new IncomingMarketMessageParserResult(requestWholesaleServicesMessage); + return requestWholesaleServicesMessage; } } diff --git a/source/IncomingMessages.IntegrationTests/MessageParsers/GivenNewDocumentTypeTests.cs b/source/IncomingMessages.IntegrationTests/MessageParsers/GivenNewDocumentTypeTests.cs index d353922090..74b71c31b4 100644 --- a/source/IncomingMessages.IntegrationTests/MessageParsers/GivenNewDocumentTypeTests.cs +++ b/source/IncomingMessages.IntegrationTests/MessageParsers/GivenNewDocumentTypeTests.cs @@ -26,12 +26,12 @@ public class GivenNewIncomingDocumentTypeTests : IncomingMessagesTestBase { private static readonly List<(IncomingDocumentType, DocumentFormat)> _unsupportedCombinationsOfIncomingDocumentTypeAndDocumentFormat = new() { - (IncomingDocumentType.B2CRequestWholesaleSettlement, DocumentFormat.Xml), - (IncomingDocumentType.B2CRequestAggregatedMeasureData, DocumentFormat.Xml), (IncomingDocumentType.RequestWholesaleSettlement, DocumentFormat.Ebix), (IncomingDocumentType.RequestAggregatedMeasureData, DocumentFormat.Ebix), - (IncomingDocumentType.B2CRequestWholesaleSettlement, DocumentFormat.Ebix), (IncomingDocumentType.B2CRequestAggregatedMeasureData, DocumentFormat.Ebix), + (IncomingDocumentType.B2CRequestWholesaleSettlement, DocumentFormat.Ebix), + (IncomingDocumentType.B2CRequestAggregatedMeasureData, DocumentFormat.Xml), + (IncomingDocumentType.B2CRequestWholesaleSettlement, DocumentFormat.Xml), }; public GivenNewIncomingDocumentTypeTests(IncomingMessagesTestFixture incomingMessagesTestFixture, ITestOutputHelper testOutputHelper) diff --git a/source/Tests/CimMessageAdapter/Messages/RequestAggregatedMeasureData/MessageParserTests.cs b/source/Tests/CimMessageAdapter/Messages/RequestAggregatedMeasureData/MessageParserTests.cs index 21d734d64b..193dda9f52 100644 --- a/source/Tests/CimMessageAdapter/Messages/RequestAggregatedMeasureData/MessageParserTests.cs +++ b/source/Tests/CimMessageAdapter/Messages/RequestAggregatedMeasureData/MessageParserTests.cs @@ -43,11 +43,18 @@ public sealed class MessageParserTests private static readonly string SubPath = $"{Path.DirectorySeparatorChar}aggregatedmeasure{Path.DirectorySeparatorChar}"; + private static readonly List _messageParsers = new() + { + new AggregatedMeasureDataXmlMessageParser(new CimXmlSchemaProvider(new CimXmlSchemas())), + new AggregatedMeasureDataJsonMessageParser(new JsonSchemaProvider(new CimJsonSchemas())), + new AggregatedMeasureDataB2CJsonMessageParserBase(new Serializer()), + }; + private readonly MarketMessageParser _marketMessageParser = new( [ - new OldAggregatedMeasureDataXmlMessageParser(new List { new AggregatedMeasureDataXmlMessageParser(new CimXmlSchemaProvider(new CimXmlSchemas())) }), - new OldAggregatedMeasureDataJsonMessageParser(new List { new AggregatedMeasureDataJsonMessageParser(new JsonSchemaProvider(new CimJsonSchemas())) }), - new AggregatedMeasureDataB2CJsonMessageParser(new Serializer()), + new OldAggregatedMeasureDataXmlMessageParser(_messageParsers), + new OldAggregatedMeasureDataJsonMessageParser(_messageParsers), + new OldAggregatedMeasureDataB2CJsonMessageParser(_messageParsers), ]); public static IEnumerable CreateMessagesWithSingleAndMultipleTransactions() diff --git a/source/Tests/CimMessageAdapter/Messages/WholesaleSettlementMessageParsers/MessageParserTests.cs b/source/Tests/CimMessageAdapter/Messages/WholesaleSettlementMessageParsers/MessageParserTests.cs index a028c80848..6c97c04d95 100644 --- a/source/Tests/CimMessageAdapter/Messages/WholesaleSettlementMessageParsers/MessageParserTests.cs +++ b/source/Tests/CimMessageAdapter/Messages/WholesaleSettlementMessageParsers/MessageParserTests.cs @@ -49,11 +49,17 @@ public sealed class MessageParserTests public MessageParserTests() { + var messageParsers = new List() + { + new WholesaleSettlementXmlMessageParser(new CimXmlSchemaProvider(new CimXmlSchemas())), + new WholesaleSettlementJsonMessageParser(new JsonSchemaProvider(new CimJsonSchemas())), + new WholesaleSettlementB2CJsonMessageParserBase(_serializer), + }; _marketMessageParser = new MarketMessageParser( [ - new OldWholesaleSettlementXmlMessageParser(new List() { new WholesaleSettlementXmlMessageParser(new CimXmlSchemaProvider(new CimXmlSchemas())) }), - new OldWholesaleSettlementJsonMessageParser(new List() { new WholesaleSettlementJsonMessageParser(new JsonSchemaProvider(new CimJsonSchemas())) }), - new WholesaleSettlementB2CJsonMessageParser(_serializer), + new OldWholesaleSettlementXmlMessageParser(messageParsers), + new OldWholesaleSettlementJsonMessageParser(messageParsers), + new OldWholesaleSettlementB2CJsonMessageParser(messageParsers), ]); } @@ -109,7 +115,11 @@ public static IEnumerable CreateMessagesWithOneBigSeriesAndOneSmall() [MemberData(nameof(CreateMessagesWithSingleAndMultipleTransactions))] public async Task Successfully_parsed(DocumentFormat format, Stream message) { - var result = await _marketMessageParser.ParseAsync(new IncomingMarketMessageStream(message), format, IncomingDocumentType.RequestWholesaleSettlement, CancellationToken.None); + var result = await _marketMessageParser.ParseAsync( + new IncomingMarketMessageStream(message), + format, + IncomingDocumentType.RequestWholesaleSettlement, + CancellationToken.None); using var assertionScope = new AssertionScope(); result.Success.Should().BeTrue(); @@ -177,7 +187,11 @@ public async Task Given_MessageWithTwoChargeTypes_When_Parsing_Then_Successfully [MemberData(nameof(CreateBadMessages))] public async Task Messages_with_errors(DocumentFormat format, Stream message, string expectedError) { - var result = await _marketMessageParser.ParseAsync(new IncomingMarketMessageStream(message), format, IncomingDocumentType.RequestWholesaleSettlement, CancellationToken.None); + var result = await _marketMessageParser.ParseAsync( + new IncomingMarketMessageStream(message), + format, + IncomingDocumentType.RequestWholesaleSettlement, + CancellationToken.None); result.Success.Should().BeFalse(); result.Errors.Should().NotBeEmpty();