Files
2026-03-13 09:28:55 -05:00

431 lines
16 KiB
C#

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<IGetRequest> _mockgetRequest;
private Mock<IPostRequest> _mockpostRequest;
private Mock<IPutRequest> _mockputRequest;
private CoreApiWrapperDao _coreApiWrapperDaoWithSecurityKey;
private CoreApiWrapperDao _coreApiWrapperDaoWithoutSecurityKey;
private readonly ApiSecurityConfiguration _apiSecurityConfigurationWithInternalKey = new ApiSecurityConfiguration()
{
ApiKeyAuthenticationFailureBehavior = ApiKeyAuthenticationFailureBehavior.Error,
ExternalApiKeySet = new Dictionary<string, string>(),
InternalApiKeySet = new Dictionary<string, string>()
{
{ ClientKeyValue, "some_guid-1234-5666" }
}
};
private readonly ApiSecurityConfiguration _apiSecurityConfigurationWithoutInternalKey = new ApiSecurityConfiguration()
{
ApiKeyAuthenticationFailureBehavior = ApiKeyAuthenticationFailureBehavior.Error,
ExternalApiKeySet = new Dictionary<string, string>(),
InternalApiKeySet = new Dictionary<string, string>()
};
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<IPostRequest>();
_mockputRequest = new Mock<IPutRequest>();
_mockgetRequest = new Mock<IGetRequest>();
_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<string>()
{
"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<AccessType>()
{
testAccessType
}
};
var testAccessChannelPersons = new List<AccessChannelPerson>()
{
testAccessChannelPerson ,
new AccessChannelPerson()
{
PersonNumber = 1,
UserId= "1C1",
AccessTypes = new List<AccessType>()
{
new AccessType()
{
Id= "VRU",
IsActive = true
}
}
}
};
var coreApiMessage = new CoreApiMessage<IEnumerable<AccessChannelPerson>>(coreMessageHeader, testAccessChannelPersons);
_mockgetRequest.Setup(x => x.AsyncWithNewton<CoreApiMessage<IEnumerable<AccessChannelPerson>>>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(),It.IsAny<int>(), It.IsAny<int>())).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<string>()
{
};
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<AccessChannelPerson>()
{
new AccessChannelPerson()
{
PersonNumber = 1,
UserId= "1C1",
AccessTypes = new List<AccessType>()
{
new AccessType()
{
Id = "WWW",
IsActive = true
}
}
}
};
var coreApiMessage = new CoreApiMessage<IEnumerable<AccessChannelPerson>>(coreMessageHeader, testAccessChannelPersons);
_mockgetRequest.Setup(x => x.Async<CoreApiMessage<IEnumerable<AccessChannelPerson>>>(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).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<FundsToAccountRequest>()
{
new FundsToAccountRequest()
{
AccountNumber = 111,
Description = "Test",
TransactionAmount = 4 * -1,
TransactionTypeCode = "MOB",
FundRequests = new List<FundRequest>()
{
new FundRequest()
{
ClearingCategoryCode = "Test",
FundAmount = 4 * -1,
FundTypeCode = "Test",
FundTypeDetailCode = "Test"
}
}
},
new FundsToAccountRequest()
{
AccountNumber = 222,
Description = "Test2",
TransactionAmount = 4,
TransactionTypeCode = "DEP",
FundRequests = new List<FundRequest>()
{
new FundRequest()
{
ClearingCategoryCode = "Test",
FundAmount = 4,
FundTypeCode = "Test",
FundTypeDetailCode = "Test"
}
}
}
};
//Set Up Response
var basicResponse = new BasicResponse()
{
Success = true,
Errors = new List<string>()
};
var basicResponseList = new List<BasicResponse>() { basicResponse };
var path = $"{_coreApiWrapperConfiguration.CoreApiWrapperUrl}{_coreApiWrapperConfiguration.PostFundsToAccount}";
var uri = new Uri(path);
_mockpostRequest.Setup(x => x.Async<List<FundsToAccountRequest>, List<BasicResponse>>(It.IsAny<Uri>(), It.IsAny<List<FundsToAccountRequest>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<int>(), It.IsAny<int>())).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<FundsToAccountRequest>()
{
new FundsToAccountRequest()
{
AccountNumber = 111,
Description = "Test",
TransactionAmount = 4 * -1,
TransactionTypeCode = "MOB",
FundRequests = new List<FundRequest>()
{
new FundRequest()
{
ClearingCategoryCode = "Test",
FundAmount = 4 * -1,
FundTypeCode = "Test",
FundTypeDetailCode = "Test"
}
}
},
new FundsToAccountRequest()
{
AccountNumber = 222,
Description = "Test2",
TransactionAmount = 4,
TransactionTypeCode = "DEP",
FundRequests = new List<FundRequest>()
{
new FundRequest()
{
ClearingCategoryCode = "Test",
FundAmount = 4,
FundTypeCode = "Test",
FundTypeDetailCode = "Test"
}
}
}
};
Assert.That(() => _coreApiWrapperDaoWithoutSecurityKey.PostFundsToAccount(listFundsRequest),
Throws.TypeOf<Exception>()); //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<string>()
};
_mockputRequest.Setup(x => x.Async<AccountNickNameUpdateRequest, BasicResponse>(It.IsAny<Uri>(), It.IsAny<AccountNickNameUpdateRequest>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<int>(), It.IsAny<int>())).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<AccountUserField>()
{
new AccountUserField()
{
UserFieldCode = "PLNB",
AccountNumber = 1,
Value = "CC2233"
}
};
var userFieldResponse = new List<AccountUserField>()
{
new AccountUserField()
{
UserFieldCode = "PLNB",
AccountNumber = 1,
Value = "CC2233"
}
};
_mockputRequest.Setup(x => x.Async<List<AccountUserField>, IEnumerable<AccountUserField>>(It.IsAny<Uri>(), It.IsAny<List<AccountUserField>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<int>(), It.IsAny<int>())).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<AccountUserField>()
{
new AccountUserField()
{
UserFieldCode = "PLDT",
AccountNumber = 1,
Value = "6/1/2023"
}
};
var userFieldResponse = new List<AccountUserField>()
{
new AccountUserField()
{
UserFieldCode = "PLDT",
AccountNumber = 1,
Value = "6/1/2023"
}
};
_mockpostRequest.Setup(x => x.Async<List<AccountUserField>, IEnumerable<AccountUserField>>(It.IsAny<Uri>(), It.IsAny<List<AccountUserField>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<int>(), It.IsAny<int>())).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<AccountUserField>()
{
new AccountUserField()
{
UserFieldCode = "PLDT",
AccountNumber = 993934933333333,
Value = "6/1/2023"
}
};
var coreMessageHeader = new CoreMessageHeader()
{
UpdateKey = 12312312323123
};
var coreApiMessage = new CoreApiMessage<IEnumerable<AccountUserField>>(coreMessageHeader, userFieldResponse);
_mockgetRequest.Setup(x => x.AsyncWithNewton<CoreApiMessage<IEnumerable<AccountUserField>>>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<int>(), It.IsAny<int>())).ReturnsAsync(coreApiMessage).Verifiable();
var response = await _coreApiWrapperDaoWithSecurityKey.GetAccountUserField(acctNbr);
Assert.IsNotNull(_mockgetRequest);
Assert.AreEqual(1, response.Count());
}
}