Skip to content

Commit d3d94ba

Browse files
Merge pull request #164 from TransactionProcessing/task/#160_domainservicerefactor
Split out settlement to its own service
2 parents a197816 + cb72d9a commit d3d94ba

12 files changed

Lines changed: 320 additions & 348 deletions

File tree

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/TransactionDomainEventHandlerTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,46 @@ public async Task TransactionDomainEventHandler_Handle_TransactionHasBeenComplet
247247
pendingSettlementAggregate.GetNumberOfFeesSettled().ShouldBe(0);
248248
}
249249

250+
[Fact]
251+
public async Task TransactionDomainEventHandler_Handle_TransactionHasBeenCompletedEvent_SuccessfulSale_MerchantWithNotSetSettlementSchedule_ErrorThrown()
252+
{
253+
this.TransactionAggregateManager.Setup(t => t.GetAggregate(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
254+
.ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregate);
255+
256+
this.FeeCalculationManager.Setup(f => f.CalculateFees(It.IsAny<List<TransactionFeeToCalculate>>(), It.IsAny<Decimal>())).Returns(new List<CalculatedFee>
257+
{
258+
TestData.CalculatedFeeMerchantFee(),
259+
TestData.CalculatedFeeServiceProviderFee
260+
});
261+
262+
this.EstateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
263+
.ReturnsAsync(new MerchantResponse
264+
{
265+
SettlementSchedule = SettlementSchedule.NotSet,
266+
});
267+
this.EstateClient.Setup(e => e.GetTransactionFeesForProduct(It.IsAny<String>(),
268+
It.IsAny<Guid>(),
269+
It.IsAny<Guid>(),
270+
It.IsAny<Guid>(),
271+
It.IsAny<Guid>(),
272+
It.IsAny<CancellationToken>())).ReturnsAsync(TestData.ContractProductTransactionFees);
273+
274+
this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
275+
276+
277+
TransactionDomainEventHandler transactionDomainEventHandler = new TransactionDomainEventHandler(this.TransactionAggregateManager.Object,
278+
this.FeeCalculationManager.Object,
279+
this.EstateClient.Object,
280+
this.SecurityServiceClient.Object,
281+
this.TransactionReceiptBuilder.Object,
282+
this.MessagingServiceClient.Object,
283+
this.SettlementAggregateRepository.Object);
284+
Should.Throw<NotSupportedException>(async () =>
285+
{
286+
await transactionDomainEventHandler.Handle(TestData.TransactionHasBeenCompletedEvent, CancellationToken.None);
287+
});
288+
}
289+
250290
[Fact]
251291
public async Task TransactionDomainEventHandler_Handle_TransactionHasBeenCompletedEvent_UnsuccessfulSale_EventIsHandled()
252292
{

TransactionProcessor.BusinessLogic.Tests/RequestHandler/TransactionRequestHandlerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ public void TransactionRequestHandler_ProcessReconciliationRequest_IsHandled()
6161
}
6262
}
6363

