using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; using TestTool = Microsoft.VisualStudio.TestTools.UnitTesting; using LCU.Api.Internal.DataCleanUp.Configuration; using LCU.Api.Internal.DataCleanUp.DataAccess; using LCU.Library.Internal.ApiSecurity.Enums; using LCU.Library.Internal.ApiSecurity.Models; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; using LCU.Library.Internal.LcuHttpClient.HttpRequest; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreResponses; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.AccessChannelPersons; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.AccessTypes; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Accounts; namespace LCU.Api.Internal.DataCleanUpTest.DataAccess; public class CoreApiWrapperDaoTest { private const string ClientKeyValue = "DataCleanUp"; private Mock _mockgetRequest; private Mock _mockpostRequest; private Mock _mockputRequest; private CoreApiWrapperDao _coreApiWrapperDaoWithSecurityKey; private CoreApiWrapperDao _coreApiWrapperDaoWithoutSecurityKey; private readonly ApiSecurityConfiguration _apiSecurityConfigurationWithInternalKey = new ApiSecurityConfiguration() { ApiKeyAuthenticationFailureBehavior = ApiKeyAuthenticationFailureBehavior.Error, ExternalApiKeySet = new Dictionary(), InternalApiKeySet = new Dictionary() { { ClientKeyValue, "some_guid-1234-5666" } } }; private readonly ApiSecurityConfiguration _apiSecurityConfigurationWithoutInternalKey = new ApiSecurityConfiguration() { ApiKeyAuthenticationFailureBehavior = ApiKeyAuthenticationFailureBehavior.Error, ExternalApiKeySet = new Dictionary(), InternalApiKeySet = new Dictionary() }; private readonly CoreApiWrapperConfiguration _coreApiWrapperConfiguration = new CoreApiWrapperConfiguration() { CoreApiWrapperUrl = "https://localhost:44364", AccessChannelPersons = "/api/core/AccessChannelPersons", AccesschannelPersonWithIncludes = "/api/core/AccessChannelPersons/GetWithIncludes", PostFundsToAccount = "/api/core/Transaction/PostFundsToAccount", PutAccountNickNameUpdateRequest = "/api/core/Account/PutAccountNickNameUpdateRequest", }; [SetUp] public void Setup() { _mockpostRequest = new Mock(); _mockputRequest = new Mock(); _mockgetRequest = new Mock(); _coreApiWrapperDaoWithSecurityKey = new CoreApiWrapperDao(_apiSecurityConfigurationWithInternalKey, _coreApiWrapperConfiguration, _mockpostRequest.Object, _mockputRequest.Object, _mockgetRequest.Object); _coreApiWrapperDaoWithoutSecurityKey = new CoreApiWrapperDao(_apiSecurityConfigurationWithoutInternalKey, _coreApiWrapperConfiguration, _mockpostRequest.Object, _mockputRequest.Object, _mockgetRequest.Object); } [Test] public async Task GetRequest_GetCoreApiMessageAccessChannelPersonsWithIncludesSecurityKeys_Success() { var accessChannelPersonsByPersonRequest = new AccessChannelPersonsByPersonRequest() { PersonNumber = 145, ActiveOnly = true, IncludeCards = false }; var testPersonNumbers = new List() { "111111" }; var coreMessageHeader = new CoreMessageHeader() { UpdateKey = 12312312323123 }; var testAccessType = new AccessType() { Id = "WWW", IsActive = true }; var testAccessChannelPerson = new AccessChannelPerson() { Id = "2313213_1", ChannelNumber = 2313213, PersonNumber = 1, UserId = "2312312312", AccessTypes = new List() { testAccessType } }; var testAccessChannelPersons = new List() { testAccessChannelPerson , new AccessChannelPerson() { PersonNumber = 1, UserId= "1C1", AccessTypes = new List() { new AccessType() { Id= "VRU", IsActive = true } } } }; var coreApiMessage = new CoreApiMessage>(coreMessageHeader, testAccessChannelPersons); _mockgetRequest.Setup(x => x.AsyncWithNewton>>(It.IsAny(), It.IsAny>(),It.IsAny(), It.IsAny())).ReturnsAsync(coreApiMessage).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.GetCoreApiMessageAccessChannelPersons(accessChannelPersonsByPersonRequest, testPersonNumbers); Assert.IsNotNull(response); Assert.AreEqual(2, response.MessageBody.Count()); } [Test] public async Task GetRequest_GetCoreApiMessageAccessChannelPersonsWithIncludesSecurityKeys_NoResponse() { var accessChannelPersonsByPersonRequest = new AccessChannelPersonsByPersonRequest() { }; var testPersonNumbers = new List() { }; var response = await _coreApiWrapperDaoWithSecurityKey.GetCoreApiMessageAccessChannelPersons(accessChannelPersonsByPersonRequest, testPersonNumbers); Assert.IsNotNull(_mockgetRequest); Assert.AreEqual(null, response); } [Test] public async Task GetRequest_GetCoreApiMessageAccessChannelPersonsWithSecurityKeys_Success() { var accessChannelPersonsByPersonRequest = new AccessChannelPersonsByPersonRequest() { PersonNumber = 145, ActiveOnly = true, IncludeCards = false }; var coreMessageHeader = new CoreMessageHeader() { UpdateKey = 12312312323123 }; var testAccessChannelPersons = new List() { new AccessChannelPerson() { PersonNumber = 1, UserId= "1C1", AccessTypes = new List() { new AccessType() { Id = "WWW", IsActive = true } } } }; var coreApiMessage = new CoreApiMessage>(coreMessageHeader, testAccessChannelPersons); _mockgetRequest.Setup(x => x.Async>>(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(coreApiMessage).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.GetCoreApiMessageAccessChannelPersons(accessChannelPersonsByPersonRequest); Assert.IsNotNull(_mockgetRequest); Assert.IsNotNull(response); } [Test] public async Task GetRequest_GetCoreApiMessageAccessChannelPersonsWithKeys_NoResponse() { var accessChannelPersonsByPersonRequest = new AccessChannelPersonsByPersonRequest() { }; var response = await _coreApiWrapperDaoWithSecurityKey.GetCoreApiMessageAccessChannelPersons(accessChannelPersonsByPersonRequest); Assert.IsNotNull(_mockgetRequest); Assert.AreEqual(null, response); } [Test] public async Task PostRequest_PostFundsWithSecurityKeys_Success() { var listFundsRequest = new List() { new FundsToAccountRequest() { AccountNumber = 111, Description = "Test", TransactionAmount = 4 * -1, TransactionTypeCode = "MOB", FundRequests = new List() { new FundRequest() { ClearingCategoryCode = "Test", FundAmount = 4 * -1, FundTypeCode = "Test", FundTypeDetailCode = "Test" } } }, new FundsToAccountRequest() { AccountNumber = 222, Description = "Test2", TransactionAmount = 4, TransactionTypeCode = "DEP", FundRequests = new List() { new FundRequest() { ClearingCategoryCode = "Test", FundAmount = 4, FundTypeCode = "Test", FundTypeDetailCode = "Test" } } } }; //Set Up Response var basicResponse = new BasicResponse() { Success = true, Errors = new List() }; var basicResponseList = new List() { basicResponse }; var path = $"{_coreApiWrapperConfiguration.CoreApiWrapperUrl}{_coreApiWrapperConfiguration.PostFundsToAccount}"; var uri = new Uri(path); _mockpostRequest.Setup(x => x.Async, List>(It.IsAny(), It.IsAny>(), It.IsAny>(), It.IsAny(), It.IsAny())).ReturnsAsync(basicResponseList).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.PostFundsToAccount(listFundsRequest); Assert.IsNotNull(_mockpostRequest); Assert.AreEqual(true, response[0].Success); } [Test] [TestTool.ExpectedException(typeof(System.Exception), AllowDerivedTypes=true)] public async Task PostRequest_PostFundsWithOutSecurityKeys_Exception() { var listFundsRequest = new List() { new FundsToAccountRequest() { AccountNumber = 111, Description = "Test", TransactionAmount = 4 * -1, TransactionTypeCode = "MOB", FundRequests = new List() { new FundRequest() { ClearingCategoryCode = "Test", FundAmount = 4 * -1, FundTypeCode = "Test", FundTypeDetailCode = "Test" } } }, new FundsToAccountRequest() { AccountNumber = 222, Description = "Test2", TransactionAmount = 4, TransactionTypeCode = "DEP", FundRequests = new List() { new FundRequest() { ClearingCategoryCode = "Test", FundAmount = 4, FundTypeCode = "Test", FundTypeDetailCode = "Test" } } } }; Assert.That(() => _coreApiWrapperDaoWithoutSecurityKey.PostFundsToAccount(listFundsRequest), Throws.TypeOf()); //Message will be Your Internal Key is Missing } [Test] public async Task PutRequest_PutAccountNickNameWithSecurityKeys_Success() { var request = new AccountNickNameUpdateRequest() { AccountDescription = "Test", AccountNumber = 1, }; //Set Up Response var basicResponse = new BasicResponse() { Success = true, Errors = new List() }; _mockputRequest.Setup(x => x.Async(It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())).ReturnsAsync(basicResponse).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.PutAccountNicknameUpdateRequest(request); Assert.IsNotNull(_mockputRequest); Assert.AreEqual(true, response.Success); } [Test] public async Task PutRequest_PutAccountUserField_Success() { var request = new List() { new AccountUserField() { UserFieldCode = "PLNB", AccountNumber = 1, Value = "CC2233" } }; var userFieldResponse = new List() { new AccountUserField() { UserFieldCode = "PLNB", AccountNumber = 1, Value = "CC2233" } }; _mockputRequest.Setup(x => x.Async, IEnumerable>(It.IsAny(), It.IsAny>(), It.IsAny>(), It.IsAny(), It.IsAny())).ReturnsAsync(userFieldResponse).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.PutAccountUserField(request); Assert.IsNotNull(_mockputRequest); Assert.AreEqual(1, response.Count()); } [Test] public async Task PostRequest_PostAccountUserField_Success() { var request = new List() { new AccountUserField() { UserFieldCode = "PLDT", AccountNumber = 1, Value = "6/1/2023" } }; var userFieldResponse = new List() { new AccountUserField() { UserFieldCode = "PLDT", AccountNumber = 1, Value = "6/1/2023" } }; _mockpostRequest.Setup(x => x.Async, IEnumerable>(It.IsAny(), It.IsAny>(), It.IsAny>(), It.IsAny(), It.IsAny())).ReturnsAsync(userFieldResponse).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.PostAccountUserField(request); Assert.IsNotNull(_mockpostRequest); Assert.AreEqual(1, response.Count()); } [Test] public async Task GetRequest_GetAccountUserField_Success() { var acctNbr = 993934933333333; var userFieldResponse = new List() { new AccountUserField() { UserFieldCode = "PLDT", AccountNumber = 993934933333333, Value = "6/1/2023" } }; var coreMessageHeader = new CoreMessageHeader() { UpdateKey = 12312312323123 }; var coreApiMessage = new CoreApiMessage>(coreMessageHeader, userFieldResponse); _mockgetRequest.Setup(x => x.AsyncWithNewton>>(It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())).ReturnsAsync(coreApiMessage).Verifiable(); var response = await _coreApiWrapperDaoWithSecurityKey.GetAccountUserField(acctNbr); Assert.IsNotNull(_mockgetRequest); Assert.AreEqual(1, response.Count()); } }