using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Moq; using NUnit.Framework; using LCU.Library.Internal.ApiSecurity.Models; using LCU.Library.Internal.CoreApiWrapperLib.Configuration; using LCU.Library.Internal.CoreApiWrapperLib.CoreDbUtilities; using LCU.Library.Internal.CoreApiWrapperLib.DataAccess; using LCU.Library.Internal.CoreApiWrapperLib.Utilities; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.IndirectLoan; using LCU.Library.Internal.LcuHttpClient.HttpRequest; namespace LCU.Api.Internal.CoreApiWrapperTest.DataAccess { public class IndirectLoanDaoTest { private IndirectLoanDao _indirectLoanDao; private Mock _mockCoreDbExecution; private CoreConfiguration _coreConfiguration; private ApiSecurityConfiguration _apiSecurityConfiguration; private Mock _mockPutRequest; [SetUp] public void SetUp() { _mockPutRequest = new Mock(); _coreConfiguration = new CoreConfiguration() { CoreSoapApiConfiguration = new CoreSoapApiConfiguration() { CoreSoapEndpointConfiguration = new CoreSoapEndpointConfiguration() { PutDealerOriginalBalance = "TEST/IndirectLoan/PutDealerOriginalBalance" } } }; _apiSecurityConfiguration = new ApiSecurityConfiguration() { InternalApiKeySet = new Dictionary() { ["CoreSoapApiWrapper"] = "TEST" } }; _mockCoreDbExecution = new Mock(); _indirectLoanDao = new IndirectLoanDao(_mockCoreDbExecution.Object, _coreConfiguration, _apiSecurityConfiguration, _mockPutRequest.Object); } [Test] public async Task GetIndirectLoanByAccountNumber_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { new IndirectLoanDetail() {LoanNumber = "1234"} }).Verifiable(); var results = await _indirectLoanDao.FindByAccountNumber(1234); Assert.NotNull(results); Assert.AreEqual(1, results.Count()); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":AcctNbr")), It.Is(obj => obj.GetHashCode().Equals(new { AcctNbr = 1234 }.GetHashCode())))); } [Test] public async Task GetIndirectLoanByAccountNumber_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Yikes")); Assert.ThrowsAsync(async () => await _indirectLoanDao.FindByAccountNumber(1234)); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":AcctNbr")), It.Is(obj => obj.GetHashCode().Equals(new { AcctNbr = 1234 }.GetHashCode())))); } [Test] public async Task GetIndirectLoansByPostedDate_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { new IndirectLoanDetail { LoanNumber = "1" }, new IndirectLoanDetail { LoanNumber = "2" }, new IndirectLoanDetail { LoanNumber = "3" } }).Verifiable(); var results = await _indirectLoanDao.FindByPostedDate(DateTime.Parse("2021-05-01T11:00:00")); Assert.NotNull(results); Assert.AreEqual(3, results.Count()); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":newerThanDateTime")), It.Is(obj => obj.GetHashCode().Equals(new { newerThanDateTime = "2021-05-01 11:00:00" }.GetHashCode())))); } [Test] public async Task GetIndirectLoansByPostedDate_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Yikes")); Assert.ThrowsAsync(async () => await _indirectLoanDao.FindByPostedDate(DateTime.Parse("2021-05-01T11:00:00"))); //Parameters are passed in using Anonymous Object,Verify they are expected object using HashCode. Obj.Equals has issues with Anonymous Objects. _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":newerThanDateTime")), It.Is(obj => obj.GetHashCode().Equals(new { newerThanDateTime = "2021-05-01 11:00:00" }.GetHashCode())))); } [Test] public async Task GetClosedIndirectLoansByPostedDate_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { new ClosedIndirectLoan { LoanNumber = "1" }, new ClosedIndirectLoan { LoanNumber = "2" }, new ClosedIndirectLoan { LoanNumber = "3" } }).Verifiable(); var results = await _indirectLoanDao.FindClosedByPostedDate(DateTime.Parse("2021-05-01T11:00:00")); Assert.NotNull(results); Assert.AreEqual(3, results.Count()); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":newerThanDateTime") && str.Contains("CLS")), It.Is(obj => obj.GetHashCode().Equals(new { newerThanDateTime = "2021-05-01 11:00:00" }.GetHashCode())))); } [Test] public async Task GetClosedIndirectLoansByPostedDate_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Yikes")); Assert.ThrowsAsync(async () => await _indirectLoanDao.FindClosedByPostedDate(DateTime.Parse("2021-05-01T11:00:00"))); //Parameters are passed in using Anonymous Object,Verify they are expected object using HashCode. Obj.Equals has issues with Anonymous Objects. _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":newerThanDateTime") && str.Contains("CLS")), It.Is(obj => obj.GetHashCode().Equals(new { newerThanDateTime = "2021-05-01 11:00:00" }.GetHashCode())))); } [Test] public async Task GetClosedIndirectLoansWithDetailByPostedDate_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { new IndirectLoanDetail { LoanNumber = "1" }, new IndirectLoanDetail { LoanNumber = "2" }, new IndirectLoanDetail { LoanNumber = "3" } }).Verifiable(); var results = await _indirectLoanDao.FindClosedWithDetailByPostedDate(DateTime.Parse("2021-05-01T11:00:00")); Assert.NotNull(results); Assert.AreEqual(3, results.Count()); _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":newerThanDateTime") && str.Contains("CLS")), It.Is(obj => obj.GetHashCode().Equals(new { newerThanDateTime = "2021-05-01 11:00:00" }.GetHashCode())))); } [Test] public async Task GetClosedIndirectLoansWithDetailByPostedDate_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Yikes")); Assert.ThrowsAsync(async () => await _indirectLoanDao.FindClosedWithDetailByPostedDate(DateTime.Parse("2021-05-01T11:00:00"))); //Parameters are passed in using Anonymous Object,Verify they are expected object using HashCode. Obj.Equals has issues with Anonymous Objects. _mockCoreDbExecution.Verify(x => x.ExecuteQuery(It.Is(str => str.Contains(":newerThanDateTime") && str.Contains("CLS")), It.Is(obj => obj.GetHashCode().Equals(new { newerThanDateTime = "2021-05-01 11:00:00" }.GetHashCode())))); } [Test] public async Task UpdateDealerOriginalBalance_Ok() { var accountNumber = "111111111111"; var response = new UpdateDealerOriginalBalanceResponse(); var responseList = new List { response }; //no errors in response var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutDealerOriginalBalance + $"?accountNumber={accountNumber}"); _mockPutRequest.Setup(x => x.Async>(endpoint, null, It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(responseList).Verifiable(); var results = await _indirectLoanDao.UpdateDealerOriginalBalance(accountNumber); Assert.IsNotNull(results); Assert.AreEqual("", results.ToList().Where(x => x.AccountNumber == "111111111111")); _mockPutRequest.Verify(); } [Test] public async Task UpdateDealerOriginalBalance_Ok_WithErrors() { var accountNumber ="111111111111"; var response = new UpdateDealerOriginalBalanceResponse() { AccountNumber = "111111111111", ErrorMessage = "Error: This account is not valid for update." }; var responseList = new List { response }; //error for second account var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutDealerOriginalBalance + $"?accountNumber={accountNumber}"); _mockPutRequest.Setup(x => x.Async>(endpoint, null, It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(responseList).Verifiable(); var results = await _indirectLoanDao.UpdateDealerOriginalBalance(accountNumber); Assert.IsNotNull(results); Assert.AreEqual(1, results.Count()); Assert.AreEqual("111111111111", results.ToList().Where(x => x.AccountNumber == "111111111111").FirstOrDefault().AccountNumber); _mockPutRequest.Verify(); } [Test] public void UpdateDealerOriginalBalance_Exception() { var accountNumber = "111111111111"; var endpoint = HttpUtilities.BuildHttpsUri(_coreConfiguration.CoreSoapApiConfiguration.CoreSoapApiWrapperHost, _coreConfiguration.CoreSoapApiConfiguration.CoreSoapEndpointConfiguration.PutDealerOriginalBalance + $"?accountNumber={accountNumber}"); _mockPutRequest.Setup(x => x.Async>(endpoint, null, It.IsAny>(), It.IsAny(), It.IsAny())) .Throws(new Exception("Error thrown in unit test.")).Verifiable(); Assert.ThrowsAsync(async () => await _indirectLoanDao.UpdateDealerOriginalBalance(accountNumber)); _mockPutRequest.Verify(); } } }