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
Binary file modified advanced_template.zip
Binary file not shown.
Binary file removed basic_template.zip
Binary file not shown.
Binary file removed soap_template.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using IbanNet;
using IbanNet.Registry;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using OutSystems.ExternalLibraries.SDK;
using Iban = OutSystems.IbanChecker.Structures.Iban;
Expand Down Expand Up @@ -78,7 +79,8 @@ public void IbanCheckerCorrectlyParsesValidIban() {
// valid IBAN string using the IbanParser.
var parser = new IbanParser(IbanRegistry.Default);
var iban = parser.Parse("NL91 ABNA 0417 1643 00");
var checker = new IbanChecker();
var logger = new LoggerFactory().CreateLogger<OutSystems.IbanChecker.IbanChecker>();
var checker = new IbanChecker(logger);

// Act: Parse a sample valid IBAN string using the IbanChecker.
var ibanStruct = checker.Parse("NL91 ABNA 0417 1643 00");
Expand All @@ -99,7 +101,8 @@ public void IbanCheckerCorrectlyTryParsesValidIban() {
// valid IBAN string using the IbanParser.
var parser = new IbanParser(IbanRegistry.Default);
var iban = parser.Parse("NL91 ABNA 0417 1643 00");
var checker = new IbanChecker();
var logger = new LoggerFactory().CreateLogger<OutSystems.IbanChecker.IbanChecker>();
var checker = new IbanChecker(logger);

// Act: Try to parse a sample valid IBAN string using the IbanChecker and
// check for success.
Expand All @@ -122,8 +125,9 @@ public void IbanCheckerCorrectlyTryParsesValidIban() {
// </summary>
[Test]
public void IbanCheckerCorrectlyTryParsesInvalidIban() {
var logger = new LoggerFactory().CreateLogger<OutSystems.IbanChecker.IbanChecker>();
// Setup: Instantiate a new IbanChecker.
var checker = new IbanChecker();
var checker = new IbanChecker(logger);

// Act: Try to parse a sample invalid IBAN string using the IbanChecker and
// check for failure.
Expand All @@ -145,8 +149,9 @@ public void IbanCheckerCorrectlyTryParsesInvalidIban() {
/// </summary>
[Test]
public void IbanCheckerCorrectlyValidatesIbanFromRejectedCountry() {
var logger = new LoggerFactory().CreateLogger<OutSystems.IbanChecker.IbanChecker>();
// Setup: Instantiate a new IbanChecker.
var checker = new IbanChecker();
var checker = new IbanChecker(logger);

// Act: Validate an IBAN using the IbanChecker, providing a list of
// rejected countries.
Expand All @@ -169,8 +174,9 @@ public void IbanCheckerCorrectlyValidatesIbanFromRejectedCountry() {
/// </summary>
[Test]
public void IbanCheckerCorrectlyValidatesIbanFromAcceptedCountry() {
var logger = new LoggerFactory().CreateLogger<OutSystems.IbanChecker.IbanChecker>();
// Setup: Instantiate a new IbanChecker.
var checker = new IbanChecker();
var checker = new IbanChecker(logger);

// Act: Validate an IBAN using the IbanChecker, providing a list of
// rejected countries that does not include the IBAN's country code.
Expand Down
26 changes: 22 additions & 4 deletions templates/advanced/OutSystems.IbanChecker/IbanChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using IbanNet;
using IbanNet.Registry;
using Microsoft.Extensions.Logging;

namespace OutSystems.IbanChecker {
/// <summary>
Expand All @@ -23,12 +24,20 @@ public class IbanChecker : IIbanChecker {
private IIbanValidator _validator;

/// <summary>
/// The constructor initializes the IbanChecker class with new instances
/// An instance of ILogger from the Microsoft.Extensions.Logging library, used for
/// logging.
/// </summary>
private readonly ILogger _logger;

/// <summary>
/// The constructor initializes the IbanChecker class with the specified logger and creates new instances
/// of the IbanParser and IbanValidator classes from the IbanNet library.
/// </summary>
public IbanChecker() {
/// <param name="logger">The logger instance used for logging operations within the IbanChecker.</param>
public IbanChecker(ILogger logger) {
_validator = new IbanValidator();
_parser = new IbanParser(_validator);
_logger = logger;
}

/// <summary>
Expand All @@ -40,6 +49,7 @@ public IbanChecker() {
/// <returns>An Iban struct representing the parsed IBAN.</returns>
/// <exception cref="System.Exception">Thrown if the parsing fails.</exception>
public Structures.Iban Parse(string value) {
_logger.LogInformation("Parsing IBAN: {IbanValue}", value);
return new Structures.Iban(_parser.Parse(value));
}

Expand All @@ -56,8 +66,10 @@ public bool TryParse(string value, out Structures.Iban? iban) {
IbanNet.Iban? internalIban;
if (_parser.TryParse(value, out internalIban)) {
iban = new Structures.Iban(internalIban);
_logger.LogInformation("Successfully parsed IBAN: {IbanValue}", value);
return true;
}
_logger.LogWarning("Failed to parse IBAN: {IbanValue}", value);
return false;
}

Expand All @@ -75,8 +87,10 @@ public Structures.ValidationResult Validate(string iban, IEnumerable<string>? re
var validatorWithRejectedCountries = new IbanValidator(new IbanValidatorOptions {
Rules = { new CustomRules.RejectCountryRule(rejectedCountries) }
});
_logger.LogInformation("Validating IBAN: {IbanValue} with rejected countries: {RejectedCountries}", iban, rejectedCountries);
return new Structures.ValidationResult(validatorWithRejectedCountries.Validate(iban));
}
_logger.LogInformation("Validating IBAN: {IbanValue} with default rules", iban);
return new Structures.ValidationResult(_validator.Validate(iban));
}

Expand All @@ -94,12 +108,16 @@ public string Format(Structures.Iban iban, string? format = null) {
var ibanBuilder = new IbanNet.Builders.IbanBuilder();
IbanRegistry.Default.TryGetValue(iban.Country.TwoLetterISORegionName, out IbanCountry? country);
if (country == null) {
throw new System.Exception("Invalid country: " + iban.Country.TwoLetterISORegionName);
}
var errorMessage = "Invalid country: " + iban.Country.TwoLetterISORegionName;
_logger.LogError("Failed to format IBAN. {ErrorMessage}.", errorMessage);
throw new System.Exception(errorMessage);
}
var ib = _parser.Parse(ibanBuilder
.WithCountry(country)
.WithBankAccountNumber(iban.Bban)
.Build());
_logger.LogInformation("Formatting IBAN for country: {CountryCode} with format: {Format}.",
iban.Country.TwoLetterISORegionName, format ?? "<default>");
return ib.ToString(format);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="IbanNet" Version="5.8.1" />
<PackageReference Include="OutSystems.ExternalLibraries.SDK" Version="1.5.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down