@@ -10,16 +10,21 @@ namespace FileProcessor.BusinessLogic.Tests
1010 using Common ;
1111 using EstateReporting . Database ;
1212 using EstateReporting . Database . Entities ;
13+ using FileAggregate ;
1314 using FIleProcessor . Models ;
1415 using Managers ;
1516 using Microsoft . EntityFrameworkCore ;
1617 using Microsoft . EntityFrameworkCore . Diagnostics ;
1718 using Moq ;
19+ using Shared . DomainDrivenDesign . EventSourcing ;
1820 using Shared . EntityFramework ;
21+ using Shared . EventStore . Aggregate ;
22+ using Shared . Exceptions ;
1923 using Shouldly ;
2024 using Testing ;
2125 using Xunit ;
2226 using FileImportLog = EstateReporting . Database . Entities . FileImportLog ;
27+ using FileLine = FIleProcessor . Models . FileLine ;
2328
2429 public class FileProcessingManagerTests
2530 {
@@ -29,7 +34,9 @@ public async Task FileProcessingManager_GetAllFileProfiles_AllFileProfilesReturn
2934 var fileProfiles = TestData . FileProfiles ;
3035 var contextFactory = this . CreateMockContextFactory ( ) ;
3136 Mock < IModelFactory > modelFactory = new Mock < IModelFactory > ( ) ;
32- FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory . Object ) ;
37+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
38+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
39+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory . Object , fileAggregateRepository . Object ) ;
3340
3441 var allFileProfiles = await manager . GetAllFileProfiles ( CancellationToken . None ) ;
3542 allFileProfiles . ShouldNotBeNull ( ) ;
@@ -42,7 +49,9 @@ public async Task FileProcessingManager_GetFileProfile_FIleProfileReturned()
4249 var fileProfiles = TestData . FileProfiles ;
4350 var contextFactory = this . CreateMockContextFactory ( ) ;
4451 Mock < IModelFactory > modelFactory = new Mock < IModelFactory > ( ) ;
45- FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory . Object ) ;
52+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
53+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
54+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory . Object , fileAggregateRepository . Object ) ;
4655
4756 var fileProfile = await manager . GetFileProfile ( TestData . SafaricomFileProfileId , CancellationToken . None ) ;
4857 fileProfile . ShouldNotBeNull ( ) ;
@@ -63,7 +72,9 @@ public async Task FileProcessingManager_GetFileImportLogs_NoMerchantId_ImportLog
6372 context . FileImportLogFiles . AddRange ( TestData . FileImportLog2Files ) ;
6473 context . SaveChanges ( ) ;
6574
66- FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory ) ;
75+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
76+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
77+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory , fileAggregateRepository . Object ) ;
6778
6879 var importLogs = await manager . GetFileImportLogs ( TestData . EstateId , TestData . ImportLogStartDate , TestData . ImportLogEndDate , null , CancellationToken . None ) ;
6980
@@ -84,7 +95,9 @@ public async Task FileProcessingManager_GetFileImportLogs_WithMerchantId_ImportL
8495 context . FileImportLogFiles . AddRange ( TestData . FileImportLog2Files ) ;
8596 context . SaveChanges ( ) ;
8697
87- FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory ) ;
98+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
99+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
100+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory , fileAggregateRepository . Object ) ;
88101
89102 var importLogs = await manager . GetFileImportLogs ( TestData . EstateId , TestData . ImportLogStartDate , TestData . ImportLogEndDate , TestData . MerchantId , CancellationToken . None ) ;
90103
@@ -105,7 +118,9 @@ public async Task FileProcessingManager_GetFileImportLog_NoMerchantId_ImportLogR
105118 context . FileImportLogFiles . AddRange ( TestData . FileImportLog2Files ) ;
106119 context . SaveChanges ( ) ;
107120
108- FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory ) ;
121+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
122+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
123+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory , fileAggregateRepository . Object ) ;
109124
110125 var importLog = await manager . GetFileImportLog ( TestData . FileImportLogId1 , TestData . EstateId , null , CancellationToken . None ) ;
111126
@@ -126,13 +141,85 @@ public async Task FileProcessingManager_GetFileImportLog_WithMerchantId_ImportLo
126141 context . FileImportLogFiles . AddRange ( TestData . FileImportLog2Files ) ;
127142 context . SaveChanges ( ) ;
128143
129- FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory ) ;
144+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
145+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
146+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory , fileAggregateRepository . Object ) ;
130147
131148 var importLog = await manager . GetFileImportLog ( TestData . FileImportLogId1 , TestData . EstateId , TestData . MerchantId , CancellationToken . None ) ;
132149
133150 this . VerifyImportLog ( TestData . FileImportLogs . First ( ) , importLog , TestData . MerchantId ) ;
134151 }
135152
153+ [ Fact ]
154+ public async Task FileProcessingManager_GetFile_FileReturned ( )
155+ {
156+ var fileProfiles = TestData . FileProfiles ;
157+ var context = await this . GetContext ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
158+ var contextFactory = this . CreateMockContextFactory ( ) ;
159+ contextFactory . Setup ( c => c . GetContext ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( context ) ;
160+ IModelFactory modelFactory = new ModelFactory ( ) ;
161+
162+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
163+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
164+ fileAggregateRepository . Setup ( f => f . GetLatestVersion ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( TestData . GetFileAggregateWithLines ) ;
165+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory , fileAggregateRepository . Object ) ;
166+
167+ var fileDetails = await manager . GetFile ( TestData . FileId , TestData . EstateId , CancellationToken . None ) ;
168+
169+ this . VerifyFile ( TestData . GetFileAggregateWithLines ( ) , fileDetails ) ;
170+ }
171+
172+ [ Fact ]
173+ public async Task FileProcessingManager_GetFile_FileNotFound_ErrorThrown ( )
174+ {
175+ var fileProfiles = TestData . FileProfiles ;
176+ var context = await this . GetContext ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
177+ var contextFactory = this . CreateMockContextFactory ( ) ;
178+ contextFactory . Setup ( c => c . GetContext ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( context ) ;
179+ IModelFactory modelFactory = new ModelFactory ( ) ;
180+
181+ Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > fileAggregateRepository =
182+ new Mock < IAggregateRepository < FileAggregate , DomainEventRecord . DomainEvent > > ( ) ;
183+ fileAggregateRepository . Setup ( f => f . GetLatestVersion ( It . IsAny < Guid > ( ) , It . IsAny < CancellationToken > ( ) ) ) . ReturnsAsync ( TestData . GetEmptyFileAggregate ) ;
184+ FileProcessorManager manager = new FileProcessorManager ( fileProfiles , contextFactory . Object , modelFactory , fileAggregateRepository . Object ) ;
185+
186+ Should . Throw < NotFoundException > ( async ( ) =>
187+ {
188+ await manager . GetFile ( TestData . FileId , TestData . EstateId , CancellationToken . None ) ;
189+ } ) ;
190+ }
191+
192+
193+ private void VerifyFile ( FileAggregate source , FileDetails fileDetails )
194+ {
195+ var fileModel = source . GetFile ( ) ;
196+
197+ fileDetails . FileId . ShouldBe ( fileModel . FileId ) ;
198+ fileDetails . FileImportLogId . ShouldBe ( fileModel . FileImportLogId ) ;
199+ fileDetails . FileLocation . ShouldBe ( fileModel . FileLocation ) ;
200+ fileDetails . FileProfileId . ShouldBe ( fileModel . FileProfileId ) ;
201+ fileDetails . MerchantId . ShouldBe ( fileModel . MerchantId ) ;
202+ fileDetails . ProcessingCompleted . ShouldBe ( fileModel . ProcessingCompleted ) ;
203+ fileDetails . UserId . ShouldBe ( fileModel . UserId ) ;
204+ fileDetails . EstateId . ShouldBe ( fileModel . EstateId ) ;
205+
206+ fileDetails . ProcessingSummary . ShouldNotBeNull ( ) ;
207+ fileDetails . ProcessingSummary . FailedLines . ShouldBe ( fileModel . ProcessingSummary . FailedLines ) ;
208+ fileDetails . ProcessingSummary . IgnoredLines . ShouldBe ( fileModel . ProcessingSummary . IgnoredLines ) ;
209+ fileDetails . ProcessingSummary . NotProcessedLines . ShouldBe ( fileModel . ProcessingSummary . NotProcessedLines ) ;
210+ fileDetails . ProcessingSummary . SuccessfullyProcessedLines . ShouldBe ( fileModel . ProcessingSummary . SuccessfullyProcessedLines ) ;
211+ fileDetails . ProcessingSummary . TotalLines . ShouldBe ( fileModel . ProcessingSummary . TotalLines ) ;
212+
213+ foreach ( FileLine fileModelFileLine in fileModel . FileLines )
214+ {
215+ FileLine ? fileLineToVerify = fileDetails . FileLines . SingleOrDefault ( f => f . LineNumber == fileModelFileLine . LineNumber ) ;
216+ fileLineToVerify . ShouldNotBeNull ( ) ;
217+ fileLineToVerify . LineData . ShouldBe ( fileModelFileLine . LineData ) ;
218+ fileLineToVerify . TransactionId . ShouldBe ( fileModelFileLine . TransactionId ) ;
219+ fileLineToVerify . ProcessingResult . ShouldBe ( fileModelFileLine . ProcessingResult ) ;
220+ }
221+ }
222+
136223 private void VerifyImportLogs ( List < FileImportLog > source , List < FIleProcessor . Models . FileImportLog > importLogs , Guid ? merchantId = null )
137224 {
138225 importLogs . ShouldNotBeNull ( ) ;
0 commit comments