using Moq; using NUnit.Framework; using Newtonsoft.Json; using System.Linq; using System.Collections.Generic; using System.Threading.Tasks; using LCU.Api.Internal.DataCleanUp.Configuration; using LCU.Api.Internal.DataCleanUp.Models; using LCU.Api.Internal.DataCleanUp.Services; using LCU.Library.Internal.DataCleanUpModel.Models; using LCU.Library.Internal.DataCleanUpModel.Enum; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreResponses; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Accounts; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; namespace LCU.Api.Internal.DataCleanUpTest.Services; public class AddFundsToIndirectSavingsAccountsServiceTest { private AddFundsToIndirectSavingsAccountsService _addFundsToIndirectSavingsAccountsService; private Mock _mockCoreApiWrapperService; private Mock _mockFileOperationsService; private DataCleanUpConfiguration _dataCleanUpConfiguration; private IndirectSavingsRequirementsDefaults _indirectSavingsRequirementsDefaults; private DataCleanUpResult _dataCleanUpResult; [SetUp] public void Setup() { _dataCleanUpConfiguration = new DataCleanUpConfiguration() { TimeZone = "America/Chicago" }; _indirectSavingsRequirementsDefaults = new IndirectSavingsRequirementsDefaults() { Description = "test", GLAccountNumber = "123445", TransactionAmount = 5.00, TransactionTypeCode = "test", TransactionTypeCodeDeposit = "test", TransactionTypeCodeGL = "test", FundsDefaults = new FundsDefaults() { ClearingCategoryCode = "IMED", FundAmount = 5.00, FundTypeCode = "EL", FundTypeDetailCode = "INTR" }, FundingCoreRequests = new FundingCoreRequests() { AccountStatusTypes = new List() { "APPR" }, BranchCodeTypes = new List() { 1310 }, MajorAccountTypes = new List() { "SAV" }, MinorAccountTypes = new List() { "SV01", "BS01" } } }; _mockCoreApiWrapperService = new Mock(); _mockFileOperationsService = new Mock(); _addFundsToIndirectSavingsAccountsService = new AddFundsToIndirectSavingsAccountsService(_mockCoreApiWrapperService.Object, _mockFileOperationsService.Object, _indirectSavingsRequirementsDefaults, _dataCleanUpConfiguration); } [Test] public async Task HandleCleanUp_Ok() { var indirectRequest = new IndirectFundingRequest() { Filename = "Test", SummaryFileDestinationPath = "Test" }; var dataCleanUpRequest = new DataCleanUpRequest() { CleanUpType = CleanUpTypes.AddFundsToIndirectSavingsAccounts, DataCleanUpPayload = JsonConvert.SerializeObject(indirectRequest) }; var response = new List() { new AccountsByMajorMinorBranchAndStatusResponse() { AccountNumber = 1, BranchCode = 1, Name = "Test" } }; var basicResponse = new BasicResponse() { Success = true, Errors = new List() }; var basicResponseList = new List() { basicResponse }; _mockCoreApiWrapperService.Setup(x => x.GetAccountsByMajorMinorBranchAndStatus(It.IsAny())) .ReturnsAsync(response); _mockCoreApiWrapperService.Setup(x => x.PostFundsToAccount(It.IsAny>())) .ReturnsAsync(basicResponseList).Verifiable(); var result = await _addFundsToIndirectSavingsAccountsService.CleanUp(dataCleanUpRequest); _mockCoreApiWrapperService.Verify(x => x.PostFundsToAccount(It.IsAny>()), Times.Exactly(1)); Assert.AreEqual(0, result.ResponseStack.Count()); Assert.AreEqual(true, result.Successful); } [Test] public async Task HandleCleanUp_WithError() { var accountToBeFunded = new AccountsByMajorMinorBranchAndStatusResponse() { BranchCode = 1, AccountNumber = 1, Name = "Test" }; var response = new List() { new AccountsByMajorMinorBranchAndStatusResponse() { AccountNumber = 1, BranchCode = 1, Name = "Test" } }; var testRequest = new DataCleanUpRequest() { CleanUpType = CleanUpTypes.AddFundsToIndirectSavingsAccounts, DataCleanUpPayload = null }; _dataCleanUpResult = new DataCleanUpResult(testRequest, true, new List()); _mockCoreApiWrapperService.Setup(x => x.GetAccountsByMajorMinorBranchAndStatus(It.IsAny())) .ReturnsAsync(response); var result = await _addFundsToIndirectSavingsAccountsService.HandleSavingsToFundsRequest(_dataCleanUpResult, accountToBeFunded); Assert.AreEqual(1, result.ResponseStack.Count()); Assert.AreEqual(false, result.Successful); } [Test] public async Task HandleExportResultsFile_Ok_NoErrors() { var indirectRequest = new IndirectFundingRequest() { Filename = "Test", SummaryFileDestinationPath = "Test" }; var indirectSavingsAccountSummaryList = new List { new IndirectSavingsAccountsSummary { AccountNumber = "1", BranchCode = "1", AmountFunded = _indirectSavingsRequirementsDefaults.FundsDefaults.FundAmount.ToString(), ErrorMessage = string.Empty, Name = "Test", } }; var summaryTotalResults = new IndirectSavingsAccountResult { TotalAccountsFunded = "1", TotalAccountsNotFunded = "0" }; await _addFundsToIndirectSavingsAccountsService.HandleExportResultsAddFundsToSavingsAccounts(indirectRequest); Assert.AreEqual(0, indirectSavingsAccountSummaryList.Where(x => x.ErrorMessage == null).Count()); Assert.AreEqual(summaryTotalResults.TotalAccountsFunded, indirectSavingsAccountSummaryList.Count().ToString()); } [Test] public async Task HandleExportResultsFile_Ok_WithErrors() { var indirectRequest = new IndirectFundingRequest() { Filename = "Test", SummaryFileDestinationPath = "Test" }; var indirectSavingsAccountSummaryList = new List { new IndirectSavingsAccountsSummary { AccountNumber = "1", BranchCode = "1", AmountFunded = _indirectSavingsRequirementsDefaults.FundsDefaults.FundAmount.ToString(), ErrorMessage = "There was a problem funding the account", Name = "Test", } }; var summaryTotalResults = new IndirectSavingsAccountResult { TotalAccountsFunded = "0", TotalAccountsNotFunded = "1" }; await _addFundsToIndirectSavingsAccountsService.HandleExportResultsAddFundsToSavingsAccounts(indirectRequest); Assert.AreEqual(1, indirectSavingsAccountSummaryList.Where(x => x.ErrorMessage != null).Count()); Assert.AreEqual(summaryTotalResults.TotalAccountsNotFunded, indirectSavingsAccountSummaryList.Count().ToString()); } [Test] public async Task HandleCreatingAccountSummary_Ok_NoErrors() { var accountToBeFunded = new AccountsByMajorMinorBranchAndStatusResponse() { BranchCode = 1, AccountNumber = 1, Name = "Test" }; var testRequest = new DataCleanUpRequest() { CleanUpType = CleanUpTypes.AddFundsToIndirectSavingsAccounts, DataCleanUpPayload = null }; _dataCleanUpResult = new DataCleanUpResult(testRequest, true, new List()); var result = _addFundsToIndirectSavingsAccountsService.HandleCreatingAccountSummary(accountToBeFunded, _dataCleanUpResult); Assert.IsNotNull(result); Assert.AreEqual(true, _dataCleanUpResult.Successful); } [Test] public async Task HandleCreatingAccountSummary_Ok_WithErrors() { var accountToBeFunded = new AccountsByMajorMinorBranchAndStatusResponse() { BranchCode = 1, AccountNumber = 1, Name = "Test" }; var testRequest = new DataCleanUpRequest() { CleanUpType = CleanUpTypes.AddFundsToIndirectSavingsAccounts, DataCleanUpPayload = null }; var errorList = new List() { "There was an error" }; _dataCleanUpResult = new DataCleanUpResult(testRequest, false, errorList); var result = _addFundsToIndirectSavingsAccountsService.HandleCreatingAccountSummary(accountToBeFunded, _dataCleanUpResult); Assert.IsNotNull(result); Assert.AreEqual(false, _dataCleanUpResult.Successful); Assert.IsNotNull(result.ErrorMessage); } }