Files
monorepo/LCU.WebApps.Indirect.ReserveManagementSystem/Api/ReserveManagementSystemApi.Tests/Features/ExportServiceSteps.cs
T
2026-03-17 08:54:34 -05:00

210 lines
8.6 KiB
C#

using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Transaction;
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests;
using Microsoft.EntityFrameworkCore;
using Moq;
using ReserveManagementSystemApi.ServiceClients;
using ReserveManagementSystemApi.Services;
using ReserveManagementSystemLib.Models;
using ReserveManagementSystemLib.Persistence;
using ReserveManagementSystemLib.Repositories;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
using Xunit;
using ReserveManagementSystemLib.Configuration;
namespace ReserveManagementSystemApi.Tests.Features
{
[Binding]
public sealed class ExportServiceSteps
{
// For additional details on SpecFlow step definitions see https://go.specflow.org/doc-stepdef
private readonly ScenarioContext _scenarioContext;
private ExportService _exportService;
private AppDbContext _appDbContext;
private Mock<ICoreApiServiceClient> _mockCoreApiServiceClient = new Mock<ICoreApiServiceClient>();
private int _exportResult;
private Mock<RmsApiConfiguration> _mockConfiguration = new Mock<RmsApiConfiguration>();
private Exception _exportException;
public ExportServiceSteps(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
}
[BeforeScenario]
public void CreateDatabase()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
_appDbContext = new AppDbContext(options);
_mockConfiguration.Object.DefaultBatchSize = 100;
_exportService = new ExportService(new LoanRepository(_appDbContext), _mockCoreApiServiceClient.Object, new MinorAccountTypeRepository(_appDbContext), _mockConfiguration.Object);
}
[AfterScenario]
public void Dispose()
{
_appDbContext.Database.EnsureDeleted();
_appDbContext.Dispose();
}
[Given(@"the current loans that look like this")]
public void GivenTheCurrentLoansThatLookLikeThis(Table table)
{
var loans = table.CreateSet<Loan>();
_appDbContext.Loans.AddRange(loans);
_appDbContext.SaveChanges();
}
[Given(@"the current minor account that look like this")]
public void GivenTheCurrentMinorAccountsThatLookLikeThis(Table table)
{
var accounts = table.CreateSet<MinorAccountType>();
_appDbContext.MinorAccountTypes.AddRange(accounts);
_appDbContext.SaveChanges();
}
[Given(@"the Core Api is successful in saving reserves")]
public void GivenTheCoreApiIsSuccessfulInSavingReserves()
{
var transactionResponse = new MonetaryTransactionResponse();
transactionResponse.AccountNumber = 6;
transactionResponse.WasSuccessful = true;
var transactionResponse2 = new MonetaryTransactionResponse();
transactionResponse2.AccountNumber = 73;
transactionResponse2.WasSuccessful = true;
var transactionResponseList = new List<MonetaryTransactionResponse> { transactionResponse, transactionResponse2 };
_mockCoreApiServiceClient.Setup(x => x.PostMonetaryTransactions(It.IsAny<List<GroupedMonetaryTransactionRequest>>())).ReturnsAsync(transactionResponseList);
}
[Given(@"the Core Api returns false when saving reserves")]
public void GivenTheCoreApiReturnsFalseWhenSavingReserves()
{
var transactionResponse = new MonetaryTransactionResponse();
transactionResponse.AccountNumber = 6;
transactionResponse.WasSuccessful = false;
var transactionResponse2 = new MonetaryTransactionResponse();
transactionResponse.AccountNumber = 73;
transactionResponse.WasSuccessful = false;
var transactionResponseList = new List<MonetaryTransactionResponse> { transactionResponse, transactionResponse2 };
_mockCoreApiServiceClient.Setup(x => x.PostMonetaryTransactions(It.IsAny<List<GroupedMonetaryTransactionRequest>>())).ReturnsAsync(transactionResponseList);
}
[Given(@"the Core Api returns false when saving chargebacks")]
public void GivenTheCoreApiReturnsFalseWhenSavingChargebacks()
{
var transactionResponse = new MonetaryTransactionResponse();
transactionResponse.AccountNumber = 731;
transactionResponse.WasSuccessful = false;
var transactionResponseList = new List<MonetaryTransactionResponse> { transactionResponse };
_mockCoreApiServiceClient.Setup(x => x.PostMonetaryTransactions(It.IsAny<List<GroupedMonetaryTransactionRequest>>())).ReturnsAsync(transactionResponseList);
}
[Given(@"the Core Api throws an error in saving reserves")]
public void GivenTheCoreApiThrowsAnErrorInSavingReserves()
{
_mockCoreApiServiceClient.Setup(x => x.PostMonetaryTransactions(It.IsAny<List<GroupedMonetaryTransactionRequest>>())).Throws(new Exception());
}
[Given(@"the Core Api is successful in saving chargebacks")]
public void GivenTheCoreApiIsSuccessfulInSavingChargebacks()
{
var transactionResponse = new MonetaryTransactionResponse();
transactionResponse.AccountNumber = 731;
transactionResponse.WasSuccessful = true;
var transactionResponseList = new List<MonetaryTransactionResponse> { transactionResponse };
_mockCoreApiServiceClient.Setup(x => x.PostMonetaryTransactions(It.IsAny<List<GroupedMonetaryTransactionRequest>>())).ReturnsAsync(transactionResponseList);
}
[Given(@"the Core Api throws an error in saving chargebacks")]
public void GivenTheCoreApiThrowsAnErrorInSavingChargebacks()
{
_mockCoreApiServiceClient.Setup(x => x.PostMonetaryTransactions(It.IsAny<List<GroupedMonetaryTransactionRequest>>())).Throws(new Exception());
}
[When(@"I export the loan reserves on the date '(.*)'")]
public async void WhenIExportTheLoanReservesOnTheDate(DateTime currentDate)
{
try
{
_exportResult = await _exportService.ExportReservesAsync(currentDate);
}
catch(Exception ex)
{
_exportException = ex;
}
}
[When(@"I export the loan chargebacks on the date '(.*)'")]
public async void WhenIExportTheLoanChargebacksOnTheDate(DateTime currentDate)
{
try
{
_exportResult = await _exportService.ExportChargebacksAsync(currentDate);
}
catch (Exception ex)
{
_exportException = ex;
}
}
[Then(@"the Core API is called with (.*) loan reserves")]
public void ThenTheCoreAPIIsCalledWithLoanReserves(int numberOfReserves)
{
_mockCoreApiServiceClient.Verify(x => x.PostMonetaryTransactions(It.Is<List<GroupedMonetaryTransactionRequest>>(x => x.Count == numberOfReserves)));
}
[Then(@"the Core API is called with (.*) loan chargebacks")]
public void ThenTheCoreAPIIsCalledWithLoanChargebacks(int numberOfChargebacks)
{
_mockCoreApiServiceClient.Verify(x => x.PostMonetaryTransactions(It.Is<List<GroupedMonetaryTransactionRequest>>(x => x.Count == numberOfChargebacks)));
}
[Then(@"the loan list will be updated to look like")]
public void ThenTheLoanListWillBeUpdatedToLookLike(Table table)
{
var loansResult = _appDbContext.Loans;
table.CompareToSet(loansResult);
}
[Then(@"the result of saving the reserves will be (.*)")]
public void ThenTheResultOfSavingTheReservesWillBe(int exportNumber)
{
Assert.Equal(exportNumber, _exportResult);
}
[Then(@"the result of saving the chargebacks will be (.*)")]
public void ThenTheResultOfSavingTheChargebacksWillBe(int exportNumber)
{
Assert.Equal(exportNumber, _exportResult);
}
[Then(@"the export service throws an Exception")]
public void ThenTheExportServiceThrowsAnException()
{
Assert.IsType<Exception>(_exportException);
}
[Given(@"the dealers that look like")]
public void GivenTheDealersThatLookLike(Table table)
{
var dealers = table.CreateSet<Dealer>().ToList();
_appDbContext.Dealers.AddRange(dealers);
_appDbContext.SaveChanges();
}
}
}