Skip to content

Commit 5c57e53

Browse files
Merge pull request #148 from TransactionProcessing/task/146_reworkprocessingflow
Some refactoring on processing and additional unit tests
2 parents 87e690b + 47b9906 commit 5c57e53

9 files changed

Lines changed: 188 additions & 54 deletions

File tree

FileProcessor.BusinessLogic.Tests/FileProcessor.BusinessLogic.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<DebugType>Full</DebugType>
5+
<DebugType>None</DebugType>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

FileProcessor.BusinessLogic.Tests/FileRequestHandlerTests.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using MediatR;
67

78
namespace FileProcessor.BusinessLogic.Tests
89
{
@@ -76,6 +77,7 @@ public FileRequestHandlerTests()
7677
SecurityServiceClient.Object,
7778
fileFormatHandlerResolver,
7879
FileSystem);
80+
Logger.Initialise(NullLogger.Instance);
7981
}
8082

8183
[Fact]
@@ -207,8 +209,11 @@ public void FileRequestHandler_SafaricomTopupRequest_RequestIsHandled()
207209
{
208210
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
209211
});
212+
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
210213
}
211214

215+
216+
212217
[Fact]
213218
public void FileRequestHandler_SafaricomTopupRequest_FileAggregateNotCreated_RequestIsHandled()
214219
{
@@ -225,10 +230,11 @@ public void FileRequestHandler_SafaricomTopupRequest_FileAggregateNotCreated_Req
225230
SafaricomTopupRequest safaricomTopupRequest =
226231
new SafaricomTopupRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
227232

228-
Should.NotThrow(async () =>
233+
Should.Throw<InvalidOperationException>(async () =>
229234
{
230235
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
231236
});
237+
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/failed");
232238
}
233239

234240
[Fact]
@@ -291,12 +297,13 @@ public async Task FileRequestHandler_SafaricomTopupRequest_ProcessedDirectoryNot
291297
SafaricomTopupRequest safaricomTopupRequest =
292298
new SafaricomTopupRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
293299

294-
Should.Throw<DirectoryNotFoundException>(async () =>
300+
Should.NotThrow(async () =>
295301
{
296302
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
297303
});
304+
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
298305
}
299-
306+
300307
[Fact]
301308
public async Task FileRequestHandler_SafaricomTopupRequest_FailedDirectoryNotFound_RequestIsHandled()
302309
{
@@ -311,11 +318,12 @@ public async Task FileRequestHandler_SafaricomTopupRequest_FailedDirectoryNotFou
311318

312319
SafaricomTopupRequest safaricomTopupRequest =
313320
new SafaricomTopupRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
314-
315-
Should.Throw<DirectoryNotFoundException>(async () =>
321+
322+
Should.NotThrow(async () =>
316323
{
317324
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
318325
});
326+
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
319327
}
320328

321329
[Fact]
@@ -340,6 +348,7 @@ public void FileRequestHandler_SafaricomTopupRequest_FileIsEmpty_RequestIsHandle
340348
});
341349

342350
this.FileAggregateRepository.Verify(f => f.SaveChanges(It.IsAny<FileAggregate>(), It.IsAny<CancellationToken>()), Times.Never);
351+
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
343352
}
344353

345354
[Theory]
@@ -793,16 +802,19 @@ public void FileRequestHandler_VoucherRequest_RequestIsHandled()
793802
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/inprogress");
794803
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/processed");
795804
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/failed");
796-
805+
797806
VoucherRequest voucherRequest =
798807
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
799808

800809
Should.NotThrow(async () =>
801810
{
802811
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
803812
});
813+
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
804814
}
805815

816+
817+
806818
[Fact]
807819
public void FileRequestHandler_VoucherRequest_FileAggregateNotCreated_RequestIsHandled()
808820
{
@@ -819,10 +831,11 @@ public void FileRequestHandler_VoucherRequest_FileAggregateNotCreated_RequestIsH
819831
VoucherRequest voucherRequest =
820832
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
821833

822-
Should.NotThrow(async () =>
834+
Should.Throw<InvalidOperationException>(async () =>
823835
{
824836
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
825837
});
838+
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/failed");
826839
}
827840

828841
[Fact]
@@ -869,7 +882,7 @@ public async Task FileRequestHandler_VoucherRequest_NoFileProfiles_RequestIsHand
869882
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
870883
});
871884
}
872-
885+
873886
[Fact]
874887
public async Task FileRequestHandler_VoucherRequest_ProcessedDirectoryNotFound_RequestIsHandled()
875888
{
@@ -882,15 +895,14 @@ public async Task FileRequestHandler_VoucherRequest_ProcessedDirectoryNotFound_R
882895
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/inprogress");
883896
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/failed");
884897

885-
Logger.Initialise(NullLogger.Instance);
886-
887898
VoucherRequest voucherRequest =
888899
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
889900

890-
Should.Throw<DirectoryNotFoundException>(async () =>
901+
Should.NotThrow(async () =>
891902
{
892903
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
893904
});
905+
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
894906
}
895907

896908
[Fact]
@@ -904,21 +916,22 @@ public async Task FileRequestHandler_VoucherRequest_FailedDirectoryNotFound_Requ
904916

905917
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/inprogress");
906918
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/processed");
907-
919+
908920
VoucherRequest voucherRequest =
909921
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);
910922

