Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ public void FeeCalculationManager_CalculateFees_SingleFixedFee_ServiceFee_FeesAr
calculatedFee.FeeValue.ShouldBe(FeeCalculationManagerTestData.FixedServiceFee5.Value);
}

[Fact]
public void FeeCalculationManager_CalculateFees_SingleFixedFee_ServiceFee_WithCalculationDate_FeesAreCalculated()
{
IFeeCalculationManager manager = new FeeCalculationManager();

List<TransactionFeeToCalculate> feesList = new List<TransactionFeeToCalculate>
{
FeeCalculationManagerTestData.FixedServiceFee5
};

List<CalculatedFee> calculatedFees = manager.CalculateFees(feesList, FeeCalculationManagerTestData.TransactionAmount100, DateTime.Now);

calculatedFees.ShouldHaveSingleItem();
CalculatedFee calculatedFee = calculatedFees.Single();
calculatedFee.CalculatedValue.ShouldBe(5.0m);
calculatedFee.FeeType.ShouldBe(FeeCalculationManagerTestData.FixedServiceFee5.FeeType);
calculatedFee.FeeCalculationType.ShouldBe(FeeCalculationManagerTestData.FixedServiceFee5.CalculationType);
calculatedFee.FeeId.ShouldBe(FeeCalculationManagerTestData.FixedServiceFee5.FeeId);
calculatedFee.FeeValue.ShouldBe(FeeCalculationManagerTestData.FixedServiceFee5.Value);
}

[Fact]
public void FeeCalculationManager_CalculateFees_MultipleFixedFees_ServiceFee_FeesAreCalculated()
{
Expand Down Expand Up @@ -83,6 +104,27 @@ public void FeeCalculationManager_CalculateFees_SinglePercentageFee_ServiceFee_F
calculatedFee.FeeValue.ShouldBe(FeeCalculationManagerTestData.PercentageServiceFeeQuarterPercent.Value);
}

[Fact]
public void FeeCalculationManager_CalculateFees_SinglePercentageFee_ServiceFee_WithFixedDate_FeesAreCalculated()
{
IFeeCalculationManager manager = new FeeCalculationManager();

List<TransactionFeeToCalculate> feesList = new List<TransactionFeeToCalculate>
{
FeeCalculationManagerTestData.PercentageServiceFeeQuarterPercent
};

List<CalculatedFee> calculatedFees = manager.CalculateFees(feesList, FeeCalculationManagerTestData.TransactionAmount100, DateTime.Now);

calculatedFees.ShouldHaveSingleItem();
CalculatedFee calculatedFee = calculatedFees.Single();
calculatedFee.CalculatedValue.ShouldBe(0.25m);
calculatedFee.FeeType.ShouldBe(FeeCalculationManagerTestData.PercentageServiceFeeQuarterPercent.FeeType);
calculatedFee.FeeCalculationType.ShouldBe(FeeCalculationManagerTestData.PercentageServiceFeeQuarterPercent.CalculationType);
calculatedFee.FeeId.ShouldBe(FeeCalculationManagerTestData.PercentageServiceFeeQuarterPercent.FeeId);
calculatedFee.FeeValue.ShouldBe(FeeCalculationManagerTestData.PercentageServiceFeeQuarterPercent.Value);
}

[Fact]
public void FeeCalculationManager_CalculateFees_MultiplePercentageFees_ServiceFee_FeesAreCalculated()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using SimpleResults;
using EventStore.Client;
using Newtonsoft.Json;
using SimpleResults;
using TransactionProcessor.BusinessLogic.Requests;
using TransactionProcessor.ProjectionEngine.Models;
using TransactionProcessor.ProjectionEngine.Repository;
using TransactionProcessor.ProjectionEngine.State;

namespace TransactionProcessor.BusinessLogic.Tests.Mediator;

Expand All @@ -9,6 +14,8 @@
using System.Threading.Tasks;
using BusinessLogic.Services;
using Models;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EventStore.EventStore;

public class DummyTransactionDomainService : ITransactionDomainService
{
Expand Down Expand Up @@ -39,3 +46,93 @@
CancellationToken cancellationToken) =>
Result.Success();
}

