diff --git a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/ReadModelDomainEventHandlerTests.cs b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/ReadModelDomainEventHandlerTests.cs index 361f7e90..c4e7c0e7 100644 --- a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/ReadModelDomainEventHandlerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/ReadModelDomainEventHandlerTests.cs @@ -1,7 +1,9 @@ +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Moq; +using Shared.DomainDrivenDesign.EventSourcing; using Shared.Logger; using Shouldly; using SimpleResults; @@ -15,6 +17,8 @@ namespace TransactionProcessor.BusinessLogic.Tests.DomainEventHandlers; public class ReadModelDomainEventHandlerTests { + private sealed record UnhandledDomainEvent() : DomainEvent(Guid.NewGuid(), Guid.NewGuid()); + private readonly Mock EstateReportingRepository; private readonly ReadModelDomainEventHandler DomainEventHandler; @@ -64,4 +68,15 @@ public async Task ReadModelDomainEventHandler_Handle_StatementGeneratedEvent_Mar this.EstateReportingRepository.Verify(r => r.MarkStatementAsGenerated(domainEvent, It.IsAny()), Times.Once); this.EstateReportingRepository.Verify(r => r.UpdateMerchant(domainEvent, It.IsAny()), Times.Never); } + + [Fact] + public async Task ReadModelDomainEventHandler_Handle_UnhandledDomainEvent_ReturnsSuccessWithoutRepositoryCalls() + { + UnhandledDomainEvent domainEvent = new(); + + Result result = await this.DomainEventHandler.Handle(domainEvent, CancellationToken.None); + + result.IsSuccess.ShouldBeTrue(); + this.EstateReportingRepository.VerifyNoOtherCalls(); + } } diff --git a/TransactionProcessor.BusinessLogic/EventHandling/ReadModelDomainEventHandler.cs b/TransactionProcessor.BusinessLogic/EventHandling/ReadModelDomainEventHandler.cs index 56b894f4..73e9c791 100644 --- a/TransactionProcessor.BusinessLogic/EventHandling/ReadModelDomainEventHandler.cs +++ b/TransactionProcessor.BusinessLogic/EventHandling/ReadModelDomainEventHandler.cs @@ -33,18 +33,26 @@ public ReadModelDomainEventHandler(ITransactionProcessorReadModelRepository esta } public Task Handle(IDomainEvent domainEvent, - CancellationToken cancellationToken) + CancellationToken cancellationToken) => + this.TryHandleDomainEvent(domainEvent, cancellationToken, out Task handledTask) + ? handledTask + : Task.FromResult(Result.Success()); + + private bool TryHandleDomainEvent(IDomainEvent domainEvent, + CancellationToken cancellationToken, + out Task handledTask) { foreach (Func> domainEventHandler in this.DomainEventHandlers) { - Task task = domainEventHandler(domainEvent, cancellationToken); - if (task != null) + handledTask = domainEventHandler(domainEvent, cancellationToken); + if (handledTask != null) { - return task; + return true; } } - return Task.FromResult(Result.Success()); + handledTask = null; + return false; } private Task HandleEstateDomainEvent(IDomainEvent domainEvent,