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