-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Extension Method to Load Requested MAUS or Fallback (#97)
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |