Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/459240 part3 writing initial result file run information #75

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7dbbce8
Adding CalculationResultModels - UD
udaydefra Oct 30, 2024
5da5e89
Adding CalcResultParameterCommunicationCost - 463833
udaydefra Oct 30, 2024
22a5b46
Results
udaydefra Oct 30, 2024
451461c
Results structure
udaydefra Oct 30, 2024
4c07728
Registering Builder / Exporter
udaydefra Oct 31, 2024
6857354
Removing DBContext and renaming action method
udaydefra Oct 31, 2024
18db078
Adding CalcResult - 463833
udaydefra Oct 31, 2024
cde4a5a
Calculation Results Model
udaydefra Oct 31, 2024
f4132fb
Merge remote-tracking branch 'origin/main' into feature/459240-part3-…
Mazar-Shaik Nov 4, 2024
48a3bf6
Checkin for Part 3 Write initial results file - Run information - 2
Mazar-Shaik Nov 5, 2024
6e592e4
Merge conflict resolved after merge from main
Mazar-Shaik Nov 5, 2024
2cb28fe
Refactored the code
Mazar-Shaik Nov 5, 2024
ef8dd92
Merge from main
Mazar-Shaik Nov 5, 2024
018d322
Refactored code for more readability
Mazar-Shaik Nov 5, 2024
095d955
Used Async and await.
Mazar-Shaik Nov 5, 2024
dcd884b
Added file extension as csv
Mazar-Shaik Nov 5, 2024
a30d18b
Test cases failure checkin
Mazar-Shaik Nov 5, 2024
e1a994e
Updated Lapcapfile name
Mazar-Shaik Nov 6, 2024
5becc8d
Merge branch 'main' into feature/459240-part3-writing-initial-result-…
udaydefra Nov 7, 2024
4b42432
Setting names changed to BlobStorage
Mazar-Shaik Nov 7, 2024
8dd3aa5
Merge branch 'feature/459240-part3-writing-initial-result-file-run-in…
Mazar-Shaik Nov 7, 2024
b9365de
Removed CsvFileName name setting and get method
Mazar-Shaik Nov 7, 2024
f62277d
Added unit tests as discussed
Mazar-Shaik Nov 7, 2024
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
82 changes: 82 additions & 0 deletions src/EPR.Calculator.API.UnitTests/CalResultsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using EPR.Calculator.API.Builder;
using EPR.Calculator.API.Controllers;
using EPR.Calculator.API.Data;
using EPR.Calculator.API.Dtos;
using EPR.Calculator.API.Exporter;
using EPR.Calculator.API.Models;
using EPR.Calculator.API.Validators;
using EPR.Calculator.API.Wrapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace EPR.Calculator.API.UnitTests
{
[TestClass]
public class CalcResultServiceTests
{
private Mock<ICalcResultBuilder> mockCalcResultBuilder;
private Mock<ICalcResultsExporter<CalcResult>> mockExporter;
private Mock<ICalcResultDetailBuilder> mockDetailBuilder;
private Mock<ICalcResultLapcapDataBuilder> mockLapcapBuilder;

private Mock<ApplicationDBContext> mockContext;
private CalculatorInternalController controller;
private CalcResultBuilder calcResultBuilder;
private CalcResultDetailBuilder detailBuilder;
private CalcResultsExporter exporter;
protected ApplicationDBContext? dbContext;
protected IOrgAndPomWrapper? wrapper;

[TestInitialize]
public void Setup()
{
mockCalcResultBuilder = new Mock<ICalcResultBuilder>();
mockExporter = new Mock<ICalcResultsExporter<CalcResult>>();
wrapper = new Mock<IOrgAndPomWrapper>().Object;
controller = new CalculatorInternalController(
dbContext,
new RpdStatusDataValidator(wrapper),
wrapper,
mockCalcResultBuilder.Object,
mockExporter.Object
);

mockDetailBuilder = new Mock<ICalcResultDetailBuilder>();
mockLapcapBuilder = new Mock<ICalcResultLapcapDataBuilder>();
calcResultBuilder = new CalcResultBuilder(mockDetailBuilder.Object, mockLapcapBuilder.Object);

mockContext = new Mock<ApplicationDBContext>();
detailBuilder = new CalcResultDetailBuilder(mockContext.Object);

}

[TestMethod]
public void PrepareCalcResults_ShouldReturnCreatedStatus()
{
var requestDto = new CalcResultsRequestDto();
var calcResult = new CalcResult();
mockCalcResultBuilder.Setup(b => b.Build(requestDto)).Returns(calcResult);

var result = controller.PrepareCalcResults(requestDto) as ObjectResult;

Assert.IsNotNull(result);
Assert.AreEqual(201, result.StatusCode);
mockExporter.Verify(e => e.Export(calcResult), Times.Once);
}

[TestMethod]
public void Build_ShouldReturnCalcResultWithDetail()
{
var requestDto = new CalcResultsRequestDto();
var detail = new CalcResultDetail();
mockDetailBuilder.Setup(d => d.Construct(requestDto)).Returns(detail);

var result = calcResultBuilder.Build(requestDto);

Assert.IsNotNull(result);
Assert.AreEqual(detail, result.CalcResultDetail);
}
}
}