911-
Should.Throw<DirectoryNotFoundException>(async () =>
923+
Should.NotThrow(async () =>
912924
{
913925
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
914926
});
927+
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
915928
}
916929

917930
[Fact]
918931
public void FileRequestHandler_VoucherRequest_FileIsEmpty_RequestIsHandled()
919932
{
920933
this.FileProcessorManager.Setup(f => f.GetFileProfile(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.FileProfileVoucher);
921-
934+
922935
this.FileAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetCreatedFileAggregate);
923936

924937
this.FileSystem.AddFile(TestData.FilePathWithName, new MockFileData(String.Empty));
@@ -936,6 +949,13 @@ public void FileRequestHandler_VoucherRequest_FileIsEmpty_RequestIsHandled()
936949
});
937950

938951
this.FileAggregateRepository.Verify(f => f.SaveChanges(It.IsAny<FileAggregate>(), It.IsAny<CancellationToken>()), Times.Never);
952+
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
953+
}
954+
955+
private void VerifyFileProcessing(String filePath)
956+
{
957+
IDirectoryInfo directoryInfo = this.FileSystem.DirectoryInfo.FromDirectoryName(filePath);
958+
directoryInfo.GetFiles("*.*").Length.ShouldBe(1);
939959
}
940960
}
941961
}

