Files
2026-03-31 14:32:23 -05:00

174 lines
6.7 KiB
C#

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.CoreApiWrapperModel.CoreModels.MemberAgreements;
using LCU.Library.Internal.LcuHttpClient.HttpRequest;
namespace LCU.Api.Internal.CoreApiWrapperTest.DataAccess
{
public class AgreePersonDaoTest
{
private AgreePersonDao _agreePersonDao;
private Mock<ICoreDbExecution> _mockCoreDbExecution;
private ApiSecurityConfiguration _apiSecurityConfiguration;
private Mock<IPutRequest> _mockPutRequest;
[SetUp]
public void SetUp()
{
_mockCoreDbExecution = new Mock<ICoreDbExecution>();
_mockPutRequest = new Mock<IPutRequest>();
var coreConfiguration = new CoreConfiguration()
{
CoreSoapApiConfiguration = new CoreSoapApiConfiguration()
{
CoreSoapApiWrapperHost = "test.lcu-internal.com",
CoreSoapEndpointConfiguration = new CoreSoapEndpointConfiguration()
{
PutPersonAgreementsDetailTypes = "api/Test/core/PutThing"
}
}
};
_apiSecurityConfiguration = new ApiSecurityConfiguration()
{
InternalApiKeySet = new Dictionary<string, string>()
{
["CoreSoapApiWrapper"] = "TEST"
}
};
_agreePersonDao = new AgreePersonDao(_mockCoreDbExecution.Object,
coreConfiguration,
_apiSecurityConfiguration,
_mockPutRequest.Object);
}
[Test]
public async Task FindByUserId_OK()
{
_mockCoreDbExecution
.Setup(x => x.ExecuteQuery<AgreePerson, object>(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(new List<AgreePerson>()
{
new AgreePerson()
{
AgreeNumber = 100,
PersonNumber = 10,
UserId = "1"
}
}).Verifiable();
var result = await _agreePersonDao.FindByUserId("1");
Assert.NotNull(result);
Assert.AreEqual(100, result.AgreeNumber);
Assert.AreEqual(10, result.PersonNumber);
Assert.AreEqual("1", result.UserId);
//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<AgreePerson, object>(It.Is<string>(str => str.Contains(":userId")),
It.Is<object>(obj => obj.GetHashCode().Equals(new { userId = "1"}.GetHashCode()))));
}
[Test]
public void FindByUserId_Exception()
{
_mockCoreDbExecution
.Setup(x => x.ExecuteQuery<AgreePerson, object>(It.IsAny<string>(), It.IsAny<object>()))
.ThrowsAsync(new Exception("Ah oh Error"));
Assert.ThrowsAsync<Exception>(async () =>
await _agreePersonDao.FindByUserId("1"));
//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<AgreePerson, object>(It.Is<string>(str => str.Contains(":userId")),
It.Is<object>(obj => obj.GetHashCode().Equals(new { userId = "1"}.GetHashCode()))));
}
[Test]
public async Task FindAgreePersonsWithHyphenatedUserId_OK()
{
_mockCoreDbExecution
.Setup(x => x.ExecuteQuery<AgreePerson>(It.IsAny<string>()))
.ReturnsAsync(new List<AgreePerson>()
{
new AgreePerson()
{
AgreeNumber = 100,
PersonNumber = 10,
UserId = "1-1"
}
}).Verifiable();
var result = await _agreePersonDao.FindAgreePersonsWithHyphenatedUserId();
Assert.NotNull(result);
Assert.AreEqual(1, result.Count());
var singleResult = result.FirstOrDefault();
Assert.NotNull(singleResult);
Assert.AreEqual(100, singleResult.AgreeNumber);
Assert.AreEqual(10, singleResult.PersonNumber);
Assert.AreEqual("1-1", singleResult.UserId);
}
[Test]
public void FindAgreePersonsWithHyphenatedUserId_Exception()
{
_mockCoreDbExecution
.Setup(x => x.ExecuteQuery<AgreePerson>(It.IsAny<string>()))
.ThrowsAsync(new Exception("Ah oh Error"));
Assert.ThrowsAsync<Exception>(async () =>
await _agreePersonDao.FindAgreePersonsWithHyphenatedUserId());
}
[Test]
public async Task FindActiveCustodialVruCardAgreementAgreePersons_OK()
{
_mockCoreDbExecution
.Setup(x => x.ExecuteQuery<AgreePerson>(It.IsAny<string>()))
.ReturnsAsync(new List<AgreePerson>()
{
new AgreePerson()
{
AgreeNumber = 100,
PersonNumber = 10,
UserId = "11"
}
}).Verifiable();
var result = await _agreePersonDao.FindActiveCustodialVruCardAgreementAgreePersons();
Assert.NotNull(result);
Assert.AreEqual(1, result.Count());
var singleResult = result.FirstOrDefault();
Assert.NotNull(singleResult);
Assert.AreEqual(100, singleResult.AgreeNumber);
Assert.AreEqual(10, singleResult.PersonNumber);
Assert.AreEqual("11", singleResult.UserId);
}
[Test]
public void FindActiveCustodialVruCardAgreementAgreePersons_Exception()
{
_mockCoreDbExecution
.Setup(x => x.ExecuteQuery<AgreePerson>(It.IsAny<string>()))
.ThrowsAsync(new Exception("Ah oh Error"));
Assert.ThrowsAsync<Exception>(async () =>
await _agreePersonDao.FindActiveCustodialVruCardAgreementAgreePersons());
}
}
}