64-
public class SettlementRequestHanslerTests
64+
public class SettlementRequestHandlerTests
6565
{
6666
[Fact]
6767
public void TransactionRequestHandler_ProcessLogonTransactionRequest_IsHandled()
6868
{
69-
Mock<ITransactionDomainService> transactionDomainService = new Mock<ITransactionDomainService>();
70-
SettlementRequestHandler handler = new SettlementRequestHandler(transactionDomainService.Object);
69+
Mock<ISettlementDomainService> settlementDomainService = new Mock<ISettlementDomainService>();
70+
SettlementRequestHandler handler = new SettlementRequestHandler(settlementDomainService.Object);
7171

7272
ProcessSettlementRequest command = TestData.ProcessSettlementRequest;
7373

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
namespace TransactionProcessor.BusinessLogic.Tests.Services
2+
{
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using BusinessLogic.Services;
7+
using Microsoft.Extensions.Configuration;
8+
using Models;
9+
using Moq;
10+
using SettlementAggregates;
11+
using Shared.DomainDrivenDesign.EventSourcing;
12+
using Shared.EventStore.Aggregate;
13+
using Shared.General;
14+
using Shared.Logger;
15+
using Shouldly;
16+
using Testing;
17+
using Xunit;
18+
19+
public class SettlementDomainServiceTests
20+
{
21+
[Fact]
22+
public async Task TransactionDomainService_ProcessSettlement_SettlementIsProcessed()
23+
{
24+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
25+
ConfigurationReader.Initialise(configurationRoot);
26+
27+
Logger.Initialise(NullLogger.Instance);
28+
29+
Mock<ITransactionAggregateManager> transactionAggregateManager = new Mock<ITransactionAggregateManager>();
30+
Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>> settlementAggregateRepository =
31+
new Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>>();
32+
settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
33+
.ReturnsAsync(TestData.GetSettlementAggregateWithPendingMerchantFees(10));
34+
35+
SettlementDomainService settlementDomainService=
36+
new SettlementDomainService(transactionAggregateManager.Object, settlementAggregateRepository.Object);
37+
38+
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
39+
TestData.EstateId,
40+
CancellationToken.None);
41+
42+
response.ShouldNotBeNull();
43+
response.NumberOfFeesFailedToSettle.ShouldBe(0);
44+
response.NumberOfFeesPendingSettlement.ShouldBe(0);
45+
response.NumberOfFeesSuccessfullySettled.ShouldBe(10);
46+
}
47+
48+
[Fact]
49+
public async Task TransactionDomainService_ProcessSettlement_SettlementAggregateNotCreated_NothingProcessed()
50+
{
51+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
52+
ConfigurationReader.Initialise(configurationRoot);
53+
54+
Logger.Initialise(NullLogger.Instance);
55+
56+
Mock<ITransactionAggregateManager> transactionAggregateManager = new Mock<ITransactionAggregateManager>();
57+
Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>> settlementAggregateRepository =
58+
new Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>>();
59+
settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
60+
.ReturnsAsync(TestData.GetEmptySettlementAggregate);
61+
62+
SettlementDomainService settlementDomainService =
63+
new SettlementDomainService(transactionAggregateManager.Object,
64+
settlementAggregateRepository.Object);
65+
66+
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
67+
TestData.EstateId,
68+
CancellationToken.None);
69+
70+
response.ShouldNotBeNull();
71+
response.NumberOfFeesFailedToSettle.ShouldBe(0);
72+
response.NumberOfFeesPendingSettlement.ShouldBe(0);
73+
response.NumberOfFeesSuccessfullySettled.ShouldBe(0);
74+
}
75+
76+
[Fact]
77+
public async Task TransactionDomainService_ProcessSettlement_SettlementAggregateNoFeesToSettles_NothingProcessed()
78+
{
79+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
80+
ConfigurationReader.Initialise(configurationRoot);
81+
82+
Logger.Initialise(NullLogger.Instance);
83+
84+
Mock<ITransactionAggregateManager> transactionAggregateManager = new Mock<ITransactionAggregateManager>();
85+
Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>> settlementAggregateRepository =
86+
new Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>>();
87+
settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
88+
.ReturnsAsync(TestData.GetCreatedSettlementAggregate);
89+
90+
SettlementDomainService settlementDomainService =
91+
new SettlementDomainService(transactionAggregateManager.Object,
92+
settlementAggregateRepository.Object);
93+
94+
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
95+
TestData.EstateId,
96+
CancellationToken.None);
97+
98+
response.ShouldNotBeNull();
99+
response.NumberOfFeesFailedToSettle.ShouldBe(0);
100+
response.NumberOfFeesPendingSettlement.ShouldBe(0);
101+
response.NumberOfFeesSuccessfullySettled.ShouldBe(0);
102+
}
103+
104+
[Fact]
105+
public async Task TransactionDomainService_ProcessSettlement_AddSettledFeeThrownException_SettlementProcessed()
106+
{
107+
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
108+
ConfigurationReader.Initialise(configurationRoot);
109+
110+
Logger.Initialise(NullLogger.Instance);
111+
112+
Mock<ITransactionAggregateManager> transactionAggregateManager = new Mock<ITransactionAggregateManager>();
113+
transactionAggregateManager.Setup(t => t.AddSettledFee(It.IsAny<Guid>(),
114+
It.IsAny<Guid>(),
115+
It.IsAny<CalculatedFee>(),
116+
It.IsAny<DateTime>(),
117+
It.IsAny<DateTime>(),
118+
It.IsAny<CancellationToken>())).ThrowsAsync(new Exception());
119+
120+
Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>> settlementAggregateRepository =
121+
new Mock<IAggregateRepository<SettlementAggregate, DomainEventRecord.DomainEvent>>();
122+
settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
123+
.ReturnsAsync(TestData.GetSettlementAggregateWithPendingMerchantFees(10));
124+
125+
SettlementDomainService settlementDomainService =
126+
new SettlementDomainService(transactionAggregateManager.Object,
127+
settlementAggregateRepository.Object);
128+
129+
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
130+
TestData.EstateId,
131+
CancellationToken.None);
132+
133+
response.ShouldNotBeNull();
134+
response.NumberOfFeesFailedToSettle.ShouldBe(10);
135+
response.NumberOfFeesPendingSettlement.ShouldBe(0);
136+
response.NumberOfFeesSuccessfullySettled.ShouldBe(0);
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)