using LCU.Library.Internal.CoreApiWrapperLib.Configuration; using LCU.Library.Internal.CoreApiWrapperLib.CoreApiUtilities; using LCU.Library.Internal.CoreApiWrapperLib.CoreDbUtilities; using LCU.Library.Internal.CoreApiWrapperLib.DataAccess; using LCU.Library.Internal.CoreApiWrapperLib.Utilities; using LCU.Library.Internal.CoreApiWrapperModel.Constants; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Organizations; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.PostalAddresses; using Moq; using Newtonsoft.Json; using NUnit.Framework; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace LCU.Api.Internal.CoreApiWrapperTest.DataAccess { public class OrganizationDaoTest { private readonly OrganizationDao _organizationDao; private readonly Mock _mockCoreRequest; private readonly Mock _coreDb; public OrganizationDaoTest() { _mockCoreRequest = new Mock(); _coreDb = new Mock(); var coreConfiguration = new CoreConfiguration { CoreApiConfiguration = new CoreApiConfiguration { CoreEndPointConfiguration = new CoreEndPointConfiguration { PersonsQueryName = "Test" } } }; _organizationDao = new OrganizationDao ( _mockCoreRequest.Object, coreConfiguration, _coreDb.Object ); } [Test] public async Task FindOrganizationByMemberAgreeNbr() { // Arrange var listOfIncludes = new List { RequestIncludeConstants.AllAddresses }; var coreRequestParameters = new CoreRequestParameters(listOfIncludes); var organizationRequest = new MemberNbrRequest { MemberNbr = 100, }; var testPostalAddress = new PostalAddress { AddressUseCode = AddressConstants.PrimaryUseCode, AddressNumber = 11, AddressLine1 = "Test Street", AddressLine2 = "", AddressLine3 = "", CityName = "City Test", PostalCode = "53183", ZipPlus = "3123", CountrySubdivisionCode = "WI" }; var testPostalAddressList = new List { testPostalAddress }; var testOrganization = new Organization { MemberAgreementNumber = 111111111, Name = "Test Organization Name", OrganizationNumber = 123, PostalAddressList = testPostalAddressList }; var header = new CoreMessageHeader(); CoreApiMessage organizationApiMessage = new CoreApiMessage(header, testOrganization); var organizationJson = JsonConvert.SerializeObject(testOrganization); _mockCoreRequest.Setup(x => x.PostRequestObject>(It.IsAny(), It.IsAny>(), It.IsAny())).ReturnsAsync(organizationApiMessage); // Act var results = _organizationDao.FindOrganizationByMemberAgreeNbr(coreRequestParameters, organizationRequest); // Assert Assert.NotNull(results); } [Test] public async Task FindOrganizationNumbersByNameSearch_AllCases_ExecutesOrganizationNumberByNameLikeQueryWithNameAndLimitParameters() { var query = QueryUtilities.GetQuery("Organization/OrganizationNumberByNameLikeQuery"); var name = Guid.NewGuid().ToString().ToUpperInvariant(); var limit = DateTime.Now.Millisecond; var resultSeries = new Random(DateTime.Now.Millisecond); var results = new long[]{ resultSeries.Next(), resultSeries.Next(), resultSeries.Next() }; _coreDb .Setup(c => c.ExecuteQuery(query, It.Is(q => q.GetPropertyValue("organizationName") == name.ToLowerInvariant() && q.GetPropertyValue("limit") == limit))) .Returns(Task.FromResult>(results)); var organizationNumbers = await _organizationDao.FindOrganizationNumbersByNameSearch(name, limit); Assert.AreSame(results, organizationNumbers); } [Test] public async Task CountOrganizationNumbersByName_AllCases_ExecutesOrganizationByNameLikeCountWithNameParameter() { var query = QueryUtilities.GetQuery("Organization/OrganizationByNameLikeCount"); var name = Guid.NewGuid().ToString().ToUpperInvariant(); var count = DateTime.Now.Millisecond; var results = new long[]{ count }; _coreDb .Setup(c => c.ExecuteQuery(query, It.Is(q => q.GetPropertyValue("organizationName") == name.ToLowerInvariant()))) .Returns(Task.FromResult>(results)); var organizationCount = await _organizationDao.CountOrganizationNumbersByName(name); Assert.AreEqual(count, organizationCount); } } } public static class ObjectPropertyExtension { public static T GetPropertyValue(this object obj, string propertyName) { return (T) obj.GetType().GetProperty(propertyName).GetValue(obj, null); } }