public class DummyMerchantBalanceStateRepository : IProjectionStateRepository<MerchantBalanceState>
{
public async Task<Result<MerchantBalanceState>> Load(IDomainEvent @event, CancellationToken cancellationToken) {
return new MerchantBalanceState();
}

public async Task<Result<MerchantBalanceState>> Load(Guid estateId, Guid stateId, CancellationToken cancellationToken)
{
return new MerchantBalanceState();
}

public async Task<Result<MerchantBalanceState>> Save(MerchantBalanceState state, IDomainEvent @event, CancellationToken cancellationToken) {
return state;
}
}

public class DummyTransactionProcessorReadRepository : ITransactionProcessorReadRepository {
public async Task<Result> AddMerchantBalanceChangedEntry(MerchantBalanceChangedEntry entry,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result<List<MerchantBalanceChangedEntry>>> GetMerchantBalanceHistory(Guid estateId,
Guid merchantId,
DateTime startDate,
DateTime endDate,
CancellationToken cancellationToken) {
return Result.Success();
}
}

public class DummyEventStoreContext : IEventStoreContext {
public async Task<Result<List<ResolvedEvent>>> GetEventsBackward(String streamName,
Int32 maxNumberOfEventsToRetrieve,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result<String>> GetPartitionResultFromProjection(String projectionName,
String partitionId,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result<String>> GetPartitionStateFromProjection(String projectionName,
String partitionId,
CancellationToken cancellationToken) {
MerchantBalanceProjectionState1 state = new MerchantBalanceProjectionState1(new Merchant("", "", 0, 0, new Deposits(0, 0, DateTime.MinValue), new Withdrawals(0, 0, DateTime.MinValue), new AuthorisedSales(0, 0, DateTime.MinValue), new DeclinedSales(0, 0, DateTime.MinValue), new Fees(0, 0)) { });

Check notice on line 97 in TransactionProcessor.BusinessLogic.Tests/Mediator/DummyTransactionDomainService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/Mediator/DummyTransactionDomainService.cs#L97

Remove the initializer; it is redundant.
return Result.Success<String>(JsonConvert.SerializeObject(state));
}

public async Task<Result<String>> GetResultFromProjection(String projectionName,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result<String>> GetStateFromProjection(String projectionName,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result> InsertEvents(String streamName,
Int64 expectedVersion,
List<EventData> aggregateEvents,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result> InsertEvents(String streamName,
Int64 expectedVersion,
List<EventData> aggregateEvents,
Object metadata,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result<List<ResolvedEvent>>> ReadEvents(String streamName,
Int64 fromVersion,
CancellationToken cancellationToken) {
return Result.Success();
}

public async Task<Result<String>> RunTransientQuery(String query,
CancellationToken cancellationToken) {
return Result.Success();
}

public event TraceHandler TraceGenerated;

Check warning on line 137 in TransactionProcessor.BusinessLogic.Tests/Mediator/DummyTransactionDomainService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/Mediator/DummyTransactionDomainService.cs#L137

Remove the unused event 'TraceGenerated' or invoke it.
}
24 changes: 22 additions & 2 deletions TransactionProcessor.BusinessLogic.Tests/Mediator/MediatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared.EventStore.EventStore;
using TransactionProcessor.ProjectionEngine.Repository;
using Xunit;

namespace TransactionProcessor.BusinessLogic.Tests.Mediator
Expand All @@ -17,6 +19,7 @@
using Lamar;
using Microsoft.Extensions.DependencyInjection;
using Testing;
using TransactionProcessor.ProjectionEngine.State;

public class MediatorTests
{
Expand All @@ -28,6 +31,20 @@
this.Requests.Add(TestData.ProcessReconciliationCommand);
this.Requests.Add(TestData.ProcessSaleTransactionCommand);
this.Requests.Add(TestData.ProcessSettlementCommand);
this.Requests.Add(TestData.GetMerchantBalanceQuery);
this.Requests.Add(TestData.GetMerchantLiveBalanceQuery);
this.Requests.Add(TestData.GetMerchantBalanceHistoryQuery);
this.Requests.Add(TestData.AddMerchantFeePendingSettlementCommand);
this.Requests.Add(TestData.AddSettledFeeToSettlementCommand);
this.Requests.Add(TestData.GetPendingSettlementQuery);
this.Requests.Add(TestData.RecordCreditPurchaseCommand);
this.Requests.Add(TestData.CalculateFeesForTransactionCommand);
this.Requests.Add(TestData.AddSettledMerchantFeeCommand);
// TODO: this needs the query handling function refactoring to use a repository not the context direct
//this.Requests.Add(TestData.GetVoucherByVoucherCodeQuery);

Check warning on line 44 in TransactionProcessor.BusinessLogic.Tests/Mediator/MediatorTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/Mediator/MediatorTests.cs#L44

Remove this commented out code.
//this.Requests.Add(TestData.GetVoucherByTransactionIdQuery);


}

[Fact]
Expand Down Expand Up @@ -56,7 +73,7 @@
}
catch (Exception ex)
{
errors.Add(ex.Message);
errors.Add($"Command: {baseRequest.GetType().Name} Exception: {ex.Message}");
}
}

Expand All @@ -70,7 +87,7 @@
private IConfigurationRoot SetupMemoryConfiguration()
{
Dictionary<String, String> configuration = new Dictionary<String, String>();

IConfigurationBuilder builder = new ConfigurationBuilder();

builder.AddInMemoryCollection(TestData.DefaultAppSettings);
Expand All @@ -93,6 +110,9 @@
s.AddSingleton<ISettlementDomainService, DummySettlementDomainService>();
s.AddSingleton<IVoucherDomainService, DummyVoucherDomainService>();
s.AddSingleton<ITransactionDomainService, DummyTransactionDomainService>();
s.AddSingleton<IProjectionStateRepository<MerchantBalanceState>, DummyMerchantBalanceStateRepository>();
s.AddSingleton<ITransactionProcessorReadRepository, DummyTransactionProcessorReadRepository>();
s.AddSingleton<IEventStoreContext, DummyEventStoreContext>();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EventStore.Aggregate;
using Shouldly;
using SimpleResults;
using TransactionProcessor.BusinessLogic.Services;
using Xunit;

namespace TransactionProcessor.BusinessLogic.Tests.Services
{
public class DomainServiceHelperTests
{
[Fact]
public void DomainServiceHelper_HandleGetAggregateResult_SuccessfulGet_ResultHandled() {
Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1");
Result<TestAggregate> result = Result.Success(new TestAggregate {
AggregateId = aggregateId});

var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, true);
handleResult.IsSuccess.ShouldBeTrue();
handleResult.Data.ShouldBeOfType(typeof(TestAggregate));
handleResult.Data.AggregateId.ShouldBe(aggregateId);
}

[Fact]
public void DomainServiceHelper_HandleGetAggregateResult_FailedGet_ResultHandled()
{
Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1");
Result<TestAggregate> result = Result.Failure("Failed Get");

var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, true);
handleResult.IsFailed.ShouldBeTrue();
handleResult.Message.ShouldBe("Failed Get");
}

[Fact]
public void DomainServiceHelper_HandleGetAggregateResult_FailedGet_NotFoundButIsError_ResultHandled()
{
Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1");
Result<TestAggregate> result = Result.NotFound("Failed Get");

var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, true);
handleResult.IsFailed.ShouldBeTrue();
handleResult.Message.ShouldBe("Failed Get");
}

[Fact]
public void DomainServiceHelper_HandleGetAggregateResult_FailedGet_NotFoundButIsNotError_ResultHandled()
{
Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1");
Result<TestAggregate> result = Result.NotFound("Failed Get");

var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, false);
handleResult.IsSuccess.ShouldBeTrue();
handleResult.Data.ShouldBeOfType(typeof(TestAggregate));
handleResult.Data.AggregateId.ShouldBe(aggregateId);
}
}

public record TestAggregate : Aggregate {
public override void PlayEvent(IDomainEvent domainEvent) {

Check failure on line 65 in TransactionProcessor.BusinessLogic.Tests/Services/DomainServiceHelperTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic.Tests/Services/DomainServiceHelperTests.cs#L65

Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation.

}

protected override Object GetMetadata() {
return new Object();
}
}

}
Loading
Loading