FileProcessor.BusinessLogic/RequestHandlers/FileRequestHandler.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -254,11 +255,6 @@ private async Task<Unit> ProcessFile(Guid fileId,
254255
FileProfile fileProfile = null;
255256
try
256257
{
257-
FileAggregate fileAggregate = await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);
258-
259-
if (fileAggregate.IsCreated == false)
260-
return new Unit();
261-
262258
fileProfile = await this.FileProcessorManager.GetFileProfile(fileProfileId, cancellationToken);
263259

264260
if (fileProfile == null)
@@ -269,12 +265,14 @@ private async Task<Unit> ProcessFile(Guid fileId,
269265
// Check the processed/failed directories exist
270266
if (this.FileSystem.Directory.Exists(fileProfile.ProcessedDirectory) == false)
271267
{
272-
throw new DirectoryNotFoundException($"Directory {fileProfile.ProcessedDirectory} not found");
268+
Logger.LogWarning($"Creating Directory {fileProfile.ProcessedDirectory} as not found");
269+
this.FileSystem.Directory.CreateDirectory(fileProfile.ProcessedDirectory);
273270
}
274271

275272
if (this.FileSystem.Directory.Exists(fileProfile.FailedDirectory) == false)
276273
{
277-
throw new DirectoryNotFoundException($"Directory {fileProfile.FailedDirectory} not found");
274+
Logger.LogWarning($"Creating Directory {fileProfile.FailedDirectory} as not found");
275+
this.FileSystem.Directory.CreateDirectory(fileProfile.FailedDirectory);
278276
}
279277

280278
inProgressFile = this.FileSystem.FileInfo.FromFileName(fileName);
@@ -284,12 +282,20 @@ private async Task<Unit> ProcessFile(Guid fileId,
284282
throw new FileNotFoundException($"File {inProgressFile.FullName} not found");
285283
}
286284

285+
FileAggregate fileAggregate =
286+
await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);
287+
288+
if (fileAggregate.IsCreated == false)
289+
{
290+
throw new InvalidOperationException($"File with Id {fileId} not created");
291+
}
292+
287293
String fileContent = null;
288294
//Open file for Read\Write
289-
using(Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
295+
using (Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
290296
{
291297
//Create object of StreamReader by passing FileStream object on which it needs to operates on
292-
using(StreamReader sr = new StreamReader(fs))
298+
using (StreamReader sr = new StreamReader(fs))
293299
{
294300
//Use ReadToEnd method to read all the content from file
295301
fileContent = await sr.ReadToEndAsync();
@@ -308,19 +314,19 @@ private async Task<Unit> ProcessFile(Guid fileId,
308314
await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);
309315
}
310316

317+
Logger.LogInformation(
318+
$"About to move file {inProgressFile.Name} to [{fileProfile.ProcessedDirectory}]");
319+
311320
// TODO: Move file now
312321
inProgressFile.MoveTo($"{fileProfile.ProcessedDirectory}/{inProgressFile.Name}");
313322

314323
return new Unit();
315324
}
316-
catch(FileNotFoundException fex)
317-
{
318-
Logger.LogError(fex);
319-
inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");
320-
throw;
321-
}
322-
catch(Exception e)
325+
catch (Exception e)
323326
{
327+
if (inProgressFile != null && fileProfile != null)
328+
inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");
329+
324330
Logger.LogError(e);
325331
throw;
326332
}

FileProcessor.Testing/FileProcessor.Testing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<DebugType>None</DebugType>
5+
<DebugType>Full</DebugType>
66
</PropertyGroup>
77

88
<ItemGroup>

FileProcessor.Testing/TestData.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,27 @@ public static FileAggregate GetFileAggregateWithLinesAlreadyProcessed()
284284
new Dictionary<String, String>
285285
{
286286
["AppSettings:ClientId"] = "clientId",
287-
["AppSettings:ClientSecret"] = "clientSecret"
287+
["AppSettings:ClientSecret"] = "clientSecret",
288+
["AppSettings:TemporaryFileLocation"] = "C:\\Temp",
289+
["AppSettings:FileProfilePollingWindowInSeconds"] = "30",
290+
["ConnectionStrings:HealthCheck"] = "HeathCheckConnString",
291+
["SecurityConfiguration:Authority"] = "https://127.0.0.1",
292+
["EventStoreSettings:ConnectionString"] = "https://127.0.0.1:2113",
293+
["EventStoreSettings:ConnectionName"] = "UnitTestConnection",
294+
["EventStoreSettings:UserName"] = "admin",
295+
["EventStoreSettings:Password"] = "changeit",
296+
["AppSettings:UseConnectionStringConfig"] = "false",
297+
["AppSettings:SecurityService"] = "http://127.0.0.1",
298+
["AppSettings:MessagingServiceApi"] = "http://127.0.0.1",
299+
["AppSettings:EstateManagementApi"] = "http://127.0.0.1",
300+
["AppSettings:TransactionProcessorApi"] = "http://127.0.0.1",
301+
["AppSettings:DatabaseEngine"] = "SqlServer"
288302
};
289303

304+
305+
306+
307+
290308
public static TokenResponse TokenResponse()
291309
{
292310
return SecurityService.DataTransferObjects.Responses.TokenResponse.Create("AccessToken", string.Empty, 100);

FileProcessor.Tests/BootstrapperTests.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace FileProcessor.Tests
1+
using FileProcessor.Testing;
2+
3+
namespace FileProcessor.Tests
24
{
35
using System;
46
using System.Collections.Generic;
@@ -31,25 +33,25 @@ public void VerifyBootstrapperIsValid()
3133

3234
private IConfigurationRoot SetupMemoryConfiguration()
3335
{
34-
Dictionary<String, String> configuration = new Dictionary<String, String>();
36+
//Dictionary<String, String> configuration = new Dictionary<String, String>();
3537

3638
IConfigurationBuilder builder = new ConfigurationBuilder();
3739

38-
configuration.Add("ConnectionStrings:HealthCheck", "HeathCheckConnString");
39-
configuration.Add("SecurityConfiguration:Authority", "https://127.0.0.1");
40-
configuration.Add("EventStoreSettings:ConnectionString", "https://127.0.0.1:2113");
41-
configuration.Add("EventStoreSettings:ConnectionName", "UnitTestConnection");
42-
configuration.Add("EventStoreSettings:UserName", "admin");
43-
configuration.Add("EventStoreSettings:Password", "changeit");
44-
configuration.Add("AppSettings:UseConnectionStringConfig", "false");
45-
configuration.Add("AppSettings:SecurityService", "http://127.0.0.1");
46-
configuration.Add("AppSettings:MessagingServiceApi", "http://127.0.0.1");
47-
configuration.Add("AppSettings:EstateManagementApi", "http://127.0.0.1");
48-
configuration.Add("AppSettings:TransactionProcessorApi", "http://127.0.0.1");
49-
configuration.Add("AppSettings:DatabaseEngine", "SqlServer");
50-
51-
builder.AddInMemoryCollection(configuration);
40+
//configuration.Add("ConnectionStrings:HealthCheck", "HeathCheckConnString");
41+
//configuration.Add("SecurityConfiguration:Authority", "https://127.0.0.1");
42+
//configuration.Add("EventStoreSettings:ConnectionString", "https://127.0.0.1:2113");
43+
//configuration.Add("EventStoreSettings:ConnectionName", "UnitTestConnection");
44+
//configuration.Add("EventStoreSettings:UserName", "admin");
45+
//configuration.Add("EventStoreSettings:Password", "changeit");
46+
//configuration.Add("AppSettings:UseConnectionStringConfig", "false");
47+
//configuration.Add("AppSettings:SecurityService", "http://127.0.0.1");
48+
//configuration.Add("AppSettings:MessagingServiceApi", "http://127.0.0.1");
49+
//configuration.Add("AppSettings:EstateManagementApi", "http://127.0.0.1");
50+
//configuration.Add("AppSettings:TransactionProcessorApi", "http://127.0.0.1");
51+
//configuration.Add("AppSettings:DatabaseEngine", "SqlServer");
5252

53+
//builder.AddInMemoryCollection(configuration);
54+
builder.AddInMemoryCollection(TestData.DefaultAppSettings);
5355
return builder.Build();
5456
}
5557

0 commit comments

Comments
 (0)