Added a-frame-architecture usiung wolverine sample.
This commit is contained in:
+129
@@ -0,0 +1,129 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Handlers;
|
||||
using FinancialApi.Application.Models;
|
||||
using FinancialApi.Domain.Entities;
|
||||
using FinancialApi.Domain.Exceptions;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
|
||||
namespace FinancialApi.Application.UnitTests;
|
||||
|
||||
public class ApplyFeeHandlerTests
|
||||
{
|
||||
public class SpendAccount
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(100.0, 20.0, 80.0)]
|
||||
[InlineData(199.78, 10.0, 189.78)]
|
||||
[InlineData(200000, 0.09, 199999.91)]
|
||||
[InlineData(1000, 1000, 0)]
|
||||
public void GivenStartingBalanceGreaterThanFee_ApplyFee_ShouldBeExpectedBalance(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new ApplyFeeCommand(1, feeAmount);
|
||||
var account = new Account(1, startingBalance, AccountType.Spend);
|
||||
var revenueAccount = new Account(99999, 0, AccountType.Revenue);
|
||||
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var (customerWrite, revenueWrite, journalWrite, @event) = ApplyFeeHandler.Handle(command, feeContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
customerWrite.Entity.Balance.Should()
|
||||
.Be(expectedBalance);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(100.0, 110.0)]
|
||||
[InlineData(199.78, 200.0)]
|
||||
[InlineData(200000, 200000.01)]
|
||||
[InlineData(1000, 1999999)]
|
||||
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldFail(decimal startingBalance, decimal feeAmount)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new ApplyFeeCommand(1, feeAmount);
|
||||
var account = new Account(1, startingBalance, AccountType.Spend);
|
||||
var revenueAccount = new Account(99999, 0, AccountType.Revenue);
|
||||
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var act = () => ApplyFeeHandler.Handle(command, feeContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
act.Should()
|
||||
.Throw<InsufficientFundsException>();
|
||||
}
|
||||
}
|
||||
|
||||
public class BorrowAccount
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(100.0, 20.0, 80.0)]
|
||||
[InlineData(199.78, 10.0, 189.78)]
|
||||
[InlineData(200000, 0.09, 199999.91)]
|
||||
[InlineData(1000, 1000, 0)]
|
||||
public void GivenStartingBalanceGreaterThanFee_ApplyFee_ShouldBeExpectedBalance(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new ApplyFeeCommand(1, feeAmount);
|
||||
var account = new Account(1, startingBalance, AccountType.Borrow);
|
||||
var revenueAccount = new Account(99999, 0, AccountType.Revenue);
|
||||
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var (customerWrite, revenueWrite, journalWrite, @event) = ApplyFeeHandler.Handle(command, feeContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
customerWrite.Entity.Balance.Should()
|
||||
.Be(expectedBalance);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(100.0, 110.0, -10.0)]
|
||||
[InlineData(199.78, 200.0, -0.22)]
|
||||
[InlineData(200000, 200000.01, -0.01)]
|
||||
[InlineData(1000, 1999999, -1998999)]
|
||||
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldBeExpectedBalance(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new ApplyFeeCommand(1, feeAmount);
|
||||
var account = new Account(1, startingBalance, AccountType.Borrow);
|
||||
var revenueAccount = new Account(99999, 0, AccountType.Revenue);
|
||||
var feeContext = new FeeContext(account, revenueAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var (customerWrite, revenueWrite, journalWrite, @event) = ApplyFeeHandler.Handle(command, feeContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
customerWrite.Entity.Balance.Should()
|
||||
.Be(expectedBalance);
|
||||
revenueWrite.Entity.Balance.Should()
|
||||
.Be(feeAmount);
|
||||
journalWrite.Should()
|
||||
.NotBeNull();
|
||||
@event.Should()
|
||||
.NotBeNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4"/>
|
||||
<PackageReference Include="FluentAssertions" Version="8.10.0"/>
|
||||
<PackageReference Include="JasperFx" Version="2.27.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.6.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1"/>
|
||||
<PackageReference Include="xunit" Version="2.9.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FinancialApi.Application\FinancialApi.Application.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Wolverine">
|
||||
<HintPath>..\..\..\..\..\..\.nuget\packages\wolverinefx\6.18.0\lib\net10.0\Wolverine.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Handlers;
|
||||
using FinancialApi.Application.Models;
|
||||
using FinancialApi.Domain.Entities;
|
||||
using FinancialApi.Domain.Events;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.UnitTests;
|
||||
|
||||
public class ReverseJournalHandlerTests
|
||||
{
|
||||
[Fact]
|
||||
public void GivenValidJournalEntry_Reversal_ShouldResetAccountBalances()
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var expectedSourceAccountBalance = 100.0m;
|
||||
var expectedDestAccountBalance = 100.0m;
|
||||
var (initialSourceAccountWrite, initialDestAccountWrite, initialJournalWrite, initialEvent) =
|
||||
CreateInitialTransferTransaction(
|
||||
expectedSourceAccountBalance,
|
||||
expectedDestAccountBalance,
|
||||
10,
|
||||
timeProvider
|
||||
);
|
||||
var command = new ReverseJournalCommand(initialJournalWrite.Entity.Id, "Invalid Transaction");
|
||||
var context = new JournalReversalContext(
|
||||
initialJournalWrite.Entity,
|
||||
[initialSourceAccountWrite.Entity, initialDestAccountWrite.Entity],
|
||||
timeProvider.GetUtcNow()
|
||||
);
|
||||
|
||||
// Act
|
||||
var (journalWrite, accountWrites, @event) = ReverseJournalHandler.Handle(command, context);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
accountWrites[0]
|
||||
.Entity.Balance.Should()
|
||||
.Be(expectedSourceAccountBalance);
|
||||
accountWrites[1]
|
||||
.Entity.Balance.Should()
|
||||
.Be(expectedDestAccountBalance);
|
||||
journalWrite.Entity.Lines.Should()
|
||||
.HaveCount(initialJournalWrite.Entity.Lines.Count);
|
||||
}
|
||||
|
||||
private (IStorageAction<Account> SourceWrite, IStorageAction<Account> DestWrite, IStorageAction<BalancedJournalEntry>
|
||||
JournalWrite, FundsTransferredEvent Event) CreateInitialTransferTransaction(
|
||||
decimal sourceStartingBalance,
|
||||
decimal destStartingBalance,
|
||||
decimal transferAmount,
|
||||
TimeProvider timeProvider
|
||||
)
|
||||
{
|
||||
var command = new TransferFundsCommand(1, 2, transferAmount);
|
||||
var sourceAccount = new Account(1, sourceStartingBalance, AccountType.Spend);
|
||||
var destAccount = new Account(2, destStartingBalance, AccountType.Spend);
|
||||
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
return TransferFundsHandler.Handle(command, transferContext);
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Handlers;
|
||||
using FinancialApi.Application.Models;
|
||||
using FinancialApi.Domain.Entities;
|
||||
using FinancialApi.Domain.Exceptions;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
|
||||
namespace FinancialApi.Application.UnitTests;
|
||||
|
||||
public class TransferFundsHandlerTests
|
||||
{
|
||||
public class LiabilityAccount
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(100.0, 20.0, 80.0)]
|
||||
[InlineData(199.78, 10.0, 189.78)]
|
||||
[InlineData(200000, 0.09, 199999.91)]
|
||||
[InlineData(1000, 1000, 0)]
|
||||
public void GivenStartingBalanceGreaterThanTransfer_ApplyFee_ShouldBeExpectedBalance(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new TransferFundsCommand(1, 2, feeAmount);
|
||||
var sourceAccount = new Account(1, startingBalance, AccountType.Spend);
|
||||
var destAccount = new Account(2, 0, AccountType.Spend);
|
||||
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var (customerWrite, revenueWrite, journalWrite, @event) =
|
||||
TransferFundsHandler.Handle(command, transferContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
customerWrite.Entity.Balance.Should()
|
||||
.Be(expectedBalance);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(100.0, 110.0)]
|
||||
[InlineData(199.78, 200.0)]
|
||||
[InlineData(200000, 200000.01)]
|
||||
[InlineData(1000, 1999999)]
|
||||
public void GivenStartingBalanceLessThanTransfer_ApplyFee_ShouldFail(decimal startingBalance, decimal feeAmount)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new TransferFundsCommand(1, 2, feeAmount);
|
||||
var sourceAccount = new Account(1, startingBalance, AccountType.Spend);
|
||||
var destAccount = new Account(2, 0, AccountType.Spend);
|
||||
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var act = () => TransferFundsHandler.Handle(command, transferContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
act.Should()
|
||||
.Throw<InsufficientFundsException>();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreditAccount
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(100.0, 20.0, 80.0)]
|
||||
[InlineData(199.78, 10.0, 189.78)]
|
||||
[InlineData(200000, 0.09, 199999.91)]
|
||||
[InlineData(1000, 1000, 0)]
|
||||
public void GivenStartingBalanceGreaterThanFee_ApplyFee_ShouldBeExpectedBalance(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new TransferFundsCommand(1, 2, feeAmount);
|
||||
var sourceAccount = new Account(1, startingBalance, AccountType.Borrow);
|
||||
var destAccount = new Account(2, 0, AccountType.Borrow);
|
||||
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var (customerWrite, revenueWrite, journalWrite, @event) =
|
||||
TransferFundsHandler.Handle(command, transferContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
customerWrite.Entity.Balance.Should()
|
||||
.Be(expectedBalance);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(100.0, 110.0, -10.0)]
|
||||
[InlineData(199.78, 200.0, -0.22)]
|
||||
[InlineData(200000, 200000.01, -0.01)]
|
||||
[InlineData(1000, 1999999, -1998999)]
|
||||
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldSucceed(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new TransferFundsCommand(1, 2, feeAmount);
|
||||
var sourceAccount = new Account(1, startingBalance, AccountType.Borrow);
|
||||
var destAccount = new Account(2, 0, AccountType.Borrow);
|
||||
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var act = () => TransferFundsHandler.Handle(command, transferContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
act.Should()
|
||||
.NotThrow();
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(100.0, 110.0, -10.0)]
|
||||
[InlineData(199.78, 200.0, -0.22)]
|
||||
[InlineData(200000, 200000.01, -0.01)]
|
||||
[InlineData(1000, 1999999, -1998999)]
|
||||
public void GivenStartingBalanceLessThanFee_ApplyFee_ShouldBeExpectedBalance(
|
||||
decimal startingBalance,
|
||||
decimal feeAmount,
|
||||
decimal expectedBalance
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var timeProvider = new FakeTimeProvider();
|
||||
var command = new TransferFundsCommand(1, 2, feeAmount);
|
||||
var sourceAccount = new Account(1, startingBalance, AccountType.Borrow);
|
||||
var destAccount = new Account(2, 0, AccountType.Borrow);
|
||||
var transferContext = new TransferContext(sourceAccount, destAccount, timeProvider.GetUtcNow());
|
||||
|
||||
// Act
|
||||
var (customerWrite, revenueWrite, journalWrite, @event) =
|
||||
TransferFundsHandler.Handle(command, transferContext);
|
||||
|
||||
// Assert
|
||||
using var _ = new AssertionScope();
|
||||
customerWrite.Entity.Balance.Should()
|
||||
.Be(expectedBalance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user