Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<DebugType>Full</DebugType>
<DebugType>None</DebugType>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
48 changes: 34 additions & 14 deletions FileProcessor.BusinessLogic.Tests/FileRequestHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediatR;

namespace FileProcessor.BusinessLogic.Tests
{
Expand Down Expand Up @@ -76,6 +77,7 @@ public FileRequestHandlerTests()
SecurityServiceClient.Object,
fileFormatHandlerResolver,
FileSystem);
Logger.Initialise(NullLogger.Instance);
}

[Fact]
Expand Down Expand Up @@ -207,8 +209,11 @@ public void FileRequestHandler_SafaricomTopupRequest_RequestIsHandled()
{
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
}



[Fact]
public void FileRequestHandler_SafaricomTopupRequest_FileAggregateNotCreated_RequestIsHandled()
{
Expand All @@ -225,10 +230,11 @@ public void FileRequestHandler_SafaricomTopupRequest_FileAggregateNotCreated_Req
SafaricomTopupRequest safaricomTopupRequest =
new SafaricomTopupRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.NotThrow(async () =>
Should.Throw<InvalidOperationException>(async () =>
{
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/failed");
}

[Fact]
Expand Down Expand Up @@ -291,12 +297,13 @@ public async Task FileRequestHandler_SafaricomTopupRequest_ProcessedDirectoryNot
SafaricomTopupRequest safaricomTopupRequest =
new SafaricomTopupRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.Throw<DirectoryNotFoundException>(async () =>
Should.NotThrow(async () =>
{
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
}

[Fact]
public async Task FileRequestHandler_SafaricomTopupRequest_FailedDirectoryNotFound_RequestIsHandled()
{
Expand All @@ -311,11 +318,12 @@ public async Task FileRequestHandler_SafaricomTopupRequest_FailedDirectoryNotFou

SafaricomTopupRequest safaricomTopupRequest =
new SafaricomTopupRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.Throw<DirectoryNotFoundException>(async () =>
Should.NotThrow(async () =>
{
await this.FileRequestHandler.Handle(safaricomTopupRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/safaricom/processed");
}

[Fact]
Expand All @@ -340,6 +348,7 @@ public void FileRequestHandler_SafaricomTopupRequest_FileIsEmpty_RequestIsHandle
});

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

[Theory]
Expand Down Expand Up @@ -793,16 +802,19 @@ public void FileRequestHandler_VoucherRequest_RequestIsHandled()
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/inprogress");
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/processed");
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/failed");

VoucherRequest voucherRequest =
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.NotThrow(async () =>
{
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
}



[Fact]
public void FileRequestHandler_VoucherRequest_FileAggregateNotCreated_RequestIsHandled()
{
Expand All @@ -819,10 +831,11 @@ public void FileRequestHandler_VoucherRequest_FileAggregateNotCreated_RequestIsH
VoucherRequest voucherRequest =
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.NotThrow(async () =>
Should.Throw<InvalidOperationException>(async () =>
{
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/failed");
}

[Fact]
Expand Down Expand Up @@ -869,7 +882,7 @@ public async Task FileRequestHandler_VoucherRequest_NoFileProfiles_RequestIsHand
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
});
}

[Fact]
public async Task FileRequestHandler_VoucherRequest_ProcessedDirectoryNotFound_RequestIsHandled()
{
Expand All @@ -882,15 +895,14 @@ public async Task FileRequestHandler_VoucherRequest_ProcessedDirectoryNotFound_R
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/inprogress");
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/failed");

Logger.Initialise(NullLogger.Instance);

VoucherRequest voucherRequest =
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.Throw<DirectoryNotFoundException>(async () =>
Should.NotThrow(async () =>
{
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
}

[Fact]
Expand All @@ -904,21 +916,22 @@ public async Task FileRequestHandler_VoucherRequest_FailedDirectoryNotFound_Requ

this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/inprogress");
this.FileSystem.AddDirectory("home/txnproc/bulkfiles/voucher/processed");

VoucherRequest voucherRequest =
new VoucherRequest(TestData.FileId, TestData.FilePathWithName, TestData.FileProfileId);

Should.Throw<DirectoryNotFoundException>(async () =>
Should.NotThrow(async () =>
{
await this.FileRequestHandler.Handle(voucherRequest, CancellationToken.None);
});
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
}

[Fact]
public void FileRequestHandler_VoucherRequest_FileIsEmpty_RequestIsHandled()
{
this.FileProcessorManager.Setup(f => f.GetFileProfile(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.FileProfileVoucher);

this.FileAggregateRepository.Setup(f => f.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetCreatedFileAggregate);

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

this.FileAggregateRepository.Verify(f => f.SaveChanges(It.IsAny<FileAggregate>(), It.IsAny<CancellationToken>()), Times.Never);
this.VerifyFileProcessing("home/txnproc/bulkfiles/voucher/processed");
}

private void VerifyFileProcessing(String filePath)
{
IDirectoryInfo directoryInfo = this.FileSystem.DirectoryInfo.FromDirectoryName(filePath);
directoryInfo.GetFiles("*.*").Length.ShouldBe(1);
}
}
}
38 changes: 22 additions & 16 deletions FileProcessor.BusinessLogic/RequestHandlers/FileRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -254,11 +255,6 @@ private async Task<Unit> ProcessFile(Guid fileId,
FileProfile fileProfile = null;
try
{
FileAggregate fileAggregate = await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);

if (fileAggregate.IsCreated == false)
return new Unit();

fileProfile = await this.FileProcessorManager.GetFileProfile(fileProfileId, cancellationToken);

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

if (this.FileSystem.Directory.Exists(fileProfile.FailedDirectory) == false)
{
throw new DirectoryNotFoundException($"Directory {fileProfile.FailedDirectory} not found");
Logger.LogWarning($"Creating Directory {fileProfile.FailedDirectory} as not found");
this.FileSystem.Directory.CreateDirectory(fileProfile.FailedDirectory);
}

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

FileAggregate fileAggregate =
await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);

if (fileAggregate.IsCreated == false)
{
throw new InvalidOperationException($"File with Id {fileId} not created");
}

String fileContent = null;
//Open file for Read\Write
using(Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
using (Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
{
//Create object of StreamReader by passing FileStream object on which it needs to operates on
using(StreamReader sr = new StreamReader(fs))
using (StreamReader sr = new StreamReader(fs))
{
//Use ReadToEnd method to read all the content from file
fileContent = await sr.ReadToEndAsync();
Expand All @@ -308,19 +314,19 @@ private async Task<Unit> ProcessFile(Guid fileId,
await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);
}

Logger.LogInformation(
$"About to move file {inProgressFile.Name} to [{fileProfile.ProcessedDirectory}]");

// TODO: Move file now
inProgressFile.MoveTo($"{fileProfile.ProcessedDirectory}/{inProgressFile.Name}");

return new Unit();
}
catch(FileNotFoundException fex)
{
Logger.LogError(fex);
inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");
throw;
}
catch(Exception e)
catch (Exception e)
{
if (inProgressFile != null && fileProfile != null)
inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");

Logger.LogError(e);
throw;
}
Expand Down
2 changes: 1 addition & 1 deletion FileProcessor.Testing/FileProcessor.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<DebugType>None</DebugType>
<DebugType>Full</DebugType>
</PropertyGroup>

<ItemGroup>
Expand Down
20 changes: 19 additions & 1 deletion FileProcessor.Testing/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,27 @@ public static FileAggregate GetFileAggregateWithLinesAlreadyProcessed()
new Dictionary<String, String>
{
["AppSettings:ClientId"] = "clientId",
["AppSettings:ClientSecret"] = "clientSecret"
["AppSettings:ClientSecret"] = "clientSecret",
["AppSettings:TemporaryFileLocation"] = "C:\\Temp",
["AppSettings:FileProfilePollingWindowInSeconds"] = "30",
["ConnectionStrings:HealthCheck"] = "HeathCheckConnString",
["SecurityConfiguration:Authority"] = "https://127.0.0.1",
["EventStoreSettings:ConnectionString"] = "https://127.0.0.1:2113",
["EventStoreSettings:ConnectionName"] = "UnitTestConnection",
["EventStoreSettings:UserName"] = "admin",
["EventStoreSettings:Password"] = "changeit",
["AppSettings:UseConnectionStringConfig"] = "false",
["AppSettings:SecurityService"] = "http://127.0.0.1",
["AppSettings:MessagingServiceApi"] = "http://127.0.0.1",
["AppSettings:EstateManagementApi"] = "http://127.0.0.1",
["AppSettings:TransactionProcessorApi"] = "http://127.0.0.1",
["AppSettings:DatabaseEngine"] = "SqlServer"
};





public static TokenResponse TokenResponse()
{
return SecurityService.DataTransferObjects.Responses.TokenResponse.Create("AccessToken", string.Empty, 100);
Expand Down
34 changes: 18 additions & 16 deletions FileProcessor.Tests/BootstrapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FileProcessor.Tests
using FileProcessor.Testing;

namespace FileProcessor.Tests
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -31,25 +33,25 @@ public void VerifyBootstrapperIsValid()

private IConfigurationRoot SetupMemoryConfiguration()
{
Dictionary<String, String> configuration = new Dictionary<String, String>();
//Dictionary<String, String> configuration = new Dictionary<String, String>();

IConfigurationBuilder builder = new ConfigurationBuilder();

configuration.Add("ConnectionStrings:HealthCheck", "HeathCheckConnString");
configuration.Add("SecurityConfiguration:Authority", "https://127.0.0.1");
configuration.Add("EventStoreSettings:ConnectionString", "https://127.0.0.1:2113");
configuration.Add("EventStoreSettings:ConnectionName", "UnitTestConnection");
configuration.Add("EventStoreSettings:UserName", "admin");
configuration.Add("EventStoreSettings:Password", "changeit");
configuration.Add("AppSettings:UseConnectionStringConfig", "false");
configuration.Add("AppSettings:SecurityService", "http://127.0.0.1");
configuration.Add("AppSettings:MessagingServiceApi", "http://127.0.0.1");
configuration.Add("AppSettings:EstateManagementApi", "http://127.0.0.1");
configuration.Add("AppSettings:TransactionProcessorApi", "http://127.0.0.1");
configuration.Add("AppSettings:DatabaseEngine", "SqlServer");

builder.AddInMemoryCollection(configuration);
//configuration.Add("ConnectionStrings:HealthCheck", "HeathCheckConnString");
//configuration.Add("SecurityConfiguration:Authority", "https://127.0.0.1");
//configuration.Add("EventStoreSettings:ConnectionString", "https://127.0.0.1:2113");
//configuration.Add("EventStoreSettings:ConnectionName", "UnitTestConnection");
//configuration.Add("EventStoreSettings:UserName", "admin");
//configuration.Add("EventStoreSettings:Password", "changeit");
//configuration.Add("AppSettings:UseConnectionStringConfig", "false");
//configuration.Add("AppSettings:SecurityService", "http://127.0.0.1");
//configuration.Add("AppSettings:MessagingServiceApi", "http://127.0.0.1");
//configuration.Add("AppSettings:EstateManagementApi", "http://127.0.0.1");
//configuration.Add("AppSettings:TransactionProcessorApi", "http://127.0.0.1");
//configuration.Add("AppSettings:DatabaseEngine", "SqlServer");

//builder.AddInMemoryCollection(configuration);
builder.AddInMemoryCollection(TestData.DefaultAppSettings);
return builder.Build();
}

Expand Down
Loading