From 6522724606707dcc77ecc155e76173cd01a74e47 Mon Sep 17 00:00:00 2001 From: konstantin Date: Fri, 14 Apr 2023 10:37:18 +0200 Subject: [PATCH] Add Extension Method to Load Requested MAUS or Fallback (#97) --- .../Interfaces/TemplateLoaderExtensions.cs | 49 +++++++++++++ EDILibraryTests/EDILibraryTests.csproj | 1 + .../TemplateLoaderExtensionTests.cs | 73 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 EDILibrary/Interfaces/TemplateLoaderExtensions.cs create mode 100644 EDILibraryTests/TemplateLoaderExtensionTests.cs diff --git a/EDILibrary/Interfaces/TemplateLoaderExtensions.cs b/EDILibrary/Interfaces/TemplateLoaderExtensions.cs new file mode 100644 index 0000000..e10648b --- /dev/null +++ b/EDILibrary/Interfaces/TemplateLoaderExtensions.cs @@ -0,0 +1,49 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using EDILibrary.MAUS; + +namespace EDILibrary.Interfaces +{ + /// + /// Extension methods for any + /// + public static class TemplateLoaderExtensions + { + /// + /// Calls the for all s younger than until a maus is found. + /// + /// + /// + /// + /// + /// + /// The MAUS matching the params or any maus younger than 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. + /// + public static async Task> 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(originalRequestedTemplate, formatVersion); + } + foreach (var olderFormatVersion in Enum.GetValues().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(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(null, null); + } + } +} diff --git a/EDILibraryTests/EDILibraryTests.csproj b/EDILibraryTests/EDILibraryTests.csproj index 7d50ab4..53c4b57 100644 --- a/EDILibraryTests/EDILibraryTests.csproj +++ b/EDILibraryTests/EDILibraryTests.csproj @@ -5,6 +5,7 @@ + diff --git a/EDILibraryTests/TemplateLoaderExtensionTests.cs b/EDILibraryTests/TemplateLoaderExtensionTests.cs new file mode 100644 index 0000000..6562292 --- /dev/null +++ b/EDILibraryTests/TemplateLoaderExtensionTests.cs @@ -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 + { + /// + /// In case the requested template exists, it should be loaded directly; Without using any fallback + /// + [TestMethod] + public async Task TestLoadingRegularTemplateIfExists() + { + var loaderMock = new Mock(); + 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(); + } + + /// + /// In case an older template exists, it should be returned instead of the requested one + /// + [TestMethod] + public async Task TestLoadingFallbackTemplateIfExists() + { + var loaderMock = new Mock(); + 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(); + } + + /// + /// If not even a fallback exists, only null should be returned + /// + [TestMethod] + public async Task TestLoadingNothingIfNotEvenAFallbackExists() + { + var loaderMock = new Mock(); + loaderMock.Setup(l => l.LoadMausTemplate(EdifactFormat.MSCONS, It.IsAny(), "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(); + } + } +}