Skip to content

Commit

Permalink
Add Extension Method to Load Requested MAUS or Fallback (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein authored Apr 14, 2023
1 parent 4617aaf commit 6522724
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
49 changes: 49 additions & 0 deletions EDILibrary/Interfaces/TemplateLoaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using EDILibrary.MAUS;

namespace EDILibrary.Interfaces
{
/// <summary>
/// Extension methods for any <see cref="ITemplateLoader"/>
/// </summary>
public static class TemplateLoaderExtensions
{
/// <summary>
/// Calls the <see cref="ITemplateLoader.LoadMausTemplate"/> for all <see cref="EdifactFormatVersion"/>s younger than <paramref name="formatVersion"/> until a maus is found.
/// </summary>
/// <param name="loader"></param>
/// <param name="format"></param>
/// <param name="formatVersion"></param>
/// <param name="pid"></param>
/// <returns>
/// The MAUS matching the params or any maus younger than <paramref name="formatVersion"/> plus the actual format version of the loaded maus.
/// The latter can be used to e.g. log a warning in the application if the requested maus was not found and a fallback is used.
/// </returns>
public static async Task<Tuple<Anwendungshandbuch, EdifactFormatVersion?>> LoadMausTemplateOrFallback(this ITemplateLoader loader, EdifactFormat format, EdifactFormatVersion formatVersion, string pid)
{
var originalRequestedTemplate = await loader.LoadMausTemplate(format, formatVersion, pid);
if (originalRequestedTemplate is not null)
{
return new Tuple<Anwendungshandbuch, EdifactFormatVersion?>(originalRequestedTemplate, formatVersion);
}
foreach (var olderFormatVersion in Enum.GetValues<EdifactFormatVersion>().Where(fv => fv < formatVersion).OrderByDescending(fv => fv))
{
var olderMaus = await loader.LoadMausTemplate(format, olderFormatVersion, pid);
if (olderMaus != null)
{
// Using the {PID} maus from format version {olderFormatVersion} as fallback for the requested {formatVersion} (DEV-20190), pid, olderFormatVersion, formatVersion);
return new Tuple<Anwendungshandbuch, EdifactFormatVersion?>(olderMaus, olderFormatVersion);
}

if (olderFormatVersion == EdifactFormatVersion.FV1710)
{
// No fallback found. This is not necessarily a problem.
// No maus and no fallback found for {Pid}, {FormatVersion}
}
}
return new Tuple<Anwendungshandbuch, EdifactFormatVersion?>(null, null);
}
}
}
1 change: 1 addition & 0 deletions EDILibraryTests/EDILibraryTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
Expand Down
73 changes: 73 additions & 0 deletions EDILibraryTests/TemplateLoaderExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using EDILibrary;
using EDILibrary.Interfaces;
using EDILibrary.MAUS;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;


namespace EDILibraryTests
{
[TestClass]
public class TemplateLoaderExtensionTests
{
/// <summary>
/// In case the requested template exists, it should be loaded directly; Without using any fallback
/// </summary>
[TestMethod]
public async Task TestLoadingRegularTemplateIfExists()
{
var loaderMock = new Mock<ITemplateLoader>();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002")).ReturnsAsync(new Anwendungshandbuch()).Verifiable();

var loaderUnderTest = loaderMock.Object;
var (actualMaus, actualFormatVersion) = await loaderUnderTest.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
actualMaus.Should().NotBeNull();
actualFormatVersion.Should().Be(EdifactFormatVersion.FV2304);

loaderMock.VerifyAll();
loaderMock.VerifyNoOtherCalls();
}

/// <summary>
/// In case an older template exists, it should be returned instead of the requested one
/// </summary>
[TestMethod]
public async Task TestLoadingFallbackTemplateIfExists()
{
var loaderMock = new Mock<ITemplateLoader>();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002")).ReturnsAsync((Anwendungshandbuch)null).Verifiable();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2210, "13002")).ReturnsAsync((Anwendungshandbuch)null).Verifiable();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, EdifactFormatVersion.FV2110, "13002")).ReturnsAsync(new Anwendungshandbuch()).Verifiable();

var loaderUnderTest = loaderMock.Object;
var (actualMaus, actualFormatVersion) = await loaderUnderTest.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
actualMaus.Should().NotBeNull();
actualFormatVersion.Should().Be(EdifactFormatVersion.FV2110);

loaderMock.VerifyAll();
loaderMock.VerifyNoOtherCalls();
}

/// <summary>
/// If not even a fallback exists, only null should be returned
/// </summary>
[TestMethod]
public async Task TestLoadingNothingIfNotEvenAFallbackExists()
{
var loaderMock = new Mock<ITemplateLoader>();
loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, It.IsAny<EdifactFormatVersion>(), "13002")).ReturnsAsync((Anwendungshandbuch)null).Verifiable();


var loaderUnderTest = loaderMock.Object;
var (actualMaus, actualFormatVersion) = await loaderUnderTest.LoadMausTemplateOrFallback(EdifactFormat.MSCONS, EdifactFormatVersion.FV2304, "13002");
actualMaus.Should().BeNull();
actualFormatVersion.Should().BeNull();

loaderMock.VerifyAll();
}
}
}

0 comments on commit 6522724

Please sign in to comment.