Skip to content

Commit

Permalink
Merge pull request #6 from The-Standard-Organization/users/lboullosa/…
Browse files Browse the repository at this point in the history
…foundations-assembly

FOUNDATIONS: Assembly Service
  • Loading branch information
cjdutoit authored May 30, 2024
2 parents de7541d + 7e2da07 commit 9053f8c
Show file tree
Hide file tree
Showing 19 changed files with 777 additions and 10 deletions.
11 changes: 9 additions & 2 deletions STX.SPAL.Core.Tests.Unit/STX.SPAL.Core.Tests.Unit.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -9,7 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="CleanMoq" Version="1.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.8" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -25,4 +28,8 @@
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STX.SPAL.Core\STX.SPAL.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// ----------------------------------------------------------------------------------
// 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(AssemblyLoadDependencyExceptions))]
private 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<string>(actualAssemblyPath =>
actualAssemblyPath == someAssemblyPath)))
.Throws(externalException);

// when
Func<Assembly> getAssemblyFunction = () =>
this.assemblyService.GetAssembly(someAssemblyPath);

AssemblyDependencyException actualAssemblyDependencyException =
Assert.Throws<AssemblyDependencyException>(
getAssemblyFunction);

//then
actualAssemblyDependencyException.Should().BeEquivalentTo(
expectedAssemblyDependencyException);

this.assemblyBroker
.Verify(broker =>
broker.GetAssembly(It.IsAny<string>()),
Times.Once);

this.assemblyBroker.VerifyNoOtherCalls();
}

[Theory]
[MemberData(nameof(AssemblyLoadValidationDependencyExceptions))]
private void ShouldThrowValidationDependencyExceptionOnLoadAssemblyIfExternalExceptionOccurs(
Exception externalException)
{
// given
string someAssemblyPath = CreateRandomPathAssembly();

var assemblyLoadException =
new AssemblyLoadException(
message: "Assembly load error occurred, contact support.",
innerException: externalException);

var expectedAssemblyValidationDependencyException =
new AssemblyValidationDependencyException(
message: "Assembly validation dependency error occurred, contact support.",
innerException: assemblyLoadException);

this.assemblyBroker
.Setup(broker =>
broker.GetAssembly(
It.Is<string>(actualAssemblyPath =>
actualAssemblyPath == someAssemblyPath)))
.Throws(externalException);

// when
Func<Assembly> getAssemblyFunction = () =>
this.assemblyService.GetAssembly(someAssemblyPath);

AssemblyValidationDependencyException actualAssemblyValidationDependencyException =
Assert.Throws<AssemblyValidationDependencyException>(
getAssemblyFunction);

//then
actualAssemblyValidationDependencyException.Should().BeEquivalentTo(
expectedAssemblyValidationDependencyException);

this.assemblyBroker
.Verify(broker =>
broker.GetAssembly(It.IsAny<string>()),
Times.Once);

this.assemblyBroker.VerifyNoOtherCalls();
}

