Skip to content

Commit

Permalink
Merge pull request #9 from The-Standard-Organization/users/cjdutoit/e…
Browse files Browse the repository at this point in the history
…xposers-serializationabstractionprovider-serialize-withinterfaces

EXPOSERS: Serialization Abstraction Provider - Serialize
  • Loading branch information
cjdutoit authored Feb 4, 2024
2 parents 4c46007 + 0406402 commit 26c2070
Show file tree
Hide file tree
Showing 22 changed files with 678 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using STX.Serialization.Providers.Abstractions.Models.Exceptions;
using Xeptions;

namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions
{
internal class TestDependencyException : Xeption, ISerializationDependencyException
{
public TestDependencyException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using STX.Serialization.Providers.Abstractions.Models.Exceptions;
using Xeptions;

namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions
{
internal class TestDependencyValidationException : Xeption, ISerializationDependencyValidationException
{
public TestDependencyValidationException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using STX.Serialization.Providers.Abstractions.Models.Exceptions;
using Xeptions;

namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions
{
internal class TestServiceException : Xeption, ISerializationServiceException
{
public TestServiceException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using STX.Serialization.Providers.Abstractions.Models.Exceptions;
using Xeptions;

namespace STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions
{
internal class TestValidationException : Xeption, ISerializationValidationException
{
public TestValidationException(string message, Xeption innerException)
: base(message, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CleanMoq" Version="42.42.42" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.8" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STX.Serialization.Providers.Abstractions\STX.Serialization.Providers.Abstractions.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using STX.Serialization.Providers.Abstractions.Models.Exceptions;
using STX.Serialization.Providers.Abstractions.Tests.Unit.Models.Exceptions;
using Xeptions;
using Xunit;

namespace STX.Serialization.Providers.Abstractions.Tests.Unit
{
public partial class SerializationAbstractionProviderTests
{
[Fact]
public async Task ShouldCaptureAndLocaliseValidationExceptionsAsync()
{
// given
dynamic dynamicPerson = new
{
Name = GetRandomString(),
Surname = GetRandomString(),
Age = GetRandomNumber()
};

dynamic inputPerson = dynamicPerson;
Xeption someException = new Xeption(message: "Some exception occurred.");

TestValidationException testValidationException =
new TestValidationException(
message: "Serialization validation errors occurred, please try again.",
innerException: someException);

SerializationValidationProviderException expectedSerializationValidationProviderException =
new SerializationValidationProviderException(
message: "Serialization validation errors occurred, please try again.",
innerException: testValidationException,
data: testValidationException.Data);

this.serializationProviderMock.Setup(provider =>
provider.Serialize(It.IsAny<object>()))
.ThrowsAsync(testValidationException);

// when
ValueTask<string> serializationTask =
this.serializationAbstractionProvider.Serialize(inputPerson);

SerializationValidationProviderException actualSerializationValidationProviderException =
await Assert.ThrowsAsync<SerializationValidationProviderException>(
serializationTask.AsTask);

// then
actualSerializationValidationProviderException.Should()
.BeEquivalentTo(expectedSerializationValidationProviderException);
}

[Fact]
public async Task ShouldCaptureAndLocaliseDependencyValidationExceptionsAsync()
{
// given
dynamic dynamicPerson = new
{
Name = GetRandomString(),
Surname = GetRandomString(),
Age = GetRandomNumber()
};

dynamic inputPerson = dynamicPerson;
Xeption someException = new Xeption(message: "Some exception occurred.");

TestDependencyValidationException testDependencyValidationException =
new TestDependencyValidationException(
message: "Serialization dependency validation errors occurred, please try again.",
innerException: someException);

SerializationValidationProviderException expectedSerializationValidationProviderException =
new SerializationValidationProviderException(
message: "Serialization validation errors occurred, please try again.",
innerException: testDependencyValidationException,
data: testDependencyValidationException.Data);

this.serializationProviderMock.Setup(provider =>
provider.Serialize(It.IsAny<object>()))
.ThrowsAsync(testDependencyValidationException);

// when
ValueTask<string> serializationTask =
this.serializationAbstractionProvider.Serialize(inputPerson);

SerializationValidationProviderException actualSerializationValidationProviderException =
await Assert.ThrowsAsync<SerializationValidationProviderException>(
serializationTask.AsTask);

// then
actualSerializationValidationProviderException.Should()
.BeEquivalentTo(expectedSerializationValidationProviderException);
}

[Fact]
public async Task ShouldCaptureAndLocaliseDependencyExceptionsAsync()
{
// given
dynamic dynamicPerson = new
{
Name = GetRandomString(),
Surname = GetRandomString(),
Age = GetRandomNumber()
};

dynamic inputPerson = dynamicPerson;
Xeption someException = new Xeption(message: "Some exception occurred.");

TestDependencyException testDependencyException =
new TestDependencyException(
message: "Serialization dependency error occurred, contact support.",
innerException: someException);

SerializationDependencyProviderException expectedSerializationDependencyProviderException =
new SerializationDependencyProviderException(
message: "Serialization dependency error occurred, contact support.",
innerException: testDependencyException,
data: testDependencyException.Data);

this.serializationProviderMock.Setup(provider =>
provider.Serialize(It.IsAny<object>()))
.ThrowsAsync(testDependencyException);

// when
ValueTask<string> serializationTask =
this.serializationAbstractionProvider.Serialize(inputPerson);

SerializationDependencyProviderException actualSerializationDependencyProviderException =
await Assert.ThrowsAsync<SerializationDependencyProviderException>(
serializationTask.AsTask);

// then
actualSerializationDependencyProviderException.Should()
.BeEquivalentTo(expectedSerializationDependencyProviderException);
}

[Fact]
public async Task ShouldCaptureAndLocaliseServiceExceptionsAsync()
{
// given
dynamic dynamicPerson = new
{
Name = GetRandomString(),
Surname = GetRandomString(),
Age = GetRandomNumber()
};

dynamic inputPerson = dynamicPerson;
Xeption someException = new Xeption(message: "Some exception occurred.");

TestServiceException testServiceException =
new TestServiceException(
message: "Serialization service error occurred, contact support.",
innerException: someException);

SerializationServiceProviderException expectedSerializationServiceProviderException =
new SerializationServiceProviderException(
message: "Serialization service error occurred, contact support.",
innerException: testServiceException,
data: testServiceException.Data);

this.serializationProviderMock.Setup(provider =>
provider.Serialize(It.IsAny<object>()))
.ThrowsAsync(testServiceException);

// when
ValueTask<string> serializationTask =
this.serializationAbstractionProvider.Serialize(inputPerson);

SerializationServiceProviderException actualSerializationServiceProviderException =
await Assert.ThrowsAsync<SerializationServiceProviderException>(
serializationTask.AsTask);

// then
actualSerializationServiceProviderException.Should()
.BeEquivalentTo(expectedSerializationServiceProviderException);
}

[Fact]
public async Task ShouldCaptureAndLocaliseAnyNonServiceExceptionsAsync()
{
// given
dynamic dynamicPerson = new
{
Name = GetRandomString(),
Surname = GetRandomString(),
Age = GetRandomNumber()
};

dynamic inputPerson = dynamicPerson;
Exception someException = new Exception(message: "Some exception occurred.");

UncatagorizedSerializationProviderException notImplementedSerializationProviderException =
new UncatagorizedSerializationProviderException(
message: "Serialization provider not properly implemented. Uncatagorized errors found, " +
"contact the serialization provider owner for support.",

innerException: someException,
data: someException.Data);

SerializationServiceProviderException expectedSerializationServiceProviderException =
new SerializationServiceProviderException(
message: "Uncatagorized serialization service error occurred, contact support.",
innerException: notImplementedSerializationProviderException,
data: notImplementedSerializationProviderException.Data);

this.serializationProviderMock.Setup(provider =>
provider.Serialize(It.IsAny<object>()))
.ThrowsAsync(someException);

// when
ValueTask<string> serializationTask =
this.serializationAbstractionProvider.Serialize(inputPerson);

SerializationServiceProviderException actualSerializationServiceProviderException =
await Assert.ThrowsAsync<SerializationServiceProviderException>(
serializationTask.AsTask);

// then
actualSerializationServiceProviderException.Should()
.BeEquivalentTo(expectedSerializationServiceProviderException);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

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

namespace STX.Serialization.Providers.Abstractions.Tests.Unit
{
public partial class SerializationAbstractionProviderTests
{
[Fact]
public async Task ShouldSerializeAsync()
{
// given
dynamic somePerson = new
{
Name = GetRandomString(),
Surname = GetRandomString(),
Age = GetRandomNumber()
};

dynamic inputPerson = somePerson;
string randomString = GetRandomString();
string outputString = GetRandomString();
string expectedString = outputString;

this.serializationProviderMock.Setup(provider =>
provider.Serialize(It.IsAny<object>()))
.ReturnsAsync(outputString);

// when
string actualString = await this.serializationAbstractionProvider.Serialize(inputPerson);

// then
actualString.Should().BeEquivalentTo(expectedString);
}
}
}
Loading

0 comments on commit 26c2070

Please sign in to comment.