using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Moq; using NUnit.Framework; using LCU.Library.Internal.CoreApiWrapperLib.Configuration; using LCU.Library.Internal.CoreApiWrapperLib.CoreApiUtilities; using LCU.Library.Internal.CoreApiWrapperLib.CoreDbUtilities; using LCU.Library.Internal.CoreApiWrapperLib.DataAccess; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Accounts; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreResponses; using LCU.Library.Internal.ApiSecurity.Models; using LCU.Library.Internal.LcuHttpClient.HttpRequest; using LCU.Library.Internal.CoreApiWrapperLib.Utilities; namespace LCU.Api.Internal.CoreApiWrapperTest.DataAccess { public class AccountDaoTest { private IAccountDao _accountDao; private CoreConfiguration _coreConfiguration; private ApiSecurityConfiguration _apiSecurityConfiguration; private Mock _mockCoreRequest; private Mock _mockCoreDbExecution; private Mock _mockPostRequest; private Mock _mockPutRequest; [SetUp] public void SetUp() { _mockCoreDbExecution = new Mock(); _mockCoreRequest = new Mock(); _mockPostRequest = new Mock(); _mockPutRequest = new Mock(); _coreConfiguration = new CoreConfiguration() { CoreSoapApiConfiguration = new CoreSoapApiConfiguration() { CoreSoapApiWrapperHost = "test.lcu-internal.com", CoreSoapEndpointConfiguration = new CoreSoapEndpointConfiguration() { PutAccountAnalysisGroupMaintenanceRequest = "TEST/Account/AccountAnalysisGroupMaintenanceRequest" } } }; _apiSecurityConfiguration = new ApiSecurityConfiguration() { InternalApiKeySet = new Dictionary() { ["CoreSoapApiWrapper"] = "TEST" } }; _accountDao = new AccountDao(_mockCoreRequest.Object, _coreConfiguration, _apiSecurityConfiguration, _mockCoreDbExecution.Object, _mockPostRequest.Object, _mockPutRequest.Object); } [Test] public async Task GetActivityAccountDetailsByAccountNumbers_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { new() {AccountNumber = 1}, new() {AccountNumber = 2}, new() {AccountNumber = 3} }).Verifiable(); var results = await _accountDao.GetActivityAccountDetailsByAccountNumbers(new List() { "1", "2", "3" }); Assert.NotNull(results); Assert.AreEqual(3,results.Count); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains("ActivityAccountDetailsByAccountNbr") && str.Contains(":accountNumbers")), It.IsAny())); } [Test] public async Task GetActivityAccountDetailsByAccountNumbers_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Oops")); Assert.ThrowsAsync(async () => await _accountDao.GetActivityAccountDetailsByAccountNumbers(new List() { "1", "2", "3" })); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains("ActivityAccountDetailsByAccountNbr") && str.Contains(":accountNumbers")), It.IsAny())); } [Test] public async Task FindOwnersByAccountNumber_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { new AccountOwnership() { FirstName = "Tim" } }).Verifiable(); var result = await _accountDao.FindOwnersByAccountNumber(12345, new DateTime()); Assert.NotNull(result); var firstResult = result.First(); Assert.AreEqual(firstResult.FirstName, "Tim"); _mockCoreDbExecution.Verify(); } [Test] public void FindOwnersByAccountNumber_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Error thrown from unit test")); Assert.ThrowsAsync(async () => await _accountDao.FindOwnersByAccountNumber(12345, new DateTime())); _mockCoreDbExecution.Verify(); } [Test] public async Task FindOwnersByPersonName_OK() { var firstName = "Tim"; var role = "OWN"; _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { new AccountOwnership() { FirstName = firstName, Role = role } }).Verifiable(); var result = await _accountDao.FindPrimaryOwnersByPersonName(firstName); Assert.NotNull(result); var firstResult = result.First(); Assert.AreEqual(firstResult.FirstName, firstName); Assert.AreEqual(firstResult.Role, role); _mockCoreDbExecution.Verify(); } [Test] public void FindOwnersByPersonName_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Error thrown from unit test")); Assert.ThrowsAsync(async () => await _accountDao.FindPrimaryOwnersByPersonName("Tim")); _mockCoreDbExecution.Verify(); } [Test] public async Task FindOwnersByOrganizationName_OK() { var organizationName = "The Big Company"; _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { new AccountOwnership() { OrganizationName = organizationName } }).Verifiable(); var result = await _accountDao.FindPrimaryOwnersByOrganizationName(organizationName); Assert.NotNull(result); var firstResult = result.First(); Assert.AreEqual(firstResult.OrganizationName, organizationName); _mockCoreDbExecution.Verify(); } [Test] public void FindOwnersByOrganizationName_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Error thrown from unit test")); Assert.ThrowsAsync(async () => await _accountDao.FindPrimaryOwnersByOrganizationName("The Big Company")); _mockCoreDbExecution.Verify(); } [Test] public async Task FindAccountsForGroupAnalysisUpdate_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny())) .ReturnsAsync(new List { { "11111111" }, { "22222222" } }).Verifiable(); var result = await _accountDao.FindAccountsToUpdateAccountAnalysis(); Assert.NotNull(result); Assert.AreEqual(result.First(), "11111111"); _mockCoreDbExecution.Verify(); } [Test] public async Task FindAccountsForGroupAnalysisUpdate_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(null, null)) .ThrowsAsync(new Exception("Error thrown from unit test")); _mockCoreDbExecution.Verify(); var result = await _accountDao.FindAccountsToUpdateAccountAnalysis(); Assert.IsEmpty(result); } [Test] public async Task UpdateAccountsForGroupAnalysisUpdate_OK() { var accountAnalysisGroupMaintenanceRequest = new AccountAnalysisGroupMaintenanceRequest() { AccountNumber = 123, IsGenerateAnalysisStatement = true, IsPrintAnalysisStatement = true, IsUseForServiceCharge = true }; var basicResponse = new BasicResponse() { Success = true }; var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutAccountAnalysisGroupMaintenanceRequest); _mockPutRequest.Setup(x => x.Async(endpoint, It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(basicResponse).Verifiable(); var result = await _accountDao.UpdateAccountAnalysis(accountAnalysisGroupMaintenanceRequest); Assert.NotNull(result); Assert.AreEqual(result.Success, true); } [Test] public async Task UpdateAccountsForGroupAnalysisUpdate_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(null, null)) .ThrowsAsync(new Exception("Error thrown from unit test")); _mockCoreDbExecution.Verify(); var result = await _accountDao.FindAccountsToUpdateAccountAnalysis(); Assert.IsEmpty(result); } [Test] public async Task UpdateAccountNickName_OK() { var accountRequest = new AccountNickNameUpdateRequest() { AccountNumber = 123, AccountDescription = "TestOk" }; var basicResponse = new BasicResponse() { Success = true }; var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutAccountNickNameUpdateRequest); _mockPutRequest.Setup(x => x.Async(endpoint, It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(basicResponse).Verifiable(); var result = await _accountDao.UpdateAccountNickName(accountRequest); Assert.NotNull(result); Assert.AreEqual(result.Success, true); } [Test] public async Task UpdateAccountNickName_Exception() { var accountRequest = new AccountNickNameUpdateRequest() { AccountNumber = 0, AccountDescription = "TestError" }; var result = await _accountDao.UpdateAccountNickName(accountRequest); Assert.IsNull(result); } [Test] public async Task UpdateAccountCreditReportType_OK() { var accountRequest = new AccountCreditReportTypeUpdateRequest() { AccountNumber = 123, CreditReportType = "EXCL" }; var basicResponse = new BasicResponse() { Success = true }; var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutAccountCreditReportTypeUpdateRequest); _mockPutRequest.Setup(x => x.Async(endpoint, It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(basicResponse).Verifiable(); var result = await _accountDao.UpdateAccountCreditReportType(accountRequest); Assert.NotNull(result); Assert.AreEqual(result.Success, true); } [Test] public async Task UpdateAccountCreditReportType_Exception() { var accountRequest = new AccountCreditReportTypeUpdateRequest() { AccountNumber = 0, CreditReportType = "EXCL" }; var result = await _accountDao.UpdateAccountCreditReportType(accountRequest); Assert.IsNull(result); } [Test] public async Task GetAccountsByMajorMinorBranchAndStatus_OK() { var request = new AccountsByMajorMinorBranchAndStatusRequest() { BranchCodes = new List() { 1234, 1235 }, AccountStatuses = new List() { "Status" }, MajorTypes = new List() { "Major" }, MinorTypes = new List() { "MinorOne", "MinorTwo" } }; var response = new List() { new AccountsByMajorMinorBranchAndStatusResponse() { AccountNumber = 1234, BranchCode = 987654321, Name = "Test Name" }, new AccountsByMajorMinorBranchAndStatusResponse() { AccountNumber = 5678, BranchCode = 987654321, Name = "Test Name Two" } }; _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(response).Verifiable(); var result = await _accountDao.GetAccountsByMajorMinorBranchAndStatus(request); Assert.NotNull(result); var firstResult = result.First(); Assert.AreEqual(firstResult.AccountNumber, 1234); Assert.AreEqual(firstResult.Name, "Test Name"); _mockCoreDbExecution.Verify(); } [Test] public async Task UpdateAccountContractDate_OK() { var accountRequest = new AccountContractDateUpdateRequest { AccountNumber = 123, ContractDate = DateTime.Now }; var basicResponse = new BasicResponse() { Success = true }; var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutAccountContractDateUpdateRequest); _mockPutRequest.Setup(x => x.Async(endpoint, It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(basicResponse).Verifiable(); var result = await _accountDao.UpdateAccountContractDate(accountRequest); Assert.NotNull(result); Assert.AreEqual(result.Success, true); } [Test] public async Task UpdateAccountContractDate_Exception() { var accountRequest = new AccountContractDateUpdateRequest { AccountNumber = 123, ContractDate = DateTime.Now }; var result = await _accountDao.UpdateAccountContractDate(accountRequest); Assert.IsNull(result); } } }