Skip to content
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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +17,8 @@ namespace TransactionProcessor.BusinessLogic.Tests.DomainEventHandlers;

public class ReadModelDomainEventHandlerTests
{
private sealed record UnhandledDomainEvent() : DomainEvent(Guid.NewGuid(), Guid.NewGuid());

private readonly Mock<ITransactionProcessorReadModelRepository> EstateReportingRepository;
private readonly ReadModelDomainEventHandler DomainEventHandler;

Expand Down Expand Up @@ -64,4 +68,15 @@ public async Task ReadModelDomainEventHandler_Handle_StatementGeneratedEvent_Mar
this.EstateReportingRepository.Verify(r => r.MarkStatementAsGenerated(domainEvent, It.IsAny<CancellationToken>()), Times.Once);
this.EstateReportingRepository.Verify(r => r.UpdateMerchant(domainEvent, It.IsAny<CancellationToken>()), 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,26 @@ public ReadModelDomainEventHandler(ITransactionProcessorReadModelRepository esta
}

public Task<Result> Handle(IDomainEvent domainEvent,
CancellationToken cancellationToken)
CancellationToken cancellationToken) =>
this.TryHandleDomainEvent(domainEvent, cancellationToken, out Task<Result> handledTask)
? handledTask
: Task.FromResult(Result.Success());

private bool TryHandleDomainEvent(IDomainEvent domainEvent,
CancellationToken cancellationToken,
out Task<Result> handledTask)
{
foreach (Func<IDomainEvent, CancellationToken, Task<Result>> domainEventHandler in this.DomainEventHandlers)
{
Task<Result> 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<Result> HandleEstateDomainEvent(IDomainEvent domainEvent,
Expand Down
Loading