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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace TransactionProcessor.BusinessLogic.Tests.Services;
public class TransactionValidationServiceTests {
private readonly TransactionValidationService TransactionValidationService;
private readonly Mock<ISecurityServiceClient> SecurityServiceClient;
private readonly Mock<IEventStoreContext> EventStoreContext;
private readonly Mock<IAggregateService> AggregateService;
public TransactionValidationServiceTests() {
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
Expand All @@ -35,12 +34,11 @@ public TransactionValidationServiceTests() {
Logger.Initialise(NullLogger.Instance);

this.SecurityServiceClient = new Mock<ISecurityServiceClient>();
this.EventStoreContext = new Mock<IEventStoreContext>();
this.AggregateService = new Mock<IAggregateService>();

this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.TokenResponse()));

this.TransactionValidationService = new TransactionValidationService(this.EventStoreContext.Object, this.AggregateService.Object);
this.TransactionValidationService = new TransactionValidationService(this.AggregateService.Object);
}

[Fact]
Expand Down Expand Up @@ -403,9 +401,6 @@ public async Task TransactionValidationService_ValidateSaleTransaction_InvalidCo
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));

var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
Guid.Empty,
Expand All @@ -428,9 +423,6 @@ public async Task TransactionValidationService_ValidateSaleTransaction_InvalidPr
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));

var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand Down Expand Up @@ -499,10 +491,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantD
this.AggregateService.Setup(e => e.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));


var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId1,
Expand All @@ -525,11 +514,6 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantH
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithNoContracts(SettlementSchedule.Immediate));



this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));

var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand All @@ -551,9 +535,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantN
this.AggregateService.Setup(e => e.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(m => m.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));
this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionStateNoCredit));

this.AggregateService.Setup(e => e.GetLatest<MerchantBalanceAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.EmptyMerchantBalanceAggregate());
var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand All @@ -576,8 +558,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantN
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.NotFound("Merchant not found"));



var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand Down Expand Up @@ -626,9 +607,6 @@ public async Task TransactionValidationService_ValidateSaleTransaction_MerchantN
.ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)))
.ReturnsAsync(Result.NotFound());

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));

var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand All @@ -652,10 +630,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_FailedGet
this.AggregateService.SetupSequence(m => m.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate)))
.ReturnsAsync(Result.NotFound());

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));


var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand Down Expand Up @@ -747,10 +722,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_ProductId
this.AggregateService.Setup(e => e.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));