[Theory]
[MemberData(nameof(AssemblyLoadServiceExceptions))]
private void ShouldThrowServiceExceptionOnLoadAssemblyIfExceptionOccurs(
Exception externalException)
{
// given
string someAssemblyPath = CreateRandomPathAssembly();

var assemblyLoadException =
new FailedAssemblyServiceException(
message: "Failed service error occurred, contact support.",
innerException: externalException);

var expectedAssemblyServiceException =
new AssemblyServiceException(
message: "Assembly service error occurred, contact support.",
innerException: assemblyLoadException);

this.assemblyBroker
.Setup(broker =>
broker.GetAssembly(
It.Is<string>(actualAssemblyPath =>
actualAssemblyPath == someAssemblyPath)))
.Throws(externalException);

// when
Func<Assembly> getAssemblyFunction = () =>
this.assemblyService.GetAssembly(someAssemblyPath);

AssemblyServiceException actualAssemblyServiceException =
Assert.Throws<AssemblyServiceException>(
getAssemblyFunction);

//then
actualAssemblyServiceException.Should().BeEquivalentTo(
expectedAssemblyServiceException);

this.assemblyBroker
.Verify(broker =>
broker.GetAssembly(It.IsAny<string>()),
Times.Once);

this.assemblyBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Threading.Tasks;
using FluentAssertions;
using Moq;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.Assemblies
{
public partial class AssemblyServiceTests
{
[Fact]
private void ShouldGetApplicationPathAssemblies()
{
// given
string[] randomApplicationPathsAssemblies =
CreateRandomPathArray();

string[] expectedApplicationPathsAssemblies =
randomApplicationPathsAssemblies;

string[] returnedApplicationPathsAssemblies =
randomApplicationPathsAssemblies;

this.assemblyBroker
.Setup(broker => broker.GetApplicationPathsAssemblies())
.Returns(returnedApplicationPathsAssemblies);

// when
string[] actualApplicationPathsAssemblies =
this.assemblyService.GetApplicationPathsAssemblies();

//then
actualApplicationPathsAssemblies.Should()
.BeEquivalentTo(expectedApplicationPathsAssemblies);

this.assemblyBroker.Verify(
broker =>
broker.GetApplicationPathsAssemblies(),
Times.Once);

this.assemblyBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Reflection;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.Assemblies
{
public partial class AssemblyServiceTests
{
[Fact]
private void ShouldGetAssembly()
{
// given
Assembly randomAssembly = CreateRandomAssembly();
Assembly expectedAssembly = randomAssembly;
Assembly returnedAssembly = randomAssembly;
string randomPathAssembly = CreateRandomPathAssembly();
string inputPathAssembly = randomPathAssembly;

this.assemblyBroker
.Setup(broker =>
broker.GetAssembly(
It.Is<string>(actualPathAssembly =>
actualPathAssembly == inputPathAssembly)))
.Returns(returnedAssembly);

// when
Assembly actualAssembly =
this.assemblyService.GetAssembly(inputPathAssembly);

//then
actualAssembly.Should().BeSameAs(expectedAssembly);

this.assemblyBroker.Verify(
broker =>
broker.GetAssembly(
It.Is<string>(actualPathAssembly =>
actualPathAssembly == inputPathAssembly)),
Times.Once);

this.assemblyBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ----------------------------------------------------------------------------------
// 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]
[InlineData(null, "Value is required")]
[InlineData("", "Value is required")]
[InlineData(" ", "Value is required")]
[InlineData("file", "Value is not a valid assembly path")]
private void ShouldThrowValidationExceptionIfInvalidAssemblyPath(
string assemblyPath,
string exceptionMessage)
{
// given
Assembly randomAssembly = CreateRandomAssembly();
Assembly expectedAssembly = randomAssembly;
Assembly returnedAssembly = randomAssembly;
string randomPathAssembly = CreateRandomPathAssembly();
string inputAssemblyPath = assemblyPath;

var invalidAssemblyPathException =
new InvalidAssemblyPathException(
message: "Invalid assembly path error occurred, fix errors and try again.");

invalidAssemblyPathException.AddData(
key: nameof(assemblyPath),
values: exceptionMessage
);

var expectedAssemblyValidationException =
new AssemblyValidationException(
message: "Assembly validation error occurred, fix errors and try again.",
innerException: invalidAssemblyPathException);

this.assemblyBroker
.Setup(broker =>
broker.GetAssembly(
It.Is<string>(actualAssemblyPath =>
actualAssemblyPath == inputAssemblyPath)));

// when
Func<Assembly> getAssemblyFunction = () =>
this.assemblyService.GetAssembly(inputAssemblyPath);

AssemblyValidationException actualAssemblyValidationException =
Assert.Throws<AssemblyValidationException>(
getAssemblyFunction);

//then
actualAssemblyValidationException.Should().BeEquivalentTo(
expectedAssemblyValidationException);

this.assemblyBroker
.Verify(broker =>
broker.GetAssembly(It.IsAny<string>()),
Times.Never);

this.assemblyBroker.VerifyNoOtherCalls();
}
}
}
Loading

0 comments on commit 9053f8c

Please sign in to comment.