Skip to content

Commit

Permalink
Merge branch 'feature/459240-part3-writing-initial-result-file-run-in…
Browse files Browse the repository at this point in the history
…formation' of https://github.com/DEFRA/epr-calculator-api into feature/459240-part3-writing-initial-result-file-run-information
  • Loading branch information
Mazar-Shaik committed Nov 7, 2024
2 parents 4b42432 + 5becc8d commit 8dd3aa5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
73 changes: 73 additions & 0 deletions src/EPR.Calculator.API.UnitTests/UtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,78 @@ public void GetParameterValueTest_For_Percent()
Assert.IsNotNull(parameterValue);
Assert.AreEqual(parameterValue, "100");
}

/// <summary>
/// Tests that GetFinancialYearAsYYYY returns the first year from a valid financial year string.
/// </summary>
/// <param name="financialYear">The financial year string to parse.</param>
/// <param name="expectedFinancialYear">The expected first year as a string.</param>
[TestMethod]
[DataRow("2024-25", "2024")]
[DataRow("2023-24", "2023")]
[DataRow("2022-23", "2022")]
public void GetFinancialYearAsYYYY_ValidString_ShouldReturnFirstYear(string financialYear, string expectedFinancialYear)
{
var result = Util.GetFinancialYearAsYYYY(financialYear);
Assert.AreEqual(expectedFinancialYear, result);
}

/// <summary>
/// Tests that GetFinancialYearAsYYYY throws a FormatException for an invalid string.
/// </summary>
/// <param name="financialYear">The invalid financial year string to parse.</param>
[TestMethod]
[DataRow("2024")]
[DataRow("24-25")]
[DataRow("2024-2025")]
[DataRow("abcd-efgh")]
public void GetFinancialYearAsYYYY_InvalidString_ShouldThrowFormatException(string financialYear)
{
var exception = Assert.ThrowsException<FormatException>(() => Util.GetFinancialYearAsYYYY(financialYear));
Assert.AreEqual("Financial year format is invalid. Expected format is 'YYYY-YY'.", exception.Message);
}

/// <summary>
/// Tests that GetFinancialYearAsYYYY throws a ArgumentException for a null or empty string.
/// </summary>
/// <param name="financialYear">The null or empty financial year string to convert.</param>
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void GetFinancialYearAsYYYY_NullOrEmptyString_ShouldThrowArgumentException(string financialYear)
{
var exception = Assert.ThrowsException<ArgumentException>(() => Util.GetFinancialYearAsYYYY(financialYear));
Assert.AreEqual("Financial year cannot be null or empty (Parameter 'value')", exception.Message);
}

/// <summary>
/// Tests that GetCalendarYear returns the previous year as a string for a valid financial year string.
/// </summary>
/// <param name="financialYear">The financial year string to convert.</param>
/// <param name="expectedCalendarYear">The expected previous calendar year as a string.</param>
[TestMethod]
[DataRow("2024-25", "2023")]
[DataRow("2023-24", "2022")]
[DataRow("2022-23", "2021")]
public void GetCalendarYear_ValidString_ShouldReturnPreviousYearAsString(string financialYear, string expectedCalendarYear)
{
var result = Util.GetCalendarYear(financialYear);
Assert.AreEqual(expectedCalendarYear, result);
}

/// <summary>
/// Tests that GetCalendarYear throws an ArgumentException for a null or empty string.
/// </summary>
/// <param name="financialYear">The null or empty financial year string to convert.</param>
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void GetCalendarYear_NullOrEmptyString_ShouldThrowArgumentException(string financialYear)
{
var exception = Assert.ThrowsException<ArgumentException>(() => Util.GetCalendarYear(financialYear));
Assert.AreEqual("Financial year cannot be null or empty (Parameter 'financialYear')", exception.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EPR.Calculator.API.Enums;
using EPR.Calculator.API.Exporter;
using EPR.Calculator.API.Models;
using EPR.Calculator.API.Utils;
using EPR.Calculator.API.Validators;
using EPR.Calculator.API.Wrapper;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -68,7 +69,7 @@ public IActionResult UpdateRpdStatus([FromBody] UpdateRpdStatus request)
var stagingOrganisationData = this.wrapper.GetOrganisationData();
var calcOrganisationMaster = new CalculatorRunOrganisationDataMaster
{
CalendarYear = "2023", //Take the financial year from Calc Run table and Derive the Calendar year
CalendarYear = Util.GetCalendarYear("2024-25"), //Take the financial year from Calc Run table and Derive the Calendar year
CreatedAt = DateTime.Now,
CreatedBy = request.UpdatedBy,
EffectiveFrom = DateTime.Now,
Expand Down
42 changes: 42 additions & 0 deletions src/EPR.Calculator.API/Utils/Util.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EPR.Calculator.API.Data.DataModels;
using EPR.Calculator.API.Dtos;
using System.Text;
using System.Text.RegularExpressions;

namespace EPR.Calculator.API.Utils
{
Expand Down Expand Up @@ -195,5 +196,46 @@ public static CreateLapcapDataErrorDto CreateLapcapDataErrorDto(string country,
UniqueReference = uniqueReference
};
}

/// <summary>
/// Extracts the first year from a financial year string in the format "YYYY-YY".
/// </summary>
/// <param name="value">The financial year string to parse, in the format "YYYY-YY".</param>
/// <returns>The first year as a string.</returns>
/// <exception cref="FormatException">Thrown when the format is invalid.</exception>
public static string GetFinancialYearAsYYYY(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Financial year cannot be null or empty", nameof(value));
}

string pattern = @"^\d{4}-\d{2}$";
TimeSpan regexTimeout = TimeSpan.FromSeconds(1);
if (!Regex.IsMatch(value, pattern, RegexOptions.None, regexTimeout))
{
throw new FormatException("Financial year format is invalid. Expected format is 'YYYY-YY'.");
}

var years = value.Split('-');
return years[0];
}

/// <summary>
/// Converts a financial year string to the previous calendar year as a string.
/// </summary>
/// <param name="financialYear">The financial year string to convert, in the format "YYYY-YY".</param>
/// <returns>The previous calendar year as a string.</returns>
/// <exception cref="ArgumentException">Thrown when the financial year string is null or empty.</exception>
public static string GetCalendarYear(string financialYear)
{
if (string.IsNullOrWhiteSpace(financialYear))
{
throw new ArgumentException("Financial year cannot be null or empty", nameof(financialYear));
}

int year = int.Parse(GetFinancialYearAsYYYY(financialYear));
return (year - 1).ToString();
}
}
}

0 comments on commit 8dd3aa5

Please sign in to comment.