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
16 changes: 16 additions & 0 deletions CallbackHander.Testing/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public class TestData
TestData.TypeString,
TestData.Reference);

public static CallbackCommands.RecordCallbackCommand RecordCallbackCommandEmptyReference =>
new(TestData.CallbackId,
TestData.CallbackMessage,
TestData.Destinations,
(MessageFormat)TestData.MessageFormat,
TestData.TypeString,
String.Empty);

public static CallbackCommands.RecordCallbackCommand RecordCallbackCommandInvalidReference =>
new(TestData.CallbackId,
TestData.CallbackMessage,
TestData.Destinations,
(MessageFormat)TestData.MessageFormat,
TestData.TypeString,
"reference");

public static CallbackQueries.GetCallbackQuery GetCallbackQuery =>
new CallbackQueries.GetCallbackQuery(TestData.CallbackId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleResults;

namespace CallbackHandler.BusinessLogic.Tests.Services;

Expand All @@ -25,15 +26,27 @@ public class CallbackDomainServiceTests
public CallbackDomainServiceTests() {
this.AggregateRepository = new Mock<IAggregateRepository<CallbackMessageAggregate, DomainEvent>>();
this.DomainService = new CallbackDomainService(this.AggregateRepository.Object);

this.AggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.EmptyCallbackMessageAggregate());
this.AggregateRepository.Setup(a => a.SaveChanges(It.IsAny<CallbackMessageAggregate>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success);
}

[Fact]
public async Task CallbackDomainService_RecordCallback_CallbackRecorded() {
this.AggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.EmptyCallbackMessageAggregate());
Result result = await this.DomainService.RecordCallback(TestData.RecordCallbackCommand, CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
}

Should.NotThrow(async () => await this.DomainService.RecordCallback(TestData.RecordCallbackCommand,
CancellationToken.None));
[Fact]
public async Task CallbackDomainService_RecordCallback_EmptyReference_ResultFailed() {
Result result = await this.DomainService.RecordCallback(TestData.RecordCallbackCommandEmptyReference, CancellationToken.None);
result.IsFailed.ShouldBeTrue();
}

[Fact]
public async Task CallbackDomainService_RecordCallback_InvalidReference_ResultFailed() {
Result result = await this.DomainService.RecordCallback(TestData.RecordCallbackCommandInvalidReference, CancellationToken.None);
result.IsFailed.ShouldBeTrue();
}
}
16 changes: 13 additions & 3 deletions CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

namespace CallbackHandler.BusinessLogic.Services;

using CallbackHandler.BusinessLogic.Requests;
using Requests;
using CallbackMessageAggregate;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EventStore.Aggregate;
using System;
using System.Threading;
using System.Threading.Tasks;

public interface ICallbackDomainService
{
Task<Result> RecordCallback(CallbackCommands.RecordCallbackCommand command,
CancellationToken cancellationToken);
}

public class CallbackDomainService : ICallbackDomainService
{
private readonly IAggregateRepository<CallbackMessageAggregate, DomainEvent> AggregateRepository;
Expand All @@ -23,12 +29,16 @@ public async Task<Result> RecordCallback(CallbackCommands.RecordCallbackCommand
CancellationToken cancellationToken) {

// split the reference string into an array of strings
String[] referenceData = command.Reference?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
// TODO: Validate the reference data has the correct number of elements
String[] referenceData = command.Reference?.Split(['-'], StringSplitOptions.RemoveEmptyEntries) ?? [];

if (referenceData.Length == 0) {
return Result.Invalid("Reference cannot be empty.");
}

if (referenceData.Length != 2) {
return Result.Invalid("Reference must contain estate and merchant references separated by a hyphen.");
}

// Element 0 is estate reference, Element 1 is merchant reference
String estateReference = referenceData[0];
String merchantReference = referenceData[1];
Expand Down
19 changes: 0 additions & 19 deletions CallbackHandler.BusinessLogic/Services/ICallbackDomainService.cs

This file was deleted.

Loading