diff --git a/CallbackHander.Testing/TestData.cs b/CallbackHander.Testing/TestData.cs index 6212a21..3e670b5 100644 --- a/CallbackHander.Testing/TestData.cs +++ b/CallbackHander.Testing/TestData.cs @@ -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); diff --git a/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs b/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs index 0b84fad..82b6052 100644 --- a/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs +++ b/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using SimpleResults; namespace CallbackHandler.BusinessLogic.Tests.Services; @@ -25,15 +26,27 @@ public class CallbackDomainServiceTests public CallbackDomainServiceTests() { this.AggregateRepository = new Mock>(); this.DomainService = new CallbackDomainService(this.AggregateRepository.Object); - + this.AggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.EmptyCallbackMessageAggregate()); + this.AggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.Success); } [Fact] public async Task CallbackDomainService_RecordCallback_CallbackRecorded() { - this.AggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())) - .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(); } } diff --git a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs index b4084b7..c3881d2 100644 --- a/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs +++ b/CallbackHandler.BusinessLogic/Services/CallbackDomainService.cs @@ -3,7 +3,7 @@ namespace CallbackHandler.BusinessLogic.Services; -using CallbackHandler.BusinessLogic.Requests; +using Requests; using CallbackMessageAggregate; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; @@ -11,6 +11,12 @@ namespace CallbackHandler.BusinessLogic.Services; using System.Threading; using System.Threading.Tasks; +public interface ICallbackDomainService +{ + Task RecordCallback(CallbackCommands.RecordCallbackCommand command, + CancellationToken cancellationToken); +} + public class CallbackDomainService : ICallbackDomainService { private readonly IAggregateRepository AggregateRepository; @@ -23,12 +29,16 @@ public async Task RecordCallback(CallbackCommands.RecordCallbackCommand CancellationToken cancellationToken) { // split the reference string into an array of strings - String[] referenceData = command.Reference?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty(); - // 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]; diff --git a/CallbackHandler.BusinessLogic/Services/ICallbackDomainService.cs b/CallbackHandler.BusinessLogic/Services/ICallbackDomainService.cs deleted file mode 100644 index a12b88b..0000000 --- a/CallbackHandler.BusinessLogic/Services/ICallbackDomainService.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Azure.Core; -using CallbackHandlers.Models; -using MediatR; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using CallbackHandler.BusinessLogic.Requests; -using SimpleResults; - -namespace CallbackHandler.BusinessLogic.Services; - -public interface ICallbackDomainService -{ - Task RecordCallback(CallbackCommands.RecordCallbackCommand command, - CancellationToken cancellationToken); -}