138 changes: 138 additions & 0 deletions src/EPR.Calculator.API.UnitTests/CalcResultsExporterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using EPR.Calculator.API.Exporter;
using EPR.Calculator.API.Models;
using EPR.Calculator.API.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Text;

namespace EPR.Calculator.API.UnitTests
{
[TestClass]
public class CalcResultsExporterTests
{
private CalcResultsExporter _calcResultsExporter;
private Mock<IBlobStorageService> _blobStorageServiceMock;

[TestInitialize]
public void Setup()
{
_blobStorageServiceMock = new Mock<IBlobStorageService>();
_calcResultsExporter = new CalcResultsExporter(_blobStorageServiceMock.Object);
}

[TestMethod]
public void Export_ShouldUploadCorrectCsvContent()
{
var calcResult = new CalcResult
{
CalcResultDetail = new CalcResultDetail
{
RunName = "Test Run",
RunId = 123,
RunDate = new DateTime(2023, 10, 10, 14, 30, 0),
RunBy = "Tester",
FinancialYear = "2023-24",
LapcapFile = "Lapcap.csv,2023-10-01,John Doe",
ParametersFile = "Params.csv,2023-10-02,Jane Doe"
}
};

var expectedCsvContent = new StringBuilder();
expectedCsvContent.AppendLine("Run Name,Test Run");
expectedCsvContent.AppendLine("Run Id,123");
expectedCsvContent.AppendLine("Run Date,10/10/2023 14:30");
expectedCsvContent.AppendLine("Run by,Tester");
expectedCsvContent.AppendLine("Financial Year,2023-24");
expectedCsvContent.AppendLine("Lapcap File,Lapcap.csv,2023-10-01,John Doe");
expectedCsvContent.AppendLine("Parameters File,Params.csv,2023-10-02,Jane Doe");

_calcResultsExporter.Export(calcResult);

_blobStorageServiceMock.Verify(service => service.UploadResultFileContentAsync(
$"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv",
It.Is<StringBuilder>(content => content.ToString() == expectedCsvContent.ToString())
), Times.Once);
}

[TestMethod]
public void Export_ShouldHandleIOExceptionGracefully()
{
var calcResult = new CalcResult
{
CalcResultDetail = new CalcResultDetail
{
RunName = "Test Run",
RunId = 123,
RunDate = DateTime.Now,
RunBy = "Tester",
FinancialYear = "2023-24",
LapcapFile = "Lapcap.csv,2023-10-01,John Doe",
ParametersFile = "Params.csv,2023-10-02,Jane Doe"
}
};

_blobStorageServiceMock
.Setup(service => service.UploadResultFileContentAsync(It.IsAny<string>(), It.IsAny<StringBuilder>()))
.Throws(new IOException("Simulated IO error"));
var exception = Assert.ThrowsException<IOException>(() => _calcResultsExporter.Export(calcResult));
Assert.AreEqual("File upload failed: Simulated IO error", exception.Message);
}


[TestMethod]
public void AppendFileInfo_ShouldNotAppendIfFilePartsAreInvalid()
{
var calcResult = new CalcResult
{
CalcResultDetail = new CalcResultDetail
{
RunName = "Test Run",
RunId = 123,
RunDate = DateTime.Now,
RunBy = "Tester",
FinancialYear = "2023-24",
LapcapFile = "InvalidFileInfo",
ParametersFile = "InvalidFileInfo"
}
};

var expectedCsvContent = new StringBuilder();
expectedCsvContent.AppendLine("Run Name,Test Run");
expectedCsvContent.AppendLine("Run Id,123");
expectedCsvContent.AppendLine("Run Date," + calcResult.CalcResultDetail.RunDate.ToString("dd/MM/yyyy HH:mm"));
expectedCsvContent.AppendLine("Run by,Tester");
expectedCsvContent.AppendLine("Financial Year,2023-24");

_calcResultsExporter.Export(calcResult);

_blobStorageServiceMock.Verify(service => service.UploadResultFileContentAsync(
$"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv",
It.Is<StringBuilder>(content => content.ToString() == expectedCsvContent.ToString())
), Times.Once);
}

[TestMethod]
public void Export_CreatesCorrectCsvContent()
{
var calcResult = new CalcResult
{
CalcResultDetail = new CalcResultDetail
{
RunName = "Test Run",
RunId = 123,
RunDate = DateTime.Now,
RunBy = "Tester",
FinancialYear = "2024",
LapcapFile = "lapcap.csv,2024-11-01,Tester",
ParametersFile = "params.csv,2024-11-01,Tester"
}
};

_calcResultsExporter.Export(calcResult);

_blobStorageServiceMock.Verify(x => x.UploadResultFileContentAsync(It.IsAny<string>(), It.IsAny<StringBuilder>()), Times.Once);
var expectedFileName = $"{calcResult.CalcResultDetail.RunId}-{DateTime.Now:yyyy-MM-dd-HHmm}.csv";
_blobStorageServiceMock.Verify(x => x.UploadResultFileContentAsync(expectedFileName, It.IsAny<StringBuilder>()), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using EPR.Calculator.API.Builder;
using EPR.Calculator.API.Controllers;
using EPR.Calculator.API.Data.DataModels;
using EPR.Calculator.API.Dtos;
using EPR.Calculator.API.Exporter;
using EPR.Calculator.API.Models;
using EPR.Calculator.API.Services;
using EPR.Calculator.API.Tests.Controllers;
using EPR.Calculator.API.Validators;
using EPR.Calculator.API.Wrapper;
Expand Down Expand Up @@ -195,5 +197,88 @@ public void UpdateRpdStatus_With_RunId_When_Successful()
}

}

[TestMethod]
public void Construct_ShouldReturnCalcResultDetail()
{
var mock = new Mock<IOrgAndPomWrapper>();
dbContext.LapcapDataMaster.RemoveRange(dbContext.LapcapDataMaster);
dbContext.SaveChanges();
dbContext.LapcapDataMaster.AddRange(GetLapcapMasterData().ToList());
dbContext.SaveChanges();

dbContext.DefaultParameterSettings.RemoveRange(dbContext.DefaultParameterSettings);
dbContext.SaveChanges();
dbContext.DefaultParameterSettings.AddRange(GetDefaultParameterSettingsMasterData().ToList());
dbContext.SaveChanges();

var mockBlobStorageService = new Mock<IBlobStorageService>();
var CalcResultsExporter = new CalcResultsExporter(mockBlobStorageService.Object);
var CalcResultDetailBuilder = new CalcResultDetailBuilder(dbContext);
var CalcResultLapcapDataBuilder = new CalcResultLapcapDataBuilder(dbContext);
var calcResultBuilder = new CalcResultBuilder(CalcResultDetailBuilder, CalcResultLapcapDataBuilder);
if (dbContext != null)
{
var controller = new CalculatorInternalController(
dbContext,
new RpdStatusDataValidator(mock.Object),
mock.Object,
calcResultBuilder,
CalcResultsExporter
);
var calResult = controller.PrepareCalcResults(new CalcResultsRequestDto() { RunId = 1 });
var objResult = calResult as ObjectResult;
Assert.IsNotNull(calResult);
Assert.AreEqual(201, objResult?.StatusCode);
}
}

protected static IEnumerable<DefaultParameterSettingMaster> GetDefaultParameterSettingsMasterData()
{
var list = new List<DefaultParameterSettingMaster>
{
new() {
CreatedAt = DateTime.Now,
CreatedBy = "Test User1",
EffectiveFrom = DateTime.Now,
EffectiveTo = DateTime.Now,
Id = 1,
},
new() {
CreatedAt = DateTime.Now,
CreatedBy = "Test User2",
EffectiveFrom = DateTime.Now,
EffectiveTo = DateTime.Now,
Id = 2,
}
};

return list;
}

protected static IEnumerable<LapcapDataMaster> GetLapcapMasterData()
{
var list = new List<LapcapDataMaster>
{
new() {
CreatedAt = DateTime.Now,
CreatedBy = "Test User1",
EffectiveFrom = DateTime.Now,
EffectiveTo = DateTime.Now,
Id = 1,
ProjectionYear = "2024-25",
},
new() {
CreatedAt = DateTime.Now,
CreatedBy = "Test User2",
EffectiveFrom = DateTime.Now,
EffectiveTo = DateTime.Now,
Id = 2,
ProjectionYear = "2024-25",
}
};

return list;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void CanCallPrepareCalcResults()
RunDate = DateTime.UtcNow,
RunBy = "TestValue887677417",
FinancialYear = "TestValue2028236729",
RpdFile = "TestValue1468463827",
RpdFileORG = "TestValue1468463827",
RpdFilePOM = "TestValue1468463837",
LapcapFile = "TestValue1811770456",
ParametersFile = "TestValue1028165412"
},
Expand Down
36 changes: 34 additions & 2 deletions src/EPR.Calculator.API/Builder/CalcResultDetailBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using EPR.Calculator.API.Data;
using EPR.Calculator.API.Constants;
using EPR.Calculator.API.Data;
using EPR.Calculator.API.Dtos;
using EPR.Calculator.API.Models;
using Microsoft.EntityFrameworkCore;

namespace EPR.Calculator.API.Builder
{
Expand All @@ -14,7 +16,37 @@ public CalcResultDetailBuilder(ApplicationDBContext context)

public CalcResultDetail Construct(CalcResultsRequestDto resultsRequestDto)
{
return new CalcResultDetail();
var calcResultDetail = context.CalculatorRuns
.Include(o => o.CalculatorRunOrganisationDataMaster)
.Include(o => o.CalculatorRunPomDataMaster)
.Include(o => o.DefaultParameterSettingMaster)
.Include(x => x.LapcapDataMaster)
.ToListAsync();

var results = new CalcResultDetail();

foreach (var item in calcResultDetail.Result)
{
results.RunId = item.Id;
results.RunName = item.Name;
results.RunBy = item.CreatedBy;
results.RunDate = item.CreatedAt;
results.FinancialYear = item.Financial_Year;
if (item.CalculatorRunOrganisationDataMaster != null)
results.RpdFileORG = item.CalculatorRunOrganisationDataMaster.CreatedAt.ToString(CalculationResults.DateFormat);
if (item.CalculatorRunPomDataMaster != null)
results.RpdFilePOM = item.CalculatorRunPomDataMaster.CreatedAt.ToString(CalculationResults.DateFormat);
if (item.LapcapDataMaster != null)
results.LapcapFile = FormatFileData(item.LapcapDataMaster.LapcapFileName, item.LapcapDataMaster.CreatedAt, item.LapcapDataMaster.CreatedBy);
if (item.DefaultParameterSettingMaster != null)
results.ParametersFile = FormatFileData(item.DefaultParameterSettingMaster.ParameterFileName, item.DefaultParameterSettingMaster.CreatedAt, item.DefaultParameterSettingMaster.CreatedBy);
}
return results;
}

private static string FormatFileData(string fileName, DateTime createdAt, string createdBy)
{
return $"{fileName},{createdAt.ToString(CalculationResults.DateFormat)},{createdBy}";
}
}
}
9 changes: 9 additions & 0 deletions src/EPR.Calculator.API/Constants/BlobStorageSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EPR.Calculator.API.Constants
{
public class BlobStorageSettings
{
public string ConnectionString { get; set; } = string.Empty;
public string ContainerName { get; set; } = string.Empty;
public string CsvFileName { get; set; } = string.Empty;
}
}
Loading