Skip to content

Commit 238d578

Browse files
Add in unit tests
1 parent 580f97e commit 238d578

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

TransactionProcessor.BusinessLogic.Tests/Services/SettlementDomainServiceTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66
using BusinessLogic.Services;
7+
using EstateManagement.Client;
78
using Microsoft.Extensions.Configuration;
89
using Models;
910
using Moq;
11+
using SecurityService.Client;
1012
using SettlementAggregates;
1113
using Shared.DomainDrivenDesign.EventSourcing;
1214
using Shared.EventStore.Aggregate;
@@ -23,16 +25,23 @@ public class SettlementDomainServiceTests
2325

2426
private Mock<IAggregateRepository<SettlementAggregate, DomainEvent>> settlementAggregateRepository;
2527

28+
private Mock<ISecurityServiceClient> securityServiceClient;
29+
30+
private Mock<IEstateClient> estateClient;
31+
2632
private SettlementDomainService settlementDomainService;
2733

2834
public SettlementDomainServiceTests() {
2935
this.transactionAggregateRepository =
3036
new Mock<IAggregateRepository<TransactionAggregate, DomainEvent>>();
3137
this.settlementAggregateRepository =
3238
new Mock<IAggregateRepository<SettlementAggregate, DomainEvent>>();
39+
this.securityServiceClient = new Mock<ISecurityServiceClient>();
40+
this.estateClient = new Mock<IEstateClient>();
3341

3442
this.settlementDomainService =
35-
new SettlementDomainService(this.transactionAggregateRepository.Object, settlementAggregateRepository.Object);
43+
new SettlementDomainService(this.transactionAggregateRepository.Object, settlementAggregateRepository.Object,
44+
this.securityServiceClient.Object, this.estateClient.Object);
3645

3746
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
3847
ConfigurationReader.Initialise(configurationRoot);
@@ -48,6 +57,11 @@ public async Task SettlementDomainService_ProcessSettlement_SettlementIsProcesse
4857
this.transactionAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
4958
.ReturnsAsync(TestData.GetCompletedAuthorisedSaleTransactionAggregate);
5059

60+
this.securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
61+
62+
this.estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
63+
.ReturnsAsync(TestData.GetMerchantResponseWithOperator1);
64+
5165
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
5266
TestData.EstateId,
5367
TestData.MerchantId,
@@ -82,6 +96,11 @@ public async Task SettlementDomainService_ProcessSettlement_SettlementAggregateN
8296
settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
8397
.ReturnsAsync(TestData.GetCreatedSettlementAggregate);
8498

99+
this.securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
100+
101+
this.estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
102+
.ReturnsAsync(TestData.GetMerchantResponseWithOperator1);
103+
85104
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
86105
TestData.EstateId,
87106
TestData.MerchantId,
@@ -99,6 +118,11 @@ public async Task SettlementDomainService_ProcessSettlement_AddSettledFeeThrownE
99118
settlementAggregateRepository.Setup(s => s.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
100119
.ReturnsAsync(TestData.GetSettlementAggregateWithPendingMerchantFees(10));
101120

121+
this.securityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.TokenResponse);
122+
123+
this.estateClient.Setup(e => e.GetMerchant(It.IsAny<String>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
124+
.ReturnsAsync(TestData.GetMerchantResponseWithOperator1);
125+
102126
ProcessSettlementResponse response = await settlementDomainService.ProcessSettlement(TestData.SettlementDate,
103127
TestData.EstateId,
104128
TestData.MerchantId,
@@ -110,4 +134,4 @@ public async Task SettlementDomainService_ProcessSettlement_AddSettledFeeThrownE
110134
response.NumberOfFeesSuccessfullySettled.ShouldBe(0);
111135
}
112136
}
113-
}
137+
}

TransactionProcessor.SettlementAggregates.Tests/SettlementAggregateTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void SettlementAggregate_StartProcessing_CalledTwice_ProcessingStarted(){
209209
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTimeSecondCall);
210210

211211
aggregate.ProcessingStarted.ShouldBeTrue();
212-
aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTimeSecondCall);
212+
aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTimeSecondCall);
213213
}
214214

215215
[Fact]
@@ -221,5 +221,38 @@ public void SettlementAggregate_StartProcessing_SettlementNotCreated_ErrorThron(
221221
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime);
222222
});
223223
}
224+
225+
[Fact]
226+
public void SettlementAggregate_ManuallyComplete_SettlementCompleted()
227+
{
228+
SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId);
229+
aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate);
230+
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime);
231+
aggregate.ManuallyComplete();
232+
233+
aggregate.SettlementComplete.ShouldBeTrue();
234+
}
235+
236+
[Fact]
237+
public void SettlementAggregate_ManuallyComplete_CalledTwice_SettlementCompleted()
238+
{
239+
SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId);
240+
aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate);
241+
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime);
242+
aggregate.ManuallyComplete();
243+
aggregate.ManuallyComplete();
244+
245+
aggregate.SettlementComplete.ShouldBeTrue();
246+
}
247+
248+
[Fact]
249+
public void SettlementAggregate_ManuallyComplete_SettlementNotCreated_ErrorThron()
250+
{
251+
SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId);
252+
253+
Should.Throw<InvalidOperationException>(() => {
254+
aggregate.ManuallyComplete();
255+
});
256+
}
224257
}
225258
}

TransactionProcessor.SettlementAggregates/SettlementAggregate.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public static void StartProcessing(this SettlementAggregate aggregate, DateTime
2323
}
2424

2525
public static void ManuallyComplete(this SettlementAggregate aggregate){
26+
27+
aggregate.CheckHasBeenCreated();
28+
29+
if (aggregate.SettlementComplete)
30+
return;
31+
2632
SettlementCompletedEvent pendingSettlementCompletedEvent = new SettlementCompletedEvent(aggregate.AggregateId, aggregate.EstateId);
2733
aggregate.ApplyAndAppend(pendingSettlementCompletedEvent);
2834
}
@@ -187,7 +193,7 @@ private static void CheckHasNotAlreadyBeenCreated(this SettlementAggregate aggre
187193
throw new InvalidOperationException($"Pending Settlement already created for this date {aggregate.SettlementDate}");
188194
}
189195
}
190-
196+
191197
public static void PlayEvent(this SettlementAggregate aggregate, MerchantFeeSettledEvent domainEvent)
192198
{
193199
// Add to the settled fees list

0 commit comments

Comments
 (0)