var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand All @@ -772,10 +744,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_Successfu
this.AggregateService.Setup(e => e.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.SetupSequence(m => m.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(JsonConvert.SerializeObject(TestData.MerchantBalanceProjectionState));

this.AggregateService.Setup(e => e.GetLatest<MerchantBalanceAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.Aggregates.MerchantBalanceAggregateWithCredit());
var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
Expand All @@ -789,19 +758,16 @@ public async Task TransactionValidationService_ValidateSaleTransaction_Successfu
}

[Fact]
public async Task TransactionValidationService_ValidateSaleTransaction_FailedGettingMerchantBalance_ResponseIsInvalidMerchantId()
public async Task TransactionValidationService_ValidateSaleTransaction_FailedGettingMerchantBalance_ResponseIsErrorGettingMerchantBalance()
{
this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success(TestData.TokenResponse()));

this.AggregateService.Setup(e => e.Get<EstateAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Success(TestData.Aggregates.EstateAggregateWithOperator()));
this.AggregateService.Setup(e => e.Get<MerchantAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.Aggregates.MerchantAggregateWithEverything(SettlementSchedule.Immediate));

this.EventStoreContext.Setup(e => e.GetPartitionStateFromProjection(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Failure());

var result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
this.AggregateService.Setup(e => e.GetLatest<MerchantBalanceAggregate>(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Failure());
Result<TransactionValidationResult> result = await this.TransactionValidationService.ValidateSaleTransaction(TestData.EstateId,
TestData.MerchantId,
TestData.ContractId,
TestData.ProductId,
Expand All @@ -811,7 +777,7 @@ public async Task TransactionValidationService_ValidateSaleTransaction_FailedGet
CancellationToken.None);

result.IsFailed.ShouldBeTrue();
result.Data.ResponseCode.ShouldBe(TransactionResponseCode.UnknownFailure);
result.Data.ResponseCode.ShouldBe(TransactionResponseCode.ErrorGettingMerchantBalance);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum TransactionResponseCode
ProductNotValidForMerchant = 1016,
OperatorNotEnabledForEstate = 1017,
OperatorNotEnabledForMerchant = 1018,
ErrorGettingMerchantBalance = 1019,

// A Catch All generic Error where reason has not been identified
UnknownFailure = 9999
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ Task<Result<TransactionValidationResult>> ValidateSaleTransaction(Guid estateId,
}

public class TransactionValidationService : ITransactionValidationService{
#region Fields
private readonly IEventStoreContext EventStoreContext;

private readonly IAggregateService AggregateService;
#endregion

public TransactionValidationService(IEventStoreContext eventStoreContext,
IAggregateService aggregateService)
public TransactionValidationService(IAggregateService aggregateService)
{
this.EventStoreContext = eventStoreContext;
this.AggregateService = aggregateService;
}

Expand Down Expand Up @@ -297,16 +293,17 @@ private async Task<Result<TransactionValidationResult>> ValidateTransactionAmoun
return CreateFailedResult(new TransactionValidationResult(TransactionResponseCode.InvalidSaleTransactionAmount, "Transaction Amount must be greater than 0"));
}

Result<String> getBalanceResult = await this.EventStoreContext.GetPartitionStateFromProjection("MerchantBalanceProjection", $"MerchantBalance-{merchantId:N}", cancellationToken);
Result<MerchantBalanceAggregate> getBalanceResult = await this.AggregateService.GetLatest<MerchantBalanceAggregate>(merchantId, cancellationToken);
if (getBalanceResult.IsFailed)
{
return CreateFailedResult(new TransactionValidationResult(TransactionResponseCode.UnknownFailure, $"Error getting balance for Merchant [{merchantName}]"));
return CreateFailedResult(new TransactionValidationResult(TransactionResponseCode.ErrorGettingMerchantBalance, $"Error getting balance for Merchant [{merchantName}]"));
}

MerchantBalanceProjectionState1 projectionState = JsonConvert.DeserializeObject<MerchantBalanceProjectionState1>(getBalanceResult.Data);
if (projectionState.merchant.balance < transactionAmount)
MerchantBalanceAggregate aggregate = getBalanceResult.Data;

if (aggregate.Balance < transactionAmount)
{
return CreateFailedResult(new TransactionValidationResult(TransactionResponseCode.MerchantDoesNotHaveEnoughCredit, $"Merchant [{merchantName}] does not have enough credit available [{projectionState.merchant.balance:0.00}] to perform transaction amount [{transactionAmount}]"));
return CreateFailedResult(new TransactionValidationResult(TransactionResponseCode.MerchantDoesNotHaveEnoughCredit, $"Merchant [{merchantName}] does not have enough credit available [{aggregate.Balance:0.00}] to perform transaction amount [{transactionAmount}]"));
}

return Result.Success(new TransactionValidationResult(TransactionResponseCode.Success, "SUCCESS"));
Expand Down
7 changes: 7 additions & 0 deletions TransactionProcessor.Testing/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,13 @@ public static MerchantBalanceAggregate EmptyMerchantBalanceAggregate()
return merchantBalanceAggregate;
}

public static MerchantBalanceAggregate MerchantBalanceAggregateWithCredit()
{
MerchantBalanceAggregate merchantBalanceAggregate = MerchantBalanceAggregate.Create(TestData.MerchantId);
merchantBalanceAggregate.RecordMerchantDeposit(CreatedMerchantAggregate(), DepositId, DepositAmount.Value, DepositDateTime);
return merchantBalanceAggregate;
}

public static MerchantAggregate CreatedMerchantAggregate()
{
MerchantAggregate merchantAggregate = MerchantAggregate.Create(TestData.MerchantId);
Expand Down
Loading