From 47211470b2da7e763d0f310ba187088d70448fe4 Mon Sep 17 00:00:00 2001 From: "Kristian F. Thomsen" Date: Tue, 28 Mar 2023 09:25:49 +0200 Subject: [PATCH 1/5] Rename to better reflect intent --- .../OutgoingMessages/DocumentFactory.cs | 2 +- .../OutgoingMessages/Peek/PeekRequestHandler.cs | 2 +- .../CimMessageAdapter/Messages/IMessageParser.cs | 2 +- .../MessageParser.cs | 6 +++--- .../MessageParser.cs | 6 +++--- .../RequestChangeOfSupplier/MessageParser.cs | 6 +++--- .../Response/IResponseFactory.cs | 2 +- .../CimMessageAdapter/Response/ResponseFactory.cs | 2 +- .../{MessageFormat.cs => DocumentFormat.cs} | 8 ++++---- source/Domain/OutgoingMessages/IMessageWriter.cs | 2 +- .../IncomingMessages/CimFormatParser.cs | 4 ++-- .../XmlMessageParser.cs | 3 ++- .../XmlMessageParser.cs | 3 ++- .../RequestChangeOfSupplier/JsonMessageParser.cs | 3 ++- .../RequestChangeOfSupplier/XmlMessageParser.cs | 3 ++- .../Response/JsonResponseFactory.cs | 2 +- .../Response/XmlResponseFactory.cs | 2 +- .../OutgoingMessages/Common/Xml/MessageWriter.cs | 4 ++-- .../ConfirmChangeOfSupplierJsonMessageWriter.cs | 4 ++-- ...ectRequestChangeOfSupplierJsonMessageWriter.cs | 4 ++-- .../RequestChangeCustomerCharacteristicsTests.cs | 2 +- .../RequestChangeOfSupplierReceiverTests.cs | 2 +- .../MessageParserTests.cs | 14 +++++++------- .../MessageParserTests.cs | 10 +++++----- .../RequestChangeOfSupplier/MessageParserTests.cs | 15 ++++++++------- .../Response/ResponseFactoryTests.cs | 4 ++-- .../IncomingMessages/CimFormatParserTests.cs | 12 ++++++------ 27 files changed, 67 insertions(+), 62 deletions(-) rename source/Domain/OutgoingMessages/{MessageFormat.cs => DocumentFormat.cs} (75%) diff --git a/source/Application/OutgoingMessages/DocumentFactory.cs b/source/Application/OutgoingMessages/DocumentFactory.cs index f133cb45df..76b4287476 100644 --- a/source/Application/OutgoingMessages/DocumentFactory.cs +++ b/source/Application/OutgoingMessages/DocumentFactory.cs @@ -31,7 +31,7 @@ public DocumentFactory(IEnumerable documentWriters) _documentWriters = documentWriters.ToList(); } - public Task CreateFromAsync(BundledMessageId bundledMessageId, MessageRecords messageRecords, MessageFormat documentFormat, Instant timestamp) + public Task CreateFromAsync(BundledMessageId bundledMessageId, MessageRecords messageRecords, DocumentFormat documentFormat, Instant timestamp) { ArgumentNullException.ThrowIfNull(bundledMessageId); ArgumentNullException.ThrowIfNull(messageRecords); diff --git a/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs b/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs index d1d1edd8d2..0aa62c26d4 100644 --- a/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs +++ b/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs @@ -71,7 +71,7 @@ public async Task Handle(PeekRequest request, CancellationToken canc private async Task CreateBundledMessageAsync(MessageRecords messageRecords) { var id = BundledMessageId.New(); - var document = await _documentFactory.CreateFromAsync(id, messageRecords, MessageFormat.Xml, _systemDateTimeProvider.Now()) + var document = await _documentFactory.CreateFromAsync(id, messageRecords, DocumentFormat.Xml, _systemDateTimeProvider.Now()) .ConfigureAwait(false); return BundledMessage.CreateFrom(id, messageRecords, document); } diff --git a/source/CimMessageAdapter/Messages/IMessageParser.cs b/source/CimMessageAdapter/Messages/IMessageParser.cs index e414e95177..27b49107fd 100644 --- a/source/CimMessageAdapter/Messages/IMessageParser.cs +++ b/source/CimMessageAdapter/Messages/IMessageParser.cs @@ -29,7 +29,7 @@ public interface IMessageParser /// The CIM format handled /// - MessageFormat HandledFormat { get; } + DocumentFormat HandledFormat { get; } /// /// Parse from stream diff --git a/source/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParser.cs b/source/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParser.cs index 58b5e69695..038a302b7a 100644 --- a/source/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParser.cs +++ b/source/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParser.cs @@ -31,10 +31,10 @@ public MessageParser(IEnumerable> ParseAsync(Stream message, MessageFormat messageFormat) + public Task> ParseAsync(Stream message, DocumentFormat documentFormat) { - var parser = _parsers.FirstOrDefault(parser => parser.HandledFormat.Equals(messageFormat)); - if (parser is null) throw new InvalidOperationException($"No message parser found for message format '{messageFormat}'"); + var parser = _parsers.FirstOrDefault(parser => parser.HandledFormat.Equals(documentFormat)); + if (parser is null) throw new InvalidOperationException($"No message parser found for message format '{documentFormat}'"); return parser.ParseAsync(message); } } diff --git a/source/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParser.cs b/source/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParser.cs index ae7ce2532b..9c3233fede 100644 --- a/source/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParser.cs +++ b/source/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParser.cs @@ -31,10 +31,10 @@ public MessageParser(IEnumerable> ParseAsync(Stream message, MessageFormat messageFormat) + public Task> ParseAsync(Stream message, DocumentFormat documentFormat) { - var parser = _parsers.FirstOrDefault(parser => parser.HandledFormat.Equals(messageFormat)); - if (parser is null) throw new InvalidOperationException($"No message parser found for message format '{messageFormat}'"); + var parser = _parsers.FirstOrDefault(parser => parser.HandledFormat.Equals(documentFormat)); + if (parser is null) throw new InvalidOperationException($"No message parser found for message format '{documentFormat}'"); return parser.ParseAsync(message); } } diff --git a/source/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParser.cs b/source/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParser.cs index 26bc7c6ab9..d316c205a3 100644 --- a/source/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParser.cs +++ b/source/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParser.cs @@ -31,10 +31,10 @@ public MessageParser(IEnumerable> ParseAsync(Stream message, MessageFormat messageFormat) + public Task> ParseAsync(Stream message, DocumentFormat documentFormat) { - var parser = _parsers.FirstOrDefault(parser => parser.HandledFormat.Equals(messageFormat)); - if (parser is null) throw new InvalidOperationException($"No message parser found for message format '{messageFormat}'"); + var parser = _parsers.FirstOrDefault(parser => parser.HandledFormat.Equals(documentFormat)); + if (parser is null) throw new InvalidOperationException($"No message parser found for message format '{documentFormat}'"); return parser.ParseAsync(message); } } diff --git a/source/CimMessageAdapter/Response/IResponseFactory.cs b/source/CimMessageAdapter/Response/IResponseFactory.cs index 6a0431cce6..72b15bba36 100644 --- a/source/CimMessageAdapter/Response/IResponseFactory.cs +++ b/source/CimMessageAdapter/Response/IResponseFactory.cs @@ -25,7 +25,7 @@ public interface IResponseFactory /// /// Specifies the handled CIM format /// - public MessageFormat HandledFormat { get; } + public DocumentFormat HandledFormat { get; } /// /// Create response message diff --git a/source/CimMessageAdapter/Response/ResponseFactory.cs b/source/CimMessageAdapter/Response/ResponseFactory.cs index 11b95c11cb..5b93514860 100644 --- a/source/CimMessageAdapter/Response/ResponseFactory.cs +++ b/source/CimMessageAdapter/Response/ResponseFactory.cs @@ -29,7 +29,7 @@ public ResponseFactory(IEnumerable factories) _factories = factories; } - public ResponseMessage From(Result result, MessageFormat format) + public ResponseMessage From(Result result, DocumentFormat format) { if (result == null) throw new ArgumentNullException(nameof(result)); if (format == null) throw new ArgumentNullException(nameof(format)); diff --git a/source/Domain/OutgoingMessages/MessageFormat.cs b/source/Domain/OutgoingMessages/DocumentFormat.cs similarity index 75% rename from source/Domain/OutgoingMessages/MessageFormat.cs rename to source/Domain/OutgoingMessages/DocumentFormat.cs index 1e8809c92e..9562ff7beb 100644 --- a/source/Domain/OutgoingMessages/MessageFormat.cs +++ b/source/Domain/OutgoingMessages/DocumentFormat.cs @@ -16,12 +16,12 @@ namespace Domain.OutgoingMessages; -public class MessageFormat : EnumerationType +public class DocumentFormat : EnumerationType { - public static readonly MessageFormat Xml = new(0, nameof(Xml)); - public static readonly MessageFormat Json = new(1, nameof(Json)); + public static readonly DocumentFormat Xml = new(0, nameof(Xml)); + public static readonly DocumentFormat Json = new(1, nameof(Json)); - private MessageFormat(int id, string name) + private DocumentFormat(int id, string name) : base(id, name) { } diff --git a/source/Domain/OutgoingMessages/IMessageWriter.cs b/source/Domain/OutgoingMessages/IMessageWriter.cs index 3205ccd1bb..c17c5a199a 100644 --- a/source/Domain/OutgoingMessages/IMessageWriter.cs +++ b/source/Domain/OutgoingMessages/IMessageWriter.cs @@ -23,7 +23,7 @@ public interface IMessageWriter /// Determine if specified format can be handled by message writer /// /// - bool HandlesFormat(MessageFormat format); + bool HandlesFormat(DocumentFormat format); /// /// Determine if specified message type can be handles by the writer diff --git a/source/Infrastructure/IncomingMessages/CimFormatParser.cs b/source/Infrastructure/IncomingMessages/CimFormatParser.cs index ba4878f636..c9dd3db906 100644 --- a/source/Infrastructure/IncomingMessages/CimFormatParser.cs +++ b/source/Infrastructure/IncomingMessages/CimFormatParser.cs @@ -21,13 +21,13 @@ namespace Infrastructure.IncomingMessages; public static class CimFormatParser { - public static MessageFormat? ParseFromContentTypeHeaderValue(string value) + public static DocumentFormat? ParseFromContentTypeHeaderValue(string value) { if (value == null) throw new ArgumentNullException(nameof(value)); var contentType = ParseContentTypeName(value); return EnumerationType.GetAll - () + () .FirstOrDefault(v => v.Name.Equals(contentType, StringComparison.OrdinalIgnoreCase)); } diff --git a/source/Infrastructure/IncomingMessages/RequestChangeAccountingPointCharacteristics/XmlMessageParser.cs b/source/Infrastructure/IncomingMessages/RequestChangeAccountingPointCharacteristics/XmlMessageParser.cs index a68b0131e9..079a087f3a 100644 --- a/source/Infrastructure/IncomingMessages/RequestChangeAccountingPointCharacteristics/XmlMessageParser.cs +++ b/source/Infrastructure/IncomingMessages/RequestChangeAccountingPointCharacteristics/XmlMessageParser.cs @@ -24,6 +24,7 @@ using DocumentValidation; using DocumentValidation.CimXml; using Domain.OutgoingMessages; +using DocumentFormat = Domain.OutgoingMessages.DocumentFormat; using MarketActivityRecord = Application.IncomingMessages.RequestChangeAccountPointCharacteristics.MarketActivityRecord; namespace Infrastructure.IncomingMessages.RequestChangeAccountingPointCharacteristics; @@ -40,7 +41,7 @@ public XmlMessageParser() _schemaProvider = new CimXmlSchemaProvider(); } - public MessageFormat HandledFormat => MessageFormat.Xml; + public DocumentFormat HandledFormat => DocumentFormat.Xml; public async Task> ParseAsync(Stream message) diff --git a/source/Infrastructure/IncomingMessages/RequestChangeCustomerCharacteristics/XmlMessageParser.cs b/source/Infrastructure/IncomingMessages/RequestChangeCustomerCharacteristics/XmlMessageParser.cs index a6193e3a70..d833dbcb63 100644 --- a/source/Infrastructure/IncomingMessages/RequestChangeCustomerCharacteristics/XmlMessageParser.cs +++ b/source/Infrastructure/IncomingMessages/RequestChangeCustomerCharacteristics/XmlMessageParser.cs @@ -24,6 +24,7 @@ using DocumentValidation; using DocumentValidation.CimXml; using Domain.OutgoingMessages; +using DocumentFormat = Domain.OutgoingMessages.DocumentFormat; using MarketActivityRecord = Application.IncomingMessages.RequestChangeCustomerCharacteristics.MarketActivityRecord; namespace Infrastructure.IncomingMessages.RequestChangeCustomerCharacteristics; @@ -40,7 +41,7 @@ public XmlMessageParser() _schemaProvider = new CimXmlSchemaProvider(); } - public MessageFormat HandledFormat => MessageFormat.Xml; + public DocumentFormat HandledFormat => DocumentFormat.Xml; public async Task> ParseAsync(Stream message) { diff --git a/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs b/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs index 64e5858610..c03ddaf3ab 100644 --- a/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs +++ b/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/JsonMessageParser.cs @@ -27,6 +27,7 @@ using DocumentValidation; using Domain.OutgoingMessages; using Json.Schema; +using DocumentFormat = Domain.OutgoingMessages.DocumentFormat; using MessageHeader = Application.IncomingMessages.MessageHeader; namespace Infrastructure.IncomingMessages.RequestChangeOfSupplier; @@ -44,7 +45,7 @@ public JsonMessageParser(JsonSchemaProvider schemaProvider) _schemaProvider = schemaProvider; } - public MessageFormat HandledFormat => MessageFormat.Json; + public DocumentFormat HandledFormat => DocumentFormat.Json; public async Task> ParseAsync(Stream message) { diff --git a/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/XmlMessageParser.cs b/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/XmlMessageParser.cs index d969c8ac5a..1f41598d5d 100644 --- a/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/XmlMessageParser.cs +++ b/source/Infrastructure/IncomingMessages/RequestChangeOfSupplier/XmlMessageParser.cs @@ -24,6 +24,7 @@ using DocumentValidation; using DocumentValidation.CimXml; using Domain.OutgoingMessages; +using DocumentFormat = Domain.OutgoingMessages.DocumentFormat; namespace Infrastructure.IncomingMessages.RequestChangeOfSupplier; @@ -39,7 +40,7 @@ public XmlMessageParser() _schemaProvider = new CimXmlSchemaProvider(); } - public MessageFormat HandledFormat => MessageFormat.Xml; + public DocumentFormat HandledFormat => DocumentFormat.Xml; public async Task> ParseAsync(Stream message) { diff --git a/source/Infrastructure/IncomingMessages/Response/JsonResponseFactory.cs b/source/Infrastructure/IncomingMessages/Response/JsonResponseFactory.cs index 12e3fa3845..8272dbbb55 100644 --- a/source/Infrastructure/IncomingMessages/Response/JsonResponseFactory.cs +++ b/source/Infrastructure/IncomingMessages/Response/JsonResponseFactory.cs @@ -25,7 +25,7 @@ namespace Infrastructure.IncomingMessages.Response; public class JsonResponseFactory : IResponseFactory { - public MessageFormat HandledFormat => MessageFormat.Json; + public DocumentFormat HandledFormat => DocumentFormat.Json; public ResponseMessage From(Result result) { diff --git a/source/Infrastructure/IncomingMessages/Response/XmlResponseFactory.cs b/source/Infrastructure/IncomingMessages/Response/XmlResponseFactory.cs index 62b710f5a0..28d5219ba7 100644 --- a/source/Infrastructure/IncomingMessages/Response/XmlResponseFactory.cs +++ b/source/Infrastructure/IncomingMessages/Response/XmlResponseFactory.cs @@ -24,7 +24,7 @@ namespace Infrastructure.IncomingMessages.Response { public class XmlResponseFactory : IResponseFactory { - public MessageFormat HandledFormat => MessageFormat.Xml; + public DocumentFormat HandledFormat => DocumentFormat.Xml; public ResponseMessage From(Result result) { diff --git a/source/Infrastructure/OutgoingMessages/Common/Xml/MessageWriter.cs b/source/Infrastructure/OutgoingMessages/Common/Xml/MessageWriter.cs index 4f5e72a81f..bd765735ef 100644 --- a/source/Infrastructure/OutgoingMessages/Common/Xml/MessageWriter.cs +++ b/source/Infrastructure/OutgoingMessages/Common/Xml/MessageWriter.cs @@ -57,9 +57,9 @@ public bool HandlesType(MessageType messageType) return messageType.Name.Equals(_documentDetails.Type.Split("_")[0], StringComparison.OrdinalIgnoreCase); } - public bool HandlesFormat(MessageFormat format) + public bool HandlesFormat(DocumentFormat format) { - return format == MessageFormat.Xml; + return format == DocumentFormat.Xml; } protected abstract Task WriteMarketActivityRecordsAsync(IReadOnlyCollection marketActivityPayloads, XmlWriter writer); diff --git a/source/Infrastructure/OutgoingMessages/ConfirmRequestChangeOfSupplier/ConfirmChangeOfSupplierJsonMessageWriter.cs b/source/Infrastructure/OutgoingMessages/ConfirmRequestChangeOfSupplier/ConfirmChangeOfSupplierJsonMessageWriter.cs index 7efe8bf0d5..1c3a589746 100644 --- a/source/Infrastructure/OutgoingMessages/ConfirmRequestChangeOfSupplier/ConfirmChangeOfSupplierJsonMessageWriter.cs +++ b/source/Infrastructure/OutgoingMessages/ConfirmRequestChangeOfSupplier/ConfirmChangeOfSupplierJsonMessageWriter.cs @@ -35,9 +35,9 @@ public ConfirmChangeOfSupplierJsonMessageWriter(IMessageRecordParser parser) _parser = parser; } - public bool HandlesFormat(MessageFormat format) + public bool HandlesFormat(DocumentFormat format) { - return format == MessageFormat.Json; + return format == DocumentFormat.Json; } public bool HandlesType(MessageType messageType) diff --git a/source/Infrastructure/OutgoingMessages/RejectRequestChangeOfSupplier/RejectRequestChangeOfSupplierJsonMessageWriter.cs b/source/Infrastructure/OutgoingMessages/RejectRequestChangeOfSupplier/RejectRequestChangeOfSupplierJsonMessageWriter.cs index 468e9703f1..0551eaf835 100644 --- a/source/Infrastructure/OutgoingMessages/RejectRequestChangeOfSupplier/RejectRequestChangeOfSupplierJsonMessageWriter.cs +++ b/source/Infrastructure/OutgoingMessages/RejectRequestChangeOfSupplier/RejectRequestChangeOfSupplierJsonMessageWriter.cs @@ -35,9 +35,9 @@ public RejectRequestChangeOfSupplierJsonMessageWriter(IMessageRecordParser parse _parser = parser; } - public bool HandlesFormat(MessageFormat format) + public bool HandlesFormat(DocumentFormat format) { - return format == MessageFormat.Json; + return format == DocumentFormat.Json; } public bool HandlesType(MessageType messageType) diff --git a/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/RequestChangeCustomerCharacteristicsTests.cs b/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/RequestChangeCustomerCharacteristicsTests.cs index 9f504bf7cc..c542f82877 100644 --- a/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/RequestChangeCustomerCharacteristicsTests.cs +++ b/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/RequestChangeCustomerCharacteristicsTests.cs @@ -223,7 +223,7 @@ private async Task ReceiveRequestChangeCustomerCharacteristicsMessage(St private Task> ParseMessageAsync(Stream message) { - return _messageParser.ParseAsync(message, MessageFormat.Xml); + return _messageParser.ParseAsync(message, DocumentFormat.Xml); } private MessageReceiver CreateMessageReceiver() diff --git a/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeOfSupplier/RequestChangeOfSupplierReceiverTests.cs b/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeOfSupplier/RequestChangeOfSupplierReceiverTests.cs index b402eee7ff..1061b50391 100644 --- a/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeOfSupplier/RequestChangeOfSupplierReceiverTests.cs +++ b/source/IntegrationTests/CimMessageAdapter/Messages/RequestChangeOfSupplier/RequestChangeOfSupplierReceiverTests.cs @@ -237,7 +237,7 @@ await CreateMessageReceiver(messageIds).ReceiveAsync(await ParseMessageAsync(dup private Task> ParseMessageAsync(Stream message) { - return _messageParser.ParseAsync(message, MessageFormat.Xml); + return _messageParser.ParseAsync(message, DocumentFormat.Xml); } private ClaimsPrincipal CreateIdentity() diff --git a/source/Tests/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParserTests.cs b/source/Tests/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParserTests.cs index fe635a0f05..516fdfff9a 100644 --- a/source/Tests/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParserTests.cs +++ b/source/Tests/CimMessageAdapter/Messages/RequestChangeAccountingPointCharacteristics/MessageParserTests.cs @@ -47,7 +47,7 @@ public static IEnumerable CreateMessages() { return new List { - new object[] { MessageFormat.Xml, CreateXmlMessage() }, + new object[] { DocumentFormat.Xml, CreateXmlMessage() }, }; } @@ -55,7 +55,7 @@ public static IEnumerable CreateMessagesWithMultipleRecords() { return new List { - new object[] { MessageFormat.Xml, CreateXmlMessageWithMultipleRecords() }, + new object[] { DocumentFormat.Xml, CreateXmlMessageWithMultipleRecords() }, }; } @@ -63,13 +63,13 @@ public static IEnumerable CreateMessagesWithInvalidStructure() { return new List { - new object[] { MessageFormat.Xml, CreateInvalidXmlMessage() }, + new object[] { DocumentFormat.Xml, CreateInvalidXmlMessage() }, }; } [Theory] [MemberData(nameof(CreateMessages))] - public async Task Can_parse(MessageFormat format, Stream message) + public async Task Can_parse(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -80,7 +80,7 @@ public async Task Can_parse(MessageFormat format, Stream message) [Theory] [MemberData(nameof(CreateMessagesWithMultipleRecords))] - public async Task Can_parse_multiple_records(MessageFormat format, Stream message) + public async Task Can_parse_multiple_records(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -96,7 +96,7 @@ public async Task Can_parse_multiple_records(MessageFormat format, Stream messag [Theory] [MemberData(nameof(CreateMessagesWithInvalidStructure))] - public async Task Return_error_when_structure_is_invalid(MessageFormat format, Stream message) + public async Task Return_error_when_structure_is_invalid(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -109,7 +109,7 @@ public async Task Throw_if_message_format_is_not_known() { var parser = new MessageParser(new List>()); - await Assert.ThrowsAsync(() => parser.ParseAsync(CreateXmlMessage(), MessageFormat.Xml)).ConfigureAwait(false); + await Assert.ThrowsAsync(() => parser.ParseAsync(CreateXmlMessage(), DocumentFormat.Xml)).ConfigureAwait(false); } private static Stream CreateXmlMessage() diff --git a/source/Tests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParserTests.cs b/source/Tests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParserTests.cs index 6cecb3a0c3..7581fc63ca 100644 --- a/source/Tests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParserTests.cs +++ b/source/Tests/CimMessageAdapter/Messages/RequestChangeCustomerCharacteristics/MessageParserTests.cs @@ -49,7 +49,7 @@ public static IEnumerable CreateMessages() { return new List { - new object[] { MessageFormat.Xml, CreateXmlMessage() }, + new object[] { DocumentFormat.Xml, CreateXmlMessage() }, }; } @@ -57,13 +57,13 @@ public static IEnumerable CreateMessagesWithInvalidStructure() { return new List { - new object[] { MessageFormat.Xml, CreateInvalidXmlMessage() }, + new object[] { DocumentFormat.Xml, CreateInvalidXmlMessage() }, }; } [Theory] [MemberData(nameof(CreateMessages))] - public async Task Can_parse(MessageFormat format, Stream message) + public async Task Can_parse(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -74,7 +74,7 @@ public async Task Can_parse(MessageFormat format, Stream message) [Theory] [MemberData(nameof(CreateMessagesWithInvalidStructure))] - public async Task Return_error_when_structure_is_invalid(MessageFormat format, Stream message) + public async Task Return_error_when_structure_is_invalid(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -87,7 +87,7 @@ public async Task Throw_if_message_format_is_not_known() { var parser = new MessageParser(new List>()); - await Assert.ThrowsAsync(() => parser.ParseAsync(CreateXmlMessage(), MessageFormat.Xml)).ConfigureAwait(false); + await Assert.ThrowsAsync(() => parser.ParseAsync(CreateXmlMessage(), DocumentFormat.Xml)).ConfigureAwait(false); } private static Stream CreateXmlMessage() diff --git a/source/Tests/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParserTests.cs b/source/Tests/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParserTests.cs index b15a6fc30c..9dc99caf2f 100644 --- a/source/Tests/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParserTests.cs +++ b/source/Tests/CimMessageAdapter/Messages/RequestChangeOfSupplier/MessageParserTests.cs @@ -27,6 +27,7 @@ using Domain.OutgoingMessages; using Infrastructure.IncomingMessages.RequestChangeOfSupplier; using Xunit; +using DocumentFormat = Domain.OutgoingMessages.DocumentFormat; namespace Tests.CimMessageAdapter.Messages.RequestChangeOfSupplier; @@ -48,8 +49,8 @@ public static IEnumerable CreateMessages() { return new List { - new object[] { MessageFormat.Xml, CreateXmlMessage() }, - new object[] { MessageFormat.Json, CreateJsonMessage() }, + new object[] { DocumentFormat.Xml, CreateXmlMessage() }, + new object[] { DocumentFormat.Json, CreateJsonMessage() }, }; } @@ -57,14 +58,14 @@ public static IEnumerable CreateMessagesWithInvalidStructure() { return new List { - new object[] { MessageFormat.Json, CreateInvalidJsonMessage() }, - new object[] { MessageFormat.Xml, CreateInvalidXmlMessage() }, + new object[] { DocumentFormat.Json, CreateInvalidJsonMessage() }, + new object[] { DocumentFormat.Xml, CreateInvalidXmlMessage() }, }; } [Theory] [MemberData(nameof(CreateMessages))] - public async Task Can_parse(MessageFormat format, Stream message) + public async Task Can_parse(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -90,7 +91,7 @@ public async Task Can_parse(MessageFormat format, Stream message) [Theory] [MemberData(nameof(CreateMessagesWithInvalidStructure))] - public async Task Return_error_when_structure_is_invalid(MessageFormat format, Stream message) + public async Task Return_error_when_structure_is_invalid(DocumentFormat format, Stream message) { var result = await _messageParser.ParseAsync(message, format).ConfigureAwait(false); @@ -103,7 +104,7 @@ public async Task Throw_if_message_format_is_not_known() { var parser = new MessageParser(new List>()); - await Assert.ThrowsAsync(() => parser.ParseAsync(CreateXmlMessage(), MessageFormat.Xml)).ConfigureAwait(false); + await Assert.ThrowsAsync(() => parser.ParseAsync(CreateXmlMessage(), DocumentFormat.Xml)).ConfigureAwait(false); } private static Stream CreateXmlMessage() diff --git a/source/Tests/CimMessageAdapter/Response/ResponseFactoryTests.cs b/source/Tests/CimMessageAdapter/Response/ResponseFactoryTests.cs index c017f1f908..861a8701f5 100644 --- a/source/Tests/CimMessageAdapter/Response/ResponseFactoryTests.cs +++ b/source/Tests/CimMessageAdapter/Response/ResponseFactoryTests.cs @@ -32,7 +32,7 @@ public void Generate_empty_response_when_no_validation_errors_has_occurred() var responseFactory = new ResponseFactory(new[] { new XmlResponseFactory() }); var result = Result.Succeeded(); - var response = responseFactory.From(result, MessageFormat.Xml); + var response = responseFactory.From(result, DocumentFormat.Xml); Assert.False(response.IsErrorResponse); Assert.Empty(response.MessageBody); @@ -44,6 +44,6 @@ public void Throw_if_requested_format_can_not_be_parsed() var responseFactory = new ResponseFactory(new List()); var result = Result.Succeeded(); - Assert.Throws(() => responseFactory.From(result, MessageFormat.Json)); + Assert.Throws(() => responseFactory.From(result, DocumentFormat.Json)); } } diff --git a/source/Tests/Infrastructure/IncomingMessages/CimFormatParserTests.cs b/source/Tests/Infrastructure/IncomingMessages/CimFormatParserTests.cs index 39287063b0..717bdcf54a 100644 --- a/source/Tests/Infrastructure/IncomingMessages/CimFormatParserTests.cs +++ b/source/Tests/Infrastructure/IncomingMessages/CimFormatParserTests.cs @@ -22,14 +22,14 @@ namespace Tests.Infrastructure.IncomingMessages; public class CimFormatParserTests { [Theory] - [InlineData("application/json", nameof(MessageFormat.Json))] - [InlineData("application/json; charset=utf-8", nameof(MessageFormat.Json))] - [InlineData("application/xml; charset=utf-8", nameof(MessageFormat.Xml))] - [InlineData("application/xml", nameof(MessageFormat.Xml))] - [InlineData("application/xml ", nameof(MessageFormat.Xml))] + [InlineData("application/json", nameof(DocumentFormat.Json))] + [InlineData("application/json; charset=utf-8", nameof(DocumentFormat.Json))] + [InlineData("application/xml; charset=utf-8", nameof(DocumentFormat.Xml))] + [InlineData("application/xml", nameof(DocumentFormat.Xml))] + [InlineData("application/xml ", nameof(DocumentFormat.Xml))] public void Can_parse_from_known_content_header_value(string contentHeaderValue, string expectedCimFormat) { - var expectedFormat = EnumerationType.FromName(expectedCimFormat); + var expectedFormat = EnumerationType.FromName(expectedCimFormat); var parsedFormat = CimFormatParser.ParseFromContentTypeHeaderValue(contentHeaderValue); Assert.Equal(expectedFormat, parsedFormat); From 9a3fea53f5318a53aa6a9f14b45a0bde928cb460 Mon Sep 17 00:00:00 2001 From: "Kristian F. Thomsen" Date: Tue, 28 Mar 2023 09:41:54 +0200 Subject: [PATCH 2/5] Add desired ducument type to request --- .../OutgoingMessages/PeekRequestListener.cs | 29 +++++++++++++++++-- .../OutgoingMessages/Peek/MessagePeeker.cs | 5 ++-- .../Peek/PeekRequestHandler.cs | 2 +- .../WhenADequeueIsRequestedTests.cs | 3 +- .../WhenAPeekIsRequestedTests.cs | 7 +---- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/source/Api/OutgoingMessages/PeekRequestListener.cs b/source/Api/OutgoingMessages/PeekRequestListener.cs index d93d008e59..1598c72b44 100644 --- a/source/Api/OutgoingMessages/PeekRequestListener.cs +++ b/source/Api/OutgoingMessages/PeekRequestListener.cs @@ -12,14 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; +using System.Linq; using System.Net; +using System.Net.Http.Headers; using System.Threading.Tasks; using Application.Configuration.Authentication; using Application.OutgoingMessages.Peek; using Domain.OutgoingMessages.Peek; using Domain.SeedWork; +using Infrastructure.IncomingMessages; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http; +using Microsoft.Extensions.Logging; namespace Api.OutgoingMessages; @@ -27,11 +32,13 @@ public class PeekRequestListener { private readonly MessagePeeker _messagePeeker; private readonly IMarketActorAuthenticator _authenticator; + private readonly ILogger _logger; - public PeekRequestListener(MessagePeeker messagePeeker, IMarketActorAuthenticator authenticator) + public PeekRequestListener(MessagePeeker messagePeeker, IMarketActorAuthenticator authenticator, ILogger logger) { _messagePeeker = messagePeeker; _authenticator = authenticator; + _logger = logger; } [Function("PeekRequestListener")] @@ -44,9 +51,20 @@ public async Task RunAsync( FunctionContext executionContext, string messageCategory) { + ArgumentNullException.ThrowIfNull(request); + + var contentType = GetContentType(request.Headers); + var desiredDocumentFormat = CimFormatParser.ParseFromContentTypeHeaderValue(contentType); + if (desiredDocumentFormat is null) + { + _logger.LogInformation($"Could not parse desired CIM format from Content-Type header value: {contentType}"); + return request.CreateResponse(HttpStatusCode.UnsupportedMediaType); + } + var result = await _messagePeeker.PeekAsync( _authenticator.CurrentIdentity.Number, - EnumerationType.FromName(messageCategory)) + EnumerationType.FromName(messageCategory), + desiredDocumentFormat) .ConfigureAwait(false); var response = HttpResponseData.CreateResponse(request); @@ -68,4 +86,11 @@ public async Task RunAsync( response.StatusCode = HttpStatusCode.OK; return response; } + + private static string GetContentType(HttpHeaders headers) + { + var contentHeader = headers.GetValues("Content-Type").FirstOrDefault(); + if (contentHeader == null) throw new InvalidOperationException("No Content-Type found in request headers"); + return contentHeader; + } } diff --git a/source/Application/OutgoingMessages/Peek/MessagePeeker.cs b/source/Application/OutgoingMessages/Peek/MessagePeeker.cs index df4c837b7a..3434ec4475 100644 --- a/source/Application/OutgoingMessages/Peek/MessagePeeker.cs +++ b/source/Application/OutgoingMessages/Peek/MessagePeeker.cs @@ -14,6 +14,7 @@ using System.Threading.Tasks; using Domain.Actors; +using Domain.OutgoingMessages; using Domain.OutgoingMessages.Peek; using MediatR; using Microsoft.EntityFrameworkCore; @@ -29,11 +30,11 @@ public MessagePeeker(IMediator mediator) _mediator = mediator; } - public async Task PeekAsync(ActorNumber actorNumber, MessageCategory messageCategory) + public async Task PeekAsync(ActorNumber actorNumber, MessageCategory messageCategory, DocumentFormat desiredDocumentFormat) { try { - return await _mediator.Send(new PeekRequest(actorNumber, messageCategory)).ConfigureAwait(false); + return await _mediator.Send(new PeekRequest(actorNumber, messageCategory, desiredDocumentFormat)).ConfigureAwait(false); } catch (DbUpdateException) { diff --git a/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs b/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs index 0aa62c26d4..9cd05bf3cd 100644 --- a/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs +++ b/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs @@ -77,6 +77,6 @@ private async Task CreateBundledMessageAsync(MessageRecords mess } } -public record PeekRequest(ActorNumber ActorNumber, MessageCategory MessageCategory) : ICommand; +public record PeekRequest(ActorNumber ActorNumber, MessageCategory MessageCategory, DocumentFormat DesiredDocumentFormat) : ICommand; public record PeekResult(Stream? Bundle, Guid? MessageId = default); diff --git a/source/IntegrationTests/Application/OutgoingMessages/WhenADequeueIsRequestedTests.cs b/source/IntegrationTests/Application/OutgoingMessages/WhenADequeueIsRequestedTests.cs index 4baf6af158..64c03593c8 100644 --- a/source/IntegrationTests/Application/OutgoingMessages/WhenADequeueIsRequestedTests.cs +++ b/source/IntegrationTests/Application/OutgoingMessages/WhenADequeueIsRequestedTests.cs @@ -48,7 +48,8 @@ public async Task Dequeue_is_Successful() await GivenAMoveInTransactionHasBeenAccepted().ConfigureAwait(false); var peekResult = await InvokeCommandAsync(new PeekRequest( ActorNumber.Create(SampleData.NewEnergySupplierNumber), - MessageCategory.MasterData)).ConfigureAwait(false); + MessageCategory.MasterData, + DocumentFormat.Xml)).ConfigureAwait(false); var dequeueResult = await InvokeCommandAsync(new DequeueRequest(peekResult.MessageId.GetValueOrDefault().ToString())).ConfigureAwait(false); diff --git a/source/IntegrationTests/Application/OutgoingMessages/WhenAPeekIsRequestedTests.cs b/source/IntegrationTests/Application/OutgoingMessages/WhenAPeekIsRequestedTests.cs index 175d1d5cae..7daa0997cb 100644 --- a/source/IntegrationTests/Application/OutgoingMessages/WhenAPeekIsRequestedTests.cs +++ b/source/IntegrationTests/Application/OutgoingMessages/WhenAPeekIsRequestedTests.cs @@ -109,11 +109,6 @@ public async Task Return_empty_bundle_if_bundle_is_already_registered() Assert.Null(peekResult.Bundle); } - private static PeekRequest CreatePeekRequest(MessageCategory messageCategory) - { - return new PeekRequest(ActorNumber.Create(SampleData.NewEnergySupplierNumber), messageCategory); - } - private static IncomingMessageBuilder MessageBuilder() { return new IncomingMessageBuilder() @@ -188,6 +183,6 @@ private async Task BundleIsRegistered() private Task PeekMessage(MessageCategory category) { - return _messagePeeker.PeekAsync(ActorNumber.Create(SampleData.NewEnergySupplierNumber), category); + return _messagePeeker.PeekAsync(ActorNumber.Create(SampleData.NewEnergySupplierNumber), category, DocumentFormat.Xml); } } From ccfbd2e4db437eab9780667a57b554fe0c55b0d4 Mon Sep 17 00:00:00 2001 From: "Kristian F. Thomsen" Date: Tue, 28 Mar 2023 09:46:16 +0200 Subject: [PATCH 3/5] Extract class --- source/Api/Common/HttpHeadersExtensions.cs | 29 +++++++++++++++++++ .../OutgoingMessages/PeekRequestListener.cs | 10 ++----- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 source/Api/Common/HttpHeadersExtensions.cs diff --git a/source/Api/Common/HttpHeadersExtensions.cs b/source/Api/Common/HttpHeadersExtensions.cs new file mode 100644 index 0000000000..93f6b2ba8d --- /dev/null +++ b/source/Api/Common/HttpHeadersExtensions.cs @@ -0,0 +1,29 @@ +// 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; +using System.Linq; +using System.Net.Http.Headers; + +namespace Api.Common; + +internal static class HttpHeadersExtensions +{ + internal static string GetContentType(this HttpHeaders headers) + { + var contentHeader = headers.GetValues("Content-Type").FirstOrDefault(); + if (contentHeader == null) throw new InvalidOperationException("No Content-Type found in request headers"); + return contentHeader; + } +} diff --git a/source/Api/OutgoingMessages/PeekRequestListener.cs b/source/Api/OutgoingMessages/PeekRequestListener.cs index 1598c72b44..92b2c52ed3 100644 --- a/source/Api/OutgoingMessages/PeekRequestListener.cs +++ b/source/Api/OutgoingMessages/PeekRequestListener.cs @@ -17,6 +17,7 @@ using System.Net; using System.Net.Http.Headers; using System.Threading.Tasks; +using Api.Common; using Application.Configuration.Authentication; using Application.OutgoingMessages.Peek; using Domain.OutgoingMessages.Peek; @@ -53,7 +54,7 @@ public async Task RunAsync( { ArgumentNullException.ThrowIfNull(request); - var contentType = GetContentType(request.Headers); + var contentType = request.Headers.GetContentType(); var desiredDocumentFormat = CimFormatParser.ParseFromContentTypeHeaderValue(contentType); if (desiredDocumentFormat is null) { @@ -86,11 +87,4 @@ public async Task RunAsync( response.StatusCode = HttpStatusCode.OK; return response; } - - private static string GetContentType(HttpHeaders headers) - { - var contentHeader = headers.GetValues("Content-Type").FirstOrDefault(); - if (contentHeader == null) throw new InvalidOperationException("No Content-Type found in request headers"); - return contentHeader; - } } From 9a76baf6149887171596fb681452e5caaf3258d4 Mon Sep 17 00:00:00 2001 From: "Kristian F. Thomsen" Date: Tue, 28 Mar 2023 09:46:53 +0200 Subject: [PATCH 4/5] Use helper --- .../RequestChangeOfSupplierMessageReceiver.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/source/Api/IncomingMessages/RequestChangeOfSupplierMessageReceiver.cs b/source/Api/IncomingMessages/RequestChangeOfSupplierMessageReceiver.cs index e81a9a18d2..56c9cb8976 100644 --- a/source/Api/IncomingMessages/RequestChangeOfSupplierMessageReceiver.cs +++ b/source/Api/IncomingMessages/RequestChangeOfSupplierMessageReceiver.cs @@ -13,11 +13,10 @@ // limitations under the License. using System; -using System.Linq; using System.Net; -using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; +using Api.Common; using Application.Configuration; using CimMessageAdapter.Messages.RequestChangeOfSupplier; using CimMessageAdapter.Response; @@ -59,7 +58,7 @@ public async Task RunAsync( if (request == null) throw new ArgumentNullException(nameof(request)); - var contentType = GetContentType(request.Headers); + var contentType = request.Headers.GetContentType(); var cimFormat = CimFormatParser.ParseFromContentTypeHeaderValue(contentType); if (cimFormat is null) { @@ -75,13 +74,6 @@ public async Task RunAsync( return CreateResponse(request, httpStatusCode, _responseFactory.From(result, cimFormat)); } - private static string GetContentType(HttpHeaders headers) - { - var contentHeader = headers.GetValues("Content-Type").FirstOrDefault(); - if (contentHeader == null) throw new InvalidOperationException("No Content-Type found in request headers"); - return contentHeader; - } - private HttpResponseData CreateResponse(HttpRequestData request, HttpStatusCode statusCode, ResponseMessage responseMessage) { var response = request.CreateResponse(statusCode); From d4547eec36538167cb498c7a40420d405789e738 Mon Sep 17 00:00:00 2001 From: "Kristian F. Thomsen" Date: Tue, 28 Mar 2023 09:49:07 +0200 Subject: [PATCH 5/5] Parse document format from request --- .../Application/OutgoingMessages/Peek/PeekRequestHandler.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs b/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs index 9cd05bf3cd..33cf5bb9f7 100644 --- a/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs +++ b/source/Application/OutgoingMessages/Peek/PeekRequestHandler.cs @@ -62,16 +62,16 @@ public async Task Handle(PeekRequest request, CancellationToken canc return new PeekResult(null); } - bundledMessage = await CreateBundledMessageAsync(messageRecords).ConfigureAwait(false); + bundledMessage = await CreateBundledMessageAsync(messageRecords, request.DesiredDocumentFormat).ConfigureAwait(false); await _bundledMessages.AddAsync(bundledMessage).ConfigureAwait(false); return new PeekResult(bundledMessage.GeneratedDocument, bundledMessage.Id.Value); } - private async Task CreateBundledMessageAsync(MessageRecords messageRecords) + private async Task CreateBundledMessageAsync(MessageRecords messageRecords, DocumentFormat desiredDocumentFormat) { var id = BundledMessageId.New(); - var document = await _documentFactory.CreateFromAsync(id, messageRecords, DocumentFormat.Xml, _systemDateTimeProvider.Now()) + var document = await _documentFactory.CreateFromAsync(id, messageRecords, desiredDocumentFormat, _systemDateTimeProvider.Now()) .ConfigureAwait(false); return BundledMessage.CreateFrom(id, messageRecords, document); }