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 _mockCoreDbExecution; private ApiSecurityConfiguration _apiSecurityConfiguration; private Mock _mockPutRequest; [SetUp] public void SetUp() { _mockCoreDbExecution = new Mock(); _mockPutRequest = new Mock(); 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() { ["CoreSoapApiWrapper"] = "TEST" } }; _agreePersonDao = new AgreePersonDao(_mockCoreDbExecution.Object, coreConfiguration, _apiSecurityConfiguration, _mockPutRequest.Object); } [Test] public async Task FindByUserId_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { 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(It.Is(str => str.Contains(":userId")), It.Is(obj => obj.GetHashCode().Equals(new { userId = "1"}.GetHashCode())))); } [Test] public void FindByUserId_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Ah oh Error")); Assert.ThrowsAsync(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(It.Is(str => str.Contains(":userId")), It.Is(obj => obj.GetHashCode().Equals(new { userId = "1"}.GetHashCode())))); } [Test] public async Task FindAgreePersonsWithHyphenatedUserId_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny())) .ReturnsAsync(new List() { 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(It.IsAny())) .ThrowsAsync(new Exception("Ah oh Error")); Assert.ThrowsAsync(async () => await _agreePersonDao.FindAgreePersonsWithHyphenatedUserId()); } [Test] public async Task FindActiveCustodialVruCardAgreementAgreePersons_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny())) .ReturnsAsync(new List() { 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(It.IsAny())) .ThrowsAsync(new Exception("Ah oh Error")); Assert.ThrowsAsync(async () => await _agreePersonDao.FindActiveCustodialVruCardAgreementAgreePersons()); } } }