diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Exceptions.GetAssembly.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Exceptions.GetAssembly.cs new file mode 100644 index 0000000..80f2c4c --- /dev/null +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Exceptions.GetAssembly.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Reflection; +using FluentAssertions; +using Moq; +using STX.SPAL.Core.Models.Services.Foundations.Assemblies.Exceptions; + +namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.Assemblies +{ + public partial class AssemblyServiceTests + { + [Theory] + [MemberData(nameof(AssemblyLoadExceptions))] + public void ShouldThrowDependencyExceptionOnLoadAssemblyIfExternalExceptionOccurs( + Exception externalException) + { + // given + string someAssemblyPath = CreateRandomPathAssembly(); + + var assemblyLoadException = + new AssemblyLoadException( + message: "Assembly load error occurred, contact support.", + innerException: externalException); + + var expectedAssemblyDependencyException = + new AssemblyDependencyException( + message: "Assembly dependency error occurred, contact support.", + innerException: assemblyLoadException); + + this.assemblyBroker + .Setup(broker => + broker.GetAssembly( + It.Is(actualAssemblyPath => + actualAssemblyPath == someAssemblyPath))) + .Throws(externalException); + + // when + Func getAssemblyFunction = () => + this.assemblyService.GetAssembly(someAssemblyPath); + + AssemblyDependencyException actualAssemblyDependencyException = + Assert.Throws( + getAssemblyFunction); + + //then + actualAssemblyDependencyException.Should().BeEquivalentTo( + expectedAssemblyDependencyException); + + this.assemblyBroker + .Verify(broker => + broker.GetAssembly(It.IsAny()), + Times.Once); + + this.assemblyBroker.VerifyNoOtherCalls(); + } + } +} diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Logic.GetAssembly.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Logic.GetAssembly.cs index 0abdea0..9e631fa 100644 --- a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Logic.GetAssembly.cs +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Logic.GetAssembly.cs @@ -18,7 +18,7 @@ private async Task ShouldGetAssembly() Assembly randomAssembly = CreateRandomAssembly(); Assembly expectedAssembly = randomAssembly; Assembly returnedAssembly = randomAssembly; - string randomPathAssembly = GetRandomPathAssembly(); + string randomPathAssembly = CreateRandomPathAssembly(); string inputPathAssembly = randomPathAssembly; this.assemblyBroker diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Validations.GetAssembly.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Validations.GetAssembly.cs index 885e35c..2179478 100644 --- a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Validations.GetAssembly.cs +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.Validations.GetAssembly.cs @@ -25,7 +25,7 @@ public void ShouldThrowValidationExceptionIfInvalidAssemblyPath( Assembly randomAssembly = CreateRandomAssembly(); Assembly expectedAssembly = randomAssembly; Assembly returnedAssembly = randomAssembly; - string randomPathAssembly = GetRandomPathAssembly(); + string randomPathAssembly = CreateRandomPathAssembly(); string inputAssemblyPath = assemblyPath; var invalidAssemblyPathException = diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs index 5db4a5a..b850b1f 100644 --- a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs @@ -2,10 +2,12 @@ // Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers // ---------------------------------------------------------------------------------- +using System; using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Emit; +using System.Security; using Moq; using STX.SPAL.Core.Brokers.Assemblies; using STX.SPAL.Core.Services.Foundations.Assemblies; @@ -32,7 +34,7 @@ private static string GetRandomString() => private static int GetRandomNumber() => new IntRange(min: 2, max: 10).GetValue(); - private static string GetRandomPathAssembly() + private static string CreateRandomPathAssembly() { string randomPathName = GetRandomString(); string randomFileName = GetRandomString(); @@ -43,7 +45,7 @@ private static string GetRandomPathAssembly() private static string[] CreateRandomPathArray() { return Enumerable.Range(0, GetRandomNumber()) - .Select(i => GetRandomPathAssembly()) + .Select(i => CreateRandomPathAssembly()) .ToArray(); } @@ -60,5 +62,20 @@ private static Assembly CreateRandomAssembly() return assemblyBuilder; } + + public static TheoryData AssemblyLoadExceptions() + { + return new TheoryData + { + new SecurityException(), + new FileLoadException(), + new FileNotFoundException(), + new BadImageFormatException(), + new InvalidOperationException(), + new NotSupportedException(), + new IOException(), + new UnauthorizedAccessException() + }; + } } } diff --git a/STX.SPAL.Core/Models/Services/Foundations/Assemblies/Exceptions/AssemblyDependencyException.cs b/STX.SPAL.Core/Models/Services/Foundations/Assemblies/Exceptions/AssemblyDependencyException.cs new file mode 100644 index 0000000..c142ad6 --- /dev/null +++ b/STX.SPAL.Core/Models/Services/Foundations/Assemblies/Exceptions/AssemblyDependencyException.cs @@ -0,0 +1,15 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using Xeptions; + +namespace STX.SPAL.Core.Models.Services.Foundations.Assemblies.Exceptions +{ + internal class AssemblyDependencyException : Xeption + { + public AssemblyDependencyException(string message, Xeption innerException) + : base(message, innerException) + { } + } +} diff --git a/STX.SPAL.Core/Models/Services/Foundations/Assemblies/Exceptions/AssemblyLoadException.cs b/STX.SPAL.Core/Models/Services/Foundations/Assemblies/Exceptions/AssemblyLoadException.cs new file mode 100644 index 0000000..29800ad --- /dev/null +++ b/STX.SPAL.Core/Models/Services/Foundations/Assemblies/Exceptions/AssemblyLoadException.cs @@ -0,0 +1,16 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using Xeptions; + +namespace STX.SPAL.Core.Models.Services.Foundations.Assemblies.Exceptions +{ + internal class AssemblyLoadException : Xeption + { + public AssemblyLoadException(string message, Exception innerException) + : base(message: message, innerException) + { } + } +}