diff --git a/TransactionProcessor.Aggregates/FloatActivityAggregate.cs b/TransactionProcessor.Aggregates/FloatActivityAggregate.cs index 8faccddf..a93c87b6 100644 --- a/TransactionProcessor.Aggregates/FloatActivityAggregate.cs +++ b/TransactionProcessor.Aggregates/FloatActivityAggregate.cs @@ -8,7 +8,6 @@ namespace TransactionProcessor.Aggregates { public static class FloatActivityAggregateExtensions { - public static void PlayEvent(this FloatActivityAggregate aggregate, FloatActivityDomainEvents.FloatAggregateCreditedEvent domainEvent) { diff --git a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantDomainEventHandlerTests.cs b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantDomainEventHandlerTests.cs index 73e942a0..dcfa904d 100644 --- a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantDomainEventHandlerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantDomainEventHandlerTests.cs @@ -12,6 +12,7 @@ using TransactionProcessor.BusinessLogic.EventHandling; using TransactionProcessor.BusinessLogic.Events; using TransactionProcessor.BusinessLogic.Requests; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.DomainEvents; using TransactionProcessor.Repository; using TransactionProcessor.Testing; @@ -22,16 +23,13 @@ namespace TransactionProcessor.BusinessLogic.Tests.DomainEventHandlers { public class MerchantDomainEventHandlerTests : DomainEventHandlerTests { - private readonly Mock> MerchantAggregateRepository; private readonly Mock TransactionProcessorReadModelRepository; private readonly MerchantDomainEventHandler DomainEventHandler; public MerchantDomainEventHandlerTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { - this.MerchantAggregateRepository = new Mock>(); this.TransactionProcessorReadModelRepository = new Mock(); - this.DomainEventHandler = new MerchantDomainEventHandler(this.MerchantAggregateRepository.Object, - this.TransactionProcessorReadModelRepository.Object, + this.DomainEventHandler = new MerchantDomainEventHandler(this.TransactionProcessorReadModelRepository.Object, this.Mediator.Object); } diff --git a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs index 1eb891a9..bbe8fd47 100644 --- a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/VoucherDomainEventHandlerTests.cs @@ -1,5 +1,6 @@ using SimpleResults; using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Database.Contexts; using TransactionProcessor.Database.Entities; @@ -67,8 +68,8 @@ public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithEmailAddress_ Mock securityServiceClient = new Mock(); securityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - Mock> voucherAggregateRepository = new Mock>(); - voucherAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + Mock aggregateService = new(); + aggregateService.Setup(t => t.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientEmail())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N"), TestDatabaseType.InMemory); @@ -98,7 +99,7 @@ public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithEmailAddress_ }); VoucherDomainEventHandler voucherDomainEventHandler = new VoucherDomainEventHandler(securityServiceClient.Object, - voucherAggregateRepository.Object, + aggregateService.Object, dbContextFactory.Object, messagingServiceClient.Object, fileSystem); @@ -116,8 +117,8 @@ public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithRecipientMobi Mock securityServiceClient = new Mock(); securityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - Mock> voucherAggregateRepository = new Mock>(); - voucherAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + Mock aggregateService = new (); + aggregateService.Setup(t => t.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N"), TestDatabaseType.InMemory); @@ -147,7 +148,7 @@ public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithRecipientMobi }); VoucherDomainEventHandler voucherDomainEventHandler = new VoucherDomainEventHandler(securityServiceClient.Object, - voucherAggregateRepository.Object, + aggregateService.Object, dbContextFactory.Object, messagingServiceClient.Object, fileSystem); diff --git a/TransactionProcessor.BusinessLogic.Tests/Manager/TransactionProcessorManagerTests.cs b/TransactionProcessor.BusinessLogic.Tests/Manager/TransactionProcessorManagerTests.cs index 8b360919..04a896fa 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Manager/TransactionProcessorManagerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Manager/TransactionProcessorManagerTests.cs @@ -10,7 +10,9 @@ using SimpleResults; using TransactionProcessor.Aggregates; using TransactionProcessor.BusinessLogic.Manager; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Models.Contract; +using TransactionProcessor.Models.Estate; using TransactionProcessor.Models.Merchant; using TransactionProcessor.Repository; using TransactionProcessor.Testing; @@ -23,37 +25,28 @@ namespace TransactionProcessor.BusinessLogic.Tests.Manager public class TransactionProcessorManagerTests { private readonly Mock TransactionProcessorReadModelRepository; - private readonly Mock> EstateAggregateRepository; - private readonly Mock> ContractAggregateRepository; - private readonly Mock> MerchantAggregateRepository; - private readonly Mock> OperatorAggregateRepository; - + private readonly Mock AggregateService; + private readonly TransactionProcessorManager TransactionProcessorManager; public TransactionProcessorManagerTests() { this.TransactionProcessorReadModelRepository = new Mock(); - this.EstateAggregateRepository = new Mock>(); - this.ContractAggregateRepository = new Mock>(); - this.MerchantAggregateRepository = new Mock>(); - this.OperatorAggregateRepository = new Mock>(); - - this.TransactionProcessorManager = new TransactionProcessorManager(this.TransactionProcessorReadModelRepository.Object, this.EstateAggregateRepository.Object, - this.ContractAggregateRepository.Object, - this.MerchantAggregateRepository.Object, - this.OperatorAggregateRepository.Object); + this.AggregateService = new Mock(); + + this.TransactionProcessorManager = new TransactionProcessorManager(this.TransactionProcessorReadModelRepository.Object, this.AggregateService.Object); } [Fact] public async Task TransactionProcessorManager_GetEstates_EstatesAreReturned() { - this.EstateAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); this.TransactionProcessorReadModelRepository.Setup(e => e.GetEstate(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.EstateModel)); - var getEstatesResult = await this.TransactionProcessorManager.GetEstates(TestData.EstateId, CancellationToken.None); + Result> getEstatesResult = await this.TransactionProcessorManager.GetEstates(TestData.EstateId, CancellationToken.None); getEstatesResult.IsSuccess.ShouldBeTrue(); - var estateModels = getEstatesResult.Data; + List estateModels = getEstatesResult.Data; estateModels.ShouldNotBeNull(); estateModels.ShouldHaveSingleItem(); estateModels.Single().EstateId.ShouldBe(TestData.EstateModel.EstateId); @@ -63,23 +56,23 @@ public async Task TransactionProcessorManager_GetEstates_EstatesAreReturned() [Fact] public async Task TransactionProcessorManager_GetEstates_RepoCallFails_ResultFailed() { - this.EstateAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); this.TransactionProcessorReadModelRepository.Setup(e => e.GetEstate(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var getEstatesResult = await this.TransactionProcessorManager.GetEstates(TestData.EstateId, CancellationToken.None); + Result> getEstatesResult = await this.TransactionProcessorManager.GetEstates(TestData.EstateId, CancellationToken.None); getEstatesResult.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetEstate_EstateIsReturned() { - this.EstateAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); this.TransactionProcessorReadModelRepository.Setup(e => e.GetEstate(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.EstateModel)); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), CancellationToken.None)).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), CancellationToken.None)).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); - var getEstateResult = await this.TransactionProcessorManager.GetEstate(TestData.EstateId, CancellationToken.None); + Result getEstateResult = await this.TransactionProcessorManager.GetEstate(TestData.EstateId, CancellationToken.None); getEstateResult.IsSuccess.ShouldBeTrue(); - var estateModel = getEstateResult.Data; + Estate estateModel = getEstateResult.Data; estateModel.ShouldNotBeNull(); estateModel.EstateId.ShouldBe(TestData.EstateModel.EstateId); estateModel.Name.ShouldBe(TestData.EstateModel.Name); @@ -93,31 +86,31 @@ public async Task TransactionProcessorManager_GetEstate_EstateIsReturned() [Fact] public async Task TransactionProcessorManager_GetEstate_InvalidEstateId_ErrorIsThrown() { - this.EstateAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); this.TransactionProcessorReadModelRepository.Setup(e => e.GetEstate(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.EstateModel)); - var getEstateResult = await this.TransactionProcessorManager.GetEstate(TestData.EstateId, CancellationToken.None); + Result getEstateResult = await this.TransactionProcessorManager.GetEstate(TestData.EstateId, CancellationToken.None); getEstateResult.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetEstate_GetLatestFailed_ErrorIsThrown() { - this.EstateAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); this.TransactionProcessorReadModelRepository.Setup(e => e.GetEstate(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.EstateModel)); - var getEstateResult = await this.TransactionProcessorManager.GetEstate(TestData.EstateId, CancellationToken.None); + Result getEstateResult = await this.TransactionProcessorManager.GetEstate(TestData.EstateId, CancellationToken.None); getEstateResult.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetContract_ContractIsReturned() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant))); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant))); Result getContractResult = await this.TransactionProcessorManager.GetContract(TestData.EstateId, TestData.ContractId, CancellationToken.None); getContractResult.IsSuccess.ShouldBeTrue(); - var contractModel = getContractResult.Data; + Contract contractModel = getContractResult.Data; contractModel.ShouldNotBeNull(); contractModel.ContractId.ShouldBe(TestData.ContractId); @@ -132,7 +125,7 @@ public async Task TransactionProcessorManager_GetContract_ContractIsReturned() [Fact] public async Task TransactionProcessorManager_GetContract_ContractNotCreated_ErrorIsThrown() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); Result getContractResult = await this.TransactionProcessorManager.GetContract(TestData.EstateId, TestData.ContractId, CancellationToken.None); getContractResult.IsFailed.ShouldBeTrue(); @@ -141,7 +134,7 @@ public async Task TransactionProcessorManager_GetContract_ContractNotCreated_Err [Fact] public async Task TransactionProcessorManager_GetContract_GetLatestFails_ErrorIsThrown() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); Result getContractResult = await this.TransactionProcessorManager.GetContract(TestData.EstateId, TestData.ContractId, CancellationToken.None); getContractResult.IsFailed.ShouldBeTrue(); @@ -152,9 +145,9 @@ public async Task TransactionProcessorManager_GetContracts_ContractAreReturned() { this.TransactionProcessorReadModelRepository.Setup(e => e.GetContracts(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new List() { TestData.ContractModelWithProductsAndTransactionFees })); - var getContractsResult = await this.TransactionProcessorManager.GetContracts(TestData.EstateId, CancellationToken.None); + Result> getContractsResult = await this.TransactionProcessorManager.GetContracts(TestData.EstateId, CancellationToken.None); getContractsResult.IsSuccess.ShouldBeTrue(); - var contractModelList = getContractsResult.Data; + List contractModelList = getContractsResult.Data; contractModelList.ShouldNotBeNull(); contractModelList.ShouldNotBeEmpty(); } @@ -164,18 +157,18 @@ public async Task TransactionProcessorManager_GetContracts_RepoCallFails_Contrac { this.TransactionProcessorReadModelRepository.Setup(e => e.GetContracts(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var getContractsResult = await this.TransactionProcessorManager.GetContracts(TestData.EstateId, CancellationToken.None); + Result> getContractsResult = await this.TransactionProcessorManager.GetContracts(TestData.EstateId, CancellationToken.None); getContractsResult.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetTransactionFeesForProduct_TransactionFeesAreReturned() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant))); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant))); - var getTransactionFeesForProductResult = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); + Result> getTransactionFeesForProductResult = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); getTransactionFeesForProductResult.IsSuccess.ShouldBeTrue(); - var transactionFees = getTransactionFeesForProductResult.Data; + List transactionFees = getTransactionFeesForProductResult.Data; transactionFees.ShouldNotBeNull(); transactionFees.ShouldHaveSingleItem(); transactionFees.First().TransactionFeeId.ShouldBe(TestData.TransactionFeeId); @@ -184,27 +177,27 @@ public async Task TransactionProcessorManager_GetTransactionFeesForProduct_Trans [Fact] public async Task TransactionProcessorManager_GetTransactionFeesForProduct_ContractNotFound_ErrorThrown() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); - var result = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); + Result> result = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetTransactionFeesForProduct_ProductNotFound_ErrorThrown() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); - var result = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); + Result> result = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetTransactionFeesForProduct_GetLatestFails_ErrorThrown() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var result = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); + Result> result = await this.TransactionProcessorManager.GetTransactionFeesForProduct(TestData.EstateId, TestData.MerchantId, TestData.ContractId, TestData.ContractProductId, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } /* @@ -232,29 +225,29 @@ public async Task EstateManagementManager_GetFileDetails_RepoCallFailed_FileDeta [Fact] public async Task TransactionProcessorManager_GetOperator_OperatorDetailsAreReturned() { - this.OperatorAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); - var getOperatorResult = await this.TransactionProcessorManager.GetOperator(TestData.EstateId, TestData.OperatorId, CancellationToken.None); + Result getOperatorResult = await this.TransactionProcessorManager.GetOperator(TestData.EstateId, TestData.OperatorId, CancellationToken.None); getOperatorResult.IsSuccess.ShouldBeTrue(); - var operatorDetails = getOperatorResult.Data; + Operator operatorDetails = getOperatorResult.Data; operatorDetails.ShouldNotBeNull(); } [Fact] public async Task TransactionProcessorManager_GetOperator_OperatorNotCreated_ExceptionThrown() { - this.OperatorAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); - var getOperatorResult = await this.TransactionProcessorManager.GetOperator(TestData.EstateId, TestData.OperatorId, CancellationToken.None); + Result getOperatorResult = await this.TransactionProcessorManager.GetOperator(TestData.EstateId, TestData.OperatorId, CancellationToken.None); getOperatorResult.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetOperator_GetLatestFails_ExceptionThrown() { - this.OperatorAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var getOperatorResult = await this.TransactionProcessorManager.GetOperator(TestData.EstateId, TestData.OperatorId, CancellationToken.None); + Result getOperatorResult = await this.TransactionProcessorManager.GetOperator(TestData.EstateId, TestData.OperatorId, CancellationToken.None); getOperatorResult.IsFailed.ShouldBeTrue(); } @@ -265,9 +258,9 @@ public async Task TransactionProcessorManager_GetOperators_OperatorDetailsAreRet TestData.OperatorModel })); - var getOperatorsResult = await this.TransactionProcessorManager.GetOperators(TestData.EstateId, CancellationToken.None); + Result> getOperatorsResult = await this.TransactionProcessorManager.GetOperators(TestData.EstateId, CancellationToken.None); getOperatorsResult.IsSuccess.ShouldBeTrue(); - var operators = getOperatorsResult.Data; + List operators = getOperatorsResult.Data; operators.ShouldNotBeNull(); operators.ShouldHaveSingleItem(); operators.Single().OperatorId.ShouldBe(TestData.OperatorId); @@ -278,7 +271,7 @@ public async Task TransactionProcessorManager_GetOperators_EmptyList_ExceptionTh { this.TransactionProcessorReadModelRepository.Setup(e => e.GetOperators(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new List())); - var getOperatorsResult = await this.TransactionProcessorManager.GetOperators(TestData.EstateId, CancellationToken.None); + Result> getOperatorsResult = await this.TransactionProcessorManager.GetOperators(TestData.EstateId, CancellationToken.None); getOperatorsResult.IsSuccess.ShouldBeTrue(); } @@ -287,21 +280,21 @@ public async Task TransactionProcessorManager_GetOperators_RepoCallFails_Excepti { this.TransactionProcessorReadModelRepository.Setup(e => e.GetOperators(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var getOperatorsResult = await this.TransactionProcessorManager.GetOperators(TestData.EstateId, CancellationToken.None); + Result> getOperatorsResult = await this.TransactionProcessorManager.GetOperators(TestData.EstateId, CancellationToken.None); getOperatorsResult.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetMerchant_MerchantIsReturned() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate))); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), CancellationToken.None)).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate))); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), CancellationToken.None)).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); Merchant expectedModel = TestData.MerchantModelWithAddressesContactsDevicesAndOperatorsAndContracts(); - var getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantResult.IsSuccess.ShouldBeTrue(); - var merchantModel = getMerchantResult.Data; + Merchant merchantModel = getMerchantResult.Data; merchantModel.ShouldNotBeNull(); merchantModel.MerchantReportingId.ShouldBe(expectedModel.MerchantReportingId); @@ -344,12 +337,12 @@ public async Task TransactionProcessorManager_GetMerchant_MerchantIsReturned() [Fact] public async Task TransactionProcessorManager_GetMerchant_MerchantIsReturnedWithNullAddressesAndContacts() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithOperator())); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), CancellationToken.None)).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithOperator())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), CancellationToken.None)).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); - var getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantResult.IsSuccess.ShouldBeTrue(); - var merchantModel = getMerchantResult.Data; + Merchant merchantModel = getMerchantResult.Data; merchantModel.ShouldNotBeNull(); merchantModel.MerchantId.ShouldBe(TestData.MerchantId); @@ -361,11 +354,11 @@ public async Task TransactionProcessorManager_GetMerchant_MerchantIsReturnedWith [Fact] public async Task TransactionProcessorManager_GetMerchant_WithAddress_MerchantIsReturnedWithNullContacts() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithAddress())); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithAddress())); - var getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantResult.IsSuccess.ShouldBeTrue(); - var merchantModel = getMerchantResult.Data; + Merchant merchantModel = getMerchantResult.Data; merchantModel.ShouldNotBeNull(); merchantModel.MerchantId.ShouldBe(TestData.MerchantId); @@ -377,11 +370,11 @@ public async Task TransactionProcessorManager_GetMerchant_WithAddress_MerchantIs [Fact] public async Task TransactionProcessorManager_GetMerchant_WithContact_MerchantIsReturnedWithNullAddresses() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithContact())); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithContact())); - var getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result getMerchantResult = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantResult.IsSuccess.ShouldBeTrue(); - var merchantModel = getMerchantResult.Data; + Merchant merchantModel = getMerchantResult.Data; merchantModel.ShouldNotBeNull(); merchantModel.MerchantId.ShouldBe(TestData.MerchantId); @@ -393,18 +386,18 @@ public async Task TransactionProcessorManager_GetMerchant_WithContact_MerchantIs [Fact] public async Task TransactionProcessorManager_GetMerchant_MerchantNotCreated_ErrorThrown() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - var result = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result result = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task TransactionProcessorManager_GetMerchant_GetLatestFails_ErrorThrown() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var result = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result result = await this.TransactionProcessorManager.GetMerchant(TestData.EstateId, TestData.MerchantId, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -413,9 +406,9 @@ public async Task TransactionProcessorManager_GetMerchantContracts_MerchantContr { this.TransactionProcessorReadModelRepository.Setup(e => e.GetMerchantContracts(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.MerchantContracts)); - var getMerchantContractsResult = await this.TransactionProcessorManager.GetMerchantContracts(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result> getMerchantContractsResult = await this.TransactionProcessorManager.GetMerchantContracts(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantContractsResult.IsSuccess.ShouldBeTrue(); - var merchantContracts = getMerchantContractsResult.Data; + List merchantContracts = getMerchantContractsResult.Data; merchantContracts.ShouldNotBeNull(); merchantContracts.ShouldHaveSingleItem(); merchantContracts.Single().ContractId.ShouldBe(TestData.ContractId); @@ -426,7 +419,7 @@ public async Task TransactionProcessorManager_GetMerchantContracts_EmptyListRetu { this.TransactionProcessorReadModelRepository.Setup(e => e.GetMerchantContracts(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.MerchantContractsEmptyList)); - var getMerchantContractsResult = await this.TransactionProcessorManager.GetMerchantContracts(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result> getMerchantContractsResult = await this.TransactionProcessorManager.GetMerchantContracts(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantContractsResult.IsFailed.ShouldBeTrue(); } @@ -435,7 +428,7 @@ public async Task TransactionProcessorManager_GetMerchantContracts_RepoCallFaile { this.TransactionProcessorReadModelRepository.Setup(e => e.GetMerchantContracts(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - var getMerchantContractsResult = await this.TransactionProcessorManager.GetMerchantContracts(TestData.EstateId, TestData.MerchantId, CancellationToken.None); + Result> getMerchantContractsResult = await this.TransactionProcessorManager.GetMerchantContracts(TestData.EstateId, TestData.MerchantId, CancellationToken.None); getMerchantContractsResult.IsFailed.ShouldBeTrue(); } @@ -450,7 +443,7 @@ public async Task TransactionProcessorManager_GetMerchants_MerchantListIsReturne Result> getMerchantsResult = await this.TransactionProcessorManager.GetMerchants(TestData.EstateId, CancellationToken.None); getMerchantsResult.IsSuccess.ShouldBeTrue(); - var merchantList = getMerchantsResult.Data; + List merchantList = getMerchantsResult.Data; merchantList.ShouldNotBeNull(); merchantList.ShouldNotBeEmpty(); diff --git a/TransactionProcessor.BusinessLogic.Tests/Manager/VoucherManagementManagerTests.cs b/TransactionProcessor.BusinessLogic.Tests/Manager/VoucherManagementManagerTests.cs index 3091f1f2..63f7d1b7 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Manager/VoucherManagementManagerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Manager/VoucherManagementManagerTests.cs @@ -10,6 +10,7 @@ using SimpleResults; using TransactionProcessor.Aggregates; using TransactionProcessor.BusinessLogic.Manager; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.BusinessLogic.Tests.DomainEventHandlers; using TransactionProcessor.Database.Contexts; using TransactionProcessor.Database.Entities; @@ -65,10 +66,10 @@ await context.VoucherProjectionStates.AddAsync(new VoucherProjectionState Mock> dbContextFactory = this.GetMockDbContextFactory(); dbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); - Mock> voucherAggregateRepository = new Mock>(); - voucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); + Mock aggregateService = new(); + aggregateService.Setup(v => v.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); - VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, voucherAggregateRepository.Object); + VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, aggregateService.Object); Models.Voucher voucher = await manager.GetVoucherByCode(TestData.EstateId, TestData.VoucherCode, CancellationToken.None); @@ -85,9 +86,10 @@ public async Task VoucherManagementManager_GetVoucherByCode_VoucherNotFound_Erro Mock> dbContextFactory = this.GetMockDbContextFactory(); dbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); - Mock> voucherAggregateRepository = new Mock>(); + Mock aggregateService = new(); + aggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); - VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, voucherAggregateRepository.Object); + VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, aggregateService.Object); Should.Throw(async () => { @@ -114,10 +116,10 @@ await context.VoucherProjectionStates.AddAsync(new VoucherProjectionState Mock> dbContextFactory = this.GetMockDbContextFactory(); dbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); - Mock> voucherAggregateRepository = new Mock>(); - voucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); + Mock aggregateService = new(); + aggregateService.Setup(v => v.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); - VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, voucherAggregateRepository.Object); + VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, aggregateService.Object); Models.Voucher voucher = await manager.GetVoucherByTransactionId(TestData.EstateId, TestData.TransactionId, CancellationToken.None); @@ -134,9 +136,10 @@ public async Task VoucherManagementManager_GetVoucherByTransactionId_VoucherNotF Mock> dbContextFactory = this.GetMockDbContextFactory(); dbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); - Mock> voucherAggregateRepository = new Mock>(); + Mock aggregateService = new(); + aggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); - VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, voucherAggregateRepository.Object); + VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, aggregateService.Object); Should.Throw(async () => { diff --git a/TransactionProcessor.BusinessLogic.Tests/RequestHandler/SettlementRequestHandlerTests.cs b/TransactionProcessor.BusinessLogic.Tests/RequestHandler/SettlementRequestHandlerTests.cs index 5e0ce9f7..debd4ea4 100644 --- a/TransactionProcessor.BusinessLogic.Tests/RequestHandler/SettlementRequestHandlerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/RequestHandler/SettlementRequestHandlerTests.cs @@ -21,9 +21,9 @@ public class SettlementRequestHandlerTests public async Task SettlementRequestHandler_ProcessSettlementRequest_IsHandled() { Mock settlementDomainService = new Mock(); - Mock> settlementAggregateRepository = new(); + Mock aggregateService = new(); Mock manager = new Mock(); - SettlementRequestHandler handler = new SettlementRequestHandler(settlementDomainService.Object, settlementAggregateRepository.Object, manager.Object); + SettlementRequestHandler handler = new SettlementRequestHandler(settlementDomainService.Object, aggregateService.Object, manager.Object); settlementDomainService .Setup(s => s.ProcessSettlement(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); var command = TestData.Commands.ProcessSettlementCommand; diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/ContractDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/ContractDomainServiceTests.cs index 68e3139e..a06e5ad7 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/ContractDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/ContractDomainServiceTests.cs @@ -18,24 +18,22 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services { public class ContractDomainServiceTests { private ContractDomainService DomainService; - private Mock> EstateAggregateRepository; - private Mock> ContractAggregateRepository; + private Mock AggregateService; private Mock EventStoreContext; public ContractDomainServiceTests() { - this.EstateAggregateRepository = new Mock>(); - this.ContractAggregateRepository = new Mock>(); + this.AggregateService = new Mock(); this.EventStoreContext = new Mock(); - this.DomainService = new ContractDomainService(this.EstateAggregateRepository.Object, this.ContractAggregateRepository.Object, this.EventStoreContext.Object); + this.DomainService = new ContractDomainService(this.AggregateService.Object, this.EventStoreContext.Object); } [Fact] public async Task ContractDomainService_CreateContract_ContractIsCreated() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); - this.ContractAggregateRepository.Setup(c => c.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(c => c.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -48,9 +46,9 @@ public async Task ContractDomainService_CreateContract_ContractIsCreated() [Fact] public async Task ContractDomainService_CreateContract_DuplicateContractNameForOperator_ResultFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); String queryResult = "{\r\n \"total\": 1,\r\n \"contractId\": \"3015e4d0-e9a9-49e5-bd55-a5492f193b62\"\r\n}"; this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) @@ -64,10 +62,10 @@ public async Task ContractDomainService_CreateContract_DuplicateContractNameForO [Fact] public async Task ContractDomainService_CreateContract_ContractAlreadyCreated_ResultFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -80,8 +78,8 @@ public async Task ContractDomainService_CreateContract_ContractAlreadyCreated_Re [Fact] public async Task ContractDomainService_CreateContract_EstateNotCreated_ResultFailed() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -93,8 +91,8 @@ public async Task ContractDomainService_CreateContract_EstateNotCreated_ResultFa [Fact] public async Task ContractDomainService_CreateContract_NoOperatorCreatedForEstate_ResultFailed() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -106,8 +104,8 @@ public async Task ContractDomainService_CreateContract_NoOperatorCreatedForEstat [Fact] public async Task ContractDomainService_CreateContract_OperatorNotFoundForEstate_ResultFailed() { - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -120,10 +118,10 @@ public async Task ContractDomainService_CreateContract_OperatorNotFoundForEstate [Fact] public async Task ContractDomainService_AddProductToContract_FixedValue_ProductAddedToContract() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); - this.ContractAggregateRepository.Setup(c => c.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); + this.AggregateService.Setup(c => c.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -135,8 +133,8 @@ public async Task ContractDomainService_AddProductToContract_FixedValue_ProductA [Fact] public async Task ContractDomainService_AddProductToContract_FixedValue_ContractNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -149,10 +147,10 @@ public async Task ContractDomainService_AddProductToContract_FixedValue_Contract [Fact] public async Task ContractDomainService_AddProductToContract_VariableValue_ProductAddedToContract() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); - this.ContractAggregateRepository.Setup(c => c.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); + this.AggregateService.Setup(c => c.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -165,8 +163,8 @@ public async Task ContractDomainService_AddProductToContract_VariableValue_Produ [Fact] public async Task ContractDomainService_AddProductToContract_VariableValue_ContractNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -179,8 +177,8 @@ public async Task ContractDomainService_AddProductToContract_VariableValue_Contr [Fact] public async Task ContractDomainService_AddProductToContract_VariableValue_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -193,8 +191,8 @@ public async Task ContractDomainService_AddProductToContract_VariableValue_Estat [Fact] public async Task ContractDomainService_AddProductToContract_FixedValue_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -211,11 +209,11 @@ public async Task ContractDomainService_AddProductToContract_FixedValue_EstateNo [InlineData(DataTransferObjects.Responses.Contract.CalculationType.Percentage, DataTransferObjects.Responses.Contract.FeeType.ServiceProvider)] public async Task ContractDomainService_AddTransactionFeeForProductToContract_TransactionFeeIsAddedToProduct(DataTransferObjects.Responses.Contract.CalculationType calculationType, DataTransferObjects.Responses.Contract.FeeType feeType) { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregateWithAProduct())); - this.ContractAggregateRepository.Setup(c => c.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(c => c.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); @@ -233,8 +231,8 @@ public async Task ContractDomainService_AddTransactionFeeForProductToContract_Tr [InlineData(DataTransferObjects.Responses.Contract.CalculationType.Percentage, DataTransferObjects.Responses.Contract.FeeType.ServiceProvider)] public async Task ContractDomainService_AddTransactionFeeForProductToContract_ContractNotCreated_ErrorThrown(DataTransferObjects.Responses.Contract.CalculationType calculationType, DataTransferObjects.Responses.Contract.FeeType feeType) { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) @@ -254,8 +252,8 @@ public async Task ContractDomainService_AddTransactionFeeForProductToContract_Co public async Task ContractDomainService_AddTransactionFeeForProductToContract_ProductNotFound_ErrorThrown( DataTransferObjects.Responses.Contract.CalculationType calculationType, DataTransferObjects.Responses.Contract.FeeType feeType) { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) @@ -276,12 +274,12 @@ public async Task ContractDomainService_DisableTransactionFeeForProduct_Transact CalculationType calculationType, FeeType feeType) { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(c => c.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(calculationType, feeType)); - this.ContractAggregateRepository.Setup(c => c.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(c => c.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); this.EventStoreContext.Setup(c => c.RunTransientQuery(It.IsAny(), It.IsAny())) .ReturnsAsync("{\r\n \"total\": 0,\r\n \"contractId\": \"\"\r\n}"); diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/EstateDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/EstateDomainServiceTests.cs index c889711f..c06a171a 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/EstateDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/EstateDomainServiceTests.cs @@ -19,21 +19,21 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services { public class EstateDomainServiceTests { private EstateDomainService DomainService; - private Mock> EstateAggregateRepository; + private Mock AggregateService; private Mock SecurityServiceClient; public EstateDomainServiceTests() { - this.EstateAggregateRepository = new Mock>(); + this.AggregateService= new Mock(); this.SecurityServiceClient = new Mock(); - this.DomainService = new EstateDomainService(this.EstateAggregateRepository.Object, this.SecurityServiceClient.Object); + this.DomainService = new EstateDomainService(this.AggregateService.Object, this.SecurityServiceClient.Object); } [Fact] public async Task EstateDomainService_CreateEstate_EstateIsCreated() { - this.EstateAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(SimpleResults.Result.Success(new EstateAggregate())); - this.EstateAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(SimpleResults.Result.Success()); Result result = await this.DomainService.CreateEstate(TestData.Commands.CreateEstateCommand, CancellationToken.None); @@ -44,8 +44,9 @@ public async Task EstateDomainService_CreateEstate_EstateIsCreated() { [Fact] public async Task EstateDomainService_AddOperatorEstate_OperatorIsAdded() { - this.EstateAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.EstateAggregateRepository.Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success()); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) + .ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(m => m.Save(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success()); Result result = await this.DomainService.AddOperatorToEstate(TestData.Commands.AddOperatorToEstateCommand, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); @@ -54,19 +55,17 @@ public async Task EstateDomainService_AddOperatorEstate_OperatorIsAdded() [Fact] public async Task EstateDomainService_RemoveOperatorFromEstate_OperatorIsRemoved() { - this.EstateAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.EstateAggregateRepository.Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(m => m.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); Result result = await this.DomainService.RemoveOperatorFromEstate(TestData.Commands.RemoveOperatorFromEstateCommand, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } [Fact] public async Task EstateDomainService_CreateEstateUser_EstateUserIsCreated() { - this.EstateAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.EstateAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(SimpleResults.Result.Success()); this.SecurityServiceClient @@ -87,11 +86,9 @@ public async Task EstateDomainService_CreateEstateUser_EstateUserIsCreated() { [Fact] public async Task EstateDomainService_CreateEstateUser_UserCreateFailed_ResultIsFailed() { - this.EstateAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.EstateAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(SimpleResults.Result.Success()); this.SecurityServiceClient @@ -112,11 +109,9 @@ public async Task EstateDomainService_CreateEstateUser_UserCreateFailed_ResultIs [Fact] public async Task EstateDomainService_CreateEstateUser_GetUsersFailed_ResultIsFailed() { - this.EstateAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.EstateAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(SimpleResults.Result.Success()); this.SecurityServiceClient @@ -133,11 +128,9 @@ public async Task EstateDomainService_CreateEstateUser_GetUsersFailed_ResultIsFa [Fact] public async Task EstateDomainService_CreateEstateUser_NullUserReturned_ResultIsFailed() { - this.EstateAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.EstateAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(SimpleResults.Result.Success()); this.SecurityServiceClient diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/FloatDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/FloatDomainServiceTests.cs index a386a975..b5988bec 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/FloatDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/FloatDomainServiceTests.cs @@ -20,11 +20,7 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services public class FloatDomainServiceTests { - private readonly Mock> FloatAggregateRepository; - private readonly Mock> FloatActivityAggregateRepository; - private readonly Mock> TransactionAggregateRepository; - private readonly Mock> EstateAggregateRepository; - private readonly Mock> ContractAggregateRepository; + private readonly Mock AggregateService; private readonly FloatDomainService FloatDomainService; public FloatDomainServiceTests(){ @@ -34,69 +30,57 @@ public FloatDomainServiceTests(){ Logger.Initialise(NullLogger.Instance); - this.FloatAggregateRepository = new Mock>(); - this.FloatActivityAggregateRepository = new Mock>(); - this.TransactionAggregateRepository = new Mock>(); - this.EstateAggregateRepository = new Mock>(); - this.ContractAggregateRepository = new Mock>(); - - this.FloatDomainService = new FloatDomainService(this.FloatAggregateRepository.Object, - this.FloatActivityAggregateRepository.Object, - this.TransactionAggregateRepository.Object, - this.EstateAggregateRepository.Object, - this.ContractAggregateRepository.Object); + this.AggregateService = new Mock(); + + this.FloatDomainService = new FloatDomainService(this.AggregateService.Object); } [Fact] public async Task FloatDomainService_CreateFloatForContractProduct_FloatCreated(){ - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync( Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(Models.Contract.CalculationType.Fixed, Models.Contract.FeeType.Merchant))); + + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new FloatAggregate()); - this.FloatAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); - this.ContractAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(Models.Contract.CalculationType.Fixed,Models.Contract.FeeType.Merchant)); - var command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, + FloatCommands.CreateFloatForContractProductCommand command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - var result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); + Result result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } [Fact] public async Task FloatDomainService_CreateFloatForContractProduct_InvalidEstate_ErrorThrown() { - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new FloatAggregate()); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - - var command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); + FloatCommands.CreateFloatForContractProductCommand command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - var result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); + Result result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task FloatDomainService_CreateFloatForContractProduct_InvalidContract_ErrorThrown() { - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new FloatAggregate()); - this.ContractAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); - var command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, + FloatCommands.CreateFloatForContractProductCommand command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - var result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); + Result result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task FloatDomainService_CreateFloatForContractProduct_InvalidContractProduct_ErrorThrown() { - this.ContractAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregate()); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedContractAggregate())); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new FloatAggregate()); - - var command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, + FloatCommands.CreateFloatForContractProductCommand command = new FloatCommands.CreateFloatForContractProductCommand(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - var result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); + Result result = await this.FloatDomainService.CreateFloatForContractProduct(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -104,13 +88,13 @@ public async Task FloatDomainService_CreateFloatForContractProduct_InvalidContra public async Task FloatDomainService_RecordCreditPurchase_PurchaseRecorded(){ FloatAggregate floatAggregate = FloatAggregate.Create(TestData.FloatAggregateId); floatAggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); - this.FloatAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); - - var command = new FloatCommands.RecordCreditPurchaseForFloatCommand(TestData.EstateId, + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(floatAggregate)); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + + FloatCommands.RecordCreditPurchaseForFloatCommand command = new FloatCommands.RecordCreditPurchaseForFloatCommand(TestData.EstateId, TestData.FloatAggregateId, TestData.FloatCreditAmount, TestData.FloatCreditCostPrice, TestData.CreditPurchasedDateTime); - var result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); + Result result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } @@ -119,13 +103,13 @@ public async Task FloatDomainService_RecordCreditPurchase_SaveFailed() { FloatAggregate floatAggregate = FloatAggregate.Create(TestData.FloatAggregateId); floatAggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); - this.FloatAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(floatAggregate)); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); - var command = new FloatCommands.RecordCreditPurchaseForFloatCommand(TestData.EstateId, + FloatCommands.RecordCreditPurchaseForFloatCommand command = new FloatCommands.RecordCreditPurchaseForFloatCommand(TestData.EstateId, TestData.FloatAggregateId, TestData.FloatCreditAmount, TestData.FloatCreditCostPrice, TestData.CreditPurchasedDateTime); - var result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); + Result result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -134,13 +118,13 @@ public async Task FloatDomainService_RecordCreditPurchase_ExceptionThrown() { FloatAggregate floatAggregate = FloatAggregate.Create(TestData.FloatAggregateId); floatAggregate.CreateFloat(TestData.EstateId, TestData.ContractId, TestData.ProductId, TestData.FloatCreatedDateTime); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); - this.FloatAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception()); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(floatAggregate)); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception()); - var command = new FloatCommands.RecordCreditPurchaseForFloatCommand(TestData.EstateId, + FloatCommands.RecordCreditPurchaseForFloatCommand command = new FloatCommands.RecordCreditPurchaseForFloatCommand(TestData.EstateId, TestData.FloatAggregateId, TestData.FloatCreditAmount, TestData.FloatCreditCostPrice, TestData.CreditPurchasedDateTime); - var result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); + Result result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -148,12 +132,12 @@ public async Task FloatDomainService_RecordCreditPurchase_ExceptionThrown() public async Task FloatDomainService_RecordCreditPurchase_FloatActivity_PurchaseRecorded() { FloatActivityAggregate floatAggregate = FloatActivityAggregate.Create(TestData.FloatAggregateId); - this.FloatActivityAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); - this.FloatActivityAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(floatAggregate)); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); - var command = new FloatActivityCommands.RecordCreditPurchaseCommand(TestData.EstateId, + FloatActivityCommands.RecordCreditPurchaseCommand command = new FloatActivityCommands.RecordCreditPurchaseCommand(TestData.EstateId, TestData.FloatAggregateId, TestData.CreditPurchasedDateTime, TestData.FloatCreditAmount, TestData.FloatCreditId); - var result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); + Result result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } @@ -161,12 +145,12 @@ public async Task FloatDomainService_RecordCreditPurchase_FloatActivity_Purchase public async Task FloatDomainService_RecordCreditPurchase_FloatActivity_SaveFailed() { FloatActivityAggregate floatAggregate = FloatActivityAggregate.Create(TestData.FloatAggregateId); - this.FloatActivityAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); - this.FloatActivityAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(floatAggregate)); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); - var command = new FloatActivityCommands.RecordCreditPurchaseCommand(TestData.EstateId, + FloatActivityCommands.RecordCreditPurchaseCommand command = new FloatActivityCommands.RecordCreditPurchaseCommand(TestData.EstateId, TestData.FloatAggregateId, TestData.CreditPurchasedDateTime, TestData.FloatCreditAmount, TestData.FloatCreditId); - var result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); + Result result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } @@ -174,12 +158,12 @@ public async Task FloatDomainService_RecordCreditPurchase_FloatActivity_SaveFail public async Task FloatDomainService_RecordCreditPurchase_FloatActivity_ExceptionThrown() { FloatActivityAggregate floatAggregate = FloatActivityAggregate.Create(TestData.FloatAggregateId); - this.FloatActivityAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); - this.FloatActivityAggregateRepository.Setup(f => f.SaveChanges(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception()); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(floatAggregate)); + this.AggregateService.Setup(f => f.Save(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception()); - var command = new FloatActivityCommands.RecordCreditPurchaseCommand(TestData.EstateId, + FloatActivityCommands.RecordCreditPurchaseCommand command = new FloatActivityCommands.RecordCreditPurchaseCommand(TestData.EstateId, TestData.FloatAggregateId, TestData.CreditPurchasedDateTime, TestData.FloatCreditAmount, TestData.FloatCreditId); - var result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); + Result result = await this.FloatDomainService.RecordCreditPurchase(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } } diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/MerchantDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/MerchantDomainServiceTests.cs index 7c859517..ed9a5349 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/MerchantDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/MerchantDomainServiceTests.cs @@ -27,17 +27,10 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services; public class MerchantDomainServiceTests { - private readonly Mock> MerchantAggregateRepository; - - private readonly Mock> - MerchantDepositListAggregateRepository; - - private readonly Mock> EstateAggregateRepository; - + private readonly Mock AggregateService; + private readonly Mock SecurityServiceClient; - private readonly Mock> ContractAggregateRepository; - private readonly MerchantDomainService DomainService; private readonly Mock EventStoreContext; @@ -48,17 +41,11 @@ public MerchantDomainServiceTests() { Logger.Initialise(new NullLogger()); - this.MerchantAggregateRepository = new Mock>(); - this.MerchantDepositListAggregateRepository = - new Mock>(); - this.EstateAggregateRepository = new Mock>(); + this.AggregateService = new Mock(); this.SecurityServiceClient = new Mock(); - this.ContractAggregateRepository = new Mock>(); this.EventStoreContext = new Mock(); - this.DomainService = new MerchantDomainService(this.EstateAggregateRepository.Object, - this.MerchantAggregateRepository.Object, this.MerchantDepositListAggregateRepository.Object, - this.ContractAggregateRepository.Object, this.SecurityServiceClient.Object, - this.EventStoreContext.Object); + this.DomainService = new MerchantDomainService(this.AggregateService.Object, + this.SecurityServiceClient.Object, this.EventStoreContext.Object); } [Theory] @@ -68,14 +55,14 @@ public MerchantDomainServiceTests() { [InlineData(DataTransferObjects.Responses.Merchant.SettlementSchedule.NotSet)] public async Task MerchantDomainService_CreateMerchant_MerchantIsCreated( DataTransferObjects.Responses.Merchant.SettlementSchedule settlementSchedule) { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); MerchantCommands.CreateMerchantCommand command = TestData.Commands.CreateMerchantCommand; @@ -87,14 +74,14 @@ public async Task MerchantDomainService_CreateMerchant_MerchantIsCreated( [Fact] public async Task MerchantDomainService_CreateMerchant_MerchantIdNotSet_MerchantIsCreated() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); MerchantCommands.CreateMerchantCommand command = TestData.Commands.CreateMerchantCommand; command.RequestDto.MerchantId = null; @@ -105,14 +92,14 @@ public async Task MerchantDomainService_CreateMerchant_MerchantIdNotSet_Merchant [Fact] public async Task MerchantDomainService_CreateMerchant_AlreadyCreated_MerchantIsCreated() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); Result result = await this.DomainService.CreateMerchant(TestData.Commands.CreateMerchantCommand, CancellationToken.None); @@ -123,13 +110,13 @@ public async Task MerchantDomainService_CreateMerchant_AlreadyCreated_MerchantIs [Fact] public async Task MerchantDomainService_CreateMerchant_EstateNotFound_ErrorThrown() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); Result result = await this.DomainService.CreateMerchant(TestData.Commands.CreateMerchantCommand, CancellationToken.None); @@ -138,14 +125,14 @@ public async Task MerchantDomainService_CreateMerchant_EstateNotFound_ErrorThrow [Fact] public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorAssigned() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); Result result = await this.DomainService.AssignOperatorToMerchant(TestData.Commands.AssignOperatorToMerchantCommand, @@ -155,14 +142,14 @@ await this.DomainService.AssignOperatorToMerchant(TestData.Commands.AssignOperat [Fact] public async Task MerchantDomainService_AssignOperatorToMerchant_MerchantNotCreated_ErrorThrown() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); Result result = await this.DomainService.AssignOperatorToMerchant(TestData.Commands.AssignOperatorToMerchantCommand, CancellationToken.None); @@ -171,14 +158,14 @@ public async Task MerchantDomainService_AssignOperatorToMerchant_MerchantNotCrea [Fact] public async Task MerchantDomainService_AssignOperatorToMerchant_EstateNotCreated_ErrorThrown() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); var result = await this.DomainService.AssignOperatorToMerchant(TestData.Commands.AssignOperatorToMerchantCommand, CancellationToken.None); @@ -187,14 +174,14 @@ public async Task MerchantDomainService_AssignOperatorToMerchant_EstateNotCreate [Fact] public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorNotFoundForEstate_ErrorThrown() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); var result = await this.DomainService.AssignOperatorToMerchant(TestData.Commands.AssignOperatorToMerchantCommand, CancellationToken.None); @@ -208,14 +195,14 @@ public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorNotFoun [InlineData("")] public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorRequiresMerchantNumber_MerchantNumberNotSet_ErrorThrown( String merchantNumber) { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); MerchantCommands.AssignOperatorToMerchantCommand command = new(TestData.EstateId, TestData.MerchantId, new DataTransferObjects.Requests.Merchant.AssignOperatorRequest() { @@ -235,14 +222,14 @@ public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorRequire [InlineData("")] public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorRequiresTerminalNumber_TerminalNumberNotSet_ErrorThrown( String terminalNumber) { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); MerchantCommands.AssignOperatorToMerchantCommand command = new(TestData.EstateId, TestData.MerchantId, new AssignOperatorRequest() { @@ -257,13 +244,13 @@ public async Task MerchantDomainService_AssignOperatorToMerchant_OperatorRequire [Fact] public async Task MerchantDomainService_CreateMerchantUser_MerchantUserIsCreated() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EstateAggregateWithOperator()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); this.SecurityServiceClient @@ -283,13 +270,13 @@ public async Task MerchantDomainService_CreateMerchantUser_MerchantUserIsCreated [Fact] public async Task MerchantDomainService_CreateMerchantUser_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); this.SecurityServiceClient @@ -302,13 +289,13 @@ public async Task MerchantDomainService_CreateMerchantUser_EstateNotCreated_Erro [Fact] public async Task MerchantDomainService_CreateMerchantUser_MerchantNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(new MerchantAggregate()); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); this.SecurityServiceClient @@ -321,13 +308,13 @@ public async Task MerchantDomainService_CreateMerchantUser_MerchantNotCreated_Er [Fact] public async Task MerchantDomainService_CreateMerchantUser_ErrorCreatingUser_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); this.SecurityServiceClient @@ -341,13 +328,13 @@ public async Task MerchantDomainService_CreateMerchantUser_ErrorCreatingUser_Err [Fact] public async Task MerchantDomainService_CreateMerchantUser_ErrorGettingUsers_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); this.SecurityServiceClient @@ -364,13 +351,13 @@ public async Task MerchantDomainService_CreateMerchantUser_ErrorGettingUsers_Err [Fact] public async Task MerchantDomainService_CreateMerchantUser_UserInListIsNull_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); this.SecurityServiceClient @@ -388,13 +375,13 @@ public async Task MerchantDomainService_CreateMerchantUser_UserInListIsNull_Erro [Fact] public async Task MerchantDomainService_AddDeviceToMerchant_MerchantDeviceIsAdded() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddDeviceToMerchant(TestData.Commands.AddMerchantDeviceCommand, CancellationToken.None); @@ -403,13 +390,13 @@ public async Task MerchantDomainService_AddDeviceToMerchant_MerchantDeviceIsAdde [Fact] public async Task MerchantDomainService_AddDeviceToMerchant_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddDeviceToMerchant(TestData.Commands.AddMerchantDeviceCommand, CancellationToken.None); @@ -418,13 +405,13 @@ public async Task MerchantDomainService_AddDeviceToMerchant_EstateNotCreated_Err [Fact] public async Task MerchantDomainService_AddDeviceToMerchant_MerchantNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddDeviceToMerchant(TestData.Commands.AddMerchantDeviceCommand, CancellationToken.None); @@ -434,20 +421,20 @@ public async Task MerchantDomainService_AddDeviceToMerchant_MerchantNotCreated_E [Fact] public async Task MerchantDomainService_MakeMerchantDeposit_DepositIsMade() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantDepositListAggregate())); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); var result = await this.DomainService.MakeMerchantDeposit(TestData.Commands.MakeMerchantDepositCommand, CancellationToken.None); @@ -457,20 +444,20 @@ public async Task MerchantDomainService_MakeMerchantDeposit_DepositIsMade() [Fact] public async Task MerchantDomainService_MakeMerchantDeposit_GetDepositListFailed_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure()); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); var result = await this.DomainService.MakeMerchantDeposit(TestData.Commands.MakeMerchantDepositCommand, CancellationToken.None); @@ -480,20 +467,20 @@ public async Task MerchantDomainService_MakeMerchantDeposit_GetDepositListFailed [Fact] public async Task MerchantDomainService_MakeMerchantDeposit_DepositListSaveFailed_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantDepositListAggregate())); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure); var result = await this.DomainService.MakeMerchantDeposit(TestData.Commands.MakeMerchantDepositCommand, CancellationToken.None); @@ -502,13 +489,13 @@ public async Task MerchantDomainService_MakeMerchantDeposit_DepositListSaveFaile [Fact] public async Task MerchantDomainService_MakeMerchantDeposit_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.MakeMerchantDeposit(TestData.Commands.MakeMerchantDepositCommand, CancellationToken.None); @@ -517,13 +504,13 @@ public async Task MerchantDomainService_MakeMerchantDeposit_EstateNotCreated_Err [Fact] public async Task MerchantDomainService_MakeMerchantDeposit_MerchantNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.MakeMerchantDeposit(TestData.Commands.MakeMerchantDepositCommand, CancellationToken.None); @@ -532,20 +519,20 @@ public async Task MerchantDomainService_MakeMerchantDeposit_MerchantNotCreated_E [Fact] public async Task MerchantDomainService_MakeMerchantDeposit_NoDepositsYet_DepositIsMade() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantDepositListAggregate)); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); var result = await this.DomainService.MakeMerchantDeposit(TestData.Commands.MakeMerchantDepositCommand, CancellationToken.None); @@ -554,13 +541,13 @@ public async Task MerchantDomainService_MakeMerchantDeposit_NoDepositsYet_Deposi [Fact] public async Task MerchantDomainService_SwapMerchantDevice_MerchantDeviceSwapped() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithDevice())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.SwapMerchantDevice(TestData.Commands.SwapMerchantDeviceCommand, CancellationToken.None); @@ -569,13 +556,13 @@ public async Task MerchantDomainService_SwapMerchantDevice_MerchantDeviceSwapped [Fact] public async Task MerchantDomainService_SwapMerchantDevice_MerchantNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.SwapMerchantDevice(TestData.Commands.SwapMerchantDeviceCommand, CancellationToken.None); @@ -584,13 +571,13 @@ public async Task MerchantDomainService_SwapMerchantDevice_MerchantNotCreated_Er [Fact] public async Task MerchantDomainService_SwapMerchantDevice_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithDevice())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.SwapMerchantDevice(TestData.Commands.SwapMerchantDeviceCommand, CancellationToken.None); @@ -599,20 +586,20 @@ public async Task MerchantDomainService_SwapMerchantDevice_EstateNotCreated_Erro [Fact] public async Task MerchantDomainService_MakeMerchantWithdrawal_WithdrawalIsMade() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantDepositListAggregate())); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.SecurityServiceClient @@ -628,17 +615,17 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_WithdrawalIsMade( [Fact] public async Task MerchantDomainService_MakeMerchantWithdrawal_GetDepositListFailed_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure()); this.SecurityServiceClient @@ -651,20 +638,20 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_GetDepositListFai [Fact] public async Task MerchantDomainService_MakeMerchantWithdrawal_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantDepositListAggregate())); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.SecurityServiceClient @@ -677,20 +664,20 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_EstateNotCreated_ [Fact] public async Task MerchantDomainService_MakeMerchantWithdrawal_MerchantNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantDepositListAggregate())); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.SecurityServiceClient @@ -703,20 +690,20 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_MerchantNotCreate [Fact] public async Task MerchantDomainService_MakeMerchantWithdrawal_MerchantDepositListNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantDepositListAggregate)); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.SecurityServiceClient @@ -729,20 +716,20 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_MerchantDepositLi [Fact] public async Task MerchantDomainService_MakeMerchantWithdrawal_NotEnoughFundsToWithdraw_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.MerchantDepositListAggregateRepository - .Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantDepositListAggregate())); - this.MerchantDepositListAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.SecurityServiceClient @@ -755,18 +742,18 @@ public async Task MerchantDomainService_MakeMerchantWithdrawal_NotEnoughFundsToW [Fact] public async Task MerchantDomainService_AddContractToMerchant_ContractAdded() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success( + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync( TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, - FeeType.Merchant))); + FeeType.Merchant)); var result = await this.DomainService.AddContractToMerchant(TestData.Commands.AddMerchantContractCommand, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); @@ -774,16 +761,16 @@ public async Task MerchantDomainService_AddContractToMerchant_ContractAdded() { [Fact] public async Task MerchantDomainService_AddContractToMerchant_ContractNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyContractAggregate())); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyContractAggregate()); var result = await this.DomainService.AddContractToMerchant(TestData.Commands.AddMerchantContractCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -792,16 +779,16 @@ public async Task MerchantDomainService_AddContractToMerchant_ContractNotCreated [Fact] public async Task MerchantDomainService_AddContractToMerchant_GetContractFailed_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.NotFound()); var result = await this.DomainService.AddContractToMerchant(TestData.Commands.AddMerchantContractCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -809,18 +796,17 @@ public async Task MerchantDomainService_AddContractToMerchant_GetContractFailed_ [Fact] public async Task MerchantDomainService_AddContractToMerchant_MerchantNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success( - TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, - FeeType.Merchant))); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, + FeeType.Merchant)); var result = await this.DomainService.AddContractToMerchant(TestData.Commands.AddMerchantContractCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -828,18 +814,17 @@ public async Task MerchantDomainService_AddContractToMerchant_MerchantNotCreated [Fact] public async Task MerchantDomainService_AddContractToMerchant_EstateNotCreated_ErrorThrown() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success( - TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, - FeeType.Merchant))); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, + FeeType.Merchant)); var result = await this.DomainService.AddContractToMerchant(TestData.Commands.AddMerchantContractCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -852,14 +837,14 @@ public async Task MerchantDomainService_AddContractToMerchant_EstateNotCreated_E [InlineData(DataTransferObjects.Responses.Merchant.SettlementSchedule.NotSet)] public async Task MerchantDomainService_UpdateMerchant_MerchantIsUpdated( DataTransferObjects.Responses.Merchant.SettlementSchedule settlementSchedule) { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); MerchantCommands.UpdateMerchantCommand command = TestData.Commands.UpdateMerchantCommand; @@ -872,14 +857,14 @@ public async Task MerchantDomainService_UpdateMerchant_MerchantIsUpdated( [Fact] public async Task MerchantDomainService_UpdateMerchant_ValidationError_ResultIsFailed() { - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); MerchantCommands.UpdateMerchantCommand command = TestData.Commands.UpdateMerchantCommand; @@ -889,13 +874,13 @@ public async Task MerchantDomainService_UpdateMerchant_ValidationError_ResultIsF [Fact] public async Task MerchantDomainService_AddMerchantAddress_AddressAdded() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddMerchantAddress(TestData.Commands.AddMerchantAddressCommand, CancellationToken.None); @@ -905,13 +890,13 @@ public async Task MerchantDomainService_AddMerchantAddress_AddressAdded() { [Fact] public async Task MerchantDomainService_AddMerchantAddress_ValidationFailure_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddMerchantAddress(TestData.Commands.AddMerchantAddressCommand, CancellationToken.None); @@ -920,13 +905,13 @@ public async Task MerchantDomainService_AddMerchantAddress_ValidationFailure_Res [Fact] public async Task MerchantDomainService_UpdateMerchantAddress_AddressUpdated() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.UpdateMerchantAddress(TestData.Commands.UpdateMerchantAddressCommand, CancellationToken.None); @@ -936,13 +921,13 @@ public async Task MerchantDomainService_UpdateMerchantAddress_AddressUpdated() { [Fact] public async Task MerchantDomainService_UpdateMerchantAddress_ValidationFailed_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.UpdateMerchantAddress(TestData.Commands.UpdateMerchantAddressCommand, CancellationToken.None); @@ -951,13 +936,13 @@ public async Task MerchantDomainService_UpdateMerchantAddress_ValidationFailed_R [Fact] public async Task MerchantDomainService_AddMerchantContact_ContactAdded() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddMerchantContact(TestData.Commands.AddMerchantContactCommand, CancellationToken.None); @@ -967,13 +952,13 @@ public async Task MerchantDomainService_AddMerchantContact_ContactAdded() { [Fact] public async Task MerchantDomainService_AddMerchantContact_ValidationError_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.AddMerchantContact(TestData.Commands.AddMerchantContactCommand, CancellationToken.None); @@ -982,13 +967,13 @@ public async Task MerchantDomainService_AddMerchantContact_ValidationError_Resul [Fact] public async Task MerchantDomainService_UpdateMerchantContact_ContactUpdated() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.UpdateMerchantContact(TestData.Commands.UpdateMerchantContactCommand, CancellationToken.None); @@ -998,13 +983,13 @@ public async Task MerchantDomainService_UpdateMerchantContact_ContactUpdated() { [Fact] public async Task MerchantDomainService_UpdateMerchantContact_ValidationError_ResultIsFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.UpdateMerchantContact(TestData.Commands.UpdateMerchantContactCommand, CancellationToken.None); @@ -1013,13 +998,13 @@ public async Task MerchantDomainService_UpdateMerchantContact_ValidationError_Re [Fact] public async Task MerchantDomainService_RemoveOperatorFromMerchant_OperatorRemoved() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithOperator())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.RemoveOperatorFromMerchant(TestData.Commands.RemoveOperatorFromMerchantCommand, CancellationToken.None); @@ -1029,13 +1014,13 @@ public async Task MerchantDomainService_RemoveOperatorFromMerchant_OperatorRemov [Fact] public async Task MerchantDomainService_RemoveOperatorFromMerchant_ValidationFailed_ResultFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithOperator())); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.RemoveOperatorFromMerchant(TestData.Commands.RemoveOperatorFromMerchantCommand, CancellationToken.None); @@ -1044,13 +1029,13 @@ public async Task MerchantDomainService_RemoveOperatorFromMerchant_ValidationFai [Fact] public async Task MerchantDomainService_RemoveContractFromMerchant_ContractRemoved() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate))); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.RemoveContractFromMerchant(TestData.Commands.RemoveMerchantContractCommand, CancellationToken.None); @@ -1060,13 +1045,13 @@ public async Task MerchantDomainService_RemoveContractFromMerchant_ContractRemov [Fact] public async Task MerchantDomainService_RemoveContractFromMerchant_ValidationFailed_ResultFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success(TestData.Aggregates.EmptyEstateAggregate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(m => m.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate))); - this.MerchantAggregateRepository - .Setup(m => m.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(m => m.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); var result = await this.DomainService.RemoveContractFromMerchant(TestData.Commands.RemoveMerchantContractCommand, CancellationToken.None); diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/OperatorDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/OperatorDomainServiceTests.cs index 7bc1b6f4..9c10522e 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/OperatorDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/OperatorDomainServiceTests.cs @@ -16,24 +16,21 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services; public class OperatorDomainServiceTests{ private IOperatorDomainService OperatorDomainService; - private Mock> EstateAggregateRepository; - private Mock> OperatorAggregateRepository; + private Mock AggregateService; public OperatorDomainServiceTests(){ - this.EstateAggregateRepository = new Mock>(); - this.OperatorAggregateRepository= new Mock>(); - this.OperatorDomainService = new OperatorDomainService(this.EstateAggregateRepository.Object, - this.OperatorAggregateRepository.Object); + this.AggregateService = new Mock(); + this.OperatorDomainService = new OperatorDomainService(this.AggregateService.Object); } [Fact] public async Task OperatorDomainService_CreateOperator_OperatorIsCreated(){ - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); - this.OperatorAggregateRepository - .Setup(o => o.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); + this.AggregateService + .Setup(o => o.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); Result result = await this.OperatorDomainService.CreateOperator(TestData.Commands.CreateOperatorCommand, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); @@ -42,9 +39,9 @@ public async Task OperatorDomainService_CreateOperator_OperatorIsCreated(){ [Fact] public async Task OperatorDomainService_CreateOperator_EstateNotCreated_ResultFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); Result result = await this.OperatorDomainService.CreateOperator(TestData.Commands.CreateOperatorCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -53,11 +50,10 @@ public async Task OperatorDomainService_CreateOperator_EstateNotCreated_ResultFa [Fact] public async Task OperatorDomainService_CreateOperator_OperatorAlreadyCreated_ResultFailed() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); Result result = await this.OperatorDomainService.CreateOperator(TestData.Commands.CreateOperatorCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -66,11 +62,11 @@ public async Task OperatorDomainService_CreateOperator_OperatorAlreadyCreated_Re [Fact] public async Task OperatorDomainService_UpdateOperator_OperatorIsUpdated() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedEstateAggregate())); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); - this.OperatorAggregateRepository - .Setup(o => o.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService + .Setup(o => o.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); Result result = await this.OperatorDomainService.UpdateOperator(TestData.Commands.UpdateOperatorCommand, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } @@ -78,7 +74,7 @@ public async Task OperatorDomainService_UpdateOperator_OperatorIsUpdated() [Fact] public async Task OperatorDomainService_UpdateOperator_OperatorNotCreated_ResultFailed() { - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); + this.AggregateService.Setup(o => o.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(SimpleResults.Result.Success(TestData.Aggregates.EmptyOperatorAggregate())); Result result = await this.OperatorDomainService.UpdateOperator(TestData.Commands.UpdateOperatorCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/SettlementDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/SettlementDomainServiceTests.cs index 7f8267ca..a83c7bfa 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/SettlementDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/SettlementDomainServiceTests.cs @@ -22,22 +22,15 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services public class SettlementDomainServiceTests { - private Mock> transactionAggregateRepository; - private Mock> settlementAggregateRepository; - private Mock> merchantAggregateRepository; + private Mock AggregateService; private SettlementDomainService settlementDomainService; public SettlementDomainServiceTests() { - this.transactionAggregateRepository = - new Mock>(); - this.settlementAggregateRepository = - new Mock>(); - this.merchantAggregateRepository = - new Mock>(); + this.AggregateService = + new Mock(); this.settlementDomainService = - new SettlementDomainService(this.transactionAggregateRepository.Object, settlementAggregateRepository.Object, - merchantAggregateRepository.Object); + new SettlementDomainService(this.AggregateService.Object); IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build(); ConfigurationReader.Initialise(configurationRoot); @@ -48,23 +41,23 @@ public SettlementDomainServiceTests() { [Fact] public async Task SettlementDomainService_ProcessSettlement_SettlementIsProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetSettlementAggregateWithPendingMerchantFees(10))); - this.transactionAggregateRepository.SetupSequence(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(0))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(1))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(2))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(3))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(4))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(5))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(6))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(7))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(8))) - .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(9))); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(s => s.GetLatest(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(0)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(1)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(2)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(3)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(4)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(5)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(6)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(7)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(8)))) + .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(9)))); + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.transactionAggregateRepository.SetupSequence(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()) @@ -76,14 +69,14 @@ public async Task SettlementDomainService_ProcessSettlement_SettlementIsProcesse .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBe(Guid.Empty); @@ -92,9 +85,9 @@ public async Task SettlementDomainService_ProcessSettlement_SettlementIsProcesse [Fact] public async Task SettlementDomainService_ProcessSettlement_MerchantWithImmediateSettlement_SettlementIsProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetSettlementAggregateWithPendingMerchantFees(10))); - this.transactionAggregateRepository.SetupSequence(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(0))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(1))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(2))) @@ -105,10 +98,10 @@ public async Task SettlementDomainService_ProcessSettlement_MerchantWithImmediat .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(7))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(8))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(9))); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.transactionAggregateRepository.SetupSequence(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()) @@ -121,14 +114,14 @@ public async Task SettlementDomainService_ProcessSettlement_MerchantWithImmediat .ReturnsAsync(Result.Success()); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBe(Guid.Empty); @@ -137,17 +130,17 @@ public async Task SettlementDomainService_ProcessSettlement_MerchantWithImmediat [Fact] public async Task SettlementDomainService_ProcessSettlement_SettlementAggregateNotCreated_NothingProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptySettlementAggregate())); - settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBe(Guid.Empty); @@ -156,18 +149,18 @@ public async Task SettlementDomainService_ProcessSettlement_SettlementAggregateN [Fact] public async Task SettlementDomainService_ProcessSettlement_SettlementAggregateNoFeesToSettles_NothingProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBe(Guid.Empty); @@ -176,13 +169,13 @@ public async Task SettlementDomainService_ProcessSettlement_SettlementAggregateN [Fact] public async Task SettlementDomainService_ProcessSettlement_AddSettledFeeThrownException_SettlementProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetSettlementAggregateWithPendingMerchantFees(10))); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.transactionAggregateRepository.SetupSequence(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(0))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(1))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(2))) @@ -193,7 +186,7 @@ public async Task SettlementDomainService_ProcessSettlement_AddSettledFeeThrownE .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(7))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(8))) .ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.FeeIds.GetValueOrDefault(9))); - this.transactionAggregateRepository.SetupSequence(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()) @@ -205,165 +198,165 @@ public async Task SettlementDomainService_ProcessSettlement_AddSettledFeeThrownE .ReturnsAsync(Result.Success()) .ReturnsAsync(Result.Success()); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_ProcessSettlement_GetTransactionThrownException_SettlementProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetSettlementAggregateWithPendingMerchantFees(10))); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.transactionAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception()); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_ProcessSettlement_GetMerchantThrownException_SettlementProcessed() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetSettlementAggregateWithPendingMerchantFees(10))); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception()); SettlementCommands.ProcessSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.ProcessSettlement(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddMerchantFeePendingSettlement_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); SettlementCommands.AddMerchantFeePendingSettlementCommand command = new(TestData.TransactionId, TestData.CalculatedFeeValue, TestData.TransactionFeeCalculateDateTime, CalculationType.Fixed, TestData.TransactionFeeId, TestData.TransactionFeeValue, TestData.TransactionFeeSettlementDueDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.AddMerchantFeePendingSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddMerchantFeePendingSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddMerchantFeePendingSettlement_AggregateNotCreated_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptySettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); SettlementCommands.AddMerchantFeePendingSettlementCommand command = new(TestData.TransactionId, TestData.CalculatedFeeValue, TestData.TransactionFeeCalculateDateTime, CalculationType.Fixed, TestData.TransactionFeeId, TestData.TransactionFeeValue, TestData.TransactionFeeSettlementDueDate, TestData.MerchantId, TestData.EstateId); - var result = await settlementDomainService.AddMerchantFeePendingSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddMerchantFeePendingSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddSettledFeeToSettlement_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.AddSettledFeeToSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId, TestData.TransactionFeeId, TestData.TransactionId); - var result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddSettledFeeToSettlement_ImmediateSettlement_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.AddSettledFeeToSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId, TestData.TransactionFeeId, TestData.TransactionId); - var result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddSettledFeeToSettlement_FailedGettingMerchant_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.NotFound()); SettlementCommands.AddSettledFeeToSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId, TestData.TransactionFeeId, TestData.TransactionId); - var result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddSettledFeeToSettlement_SaveFailed_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.AddSettledFeeToSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId, TestData.TransactionFeeId, TestData.TransactionId); - var result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } [Fact] public async Task SettlementDomainService_AddSettledFeeToSettlement_ExceptionThrown_FeeAdded() { - settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(s => s.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCreatedSettlementAggregate())); - this.settlementAggregateRepository - .Setup(s => s.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService + .Setup(s => s.Save(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception()); - this.merchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); SettlementCommands.AddSettledFeeToSettlementCommand command = new(TestData.SettlementDate, TestData.MerchantId, TestData.EstateId, TestData.TransactionFeeId, TestData.TransactionId); - var result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); + Result result = await settlementDomainService.AddSettledFeeToSettlement(command, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } } diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs index 69af440f..946988f4 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs @@ -30,29 +30,15 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services{ public class TransactionDomainServiceTests{ #region Fields - //private readonly Mock EstateClient; - + private readonly Mock AggregateService; private readonly Mock OperatorProxy; - - private readonly Mock> ReconciliationAggregateRepository; - private readonly Mock SecurityServiceClient; - - private readonly Mock> TransactionAggregateRepository; - private readonly Mock> MerchantAggregateRepository; - private readonly TransactionDomainService TransactionDomainService; - private readonly Mock TransactionValidationService; - - private readonly Mock> FloatAggregateRepository; private readonly Mock MemoryCacheWrapper; private readonly Mock FeeCalculationManager; private readonly Mock TransactionReceiptBuilder; private readonly Mock MessagingServiceClient; - private readonly Mock> EstateAggregateRepository; - private readonly Mock> OperatorAggregateRepository; - private readonly Mock> ContractAggregateRepository; #endregion #region Constructors @@ -63,37 +49,24 @@ public TransactionDomainServiceTests(){ Logger.Initialise(NullLogger.Instance); - this.TransactionAggregateRepository = new Mock>(); - //this.EstateClient = new Mock(); + this.AggregateService= new Mock(); this.SecurityServiceClient = new Mock(); this.OperatorProxy = new Mock(); - this.ReconciliationAggregateRepository = new Mock>(); Func operatorProxyResolver = operatorName => { return this.OperatorProxy.Object; }; this.TransactionValidationService = new Mock(); - this.FloatAggregateRepository = new Mock>(); this.MemoryCacheWrapper = new Mock(); this.FeeCalculationManager = new Mock(); this.TransactionReceiptBuilder = new Mock(); this.MessagingServiceClient = new Mock(); - this.EstateAggregateRepository = new Mock>(); - this.OperatorAggregateRepository= new Mock>(); - this.MerchantAggregateRepository = new Mock>(); - this.ContractAggregateRepository = new Mock>(); - - this.TransactionDomainService = new TransactionDomainService(this.TransactionAggregateRepository.Object, + + this.TransactionDomainService = new TransactionDomainService(this.AggregateService.Object, operatorProxyResolver, - this.ReconciliationAggregateRepository.Object, this.TransactionValidationService.Object, this.SecurityServiceClient.Object, - this.FloatAggregateRepository.Object, this.MemoryCacheWrapper.Object, this.FeeCalculationManager.Object, this.TransactionReceiptBuilder.Object, - this.MessagingServiceClient.Object, - this.EstateAggregateRepository.Object, - this.OperatorAggregateRepository.Object, - this.MerchantAggregateRepository.Object, - this.ContractAggregateRepository.Object); + this.MessagingServiceClient.Object); } #endregion @@ -102,15 +75,13 @@ public TransactionDomainServiceTests(){ [Fact] public async Task TransactionDomainService_ProcessLogonTransaction_DeviceNeedsAdded_TransactionIsProcessed(){ - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptyTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); + this.AggregateService.Setup(e => e.GetLatest(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedMerchantAggregate())); - //this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionValidationService.Setup(t => t.ValidateLogonTransaction(It.IsAny(), It.IsAny(), It.IsAny(), @@ -135,9 +106,9 @@ public async Task TransactionDomainService_ProcessLogonTransaction_DeviceNeedsAd [Fact] public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIsProcessed(){ - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptyTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.TransactionValidationService.Setup(t => t.ValidateLogonTransaction(It.IsAny(), @@ -167,9 +138,9 @@ public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIs [InlineData(TransactionResponseCode.InvalidMerchantId)] [InlineData(TransactionResponseCode.InvalidDeviceIdentifier)] public async Task TransactionDomainService_ProcessLogonTransaction_ValidationFailed_TransactionIsProcessed(TransactionResponseCode responseCode){ - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptyTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.TransactionValidationService.Setup(t => t.ValidateLogonTransaction(It.IsAny(), It.IsAny(), @@ -195,9 +166,9 @@ public async Task TransactionDomainService_ProcessLogonTransaction_ValidationFai [Fact] public async Task TransactionDomainService_ProcessReconciliationTransaction_ReconciliationIsProcessed(){ - this.ReconciliationAggregateRepository.Setup(r => r.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(r => r.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(new ReconciliationAggregate()); - this.ReconciliationAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.TransactionValidationService.Setup(t => t.ValidateReconciliationTransaction(It.IsAny(), It.IsAny(), @@ -224,9 +195,9 @@ public async Task TransactionDomainService_ProcessReconciliationTransaction_Reco [InlineData(TransactionResponseCode.NoValidDevices)] [InlineData(TransactionResponseCode.InvalidDeviceIdentifier)] public async Task TransactionDomainService_ProcessReconciliationTransaction_ValidationFailed_ReconciliationIsProcessed(TransactionResponseCode responseCode){ - this.ReconciliationAggregateRepository.Setup(r => r.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(r => r.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(new ReconciliationAggregate()); - this.ReconciliationAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.TransactionValidationService.Setup(t => t.ValidateReconciliationTransaction(It.IsAny(), It.IsAny(), @@ -249,16 +220,16 @@ public async Task TransactionDomainService_ProcessReconciliationTransaction_Vali [Fact] public async Task TransactionDomainService_ProcessSaleTransaction_DeclinedByOperator_TransactionIsProcessed(){ - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(o => o.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptyTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); this.TransactionValidationService.Setup(t => t.ValidateSaleTransaction(It.IsAny(), It.IsAny(), @@ -303,16 +274,16 @@ public async Task TransactionDomainService_ProcessSaleTransaction_OperatorProxyT { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(o => o.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptyTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); this.TransactionValidationService.Setup(t => t.ValidateSaleTransaction(It.IsAny(), It.IsAny(), @@ -343,20 +314,20 @@ public async Task TransactionDomainService_ProcessSaleTransaction_OperatorProxyT public async Task TransactionDomainService_ProcessSaleTransaction_TransactionIsProcessed(){ this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(o => o.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); TransactionAggregate transactionAggregate = TestData.GetEmptyTransactionAggregate(); FloatAggregate floatAggregate = TestData.GetFloatAggregateWithCostValues(); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(transactionAggregate); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(floatAggregate); this.TransactionValidationService.Setup(t => t.ValidateSaleTransaction(It.IsAny(), It.IsAny(), @@ -406,18 +377,18 @@ public async Task TransactionDomainService_ProcessSaleTransaction_NoFloatFound_T { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.OperatorAggregateRepository.Setup(o => o.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(o => o.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedOperatorAggregate())); TransactionAggregate transactionAggregate = TestData.GetEmptyTransactionAggregate(); - - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(transactionAggregate); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.NotFound()); this.TransactionValidationService.Setup(t => t.ValidateSaleTransaction(It.IsAny(), It.IsAny(), @@ -470,9 +441,9 @@ public async Task TransactionDomainService_ProcessSaleTransaction_NoFloatFound_T [InlineData(TransactionResponseCode.ContractNotValidForMerchant)] [InlineData(TransactionResponseCode.ProductNotValidForMerchant)] public async Task TransactionDomainService_ProcessSaleTransaction_ValidationFailed_TransactionIsProcessed(TransactionResponseCode responseCode){ - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetEmptyTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); this.TransactionValidationService.Setup(t => t.ValidateSaleTransaction(It.IsAny(), @@ -484,7 +455,7 @@ public async Task TransactionDomainService_ProcessSaleTransaction_ValidationFail It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new TransactionValidationResult(responseCode, responseCode.ToString()))); - this.FloatAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); + this.AggregateService.Setup(f => f.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetEmptyFloatAggregate())); TransactionCommands.ProcessSaleTransactionCommand command = new TransactionCommands.ProcessSaleTransactionCommand(TestData.TransactionId, TestData.EstateId, @@ -503,9 +474,9 @@ public async Task TransactionDomainService_ProcessSaleTransaction_ValidationFail [Fact] public async Task TransactionDomainService_ResendTransactionReceipt_TransactionReceiptResendIsRequested(){ - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionWithReceiptRequestedAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success()); TransactionCommands.ResendTransactionReceiptCommand command = new(TestData.TransactionId, TestData.EstateId); var result = await this.TransactionDomainService.ResendTransactionReceipt(command, CancellationToken.None); @@ -622,12 +593,12 @@ public async Task TransactionDomainService_CalculateSettlementDate_CorrectDateRe [Fact] public async Task TransactionDomainService_CalculateFeesForTransaction_FeesCalculated() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant)); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant)); this.FeeCalculationManager.Setup(f => f.CalculateFees(It.IsAny>(), It.IsAny(), It.IsAny())).Returns(TestData.CalculatedMerchantFees); TransactionCommands.CalculateFeesForTransactionCommand command = new(TestData.TransactionId, TestData.TransactionDateTime, TestData.EstateId, TestData.MerchantId); @@ -639,11 +610,11 @@ public async Task TransactionDomainService_CalculateFeesForTransaction_FeesCalcu [Fact] public async Task TransactionDomainService_CalculateFeesForTransaction_MerchantWithImmediateSettlement_FeesCalculated() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant)); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.Merchant)); this.FeeCalculationManager.Setup(f => f.CalculateFees(It.IsAny>(), It.IsAny(), It.IsAny())).Returns(TestData.CalculatedMerchantFees); TransactionCommands.CalculateFeesForTransactionCommand command = new(TestData.TransactionId, TestData.TransactionDateTime, TestData.EstateId, TestData.MerchantId); @@ -655,13 +626,12 @@ public async Task TransactionDomainService_CalculateFeesForTransaction_MerchantW [Fact] public async Task TransactionDomainService_CalculateFeesForTransaction_NonMerchantFees_FeesCalculated() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); - //this.EstateClient.Setup(e => e.GetTransactionFeesForProduct(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(TestData.ContractProductTransactionFees); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.ContractAggregateRepository.Setup(c => c.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.ServiceProvider)); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedContractAggregateWithAProductAndTransactionFee(CalculationType.Fixed, FeeType.ServiceProvider)); this.FeeCalculationManager.Setup(f => f.CalculateFees(It.IsAny>(), It.IsAny(), It.IsAny())).Returns(TestData.CalculatedServiceProviderFees); TransactionCommands.CalculateFeesForTransactionCommand command = new(TestData.TransactionId, TestData.TransactionDateTime, TestData.EstateId, TestData.MerchantId); @@ -673,10 +643,9 @@ public async Task TransactionDomainService_CalculateFeesForTransaction_NonMercha [Fact] public async Task TransactionDomainService_CalculateFeesForTransaction_TransactionNotNeedingFeeCaclulation_FeesCalculated() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedLogonTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); - //this.EstateClient.Setup(e => e.GetTransactionFeesForProduct(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(TestData.ContractProductTransactionFees); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedLogonTransactionAggregate())); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); this.FeeCalculationManager.Setup(f => f.CalculateFees(It.IsAny>(), It.IsAny(), It.IsAny())).Returns(TestData.CalculatedServiceProviderFees); @@ -690,11 +659,11 @@ public async Task TransactionDomainService_CalculateFeesForTransaction_Transacti [Fact] public async Task TransactionDomainService_CalculateFeesForTransaction_NoFeesReturned_FeesCalculated() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); //this.EstateClient.Setup(e => e.GetTransactionFeesForProduct(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); @@ -708,8 +677,8 @@ public async Task TransactionDomainService_CalculateFeesForTransaction_NoFeesRet [Fact] public async Task TransactionDomainService_AddSettledMerchantFee_FeeAdded() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.TransactionFeeId))); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregateWithPendingFee(TestData.TransactionFeeId))); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); TransactionCommands.AddSettledMerchantFeeCommand command = new(TestData.TransactionId, TestData.CalculatedFeeValue, TestData.TransactionFeeCalculateDateTime, CalculationType.Fixed, TestData.TransactionFeeId, TestData.CalculatedFeeValue, TestData.SettlementDate, TestData.SettlementAggregateId); @@ -720,8 +689,8 @@ public async Task TransactionDomainService_AddSettledMerchantFee_FeeAdded() { [Fact] public async Task TransactionDomainService_AddSettledMerchantFee_SaveFailed_ResultFailed() { - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); - this.TransactionAggregateRepository.Setup(t => t.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(t => t.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); TransactionCommands.AddSettledMerchantFeeCommand command = new(TestData.TransactionId, TestData.CalculatedFeeValue, TestData.TransactionFeeCalculateDateTime, CalculationType.Fixed, TestData.TransactionFeeId, TestData.CalculatedFeeValue, TestData.SettlementDate, TestData.SettlementAggregateId); @@ -732,9 +701,9 @@ public async Task TransactionDomainService_AddSettledMerchantFee_SaveFailed_Resu [Fact] public async Task TransactionDomainService_SendCustomerEmailReceipt_ReceiptSent() { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); - this.EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(c => c.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithOperator()); this.TransactionReceiptBuilder.Setup(r => r.GetEmailReceiptMessage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync("EmailMessage"); this.MessagingServiceClient.Setup(m => m.SendEmail(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); @@ -747,7 +716,7 @@ public async Task TransactionDomainService_SendCustomerEmailReceipt_ReceiptSent( public async Task TransactionDomainService_SendCustomerEmailReceipt_GetTransactionFailed_ResultFailed() { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); TransactionCommands.SendCustomerEmailReceiptCommand command = new(TestData.EstateId, TestData.TransactionId, Guid.NewGuid(), TestData.CustomerEmailAddress); var result = await this.TransactionDomainService.SendCustomerEmailReceipt(command, CancellationToken.None); @@ -758,7 +727,7 @@ public async Task TransactionDomainService_SendCustomerEmailReceipt_GetTransacti public async Task TransactionDomainService_ResendCustomerEmailReceipt_ReceiptSent() { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.GetCompletedAuthorisedSaleTransactionAggregate())); this.MessagingServiceClient.Setup(m => m.ResendEmail(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); TransactionCommands.ResendCustomerEmailReceiptCommand command = new(TestData.EstateId, TestData.TransactionId); @@ -770,7 +739,7 @@ public async Task TransactionDomainService_ResendCustomerEmailReceipt_ReceiptSen public async Task TransactionDomainService_ResendCustomerEmailReceipt_GetTransactionFailed_ResultFailed() { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionAggregateRepository.Setup(t => t.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(t => t.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); TransactionCommands.ResendCustomerEmailReceiptCommand command = new(TestData.EstateId, TestData.TransactionId); var result = await this.TransactionDomainService.ResendCustomerEmailReceipt(command, CancellationToken.None); diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionValidationServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionValidationServiceTests.cs index 2b607e1c..2048559a 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionValidationServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionValidationServiceTests.cs @@ -26,11 +26,8 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services; public class TransactionValidationServiceTests { private readonly TransactionValidationService TransactionValidationService; private readonly Mock SecurityServiceClient; - - private readonly Mock> StateRepository; private readonly Mock EventStoreContext; - private readonly Mock> EstateAggregateRepository; - private readonly Mock> MerchantAggregateRepository; + private readonly Mock AggregateService; public TransactionValidationServiceTests() { IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build(); ConfigurationReader.Initialise(configurationRoot); @@ -38,23 +35,19 @@ public TransactionValidationServiceTests() { Logger.Initialise(NullLogger.Instance); this.SecurityServiceClient = new Mock(); - this.StateRepository = new Mock>(); this.EventStoreContext = new Mock(); - this.EstateAggregateRepository = new Mock>(); - this.MerchantAggregateRepository = new Mock>(); + this.AggregateService = new Mock(); this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionValidationService = new TransactionValidationService(this.EventStoreContext.Object, - this.EstateAggregateRepository.Object, - this.MerchantAggregateRepository.Object); + this.TransactionValidationService = new TransactionValidationService(this.EventStoreContext.Object, this.AggregateService.Object); } [Fact] public async Task ValidateLogonTransactionX_ValidationSuccessful_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); Result result = await this.TransactionValidationService.ValidateLogonTransaction(TestData.EstateId, TestData.MerchantId, @@ -66,7 +59,7 @@ public async Task ValidateLogonTransactionX_ValidationSuccessful_CorrectResponse [Fact] public async Task ValidateLogonTransactionX_InvalidEstate_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.NotFound("Estate not found")); Result result = await this.TransactionValidationService.ValidateLogonTransaction(TestData.EstateId, TestData.MerchantId, @@ -79,7 +72,7 @@ public async Task ValidateLogonTransactionX_InvalidEstate_CorrectResponseReturne [Fact] public async Task ValidateLogonTransactionX_FailureWhileGettingEstate_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure("Failed")); Result result = await this.TransactionValidationService.ValidateLogonTransaction(TestData.EstateId, TestData.MerchantId, @@ -92,9 +85,9 @@ public async Task ValidateLogonTransactionX_FailureWhileGettingEstate_CorrectRes [Fact] public async Task ValidateLogonTransactionX_InvalidMerchant_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.NotFound("Merchant Not Found")); Result result = await this.TransactionValidationService.ValidateLogonTransaction(TestData.EstateId, TestData.MerchantId, @@ -107,9 +100,9 @@ public async Task ValidateLogonTransactionX_InvalidMerchant_CorrectResponseRetur [Fact] public async Task ValidateLogonTransactionX_FailureWhileGettingMerchant_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure("Merchant Not Found")); Result result = await this.TransactionValidationService.ValidateLogonTransaction(TestData.EstateId, TestData.MerchantId, @@ -122,9 +115,9 @@ public async Task ValidateLogonTransactionX_FailureWhileGettingMerchant_CorrectR [Fact] public async Task ValidateLogonTransactionX_InvalidDeviceId_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); Result result = await this.TransactionValidationService.ValidateLogonTransaction(TestData.EstateId, TestData.MerchantId, @@ -137,9 +130,9 @@ public async Task ValidateLogonTransactionX_InvalidDeviceId_CorrectResponseRetur [Fact] public async Task ValidateLogonTransactionX_MerchantHasNoDevices_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); @@ -153,9 +146,9 @@ public async Task ValidateLogonTransactionX_MerchantHasNoDevices_CorrectResponse [Fact] public async Task ValidateReconciliationTransactionX_ValidationSuccessful_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -168,7 +161,7 @@ public async Task ValidateReconciliationTransactionX_ValidationSuccessful_Correc [Fact] public async Task ValidateReconciliationTransactionX_InvalidEstate_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.NotFound("Estate Not Found")); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -181,7 +174,7 @@ public async Task ValidateReconciliationTransactionX_InvalidEstate_CorrectRespon [Fact] public async Task ValidateReconciliationTransactionX_FailureWhileGettingEstate_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure("Failed")); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -194,9 +187,9 @@ public async Task ValidateReconciliationTransactionX_FailureWhileGettingEstate_C [Fact] public async Task ValidateReconciliationTransactionX_InvalidMerchant_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.NotFound("Merchant Not Found")); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -209,9 +202,9 @@ public async Task ValidateReconciliationTransactionX_InvalidMerchant_CorrectResp [Fact] public async Task ValidateReconciliationTransactionX_FailureWhileGettingMerchant_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure("Merchant Not Found")); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -224,9 +217,9 @@ public async Task ValidateReconciliationTransactionX_FailureWhileGettingMerchant [Fact] public async Task ValidateReconciliationTransactionX_InvalidDeviceId_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -239,9 +232,9 @@ public async Task ValidateReconciliationTransactionX_InvalidDeviceId_CorrectResp [Fact] public async Task ValidateReconciliationTransactionX_MerchantHasNoDevices_CorrectResponseReturned() { - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); Result result = await this.TransactionValidationService.ValidateReconciliationTransaction(TestData.EstateId, TestData.MerchantId, @@ -256,9 +249,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_DeviceNot { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -279,7 +272,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_EstateFou { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -300,7 +293,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_EstateFou { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperatorDeleted())); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -321,7 +314,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_EstateFou { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -342,7 +335,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_EstateFou { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -363,7 +356,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_EstateNot { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.NotFound("Estate Not Found")); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -384,7 +377,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_GetEstate { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure("Failed")); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -405,9 +398,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_InvalidCo { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) @@ -431,9 +424,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_InvalidPr { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState)); @@ -458,9 +451,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_InvalidTr { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, TestData.MerchantId, @@ -480,9 +473,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantD { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.CreatedMerchantAggregate()); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -503,9 +496,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantD { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState)); @@ -528,9 +521,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantH { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithNoContracts(SettlementSchedule.Immediate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithNoContracts(SettlementSchedule.Immediate)); @@ -555,9 +548,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantN { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(m => m.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionStateNoCredit)); @@ -579,9 +572,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantN { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.NotFound("Merchant not found")); @@ -603,9 +596,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_FailedGet { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Failure("Merchant not found")); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -626,11 +619,11 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantN { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.SetupSequence(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)) + this.AggregateService.SetupSequence(m => m.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate))) .ReturnsAsync(Result.NotFound()); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) @@ -654,10 +647,10 @@ public async Task TransactionValidationService_ValidateSaleTransaction_FailedGet { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.SetupSequence(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) - .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)) + this.AggregateService.SetupSequence(m => m.Get(It.IsAny(), It.IsAny())) + .ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate))) .ReturnsAsync(Result.NotFound()); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) @@ -681,10 +674,10 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantO { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.SetupSequence(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.SetupSequence(m => m.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithDevice()); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -705,9 +698,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantO { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithDeletedOperator(SettlementSchedule.Immediate)); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -728,9 +721,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_OperatorN { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator2())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId, @@ -751,9 +744,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_ProductId { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState)); @@ -776,9 +769,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_Successfu { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.SetupSequence(m => m.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); + this.AggregateService.SetupSequence(m => m.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState)); @@ -800,9 +793,9 @@ public async Task TransactionValidationService_ValidateSaleTransaction_FailedGet { this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.EstateAggregateRepository.Setup(e => e.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); - this.MerchantAggregateRepository.Setup(m => m.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(e => e.Get(It.IsAny(), It.IsAny())) .ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)); this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny(), It.IsAny(), It.IsAny())) diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs index 46cc76e3..9cf7fe73 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/VoucherDomainServiceTests.cs @@ -25,8 +25,7 @@ public class VoucherDomainServiceTests { #region Methods - private Mock> VoucherAggregateRepository; - private Mock> EstateAggregateRepository; + private Mock AggregateService; private Mock> DbContextFactory; private VoucherDomainService VoucherDomainService; public VoucherDomainServiceTests() { @@ -35,21 +34,18 @@ public VoucherDomainServiceTests() { Logger.Initialise(NullLogger.Instance); - this.VoucherAggregateRepository = new Mock>(); - this.EstateAggregateRepository = new Mock>(); + this.AggregateService = new Mock(); this.DbContextFactory = new Mock>(); - this.VoucherDomainService = new VoucherDomainService(VoucherAggregateRepository.Object, - DbContextFactory.Object, - EstateAggregateRepository.Object); + this.VoucherDomainService = new VoucherDomainService(this.AggregateService.Object, DbContextFactory.Object); } [Fact] public async Task VoucherDomainService_IssueVoucher_EstateWithNoOperators_ErrorThrown() { - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new VoucherAggregate()); + this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); DbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.CreatedEstateAggregate()); var result = await this.VoucherDomainService.IssueVoucher(TestData.VoucherId, TestData.OperatorId, TestData.EstateId, @@ -101,8 +97,8 @@ public async Task VoucherDomainService_IssueVoucher_EstateWithNoOperators_ErrorT [Fact] public async Task VoucherDomainService_IssueVoucher_InvalidEstate_ErrorThrown() { - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new VoucherAggregate()); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); DbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); @@ -121,8 +117,8 @@ public async Task VoucherDomainService_IssueVoucher_InvalidEstate_ErrorThrown() [Fact] public async Task VoucherDomainService_IssueVoucher_OperatorNotSupportedByEstate_ErrorThrown() { - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new VoucherAggregate()); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); DbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); @@ -142,9 +138,9 @@ public async Task VoucherDomainService_IssueVoucher_OperatorNotSupportedByEstate [Fact] public async Task VoucherDomainService_IssueVoucher_VoucherIssued() { - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(new VoucherAggregate()); - VoucherAggregateRepository.Setup(v => v.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); + this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(new VoucherAggregate())); + this.AggregateService.Setup(v => v.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); DbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); @@ -165,10 +161,10 @@ public async Task VoucherDomainService_IssueVoucher_VoucherIssued() { [Fact] public async Task VoucherDomainService_RedeemVoucher_InvalidEstate_ErrorThrown() { - - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())) + + AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.Aggregates.EmptyEstateAggregate); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); context.VoucherProjectionStates.Add(new TransactionProcessor.Database.Entities.VoucherProjectionState() { @@ -190,11 +186,11 @@ public async Task VoucherDomainService_RedeemVoucher_InvalidEstate_ErrorThrown() [Fact] public async Task VoucherDomainService_RedeemVoucher_VoucherRedeemed() { - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); - VoucherAggregateRepository.Setup(v => v.SaveChanges(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(v => v.Save(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); context.VoucherProjectionStates.Add(new TransactionProcessor.Database.Entities.VoucherProjectionState() { @@ -217,9 +213,9 @@ public async Task VoucherDomainService_RedeemVoucher_VoucherRedeemed() { [Fact] public async Task VoucherDomainService_RedeemVoucher_VoucherNotFound_ErrorThrown() { - VoucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateService.Setup(v => v.GetLatest(It.IsAny(), It.IsAny())) .ReturnsAsync(Result.Success(TestData.GetVoucherAggregateWithRecipientMobile())); - EstateAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); + this.AggregateService.Setup(f => f.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.CreatedEstateAggregate())); EstateManagementGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N")); DbContextFactory.Setup(d => d.GetContext(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(context); diff --git a/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs b/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs index 0203487d..f28da5e2 100644 --- a/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs +++ b/TransactionProcessor.BusinessLogic/EventHandling/MerchantDomainEventHandler.cs @@ -13,6 +13,7 @@ using TransactionProcessor.BusinessLogic.Common; using TransactionProcessor.BusinessLogic.Events; using TransactionProcessor.BusinessLogic.Requests; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Models.Merchant; using TransactionProcessor.Repository; using MakeMerchantDepositRequest = TransactionProcessor.DataTransferObjects.Requests.Merchant.MakeMerchantDepositRequest; @@ -24,19 +25,14 @@ public class MerchantDomainEventHandler : IDomainEventHandler #region Fields private readonly IMediator Mediator; - - private readonly IAggregateRepository MerchantAggregateRepository; - private readonly ITransactionProcessorReadModelRepository EstateReportingRepository; #endregion #region Constructors - public MerchantDomainEventHandler(IAggregateRepository merchantAggregateRepository, - ITransactionProcessorReadModelRepository estateReportingRepository, + public MerchantDomainEventHandler(ITransactionProcessorReadModelRepository estateReportingRepository, IMediator mediator) { - this.MerchantAggregateRepository = merchantAggregateRepository; this.EstateReportingRepository = estateReportingRepository; this.Mediator = mediator; } diff --git a/TransactionProcessor.BusinessLogic/EventHandling/VoucherDomainEventHandler.cs b/TransactionProcessor.BusinessLogic/EventHandling/VoucherDomainEventHandler.cs index f724ad9a..3413433e 100644 --- a/TransactionProcessor.BusinessLogic/EventHandling/VoucherDomainEventHandler.cs +++ b/TransactionProcessor.BusinessLogic/EventHandling/VoucherDomainEventHandler.cs @@ -1,6 +1,7 @@ using Shared.Results; using SimpleResults; using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Database.Contexts; using TransactionProcessor.Database.Entities; using TransactionProcessor.DomainEvents; @@ -47,15 +48,9 @@ public class VoucherDomainEventHandler : IDomainEventHandler /// private readonly ISecurityServiceClient SecurityServiceClient; - /// - /// The token response - /// - private TokenResponse TokenResponse; + private readonly IAggregateService AggregateService; - /// - /// The voucher aggregate repository - /// - private readonly IAggregateRepository VoucherAggregateRepository; + private TokenResponse TokenResponse; private const String ConnectionStringIdentifier = "EstateReportingReadModel"; @@ -63,22 +58,14 @@ public class VoucherDomainEventHandler : IDomainEventHandler #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// The security service client. - /// The voucher aggregate repository. - /// The database context factory. - /// The messaging service client. - /// The file system. public VoucherDomainEventHandler(ISecurityServiceClient securityServiceClient, - IAggregateRepository voucherAggregateRepository, + IAggregateService aggregateService, Shared.EntityFramework.IDbContextFactory dbContextFactory, IMessagingServiceClient messagingServiceClient, IFileSystem fileSystem) { this.SecurityServiceClient = securityServiceClient; - this.VoucherAggregateRepository = voucherAggregateRepository; + this.AggregateService = aggregateService; this.DbContextFactory = dbContextFactory; this.MessagingServiceClient = messagingServiceClient; this.FileSystem = fileSystem; @@ -160,7 +147,7 @@ private async Task HandleSpecificDomainEvent(VoucherDomainEvents.Voucher CancellationToken cancellationToken) { // Get the voucher aggregate - Result voucherAggregateResult = await this.VoucherAggregateRepository.GetLatestVersion(domainEvent.VoucherId, cancellationToken); + Result voucherAggregateResult = await this.AggregateService.Get(domainEvent.VoucherId, cancellationToken); if (voucherAggregateResult.IsFailed) return ResultHelpers.CreateFailure(voucherAggregateResult); diff --git a/TransactionProcessor.BusinessLogic/Manager/IVoucherManagementManager.cs b/TransactionProcessor.BusinessLogic/Manager/IVoucherManagementManager.cs index fa911802..34241a95 100644 --- a/TransactionProcessor.BusinessLogic/Manager/IVoucherManagementManager.cs +++ b/TransactionProcessor.BusinessLogic/Manager/IVoucherManagementManager.cs @@ -4,8 +4,10 @@ using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; using Shared.Exceptions; +using Shared.Results; using SimpleResults; using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Database.Contexts; using TransactionProcessor.Database.Entities; @@ -47,10 +49,7 @@ public class VoucherManagementManager : IVoucherManagementManager /// private readonly Shared.EntityFramework.IDbContextFactory DbContextFactory; - /// - /// The voucher aggregate repository - /// - private readonly IAggregateRepository VoucherAggregateRepository; + private readonly IAggregateService AggregateService; private const String ConnectionStringIdentifier = "EstateReportingReadModel"; @@ -58,16 +57,11 @@ public class VoucherManagementManager : IVoucherManagementManager #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// The database context factory. - /// The voucher aggregate repository. public VoucherManagementManager(Shared.EntityFramework.IDbContextFactory dbContextFactory, - IAggregateRepository voucherAggregateRepository) + IAggregateService aggregateService) { this.DbContextFactory = dbContextFactory; - this.VoucherAggregateRepository = voucherAggregateRepository; + this.AggregateService = aggregateService; } #endregion @@ -96,9 +90,13 @@ public async Task> GetVoucherByCode(Guid estateId, } // Get the aggregate - VoucherAggregate voucherAggregate = await this.VoucherAggregateRepository.GetLatestVersion(voucher.VoucherId, cancellationToken); + Result result= await this.AggregateService.Get(voucher.VoucherId, cancellationToken); - return voucherAggregate.GetVoucher(); + if (result.IsFailed) { + return ResultHelpers.CreateFailure(result); + } + + return result.Data.GetVoucher(); } public async Task> GetVoucherByTransactionId(Guid estateId, @@ -114,9 +112,14 @@ public async Task> GetVoucherByTransactionId(Guid estateId, } // Get the aggregate - VoucherAggregate voucherAggregate = await this.VoucherAggregateRepository.GetLatestVersion(voucher.VoucherId, cancellationToken); + Result result = await this.AggregateService.Get(voucher.VoucherId, cancellationToken); + + if (result.IsFailed) + { + return ResultHelpers.CreateFailure(result); + } - return voucherAggregate.GetVoucher(); + return result.Data.GetVoucher(); } #endregion diff --git a/TransactionProcessor.BusinessLogic/Manager/TransactionProcessorManager.cs b/TransactionProcessor.BusinessLogic/Manager/TransactionProcessorManager.cs index ea6f2691..3d2546da 100644 --- a/TransactionProcessor.BusinessLogic/Manager/TransactionProcessorManager.cs +++ b/TransactionProcessor.BusinessLogic/Manager/TransactionProcessorManager.cs @@ -8,6 +8,7 @@ using Shared.Results; using SimpleResults; using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Models.Contract; using TransactionProcessor.Models.Merchant; using TransactionProcessor.Models.Settlement; @@ -26,30 +27,17 @@ public class TransactionProcessorManager : ITransactionProcessorManager #region Fields private readonly ITransactionProcessorReadModelRepository TransactionProcessorReadModelRepository; - - private readonly IAggregateRepository EstateAggregateRepository; - - private readonly IAggregateRepository ContractAggregateRepository; - - private readonly IAggregateRepository MerchantAggregateRepository; - - private readonly IAggregateRepository OperatorAggregateRepository; + private readonly IAggregateService AggregateService; #endregion #region Constructors public TransactionProcessorManager(ITransactionProcessorReadModelRepository transactionProcessorReadModelRepository, - IAggregateRepository estateAggregateRepository, - IAggregateRepository contractAggregateRepository, - IAggregateRepository merchantAggregateRepository, - IAggregateRepository operatorAggregateRepository) + IAggregateService aggregateService) { this.TransactionProcessorReadModelRepository = transactionProcessorReadModelRepository; - this.EstateAggregateRepository = estateAggregateRepository; - this.ContractAggregateRepository = contractAggregateRepository; - this.MerchantAggregateRepository = merchantAggregateRepository; - this.OperatorAggregateRepository = operatorAggregateRepository; + this.AggregateService = aggregateService; } #endregion @@ -71,7 +59,7 @@ public async Task> GetContract(Guid estateId, Guid contractId, CancellationToken cancellationToken) { - Result getContractResult = await this.ContractAggregateRepository.GetLatestVersion(contractId, cancellationToken); + Result getContractResult = await this.AggregateService.GetLatest(contractId, cancellationToken); if (getContractResult.IsFailed) return ResultHelpers.CreateFailure(getContractResult); @@ -89,7 +77,7 @@ public async Task> GetContract(Guid estateId, public async Task> GetEstate(Guid estateId, CancellationToken cancellationToken){ - Result getEstateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); + Result getEstateResult = await this.AggregateService.GetLatest(estateId, cancellationToken); if (getEstateResult.IsFailed) return ResultHelpers.CreateFailure(getEstateResult); @@ -104,7 +92,7 @@ public async Task> GetContract(Guid estateId, { foreach (Operator @operator in estateModel.Operators) { - OperatorAggregate operatorAggregate = await this.OperatorAggregateRepository.GetLatestVersion(@operator.OperatorId, cancellationToken); + OperatorAggregate operatorAggregate = await this.AggregateService.GetLatest(@operator.OperatorId, cancellationToken); @operator.Name = operatorAggregate.Name; } } @@ -127,7 +115,7 @@ public async Task> GetMerchant(Guid estateId, Guid merchantId, CancellationToken cancellationToken) { - Result getMerchantResult = await this.MerchantAggregateRepository.GetLatestVersion(merchantId, cancellationToken); + Result getMerchantResult = await this.AggregateService.GetLatest(merchantId, cancellationToken); if (getMerchantResult.IsFailed) return ResultHelpers.CreateFailure(getMerchantResult); @@ -141,11 +129,11 @@ public async Task> GetMerchant(Guid estateId, if (merchantModel.Operators != null) { - var operators = new List(); + List operators = new(); foreach (Models.Merchant.Operator @operator in merchantModel.Operators) { - OperatorAggregate operatorAggregate = await this.OperatorAggregateRepository.GetLatestVersion(@operator.OperatorId, cancellationToken); - var newOperator = @operator with { Name = operatorAggregate.Name }; + OperatorAggregate operatorAggregate = await this.AggregateService.GetLatest(@operator.OperatorId, cancellationToken); + Models.Merchant.Operator newOperator = @operator with { Name = operatorAggregate.Name }; operators.Add(newOperator); } } @@ -161,7 +149,7 @@ public async Task>> GetMerchantContracts(Guid estateId, if (getMerchantContractsResult.IsFailed) return ResultHelpers.CreateFailure(getMerchantContractsResult); - var contractModels = getMerchantContractsResult.Data; + List contractModels = getMerchantContractsResult.Data; if (contractModels.Any() == false) return Result.NotFound($"No contracts for Estate {estateId} and Merchant {merchantId}"); @@ -171,10 +159,10 @@ public async Task>> GetMerchantContracts(Guid estateId, public async Task>> GetMerchants(Guid estateId, CancellationToken cancellationToken) { - var getMerchantsResult = await this.TransactionProcessorReadModelRepository.GetMerchants(estateId, cancellationToken); + Result> getMerchantsResult = await this.TransactionProcessorReadModelRepository.GetMerchants(estateId, cancellationToken); if (getMerchantsResult.IsFailed) return ResultHelpers.CreateFailure(getMerchantsResult); - var merchants = getMerchantsResult.Data; + List merchants = getMerchantsResult.Data; if (merchants == null || merchants.Any() == false) { return Result.NotFound($"No Merchants found for estate Id {estateId}"); @@ -191,7 +179,7 @@ public async Task>> GetMerchants(Guid estateId, { // TODO: this will need updated to handle merchant specific fees when that is available - Result getContractResult = await this.ContractAggregateRepository.GetLatestVersion(contractId, cancellationToken); + Result getContractResult = await this.AggregateService.GetLatest(contractId, cancellationToken); if (getContractResult.IsFailed) return ResultHelpers.CreateFailure(getContractResult); ContractAggregate contract = getContractResult.Data; @@ -210,23 +198,14 @@ public async Task>> GetMerchants(Guid estateId, } return Result.Success(product.TransactionFees); - } - //public async Task> GetFileDetails(Guid estateId, Guid fileId, CancellationToken cancellationToken){ - // var getFileDetailsResult= await this.EstateManagementRepository.GetFileDetails(estateId, fileId, cancellationToken); - // if (getFileDetailsResult.IsFailed) - // return ResultHelpers.CreateFailure(getFileDetailsResult); - - // return Result.Success(getFileDetailsResult.Data); - //} - public async Task> GetOperator(Guid estateId, Guid operatorId, CancellationToken cancellationToken) { - var getOperatorResult = await this.OperatorAggregateRepository.GetLatestVersion(operatorId, cancellationToken); + Result getOperatorResult = await this.AggregateService.GetLatest(operatorId, cancellationToken); if (getOperatorResult.IsFailed) return ResultHelpers.CreateFailure(getOperatorResult); - var operatorAggregate = getOperatorResult.Data; + OperatorAggregate operatorAggregate = getOperatorResult.Data; if (operatorAggregate.IsCreated == false) { return Result.NotFound($"No operator found with Id [{operatorId}]"); diff --git a/TransactionProcessor.BusinessLogic/RequestHandlers/SettlementRequestHandler.cs b/TransactionProcessor.BusinessLogic/RequestHandlers/SettlementRequestHandler.cs index 0102ce74..3a7d3d56 100644 --- a/TransactionProcessor.BusinessLogic/RequestHandlers/SettlementRequestHandler.cs +++ b/TransactionProcessor.BusinessLogic/RequestHandlers/SettlementRequestHandler.cs @@ -24,14 +24,14 @@ public class SettlementRequestHandler : IRequestHandler>> { private readonly ISettlementDomainService SettlementDomainService; - private readonly IAggregateRepository SettlementAggregateRepository; + private readonly IAggregateService AggregateService; private readonly ITransactionProcessorManager TransactionProcessorManager; public SettlementRequestHandler(ISettlementDomainService settlementDomainService, - IAggregateRepository settlementAggregateRepository, + IAggregateService aggregateService, ITransactionProcessorManager transactionProcessorManager) { this.SettlementDomainService = settlementDomainService; - this.SettlementAggregateRepository = settlementAggregateRepository; + this.AggregateService = aggregateService; this.TransactionProcessorManager = transactionProcessorManager; } @@ -57,7 +57,7 @@ public async Task> Handle(SettlementQueries.GetPendi // Convert the date passed in to a guid Guid aggregateId = Helpers.CalculateSettlementAggregateId(query.SettlementDate, query.MerchantId, query.EstateId); - Result getSettlementResult = await this.SettlementAggregateRepository.GetLatestVersion(aggregateId, cancellationToken); + Result getSettlementResult = await this.AggregateService.GetLatest(aggregateId, cancellationToken); if (getSettlementResult.IsFailed) return getSettlementResult; diff --git a/TransactionProcessor.BusinessLogic/Services/AggregateService.cs b/TransactionProcessor.BusinessLogic/Services/AggregateService.cs new file mode 100644 index 00000000..1ce03b0b --- /dev/null +++ b/TransactionProcessor.BusinessLogic/Services/AggregateService.cs @@ -0,0 +1,359 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using Prometheus; +using Shared.DomainDrivenDesign.EventSourcing; +using Shared.EventStore.Aggregate; +using SimpleResults; +using SixLabors.Fonts.Tables.AdvancedTypographic; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.Metrics; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Shared.Logger; +using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Services; + +namespace TransactionProcessor.BusinessLogic.Services { + public interface IAggregateRepositoryResolver { + IAggregateRepository Resolve() where TAggregate : Aggregate where TEvent : DomainEvent; + } + + public class AggregateRepositoryResolver : IAggregateRepositoryResolver { + private readonly IServiceProvider _provider; + + public AggregateRepositoryResolver(IServiceProvider provider) { + _provider = provider; + } + + public IAggregateRepository Resolve() where TAggregate : Aggregate where TEvent : DomainEvent { + Type repoType = typeof(IAggregateRepository<,>).MakeGenericType(typeof(TAggregate), typeof(TEvent)); + return (IAggregateRepository)_provider.GetRequiredService(repoType); + } + } + + public interface IAggregateService { + Task> Get(Guid aggregateId, + CancellationToken cancellationToken) where TAggregate : Aggregate, new(); + + Task> GetLatest(Guid aggregateId, + CancellationToken cancellationToken) where TAggregate : Aggregate, new(); + + Task> GetLatestFromLastEvent(Guid aggregateId, + CancellationToken cancellationToken) where TAggregate : Aggregate, new(); + + Task> GetLatestAggregateAsync(Guid aggregateId, + Func, Guid, CancellationToken, Task>> getLatestVersionFunc, + CancellationToken cancellationToken) where TAggregate : Aggregate, new(); + + Task Save(TAggregate aggregate, + CancellationToken cancellationToken) where TAggregate : Aggregate, new(); + } + + public class AggregateService : IAggregateService { + private readonly IAggregateRepositoryResolver AggregateRepositoryResolver; + private readonly AggregateMemoryCache Cache; + private readonly List<(Type, MemoryCacheEntryOptions, Object)> AggregateTypes; + + public AggregateService(IAggregateRepositoryResolver aggregateRepositoryResolver, + IMemoryCache cache) { + this.AggregateRepositoryResolver = aggregateRepositoryResolver; + this.Cache = new AggregateMemoryCache(cache); + + //We update this list to contain MemoryCacheEntryOptions + // TODO: We might make this configurable in the future + this.AggregateTypes = new(); + + // Set default caching options + MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(30)) + .RegisterPostEvictionCallback(AggregateService.EvictionCallback); + + this.AggregateTypes.Add((typeof(EstateAggregate), memoryCacheEntryOptions, new Object())); + } + + internal static void EvictionCallback(Object key, + Object value, + EvictionReason reason, + Object state) + { + Logger.LogWarning($"Key [{key}] of type [{value.GetType()}] removed from the cache {reason.ToString()}"); + } + + internal (Type, MemoryCacheEntryOptions, Object) GetAggregateType() where TAggregate : Aggregate, new() + { + return this.AggregateTypes.SingleOrDefault(a => a.Item1 == typeof(TAggregate)); + } + + internal void SetCache((Type, MemoryCacheEntryOptions, Object) aggregateType, + Aggregate aggregate) where TAggregate : Aggregate, new() + { + //Changed the trace here. + //We have at least one scenario where something in aggregateType is null, and stopped us actually setting the cache! + //This approach should be safer. + if (aggregate == null) + { + Logger.LogWarning($"aggregate is null"); + } + + Logger.LogWarning($"About to save to cache."); + + String g = typeof(TAggregate).Name; + String key = $"{g}-{aggregate.AggregateId}"; + + this.Cache.Set(key, aggregate, aggregateType.Item2); + } + + public async Task> Get(Guid aggregateId, + CancellationToken cancellationToken) where TAggregate : Aggregate, new() + { + Debug.WriteLine("In Get"); + (Type, MemoryCacheEntryOptions, Object) at = GetAggregateType(); + TAggregate aggregate = default; + String g = typeof(TAggregate).Name; + String key = $"{g}-{aggregateId}"; + + // Check the cache + if (at != default && this.Cache.TryGetValue(key, out aggregate)) + { + return Result.Success(aggregate); + } + + if (at == default) + { + // We don't use caching for this aggregate so just hit GetLatest + Result getResult = await this.GetLatest(aggregateId, cancellationToken); + + if (getResult.IsFailed) { + return getResult; + } + + return Result.Success(getResult.Data); + } + + try + { + // Lock + Monitor.Enter(at.Item3); + + if (this.Cache.TryGetValueWithMetrics(key, out TAggregate cachedAggregate)) + { + return Result.Success(cachedAggregate); + } + else + { + // Not found in cache so call GetLatest + SimpleResults.Result aggregateResult = this.GetLatest(aggregateId, cancellationToken).Result; + + if (aggregateResult.IsSuccess) + { + aggregate = aggregateResult.Data; + this.SetCache(at, aggregateResult.Data); + return Result.Success(aggregate); + } + else + { + Logger.LogWarning($"aggregateResult failed {aggregateResult.Message}"); + return aggregateResult; + } + } + } + finally + { + // Release + Monitor.Exit(at.Item3); + } + } + + public async Task> GetLatest(Guid aggregateId, + CancellationToken cancellationToken) where TAggregate : Aggregate, new() { + return await this.GetLatestAggregateAsync(aggregateId, (repo, + id, + cancellation) => repo.GetLatestVersion(id, cancellation), cancellationToken); + } + + public async Task> GetLatestFromLastEvent(Guid aggregateId, + CancellationToken cancellationToken) where TAggregate : Aggregate, new() { + return await this.GetLatestAggregateAsync(aggregateId, (repo, + id, + cancellation) => repo.GetLatestVersionFromLastEvent(id, cancellation), cancellationToken); + } + + public async Task> GetLatestAggregateAsync(Guid aggregateId, + Func, Guid, CancellationToken, Task>> getLatestVersionFunc, + CancellationToken cancellationToken) where TAggregate : Aggregate, new() { + Stopwatch stopwatch = Stopwatch.StartNew(); + IAggregateRepository repository = this.AggregateRepositoryResolver.Resolve(); + + String g = typeof(TAggregate).Name; + String m = $"AggregateService"; + Counter counterCalls = AggregateService.GetCounterMetric($"{m}_{g}_times_rehydrated"); + Histogram histogramMetric = AggregateService.GetHistogramMetric($"{m}_{g}"); + + counterCalls.Inc(); + TAggregate aggregate = null; + try { + var aggregateResult = await getLatestVersionFunc(repository, aggregateId, cancellationToken); + if (aggregateResult.IsFailed) + return aggregateResult; + aggregate = aggregateResult.Data; + } + catch (Exception ex) { + return Result.Failure(ex.Message); + } + + stopwatch.Stop(); + histogramMetric.Observe(stopwatch.Elapsed.TotalSeconds); + + return Result.Success(aggregate); + } + + + public async Task Save(TAggregate aggregate, + CancellationToken cancellationToken) where TAggregate : Aggregate, new() { + Stopwatch stopwatch = Stopwatch.StartNew(); + IAggregateRepository repository = this.AggregateRepositoryResolver.Resolve(); + + String g = typeof(TAggregate).Name; + String m = $"AggregateService"; + Counter counterCalls = AggregateService.GetCounterMetric($"{m}_{g}_times_saved"); + Histogram histogramMetric = AggregateService.GetHistogramMetric($"{m}_{g}"); + + counterCalls.Inc(); + + // TODO: Check the pending events so dont save blindly, this would need a change to the base aggregate ? + Result result = await repository.SaveChanges(aggregate, cancellationToken); + + stopwatch.Stop(); + + histogramMetric.Observe(stopwatch.Elapsed.TotalSeconds); + + if (result.IsFailed) + { + // Get out before any caching + return result; + } + + (Type, MemoryCacheEntryOptions, Object) at = GetAggregateType(); + + if (at != default) { + this.SetCache(at, aggregate); + } + + return result; + } + + public static readonly ConcurrentDictionary DynamicCounter = new(); + + public static readonly ConcurrentDictionary DynamicHistogram = new(); + + private static readonly Func FormatMetricName = (methodName, + metricType) => $"{methodName}_{metricType}"; + + public static Histogram GetHistogramMetric(String methodName) + { + String n = AggregateService.FormatMetricName(methodName, nameof(Histogram).ToLower()); + + HistogramConfiguration histogramConfiguration = new() + { + Buckets = new[] { 1.0, 2.0, 5.0, 10.0, Double.PositiveInfinity } + }; + + var histogram = AggregateService.DynamicHistogram.GetOrAdd(methodName, + name => Metrics.CreateHistogram(name: n, + help: $"Histogram of the execution time for {n}", + histogramConfiguration)); + + return histogram; + } + + public static Counter GetCounterMetric(String methodName) + { + String n = AggregateService.FormatMetricName(methodName, nameof(Counter).ToLower()); + + var counter = AggregateService.DynamicCounter.GetOrAdd(methodName, name => Metrics.CreateCounter(name: n, help: $"Total number times executed {n}")); + + return counter; + } + } +} + + +public class AggregateMemoryCache +{ + private readonly IMemoryCache MemoryCache; + + private readonly ConcurrentDictionary KeyTracker; + + public AggregateMemoryCache(IMemoryCache memoryCache) + { + this.MemoryCache = memoryCache; + this.KeyTracker = new ConcurrentDictionary(); + } + + private static readonly Dictionary CallbackRegistered = new(); + + private static readonly Object CallbackLock = new(); + + public void Set(String key, + Aggregate aggregate, + MemoryCacheEntryOptions memoryCacheEntryOptions) where TAggregate : Aggregate, new() + { + Type aggregateType = typeof(TAggregate); + + // Ensure the eviction callback is registered only once per TAggregate type + if (!AggregateMemoryCache.CallbackRegistered.TryGetValue(aggregateType, out Boolean isRegistered) || !isRegistered) + { + Monitor.Enter(AggregateMemoryCache.CallbackLock); + + if (!AggregateMemoryCache.CallbackRegistered.TryGetValue(aggregateType, out isRegistered) || !isRegistered) // Double-check locking + { + // Register a callback to remove the item from our internal tracking + memoryCacheEntryOptions.RegisterPostEvictionCallback((evictedKey, + _, _, _) => { + this.KeyTracker.TryRemove(evictedKey.ToString(), out _); + }); + + AggregateMemoryCache.CallbackRegistered[aggregateType] = true; + } + + Monitor.Exit(AggregateMemoryCache.CallbackLock); + } + + // Set the cache entry + this.MemoryCache.Set(key, aggregate, memoryCacheEntryOptions); + this.KeyTracker.TryAdd(key, aggregate); + + Counter counterCalls = AggregateService.GetCounterMetric($"AggregateService_{aggregateType.Name}_times_cache_saved"); + counterCalls.Inc(); + + Counter counterItems = AggregateService.GetCounterMetric($"AggregateService_{aggregateType.Name}_total_cached_items"); + counterItems.IncTo(this.KeyTracker.Count); + } + + public Boolean TryGetValueWithMetrics(String key, + out TAggregate aggregate) where TAggregate : Aggregate, new() + { + String g = typeof(TAggregate).Name; + + var found = this.MemoryCache.TryGetValue(key, out aggregate); + + if (!found) + { + //TODO: Failed cache hit? + Counter counterCalls = AggregateService.GetCounterMetric($"AggregateService_{g}_failed_cache_hit"); + counterCalls.Inc(); + } + + return found; + } + + public Boolean TryGetValue(String key, + out TAggregate aggregate) where TAggregate : Aggregate, new() + { + return this.MemoryCache.TryGetValue(key, out aggregate); + } +} \ No newline at end of file diff --git a/TransactionProcessor.BusinessLogic/Services/ContractDomainService.cs b/TransactionProcessor.BusinessLogic/Services/ContractDomainService.cs index 666e4ca9..3c6f927f 100644 --- a/TransactionProcessor.BusinessLogic/Services/ContractDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/ContractDomainService.cs @@ -35,21 +35,15 @@ public class ContractDomainService : IContractDomainService #region Fields - private readonly IAggregateRepository ContractAggregateRepository; + private readonly IAggregateService AggregateService; private readonly IEventStoreContext Context; - private readonly IAggregateRepository EstateAggregateRepository; - #endregion #region Constructors - public ContractDomainService(IAggregateRepository estateAggregateRepository, - IAggregateRepository contractAggregateRepository, - IEventStoreContext context) - { - this.EstateAggregateRepository = estateAggregateRepository; - this.ContractAggregateRepository = contractAggregateRepository; + public ContractDomainService(IAggregateService aggregateService, IEventStoreContext context) { + this.AggregateService = aggregateService; this.Context = context; } @@ -61,12 +55,11 @@ private async Task ApplyUpdates(Func<(EstateAggregate estateAggregate, C { try { - Result getEstateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); - if (getEstateResult.IsFailed) - return ResultHelpers.CreateFailure(getEstateResult); - EstateAggregate estateAggregate = getEstateResult.Data; - - Result getContractResult = await this.ContractAggregateRepository.GetLatestVersion(contractId, cancellationToken); + EstateAggregate estateAggregate = await this.AggregateService.Get(estateId, cancellationToken); + if (estateAggregate.IsCreated == false) + return Result.Failure("Estate is noty created"); + + Result getContractResult = await this.AggregateService.GetLatest(contractId, cancellationToken); Result contractAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getContractResult, contractId, isNotFoundError); if (contractAggregateResult.IsFailed) @@ -77,7 +70,7 @@ private async Task ApplyUpdates(Func<(EstateAggregate estateAggregate, C if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result saveResult = await this.ContractAggregateRepository.SaveChanges(contractAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(contractAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); diff --git a/TransactionProcessor.BusinessLogic/Services/EstateDomainService.cs b/TransactionProcessor.BusinessLogic/Services/EstateDomainService.cs index f57be98f..89b14a6d 100644 --- a/TransactionProcessor.BusinessLogic/Services/EstateDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/EstateDomainService.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using SecurityService.Client; using SecurityService.DataTransferObjects; +using SecurityService.DataTransferObjects.Responses; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; using Shared.Exceptions; @@ -34,18 +35,16 @@ public class EstateDomainService : IEstateDomainService { #region Fields - private readonly IAggregateRepository EstateAggregateRepository; - + private readonly IAggregateService AggregateService; private readonly ISecurityServiceClient SecurityServiceClient; #endregion #region Constructors - public EstateDomainService(IAggregateRepository estateAggregateRepository, - ISecurityServiceClient securityServiceClient) - { - this.EstateAggregateRepository = estateAggregateRepository; + public EstateDomainService(IAggregateService aggregateService, + ISecurityServiceClient securityServiceClient) { + this.AggregateService = aggregateService; this.SecurityServiceClient = securityServiceClient; } @@ -57,7 +56,7 @@ private async Task ApplyUpdates(Action action, Guid est { try { - Result getLatestVersionResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); + Result getLatestVersionResult = await this.AggregateService.GetLatest(estateId, cancellationToken); Result estateAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getLatestVersionResult, estateId, isNotFoundError); if (estateAggregateResult.IsFailed) @@ -67,7 +66,7 @@ private async Task ApplyUpdates(Action action, Guid est action(estateAggregate); - Result saveResult = await this.EstateAggregateRepository.SaveChanges(estateAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(estateAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); @@ -122,11 +121,11 @@ public async Task CreateEstateUser(EstateCommands.CreateEstateUserComman if (createUserResult.IsFailed) return ResultHelpers.CreateFailure(createUserResult); - var userDetailsResult = await this.SecurityServiceClient.GetUsers(createUserRequest.EmailAddress, cancellationToken); + Result> userDetailsResult = await this.SecurityServiceClient.GetUsers(createUserRequest.EmailAddress, cancellationToken); if (userDetailsResult.IsFailed) return ResultHelpers.CreateFailure(userDetailsResult); - var user = userDetailsResult.Data.SingleOrDefault(); + UserDetails user = userDetailsResult.Data.SingleOrDefault(); if (user == null) return Result.Failure($"Unable to get user details for username {createUserRequest.EmailAddress}"); diff --git a/TransactionProcessor.BusinessLogic/Services/FloatDomainService.cs b/TransactionProcessor.BusinessLogic/Services/FloatDomainService.cs index cf7e901d..ccb2c580 100644 --- a/TransactionProcessor.BusinessLogic/Services/FloatDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/FloatDomainService.cs @@ -27,32 +27,18 @@ Task RecordTransaction(FloatActivityCommands.RecordTransactionCommand co } public class FloatDomainService : IFloatDomainService{ - private readonly IAggregateRepository FloatAggregateRepository; - private readonly IAggregateRepository FloatActivityAggregateRepository; - private readonly IAggregateRepository TransactionAggregateRepository; - private readonly IAggregateRepository EstateAggregateRepository; - private readonly IAggregateRepository ContractAggregateRepository; - - public FloatDomainService(IAggregateRepository floatAggregateRepository, - IAggregateRepository floatActivityAggregateRepository, - IAggregateRepository transactionAggregateRepository, - IAggregateRepository estateAggregateRepository, - IAggregateRepository contractAggregateRepository) + private readonly IAggregateService AggregateService; + + public FloatDomainService(IAggregateService aggregateService) { - this.FloatAggregateRepository = floatAggregateRepository; - this.FloatActivityAggregateRepository = floatActivityAggregateRepository; - this.TransactionAggregateRepository = transactionAggregateRepository; - this.EstateAggregateRepository = estateAggregateRepository; - this.ContractAggregateRepository = contractAggregateRepository; + this.AggregateService = aggregateService; } - - //private TokenResponse TokenResponse; private async Task ApplyFloatUpdates(Func action, Guid floatId, CancellationToken cancellationToken, Boolean isNotFoundError = true) { try { - Result getFloatResult = await this.FloatAggregateRepository.GetLatestVersion(floatId, cancellationToken); + Result getFloatResult = await this.AggregateService.GetLatest(floatId, cancellationToken); Result floatAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getFloatResult, floatId, isNotFoundError); if (floatAggregateResult.IsFailed) @@ -64,7 +50,7 @@ private async Task ApplyFloatUpdates(Func action if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result saveResult = await this.FloatAggregateRepository.SaveChanges(floatAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(floatAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); @@ -80,7 +66,7 @@ private async Task ApplyFloatActivityUpdates(Func getFloatResult = await this.FloatActivityAggregateRepository.GetLatestVersion(floatId, cancellationToken); + Result getFloatResult = await this.AggregateService.GetLatest(floatId, cancellationToken); Result floatActivityAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getFloatResult, floatId, isNotFoundError); if (floatActivityAggregateResult.IsFailed) @@ -92,7 +78,7 @@ private async Task ApplyFloatActivityUpdates(Func ApplyFloatActivityUpdates(Func ValidateEstate(Guid estateId, CancellationToken cancellationToken) { - Result result = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); + Result getEstateResult= await this.AggregateService.Get(estateId, cancellationToken); - if (result.IsFailed) { - return ResultHelpers.CreateFailure(result); + if (getEstateResult.IsFailed) { + return ResultHelpers.CreateFailure(getEstateResult); } return Result.Success(); } private async Task ValidateContractProduct(Guid estateId, Guid contractId, Guid productId, CancellationToken cancellationToken) { - Result getContractResult = await this.ContractAggregateRepository.GetLatestVersion(contractId, cancellationToken); + Result getContractResult = await this.AggregateService.Get(contractId, cancellationToken); if (getContractResult.IsFailed) { return ResultHelpers.CreateFailure(getContractResult); } - - Models.Contract.Contract contract = getContractResult.Data.GetContract(); + ContractAggregate contractAggregate = getContractResult.Data; + Models.Contract.Contract contract = contractAggregate.GetContract(); Boolean productExists = contract.Products.Any(cp => cp.ContractProductId == productId); return productExists switch { @@ -176,7 +162,7 @@ public async Task RecordCreditPurchase(FloatActivityCommands.RecordCredi public async Task RecordTransaction(FloatActivityCommands.RecordTransactionCommand command, CancellationToken cancellationToken) { - Result getTransactionResult = await this.TransactionAggregateRepository.GetLatestVersion(command.TransactionId, cancellationToken); + Result getTransactionResult = await this.AggregateService.GetLatest(command.TransactionId, cancellationToken); if (getTransactionResult.IsFailed) return ResultHelpers.CreateFailure(getTransactionResult); diff --git a/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs b/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs index ed860de4..ab5dc20d 100644 --- a/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs @@ -46,15 +46,8 @@ public interface IMerchantDomainService public class MerchantDomainService : IMerchantDomainService { #region Fields - - private readonly IAggregateRepository EstateAggregateRepository; - - private readonly IAggregateRepository MerchantAggregateRepository; - - private readonly IAggregateRepository MerchantDepositListAggregateRepository; - - private readonly IAggregateRepository ContractAggregateRepository; - + + private readonly IAggregateService AggregateService; private readonly ISecurityServiceClient SecurityServiceClient; private readonly IEventStoreContext EventStoreContext; @@ -62,17 +55,11 @@ public class MerchantDomainService : IMerchantDomainService #region Constructors - public MerchantDomainService(IAggregateRepository estateAggregateRepository, - IAggregateRepository merchantAggregateRepository, - IAggregateRepository merchantDepositListAggregateRepository, - IAggregateRepository contractAggregateRepository, + public MerchantDomainService(IAggregateService aggregateService, ISecurityServiceClient securityServiceClient, IEventStoreContext eventStoreContext) { - this.EstateAggregateRepository = estateAggregateRepository; - this.MerchantAggregateRepository = merchantAggregateRepository; - this.MerchantDepositListAggregateRepository = merchantDepositListAggregateRepository; - this.ContractAggregateRepository = contractAggregateRepository; + this.AggregateService = aggregateService; this.SecurityServiceClient = securityServiceClient; this.EventStoreContext = eventStoreContext; } @@ -85,12 +72,12 @@ private async Task ApplyUpdates(Func<(EstateAggregate estateAggregate, M { try { - Result getEstateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); - if (getEstateResult.IsFailed) + Result getEstateResult = await this.AggregateService.Get(estateId, cancellationToken); + if (getEstateResult.IsFailed) { return ResultHelpers.CreateFailure(getEstateResult); + } EstateAggregate estateAggregate = getEstateResult.Data; - - Result getMerchantResult = await this.MerchantAggregateRepository.GetLatestVersion(merchantId, cancellationToken); + Result getMerchantResult = await this.AggregateService.GetLatest(merchantId, cancellationToken); Result merchantAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getMerchantResult, merchantId, isNotFoundError); if (merchantAggregateResult.IsFailed) @@ -102,7 +89,7 @@ private async Task ApplyUpdates(Func<(EstateAggregate estateAggregate, M if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result saveResult = await this.MerchantAggregateRepository.SaveChanges(merchantAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(merchantAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); @@ -305,7 +292,7 @@ public async Task MakeMerchantDeposit(MerchantCommands.MakeMerchantDepos if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result getDepositListResult = await this.MerchantDepositListAggregateRepository.GetLatestVersion(command.MerchantId, cancellationToken); + Result getDepositListResult = await this.AggregateService.GetLatest(command.MerchantId, cancellationToken); Result merchantDepositListAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getDepositListResult, command.MerchantId, false); if (merchantDepositListAggregateResult.IsFailed) @@ -325,7 +312,7 @@ public async Task MakeMerchantDeposit(MerchantCommands.MakeMerchantDepos }; merchantDepositListAggregate.MakeDeposit(depositSource, command.RequestDto.Reference, command.RequestDto.DepositDateTime, amount); - Result saveResult = await this.MerchantDepositListAggregateRepository.SaveChanges(merchantDepositListAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(merchantDepositListAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); @@ -351,7 +338,7 @@ public async Task MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result getDepositListResult = await this.MerchantDepositListAggregateRepository.GetLatestVersion(command.MerchantId, cancellationToken); + Result getDepositListResult = await this.AggregateService.GetLatest(command.MerchantId, cancellationToken); Result merchantDepositListAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getDepositListResult, command.MerchantId, false); if (merchantDepositListAggregateResult.IsFailed) @@ -382,6 +369,10 @@ public async Task MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi merchantDepositListAggregate.MakeWithdrawal(command.RequestDto.WithdrawalDateTime, amount); + Result saveResult = await this.AggregateService.Save(merchantDepositListAggregate, cancellationToken); + if (saveResult.IsFailed) + return ResultHelpers.CreateFailure(saveResult); + return Result.Success(); }, command.EstateId, command.MerchantId, cancellationToken); @@ -406,9 +397,8 @@ public async Task AddContractToMerchant(MerchantCommands.AddMerchantCont if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result getContractResult = await this.ContractAggregateRepository.GetLatestVersion(command.RequestDto.ContractId, cancellationToken); - if (getContractResult.IsFailed) - { + var getContractResult = await this.AggregateService.Get(command.RequestDto.ContractId, cancellationToken); + if (getContractResult.IsFailed) { return ResultHelpers.CreateFailure(getContractResult); } diff --git a/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs b/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs index 74fda4f1..3af9416b 100644 --- a/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs @@ -40,21 +40,13 @@ public interface IMerchantStatementDomainService public class MerchantStatementDomainService : IMerchantStatementDomainService { - #region Fields - - private readonly IAggregateRepository MerchantAggregateRepository; - - private readonly IAggregateRepository MerchantStatementAggregateRepository; + private readonly IAggregateService AggregateService; - #endregion - #region Constructors - public MerchantStatementDomainService(IAggregateRepository merchantAggregateRepository, - IAggregateRepository merchantStatementAggregateRepository) + public MerchantStatementDomainService(IAggregateService aggregateService) { - this.MerchantAggregateRepository = merchantAggregateRepository; - this.MerchantStatementAggregateRepository = merchantStatementAggregateRepository; + this.AggregateService = aggregateService; } #endregion @@ -65,7 +57,7 @@ private async Task ApplyUpdates(Func getMerchantStatementResult = await this.GetLatestVersion(statementId, cancellationToken); + Result getMerchantStatementResult = await this.AggregateService.GetLatest(statementId, cancellationToken); Result merchantStatementAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getMerchantStatementResult, statementId, isNotFoundError); @@ -78,7 +70,7 @@ private async Task ApplyUpdates(Func ApplyUpdates(Func> GetLatestVersion(Guid statementId, CancellationToken cancellationToken) - { - Stopwatch sw = Stopwatch.StartNew(); - - Result merchantStatementAggregate = - await this.MerchantStatementAggregateRepository.GetLatestVersion(statementId, cancellationToken); - - sw.Stop(); - Int64 elapsedTime = sw.ElapsedMilliseconds; - - if (elapsedTime > 1000) - { - Logger.LogWarning($"Rehydration of MerchantStatementAggregate Id [{statementId}] took {elapsedTime} ms"); - } - - return merchantStatementAggregate; - } - public async Task AddSettledFeeToStatement(MerchantStatementCommands.AddSettledFeeToMerchantStatementCommand command, CancellationToken cancellationToken) { diff --git a/TransactionProcessor.BusinessLogic/Services/OperatorDomainService.cs b/TransactionProcessor.BusinessLogic/Services/OperatorDomainService.cs index c5e9d566..a7173e81 100644 --- a/TransactionProcessor.BusinessLogic/Services/OperatorDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/OperatorDomainService.cs @@ -1,4 +1,5 @@ using System; +using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; using Shared.DomainDrivenDesign.EventSourcing; @@ -20,27 +21,22 @@ public interface IOperatorDomainService public class OperatorDomainService : IOperatorDomainService { - private readonly IAggregateRepository EstateAggregateRepository; + private readonly IAggregateService AggregateService; - private readonly IAggregateRepository OperatorAggregateRepository; - - public OperatorDomainService(IAggregateRepository estateAggregateRepository, - IAggregateRepository operatorAggregateRepository) - { - this.EstateAggregateRepository = estateAggregateRepository; - this.OperatorAggregateRepository = operatorAggregateRepository; + public OperatorDomainService(IAggregateService aggregateService) { + this.AggregateService = aggregateService; } private async Task ApplyUpdates(Func<(EstateAggregate, OperatorAggregate), Result> action, Guid estateId, Guid operatorId, CancellationToken cancellationToken, Boolean isNotFoundError = true) { try { - Result getEstateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); - if (getEstateResult.IsFailed) + Result getEstateResult = await this.AggregateService.Get(estateId, cancellationToken); + if (getEstateResult.IsFailed) { return ResultHelpers.CreateFailure(getEstateResult); + } EstateAggregate estateAggregate = getEstateResult.Data; - - Result getOperatorResult = await this.OperatorAggregateRepository.GetLatestVersion(operatorId, cancellationToken); + Result getOperatorResult = await this.AggregateService.GetLatest(operatorId, cancellationToken); Result operatorAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getOperatorResult, operatorId, isNotFoundError); if (operatorAggregateResult.IsFailed) @@ -52,7 +48,7 @@ private async Task ApplyUpdates(Func<(EstateAggregate, OperatorAggregate if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result saveResult = await this.OperatorAggregateRepository.SaveChanges(operatorAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(operatorAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); diff --git a/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs b/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs index fee6b793..390c03b7 100644 --- a/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs @@ -31,9 +31,7 @@ Task AddSettledFeeToSettlement(SettlementCommands.AddSettledFeeToSettlem } public class SettlementDomainService : ISettlementDomainService { - private readonly IAggregateRepository TransactionAggregateRepository; - private readonly IAggregateRepository SettlementAggregateRepository; - private readonly IAggregateRepository MerchantAggregateRepository; + private readonly IAggregateService AggregateService; private async Task ApplySettlementUpdates(Func> action, Guid settlementId, @@ -43,7 +41,7 @@ private async Task ApplySettlementUpdates(Func getSettlementResult = await this.SettlementAggregateRepository.GetLatestVersion(settlementId, cancellationToken); + Result getSettlementResult = await this.AggregateService.GetLatest(settlementId, cancellationToken); Result settlementAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getSettlementResult, settlementId, isNotFoundError); @@ -56,7 +54,7 @@ private async Task ApplySettlementUpdates(Func ApplyTransactionUpdates(Func getTransactionResult = await this.TransactionAggregateRepository.GetLatestVersion(transactionId, cancellationToken); + Result getTransactionResult = await this.AggregateService.GetLatest(transactionId, cancellationToken); Result transactionAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getTransactionResult, transactionId, isNotFoundError); @@ -86,7 +84,7 @@ private async Task ApplyTransactionUpdates(Func> ProcessSettlement(SettlementCommands.ProcessSett return Result.Success(); } - Result merchantResult = await this.MerchantAggregateRepository.GetLatestVersion(command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); + Result getMerchantResult = await this.AggregateService.Get(command.MerchantId, cancellationToken); + if (getMerchantResult.IsFailed) + return ResultHelpers.CreateFailure(getMerchantResult); - MerchantAggregate merchant = merchantResult.Data; + MerchantAggregate merchant = getMerchantResult.Data; if (merchant.SettlementSchedule == SettlementSchedule.Immediate) { // Mark the settlement as completed settlementAggregate.StartProcessing(DateTime.Now); settlementAggregate.ManuallyComplete(); - Result result = await this.SettlementAggregateRepository.SaveChanges(settlementAggregate, cancellationToken); + Result result = await this.AggregateService.Save(settlementAggregate, cancellationToken); return result; } @@ -133,7 +131,7 @@ public async Task> ProcessSettlement(SettlementCommands.ProcessSett { // Record the process call settlementAggregate.StartProcessing(DateTime.Now); - return await this.SettlementAggregateRepository.SaveChanges(settlementAggregate, cancellationToken); + return await this.AggregateService.Save(settlementAggregate, cancellationToken); } return Result.Success(); @@ -208,11 +206,12 @@ public async Task AddSettledFeeToSettlement(SettlementCommands.AddSettle Guid aggregateId = Helpers.CalculateSettlementAggregateId(command.SettledDate.Date, command.MerchantId, command.EstateId); Result result = await ApplySettlementUpdates(async (SettlementAggregate settlementAggregate) => { - Result merchantResult = await this.MerchantAggregateRepository.GetLatestVersion(command.MerchantId, cancellationToken); - if (merchantResult.IsFailed) - return ResultHelpers.CreateFailure(merchantResult); + Result getMerchantResult = await this.AggregateService.Get(command.MerchantId, cancellationToken); + if (getMerchantResult.IsFailed) + return ResultHelpers.CreateFailure(getMerchantResult); + + MerchantAggregate merchant = getMerchantResult.Data; - MerchantAggregate merchant = merchantResult.Data; if (merchant.SettlementSchedule == SettlementSchedule.Immediate){ settlementAggregate.ImmediatelyMarkFeeAsSettled(command.MerchantId, command.TransactionId, command.FeeId); } @@ -226,13 +225,9 @@ public async Task AddSettledFeeToSettlement(SettlementCommands.AddSettle return result; } - public SettlementDomainService(IAggregateRepository transactionAggregateRepository, - IAggregateRepository settlementAggregateRepository, - IAggregateRepository merchantAggregateRepository) + public SettlementDomainService(IAggregateService aggregateService) { - this.TransactionAggregateRepository = transactionAggregateRepository; - this.SettlementAggregateRepository = settlementAggregateRepository; - this.MerchantAggregateRepository = merchantAggregateRepository; + this.AggregateService = aggregateService; } } } \ No newline at end of file diff --git a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs index 72d53b51..50105ae6 100644 --- a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs @@ -10,12 +10,6 @@ using Operator = TransactionProcessor.Models.Estate.Operator; namespace TransactionProcessor.BusinessLogic.Services{ - using System; - using System.Collections.Generic; - using System.Diagnostics.CodeAnalysis; - using System.Linq; - using System.Threading; - using System.Threading.Tasks; using Common; using MessagingService.Client; using MessagingService.DataTransferObjects; @@ -28,6 +22,12 @@ namespace TransactionProcessor.BusinessLogic.Services{ using Shared.EventStore.Aggregate; using Shared.General; using Shared.Logger; + using System; + using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; using TransactionProcessor.BusinessLogic.Manager; using TransactionProcessor.BusinessLogic.Requests; @@ -64,67 +64,36 @@ Task ResendCustomerEmailReceipt(TransactionCommands.ResendCustomerEmailR public class TransactionDomainService : ITransactionDomainService { #region Fields - /// - /// The operator proxy resolver - /// + private readonly IAggregateService AggregateService; private readonly Func OperatorProxyResolver; - - /// - /// The reconciliation aggregate repository - /// - private readonly IAggregateRepository ReconciliationAggregateRepository; - private readonly ISecurityServiceClient SecurityServiceClient; - private readonly IAggregateRepository OperatorAggregateRepository; - private readonly IAggregateRepository FloatAggregateRepository; private readonly IMemoryCacheWrapper MemoryCache; private readonly IFeeCalculationManager FeeCalculationManager; private readonly ITransactionReceiptBuilder TransactionReceiptBuilder; private readonly IMessagingServiceClient MessagingServiceClient; - private TokenResponse TokenResponse; - - private readonly IAggregateRepository TransactionAggregateRepository; - private readonly ITransactionValidationService TransactionValidationService; - - private readonly IAggregateRepository EstateAggregateRepository; - private readonly IAggregateRepository MerchantAggregateRepository; - private readonly IAggregateRepository ContractAggregateRepository; - + #endregion #region Constructors - public TransactionDomainService(IAggregateRepository transactionAggregateRepository, + public TransactionDomainService(IAggregateService aggregateService, Func operatorProxyResolver, - IAggregateRepository reconciliationAggregateRepository, ITransactionValidationService transactionValidationService, ISecurityServiceClient securityServiceClient, - IAggregateRepository floatAggregateRepository, IMemoryCacheWrapper memoryCache, IFeeCalculationManager feeCalculationManager, ITransactionReceiptBuilder transactionReceiptBuilder, - IMessagingServiceClient messagingServiceClient, - IAggregateRepository estateAggregateRepository, - IAggregateRepository operatorAggregateRepository, - IAggregateRepository merchantAggregateRepository, - IAggregateRepository contractAggregateRepository) { - this.TransactionAggregateRepository = transactionAggregateRepository; - //this.EstateClient = estateClient; + IMessagingServiceClient messagingServiceClient) { + this.AggregateService = aggregateService; this.OperatorProxyResolver = operatorProxyResolver; - this.ReconciliationAggregateRepository = reconciliationAggregateRepository; this.TransactionValidationService = transactionValidationService; this.SecurityServiceClient = securityServiceClient; - this.FloatAggregateRepository = floatAggregateRepository; this.MemoryCache = memoryCache; this.FeeCalculationManager = feeCalculationManager; this.TransactionReceiptBuilder = transactionReceiptBuilder; this.MessagingServiceClient = messagingServiceClient; - EstateAggregateRepository = estateAggregateRepository; - this.OperatorAggregateRepository = operatorAggregateRepository; - MerchantAggregateRepository = merchantAggregateRepository; - this.ContractAggregateRepository = contractAggregateRepository; } #endregion @@ -135,7 +104,7 @@ private async Task> ApplyUpdates(Func getTransactionResult = await this.TransactionAggregateRepository.GetLatestVersion(transactionId, cancellationToken); + Result getTransactionResult = await this.AggregateService.GetLatest(transactionId, cancellationToken); Result transactionAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getTransactionResult, transactionId, isNotFoundError); if (transactionAggregateResult.IsFailed) @@ -146,7 +115,7 @@ private async Task> ApplyUpdates(Func ApplyUpdates(Func> Boolean isNotFoundError = true) { try { - Result getTransactionResult = await this.TransactionAggregateRepository.GetLatestVersion(transactionId, cancellationToken); + Result getTransactionResult = await this.AggregateService.GetLatest(transactionId, cancellationToken); Result transactionAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getTransactionResult, transactionId, isNotFoundError); if (transactionAggregateResult.IsFailed) @@ -173,7 +142,7 @@ private async Task ApplyUpdates(Func> if (result.IsFailed) return ResultHelpers.CreateFailure(result); - Result saveResult = await this.TransactionAggregateRepository.SaveChanges(transactionAggregate, cancellationToken); + Result saveResult = await this.AggregateService.Save(transactionAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); return Result.Success(); @@ -189,7 +158,7 @@ private async Task> ApplyUpdates(Func getTransactionResult = await this.ReconciliationAggregateRepository.GetLatestVersion(transactionId, cancellationToken); + Result getTransactionResult = await this.AggregateService.GetLatest(transactionId, cancellationToken); Result reconciliationAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getTransactionResult, transactionId, isNotFoundError); ReconciliationAggregate reconciliationAggregate = reconciliationAggregateResult.Data; @@ -197,7 +166,7 @@ private async Task> ApplyUpdates(Func> ProcessSaleTransaction Logger.LogInformation($"Validation response is [{JsonConvert.SerializeObject(validationResult)}]"); Guid floatAggregateId = IdGenerationService.GenerateFloatAggregateId(command.EstateId, command.ContractId, command.ProductId); - var floatAggregateResult = await this.FloatAggregateRepository.GetLatestVersion(floatAggregateId, cancellationToken); + Result floatAggregateResult = await this.AggregateService.GetLatest(floatAggregateId, cancellationToken); Decimal unitCost = 0; Decimal totalCost = 0; if (floatAggregateResult.IsSuccess) { // TODO: Move calculation to float - var floatAggregate = floatAggregateResult.Data; + FloatAggregate floatAggregate = floatAggregateResult.Data; unitCost = floatAggregate.GetUnitCostPrice(); totalCost = transactionAmount.GetValueOrDefault() * unitCost; } @@ -491,7 +460,7 @@ public async Task SendCustomerEmailReceipt(TransactionCommands.SendCusto CancellationToken cancellationToken) { this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken); - Result transactionAggregateResult = await this.TransactionAggregateRepository.GetLatestVersion(command.TransactionId, cancellationToken); + Result transactionAggregateResult = await this.AggregateService.GetLatest(command.TransactionId, cancellationToken); if (transactionAggregateResult.IsFailed) return ResultHelpers.CreateFailure(transactionAggregateResult); @@ -501,12 +470,15 @@ public async Task SendCustomerEmailReceipt(TransactionCommands.SendCusto Result merchantResult = await this.GetMerchant(transaction.MerchantId, cancellationToken); if (merchantResult.IsFailed) return ResultHelpers.CreateFailure(merchantResult); - - Result estateResult = await this.EstateAggregateRepository.GetLatestVersion(command.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - Estate estate = estateResult.Data.GetEstate(); - Operator @operator = estate.Operators.Single(o => o.OperatorId == transaction.OperatorId); + + Result getEstateResult = await this.AggregateService.Get(command.EstateId, cancellationToken); + if (getEstateResult.IsFailed) + { + return ResultHelpers.CreateFailure(getEstateResult); + } + EstateAggregate estateAggregate = getEstateResult.Data; + Models.Estate.Estate estate = estateAggregate.GetEstate(); + Models.Estate.Operator @operator = estate.Operators.Single(o => o.OperatorId == transaction.OperatorId); // Determine the body of the email String receiptMessage = await this.TransactionReceiptBuilder.GetEmailReceiptMessage(transaction, merchantResult.Data, @operator.Name, cancellationToken); @@ -519,7 +491,7 @@ public async Task ResendCustomerEmailReceipt(TransactionCommands.ResendC CancellationToken cancellationToken) { this.TokenResponse = await Helpers.GetToken(this.TokenResponse, this.SecurityServiceClient, cancellationToken); - Result transactionAggregateResult = await this.TransactionAggregateRepository.GetLatestVersion(command.TransactionId, cancellationToken); + Result transactionAggregateResult = await this.AggregateService.GetLatest(command.TransactionId, cancellationToken); if (transactionAggregateResult.IsFailed) return ResultHelpers.CreateFailure(transactionAggregateResult); @@ -617,9 +589,9 @@ private async Task AddDeviceToMerchant(Guid merchantId, CancellationToken cancellationToken) { // TODO: Should this be firing a command to add the device?? // Add the device to the merchant - Result merchantAggregate = await this.MerchantAggregateRepository.GetLatestVersion(merchantId, cancellationToken); + Result merchantAggregate = await this.AggregateService.GetLatest(merchantId, cancellationToken); merchantAggregate.Data.AddDevice(Guid.NewGuid(), deviceIdentifier); - await this.MerchantAggregateRepository.SaveChanges(merchantAggregate.Data, cancellationToken); + await this.AggregateService.Save(merchantAggregate.Data, cancellationToken); } /// @@ -638,11 +610,11 @@ private String GenerateTransactionReference() { private async Task> GetMerchant(Guid merchantId, CancellationToken cancellationToken) { - Result merchantAggregateResult = await this.MerchantAggregateRepository.GetLatestVersion(merchantId, cancellationToken); + Result getMerchantResult= await this.AggregateService.Get(merchantId, cancellationToken); - if (merchantAggregateResult.IsFailed) - return ResultHelpers.CreateFailure(merchantAggregateResult); - Merchant merchant = merchantAggregateResult.Data.GetMerchant(); + if (getMerchantResult.IsFailed) + return ResultHelpers.CreateFailure(getMerchantResult); + Models.Merchant.Merchant merchant = getMerchantResult.Data.GetMerchant(); return merchant; } @@ -654,18 +626,12 @@ private async Task> ProcessMessageWithOperator(Models.M Dictionary additionalTransactionMetadata, String transactionReference, CancellationToken cancellationToken) { - - // TODO: introduce some kind of mapping in here to link operator id to the name - Result estateResult = await this.EstateAggregateRepository.GetLatestVersion(merchant.EstateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); - Estate estate = estateResult.Data.GetEstate(); - Operator @operator = estate.Operators.SingleOrDefault(o => o.OperatorId == operatorId); - - var operatorResult = await this.OperatorAggregateRepository.GetLatestVersion(operatorId, cancellationToken); - if (operatorResult.IsFailed) - return ResultHelpers.CreateFailure(operatorResult); - IOperatorProxy operatorProxy = this.OperatorProxyResolver(operatorResult.Data.Name.Replace(" ", "")); + + Result getOperatorResult= await this.AggregateService.Get(operatorId, cancellationToken); + if (getOperatorResult.IsFailed) + return ResultHelpers.CreateFailure(getOperatorResult); + Models.Operator.Operator operatorResult = getOperatorResult.Data.GetOperator(); + IOperatorProxy operatorProxy = this.OperatorProxyResolver(operatorResult.Name.Replace(" ", "")); try { Result saleResult = await operatorProxy.ProcessSaleMessage(transactionId, operatorId, merchant, transactionDateTime, transactionReference, additionalTransactionMetadata, cancellationToken); if (saleResult.IsFailed) { @@ -742,11 +708,11 @@ private async Task ResendEmailMessage(String accessToken, private async Task>> GetTransactionFeesForProduct(Guid contractId, Guid productId, CancellationToken cancellationToken) { - Result contractAggregateResult = await this.ContractAggregateRepository.GetLatestVersion(contractId, CancellationToken.None); + Result contractAggregateResult = await this.AggregateService.Get(contractId, CancellationToken.None); if (contractAggregateResult.IsFailed) return ResultHelpers.CreateFailure(contractAggregateResult); - Contract contract = contractAggregateResult.Data.GetContract(); + Models.Contract.Contract contract = contractAggregateResult.Data.GetContract(); Product product = contract.Products.SingleOrDefault(p => p.ContractProductId == productId); if (product == null) diff --git a/TransactionProcessor.BusinessLogic/Services/TransactionValidationService.cs b/TransactionProcessor.BusinessLogic/Services/TransactionValidationService.cs index d35dfd86..a6829019 100644 --- a/TransactionProcessor.BusinessLogic/Services/TransactionValidationService.cs +++ b/TransactionProcessor.BusinessLogic/Services/TransactionValidationService.cs @@ -44,17 +44,14 @@ Task> ValidateSaleTransaction(Guid estateId, public class TransactionValidationService : ITransactionValidationService{ #region Fields private readonly IEventStoreContext EventStoreContext; - private readonly IAggregateRepository EstateAggregateRepository; - private readonly IAggregateRepository MerchantAggregateRepository; + private readonly IAggregateService AggregateService; #endregion public TransactionValidationService(IEventStoreContext eventStoreContext, - IAggregateRepository estateAggregateRepository, - IAggregateRepository merchantAggregateRepository) + IAggregateService aggregateService) { this.EventStoreContext = eventStoreContext; - this.EstateAggregateRepository = estateAggregateRepository; - this.MerchantAggregateRepository = merchantAggregateRepository; + this.AggregateService = aggregateService; } #region Methods @@ -164,7 +161,7 @@ public async Task> ValidateSaleTransaction(G private async Task>> ValidateEstate(Guid estateId, CancellationToken cancellationToken) { - Result getEstateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); + var getEstateResult= await this.AggregateService.Get(estateId, cancellationToken); if (getEstateResult.IsFailed) { TransactionValidationResult transactionValidationResult = getEstateResult.Status switch { @@ -196,7 +193,7 @@ private Result ValidateEstateOperator(EstateAggrega private async Task>> ValidateMerchant(Guid estateId, String estateName, Guid merchantId, CancellationToken cancellationToken) { - Result getMerchantResult = await this.MerchantAggregateRepository.GetLatestVersion(merchantId, cancellationToken); + Result getMerchantResult = await this.AggregateService.Get(merchantId, cancellationToken); if (getMerchantResult.IsFailed) { TransactionValidationResult transactionValidationResult = getMerchantResult.Status switch diff --git a/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs b/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs index 63ebba5a..b390484f 100644 --- a/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/VoucherDomainService.cs @@ -63,36 +63,19 @@ Task> RedeemVoucher(Guid estateId, public class VoucherDomainService : IVoucherDomainService { - /// - /// The voucher aggregate repository - /// - private readonly IAggregateRepository VoucherAggregateRepository; - - /// - /// The database context factory - /// - private readonly Shared.EntityFramework.IDbContextFactory DbContextFactory; + private readonly IAggregateService AggregateService; - private readonly IAggregateRepository EstateAggregateRepository; + private readonly Shared.EntityFramework.IDbContextFactory DbContextFactory; private const String ConnectionStringIdentifier = "EstateReportingReadModel"; #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// The voucher aggregate repository. - /// The security service client. - /// The estate client. - /// The database context factory. - public VoucherDomainService(IAggregateRepository voucherAggregateRepository, - Shared.EntityFramework.IDbContextFactory dbContextFactory, - IAggregateRepository estateAggregateRepository) + public VoucherDomainService(IAggregateService aggregateService, + Shared.EntityFramework.IDbContextFactory dbContextFactory) { - this.VoucherAggregateRepository = voucherAggregateRepository; + this.AggregateService = aggregateService; this.DbContextFactory = dbContextFactory; - this.EstateAggregateRepository = estateAggregateRepository; } #endregion @@ -105,7 +88,7 @@ private async Task> ApplyUpdates(Func getVoucherResult = await this.VoucherAggregateRepository.GetLatestVersion(voucherId, cancellationToken); + Result getVoucherResult = await this.AggregateService.GetLatest(voucherId, cancellationToken); Result voucherAggregateResult = DomainServiceHelper.HandleGetAggregateResult(getVoucherResult, voucherId, isNotFoundError); @@ -117,7 +100,7 @@ private async Task> ApplyUpdates(Func> RedeemVoucher(Guid estateId, voucherAggregate.Redeem(redeemedDateTime); // Save the changes - await this.VoucherAggregateRepository.SaveChanges(voucherAggregate, cancellationToken); + await this.AggregateService.Save(voucherAggregate, cancellationToken); Models.Voucher voucherModel = voucherAggregate.GetVoucher(); @@ -216,11 +199,14 @@ public async Task> RedeemVoucher(Guid estateId, private async Task ValidateVoucherIssue(Guid estateId, Guid operatorId, CancellationToken cancellationToken) { // Validate the Estate Record is a valid estate - Result estateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result getEstateResult = await this.AggregateService.Get(estateId, cancellationToken); + if (getEstateResult.IsFailed) + { + return ResultHelpers.CreateFailure(getEstateResult); + } + EstateAggregate estateAggregate = getEstateResult.Data; - Estate estate = estateResult.Data.GetEstate(); + Estate estate = estateAggregate.GetEstate(); if (estate.Operators == null || estate.Operators.Any() == false) { return Result.NotFound($"Estate {estate.Name} has no operators defined"); @@ -238,9 +224,10 @@ private async Task ValidateVoucherIssue(Guid estateId, Guid operatorId, private async Task ValidateVoucherRedemption(Guid estateId, CancellationToken cancellationToken) { // Validate the Estate Record is a valid estate - Result estateResult = await this.EstateAggregateRepository.GetLatestVersion(estateId, cancellationToken); - if (estateResult.IsFailed) - return ResultHelpers.CreateFailure(estateResult); + Result getEstateResult = await this.AggregateService.Get(estateId, cancellationToken); + if (getEstateResult.IsFailed) { + return ResultHelpers.CreateFailure(getEstateResult); + } return Result.Success(); } diff --git a/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj b/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj index 71cd0d82..ba86b122 100644 --- a/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj +++ b/TransactionProcessor.BusinessLogic/TransactionProcessor.BusinessLogic.csproj @@ -12,6 +12,7 @@ + diff --git a/TransactionProcessor/Bootstrapper/RepositoryRegistry.cs b/TransactionProcessor/Bootstrapper/RepositoryRegistry.cs index 0bb988a2..27b58bc2 100644 --- a/TransactionProcessor/Bootstrapper/RepositoryRegistry.cs +++ b/TransactionProcessor/Bootstrapper/RepositoryRegistry.cs @@ -1,4 +1,5 @@ using TransactionProcessor.Aggregates; +using TransactionProcessor.BusinessLogic.Services; using TransactionProcessor.Database.Contexts; using TransactionProcessor.Repository; @@ -64,7 +65,9 @@ public RepositoryRegistry() } this.AddTransient(); - + + this.AddSingleton(); + this.AddSingleton(); this.AddSingleton, AggregateRepository>(); this.AddSingleton, @@ -86,7 +89,7 @@ public RepositoryRegistry() this.AddSingleton(); this.AddSingleton(); this.AddSingleton, MerchantBalanceProjection>(); - + this.AddSingleton, DbContextFactory>(); this.AddSingleton>(cont => connectionString => diff --git a/TransactionProcessor/Startup.cs b/TransactionProcessor/Startup.cs index 338581d2..86f75f4a 100644 --- a/TransactionProcessor/Startup.cs +++ b/TransactionProcessor/Startup.cs @@ -1,3 +1,5 @@ +using Prometheus; + namespace TransactionProcessor { using System; @@ -133,7 +135,8 @@ public void Configure(IApplicationBuilder app, app.UseAuthentication(); app.UseAuthorization(); - + app.UseMetricServer(s => { + }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapHealthChecks("health", diff --git a/TransactionProcessor/TransactionProcessor.csproj b/TransactionProcessor/TransactionProcessor.csproj index 30362bc8..23d5e93e 100644 --- a/TransactionProcessor/TransactionProcessor.csproj +++ b/TransactionProcessor/TransactionProcessor.csproj @@ -32,6 +32,8 @@ + +