Files
monorepo/LCU.Api.Internal.CoreApiWrapper/LCU.Api.Internal.CoreApiWrapperTest/Service/AddressServiceTest.cs
T
2026-03-31 14:32:23 -05:00

178 lines
6.6 KiB
C#

using LCU.Api.Internal.CoreApiWrapper.Services;
using LCU.Library.Internal.CoreApiWrapperLib.DataAccess;
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels;
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.PostalAddresses;
using LCU.Library.Internal.Exceptions;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LCU.Api.Internal.CoreApiWrapperTest.Service
{
public class AddressServiceTest
{
private AddressService _addressService;
private Mock<IAddressDao> _mockAddressDao;
private PersonAddrLine _updatedPersonAddrLineOneAddressLine = new PersonAddrLine()
{
PersonNumber = 111,
AddressNumber = 222,
AddressUseCode = "PRI",
AddressLine1 = "1 Imaginary Street",
AddressLineTypeCode1 = "ST",
CityName = "Milwaukee",
PostalCode = "53220",
CountrySubdivisionCode = "WI",
CountryCode = "USA",
};
[SetUp]
public void SetUp()
{
_mockAddressDao = new Mock<IAddressDao>();
_addressService = new AddressService(_mockAddressDao.Object);
}
[Test]
public async Task UpdateAddrLine_ok()
{
_mockAddressDao.Setup(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>())).ReturnsAsync(
_updatedPersonAddrLineOneAddressLine).Verifiable();
var personpostal = new PersonPostalAddress()
{
PersonNumber = 111,
AddressNumber = 222
};
var header = new CoreMessageHeader()
{
UpdateKey = 123456
};
var personList = new List<PersonPostalAddress>()
{
personpostal
}
.AsReadOnly();
var response = new CoreApiMessage<IReadOnlyCollection<PersonPostalAddress>>(header, personList);
_mockAddressDao.Setup(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>())).ReturnsAsync(
response);
var personAddrLine = _updatedPersonAddrLineOneAddressLine;
var result = await _addressService.UpdatePersonAddrLine(personAddrLine);
_mockAddressDao.Verify(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>()), Times.Once);
_mockAddressDao.Verify(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>()), Times.Once);
Assert.AreEqual(_updatedPersonAddrLineOneAddressLine, result);
}
[Test]
public async Task UpdateAddrLine_Fail_WrongPersonId()
{
_mockAddressDao.Setup(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>())).ReturnsAsync(
_updatedPersonAddrLineOneAddressLine).Verifiable();
var personpostal = new PersonPostalAddress()
{
PersonNumber = 999,
AddressNumber = 888
};
var header = new CoreMessageHeader()
{
UpdateKey = 123456
};
var personList = new List<PersonPostalAddress>()
{
personpostal
}
.AsReadOnly();
var response = new CoreApiMessage<IReadOnlyCollection<PersonPostalAddress>>(header, personList);
_mockAddressDao.Setup(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>())).ReturnsAsync(
response);
var personAddrLine = _updatedPersonAddrLineOneAddressLine;
Assert.ThrowsAsync<ServiceException>(async () => await _addressService.UpdatePersonAddrLine(personAddrLine));
_mockAddressDao.Verify(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>()), Times.Once);
_mockAddressDao.Verify(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>()), Times.Never);
}
[Test]
public async Task UpdateAddrLine_Fail_NullPerson()
{
_mockAddressDao.Setup(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>())).ReturnsAsync(
_updatedPersonAddrLineOneAddressLine).Verifiable();
var header = new CoreMessageHeader()
{
UpdateKey = 123456
};
var personList = new List<PersonPostalAddress>().AsReadOnly();
var response = new CoreApiMessage<IReadOnlyCollection<PersonPostalAddress>>(header, personList);
_mockAddressDao.Setup(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>())).ReturnsAsync(
response);
var personAddrLine = _updatedPersonAddrLineOneAddressLine;
Assert.ThrowsAsync<ServiceException>(async () => await _addressService.UpdatePersonAddrLine(personAddrLine));
_mockAddressDao.Verify(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>()), Times.Once);
_mockAddressDao.Verify(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>()), Times.Never);
}
[Test]
public async Task UpdateAddrLine_Fail_NullResponse()
{
_mockAddressDao.Setup(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>())).Returns(
Task.FromResult((PersonAddrLine)null)).Verifiable();
var personpostal = new PersonPostalAddress()
{
PersonNumber = 111,
AddressNumber = 222
};
var header = new CoreMessageHeader()
{
UpdateKey = 123456
};
var personList = new List<PersonPostalAddress>()
{
personpostal
}
.AsReadOnly();
var response = new CoreApiMessage<IReadOnlyCollection<PersonPostalAddress>>(header, personList);
_mockAddressDao.Setup(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>())).ReturnsAsync(
response);
var personAddrLine = _updatedPersonAddrLineOneAddressLine;
Assert.ThrowsAsync<ServiceException>(async () => await _addressService.UpdatePersonAddrLine(personAddrLine));
_mockAddressDao.Verify(x => x.FindByPersonNumber(It.IsAny<CoreRequestParameters>(), It.IsAny<string>()), Times.Once);
_mockAddressDao.Verify(x => x.UpdatePersonAddrLine(It.IsAny<CoreApiMessage<PersonAddrLine>>()), Times.Once);
}
}
}