66
77namespace TransactionProcessor . BusinessLogic . Tests . DomainEventHandlers ;
88
9- using System ;
10- using System . Collections . Generic ;
11- using System . IO ;
12- using System . IO . Abstractions . TestingHelpers ;
13- using System . Reflection ;
14- using System . Threading ;
15- using System . Threading . Tasks ;
169using EventHandling ;
1710using MessagingService . Client ;
1811using Microsoft . EntityFrameworkCore ;
1912using Microsoft . EntityFrameworkCore . Diagnostics ;
2013using Microsoft . Extensions . Configuration ;
14+ using Microsoft . Extensions . DependencyInjection ;
2115using Moq ;
2216using SecurityService . Client ;
2317using Shared . DomainDrivenDesign . EventSourcing ;
18+ using Shared . EntityFramework ;
2419using Shared . EventStore . Aggregate ;
2520using Shared . General ;
2621using Shared . Logger ;
22+ using System ;
23+ using System . Collections . Generic ;
24+ using System . IO ;
25+ using System . IO . Abstractions . TestingHelpers ;
26+ using System . Reflection ;
27+ using System . Threading ;
28+ using System . Threading . Tasks ;
2729using Testing ;
2830using Xunit ;
2931
@@ -35,124 +37,99 @@ public enum TestDatabaseType
3537
3638public class VoucherDomainEventHandlerTests
3739{
38- private Mock < Shared . EntityFramework . IDbContextFactory < EstateManagementContext > > GetMockDbContextFactory ( )
40+ private readonly Mock < IAggregateService > AggregateService ;
41+ private readonly Mock < IDbContextResolver < EstateManagementContext > > DbContextFactory ;
42+ private readonly EstateManagementContext Context ;
43+ private readonly Mock < ISecurityServiceClient > SecurityServiceClient ;
44+ private readonly Mock < IMessagingServiceClient > MessagingServiceClient ;
45+ private readonly VoucherDomainEventHandler VoucherDomainEventHandler ;
46+
47+ private EstateManagementContext GetContext ( String databaseName )
3948 {
40- return new Mock < Shared . EntityFramework . IDbContextFactory < EstateManagementContext > > ( ) ;
49+ EstateManagementContext context = null ;
50+ DbContextOptionsBuilder < EstateManagementContext > builder = new DbContextOptionsBuilder < EstateManagementContext > ( ) . UseInMemoryDatabase ( databaseName ) . ConfigureWarnings ( w => w . Ignore ( InMemoryEventId . TransactionIgnoredWarning ) ) ;
51+ return new EstateManagementContext ( builder . Options ) ;
4152 }
4253
43- private async Task < EstateManagementContext > GetContext ( String databaseName , TestDatabaseType databaseType = TestDatabaseType . InMemory )
44- {
45- EstateManagementContext context = null ;
46- if ( databaseType == TestDatabaseType . InMemory )
47- {
48- DbContextOptionsBuilder < EstateManagementContext > builder = new DbContextOptionsBuilder < EstateManagementContext > ( )
49- . UseInMemoryDatabase ( databaseName )
50- . ConfigureWarnings ( w => w . Ignore ( InMemoryEventId . TransactionIgnoredWarning ) ) ;
51- context = new EstateManagementContext ( builder . Options ) ;
52- }
53- else
54+ public VoucherDomainEventHandlerTests ( ) {
55+ IConfigurationRoot configurationRoot = new ConfigurationBuilder ( ) . AddInMemoryCollection ( TestData . DefaultAppSettings ) . Build ( ) ;
56+ ConfigurationReader . Initialise ( configurationRoot ) ;
57+ Logger . Initialise ( NullLogger . Instance ) ;
58+
59+ SecurityServiceClient = new Mock < ISecurityServiceClient > ( ) ;
60+ MessagingServiceClient = new Mock < IMessagingServiceClient > ( ) ;
61+
62+ DirectoryInfo path = Directory . GetParent ( Assembly . GetExecutingAssembly ( ) . Location ) ;
63+ MockFileSystem fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
5464 {
55- throw new NotSupportedException ( $ "Database type [{ databaseType } ] not supported") ;
56- }
65+ { $ "{ path } /VoucherMessages/VoucherEmail.html", new MockFileData ( "Transaction Number: [TransactionNumber]" ) } ,
66+ { $ "{ path } /VoucherMessages/VoucherSMS.txt", new MockFileData ( "Transaction Number: [TransactionNumber]" ) }
67+ } ) ;
68+
69+ this . AggregateService = new Mock < IAggregateService > ( ) ;
70+ this . DbContextFactory = new Mock < IDbContextResolver < EstateManagementContext > > ( ) ;
71+ this . Context = this . GetContext ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
72+ var services = new ServiceCollection ( ) ;
73+ services . AddTransient < EstateManagementContext > ( _ => this . Context ) ;
74+ var serviceProvider = services . BuildServiceProvider ( ) ;
75+ var scope = serviceProvider . CreateScope ( ) ;
76+ this . DbContextFactory . Setup ( d => d . Resolve ( It . IsAny < String > ( ) , It . IsAny < String > ( ) ) ) . Returns ( new ResolvedDbContext < EstateManagementContext > ( scope ) ) ;
77+
78+ VoucherDomainEventHandler = new VoucherDomainEventHandler ( this . SecurityServiceClient . Object ,
79+ this . AggregateService . Object ,
80+ this . DbContextFactory . Object ,
81+ this . MessagingServiceClient . Object ,
82+ fileSystem ) ;
5783
58- return context ;
5984 }
6085
6186 [ Fact ]
6287 public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithEmailAddress_IsHandled ( )
6388 {
64- IConfigurationRoot configurationRoot = new ConfigurationBuilder ( ) . AddInMemoryCollection ( TestData . DefaultAppSettings ) . Build ( ) ;
65- ConfigurationReader . Initialise ( configurationRoot ) ;
66- Logger . Initialise ( NullLogger . Instance ) ;
89+ this . SecurityServiceClient . Setup ( s => s . GetToken ( It . IsAny < String > ( ) , It . IsAny < String > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( Result . Success ( TestData . TokenResponse ( ) ) ) ;
6790
68- Mock < ISecurityServiceClient > securityServiceClient = new Mock < ISecurityServiceClient > ( ) ;
69- securityServiceClient . Setup ( s => s . GetToken ( It . IsAny < String > ( ) , It . IsAny < String > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( Result . Success ( TestData . TokenResponse ( ) ) ) ;
70-
71- Mock < IAggregateService > aggregateService = new ( ) ;
72- aggregateService . Setup ( t => t . Get < VoucherAggregate > ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) )
91+ this . AggregateService . Setup ( t => t . Get < VoucherAggregate > ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) )
7392 . ReturnsAsync ( Result . Success ( TestData . GetVoucherAggregateWithRecipientEmail ( ) ) ) ;
7493
75- EstateManagementContext context = await this . GetContext ( Guid . NewGuid ( ) . ToString ( "N" ) , TestDatabaseType . InMemory ) ;
76- context . Transactions . Add ( new Database . Entities . Transaction ( )
94+ this . Context . Transactions . Add ( new Database . Entities . Transaction ( )
7795 {
7896 TransactionId = TestData . TransactionId ,
7997 MerchantId = TestData . MerchantId ,
8098 ContractId = TestData . ContractId
8199 } ) ;
82- context . Contracts . Add ( new Contract
100+ this . Context . Contracts . Add ( new Contract
83101 {
84102 ContractId = TestData . ContractId ,
85103 EstateId = TestData . EstateId ,
86104 Description = TestData . OperatorIdentifier
87105 } ) ;
88- await context . SaveChangesAsync ( CancellationToken . None ) ;
89-
90- var dbContextFactory = this . GetMockDbContextFactory ( ) ;
91- dbContextFactory . Setup ( d => d . GetContext ( It . IsAny < Guid > ( ) , It . IsAny < String > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( context ) ;
92-
93- Mock < IMessagingServiceClient > messagingServiceClient = new Mock < IMessagingServiceClient > ( ) ;
94-
95- DirectoryInfo path = Directory . GetParent ( Assembly . GetExecutingAssembly ( ) . Location ) ;
96- MockFileSystem fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
97- {
98- { $ "{ path } /VoucherMessages/VoucherEmail.html", new MockFileData ( "Transaction Number: [TransactionNumber]" ) }
99- } ) ;
100-
101- VoucherDomainEventHandler voucherDomainEventHandler = new VoucherDomainEventHandler ( securityServiceClient . Object ,
102- aggregateService . Object ,
103- dbContextFactory . Object ,
104- messagingServiceClient . Object ,
105- fileSystem ) ;
106+ await this . Context . SaveChangesAsync ( CancellationToken . None ) ;
106107
107- await voucherDomainEventHandler . Handle ( TestData . VoucherIssuedEvent , CancellationToken . None ) ;
108+ await this . VoucherDomainEventHandler . Handle ( TestData . VoucherIssuedEvent , CancellationToken . None ) ;
108109 }
109-
110+
110111 [ Fact ]
111112 public async Task VoucherDomainEventHandler_VoucherIssuedEvent_WithRecipientMobile_IsHandled ( )
112113 {
113- IConfigurationRoot configurationRoot = new ConfigurationBuilder ( ) . AddInMemoryCollection ( TestData . DefaultAppSettings ) . Build ( ) ;
114- ConfigurationReader . Initialise ( configurationRoot ) ;
115- Logger . Initialise ( NullLogger . Instance ) ;
116-
117- Mock < ISecurityServiceClient > securityServiceClient = new Mock < ISecurityServiceClient > ( ) ;
118- securityServiceClient . Setup ( s => s . GetToken ( It . IsAny < String > ( ) , It . IsAny < String > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( Result . Success ( TestData . TokenResponse ( ) ) ) ;
114+ this . SecurityServiceClient . Setup ( s => s . GetToken ( It . IsAny < String > ( ) , It . IsAny < String > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( Result . Success ( TestData . TokenResponse ( ) ) ) ;
119115
120- Mock < IAggregateService > aggregateService = new ( ) ;
121- aggregateService . Setup ( t => t . Get < VoucherAggregate > ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) )
116+ this . AggregateService . Setup ( t => t . Get < VoucherAggregate > ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) )
122117 . ReturnsAsync ( Result . Success ( TestData . GetVoucherAggregateWithRecipientMobile ( ) ) ) ;
123118
124- EstateManagementContext context = await this . GetContext ( Guid . NewGuid ( ) . ToString ( "N" ) , TestDatabaseType . InMemory ) ;
125- context . Transactions . Add ( new Database . Entities . Transaction ( )
119+ this . Context . Transactions . Add ( new Database . Entities . Transaction ( )
126120 {
127121 TransactionId = TestData . TransactionId ,
128122 MerchantId = TestData . MerchantId ,
129123 ContractId = TestData . ContractId
130124 } ) ;
131- context . Contracts . Add ( new Contract
125+ this . Context . Contracts . Add ( new Contract
132126 {
133127 ContractId = TestData . ContractId ,
134128 EstateId = TestData . EstateId ,
135129 Description = TestData . OperatorIdentifier
136130 } ) ;
137- await context . SaveChangesAsync ( CancellationToken . None ) ;
138-
139- Mock < Shared . EntityFramework . IDbContextFactory < EstateManagementContext > > dbContextFactory = this . GetMockDbContextFactory ( ) ;
140- dbContextFactory . Setup ( d => d . GetContext ( It . IsAny < Guid > ( ) , It . IsAny < String > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( context ) ;
141-
142- Mock < IMessagingServiceClient > messagingServiceClient = new Mock < IMessagingServiceClient > ( ) ;
143-
144- DirectoryInfo path = Directory . GetParent ( Assembly . GetExecutingAssembly ( ) . Location ) ;
145- MockFileSystem fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
146- {
147- { $ "{ path } /VoucherMessages/VoucherSMS.txt", new MockFileData ( "Transaction Number: [TransactionNumber]" ) }
148- } ) ;
149-
150- VoucherDomainEventHandler voucherDomainEventHandler = new VoucherDomainEventHandler ( securityServiceClient . Object ,
151- aggregateService . Object ,
152- dbContextFactory . Object ,
153- messagingServiceClient . Object ,
154- fileSystem ) ;
131+ await this . Context . SaveChangesAsync ( CancellationToken . None ) ;
155132
156- await voucherDomainEventHandler . Handle ( TestData . VoucherIssuedEvent , CancellationToken . None ) ;
133+ await VoucherDomainEventHandler . Handle ( TestData . VoucherIssuedEvent , CancellationToken . None ) ;
157134 }
158135}
0 commit comments