154 lines
7.3 KiB
C#
154 lines
7.3 KiB
C#
using Microsoft.Extensions.Caching.Memory;
|
|
using LCU.API.Internal.RepoApi.Old_Repo_Services;
|
|
using LCU.API.Internal.RepoApi.Repos.Repositories;
|
|
using LCU.API.Internal.RepoApi.Repos.Models.Settings;
|
|
using LCU.API.Internal.RepoApi.Configurations;
|
|
using LCU.API.Internal.RepoApi.Old_Repo_DataAccess;
|
|
using LCU.API.Internal.RepoApi.Repos.Repositories.Settings;
|
|
|
|
namespace LCU.API.Internal.RepoApiTests.ServiceTests
|
|
{
|
|
public class SettingsServiceTests
|
|
{
|
|
private readonly Mock<IAccountTypeRepository> _mockAccountTypeRepository;
|
|
private readonly Mock<IAllowedStatusChangeRepository> _mockAllowedStatusChangeRepository;
|
|
private readonly Mock<IBodyStyleRepository> _mockBodyStyleRepository;
|
|
private readonly Mock<ICleaningLevelRepository> _mockCleaningLevelRepository;
|
|
private readonly Mock<ICollateralTypeRepository> _mockCollateralTypeRepository;
|
|
private readonly Mock<ICollateralTypeToVehicleTypeRepository> _mockCollateralTypeToVehicleTypeRepository;
|
|
private readonly Mock<IHoldStatusRepository> _mockHoldStatusRepository;
|
|
private readonly Mock<ILoanCategoryRepository> _mockLoanCategoryRepository;
|
|
private readonly Mock<ILocationTypeRepository> _mockLocationTypeRepository;
|
|
private readonly Mock<IVehicleTypeRepository> _mockVehicleTypeRepository;
|
|
private readonly Mock<IExternalUserRepository> _mockExternalUserRepository;
|
|
private readonly Mock<IRepoCompanyRepository> _mockRepoCompanyRepository;
|
|
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
|
|
private readonly Mock<IMemoryCache> _mockMemoryCache;
|
|
private readonly Mock<ICollectionsDao> _mockCollectionsDao;
|
|
private readonly ApiConfiguration _apiConfiguration;
|
|
private readonly SettingsService _settingsService;
|
|
|
|
public SettingsServiceTests()
|
|
{
|
|
_mockAccountTypeRepository = new Mock<IAccountTypeRepository>();
|
|
_mockAllowedStatusChangeRepository = new Mock<IAllowedStatusChangeRepository>();
|
|
_mockBodyStyleRepository = new Mock<IBodyStyleRepository>();
|
|
_mockCleaningLevelRepository = new Mock<ICleaningLevelRepository>();
|
|
_mockCollateralTypeRepository = new Mock<ICollateralTypeRepository>();
|
|
_mockCollateralTypeToVehicleTypeRepository = new Mock<ICollateralTypeToVehicleTypeRepository>();
|
|
_mockHoldStatusRepository = new Mock<IHoldStatusRepository>();
|
|
_mockLoanCategoryRepository = new Mock<ILoanCategoryRepository>();
|
|
_mockLocationTypeRepository = new Mock<ILocationTypeRepository>();
|
|
_mockVehicleTypeRepository = new Mock<IVehicleTypeRepository>();
|
|
_mockExternalUserRepository = new Mock<IExternalUserRepository>();
|
|
_mockRepoCompanyRepository = new Mock<IRepoCompanyRepository>();
|
|
_mockUnitOfWork = new Mock<IUnitOfWork>();
|
|
_mockMemoryCache = new Mock<IMemoryCache>();
|
|
_mockCollectionsDao = new Mock<ICollectionsDao>();
|
|
_apiConfiguration = new ApiConfiguration
|
|
{
|
|
SettingsCacheExpirationSeconds = 60,
|
|
TransactionTypeCodes = new List<string> { "Code1", "Code2" },
|
|
TransactionTypeCodeQueryPrefix = "Prefix",
|
|
TransactionHistoryParameterDateFormat = "yyyy-MM-dd",
|
|
TransactionHistoryStartDateOffset = 30
|
|
};
|
|
|
|
_settingsService = new SettingsService(
|
|
_mockAccountTypeRepository.Object,
|
|
_mockAllowedStatusChangeRepository.Object,
|
|
_mockBodyStyleRepository.Object,
|
|
_mockCleaningLevelRepository.Object,
|
|
_mockCollateralTypeRepository.Object,
|
|
_mockCollateralTypeToVehicleTypeRepository.Object,
|
|
_mockHoldStatusRepository.Object,
|
|
_mockLoanCategoryRepository.Object,
|
|
_mockLocationTypeRepository.Object,
|
|
_mockVehicleTypeRepository.Object,
|
|
_mockExternalUserRepository.Object,
|
|
_mockRepoCompanyRepository.Object,
|
|
_mockUnitOfWork.Object,
|
|
_mockMemoryCache.Object,
|
|
_mockCollectionsDao.Object,
|
|
_apiConfiguration
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetCachedRepoSettingsAsync_ShouldReturnSettings_WhenCacheIsEmpty()
|
|
{
|
|
// Arrange
|
|
var cacheEntryMock = new Mock<ICacheEntry>();
|
|
_mockMemoryCache
|
|
.Setup(mc => mc.CreateEntry(It.IsAny<object>()))
|
|
.Returns(cacheEntryMock.Object);
|
|
|
|
_mockAccountTypeRepository.Setup(repo => repo.ListAllAsync())
|
|
.ReturnsAsync(new List<AccountType> { new AccountType { ID = 1, AccountTypeCode = "AT1" } });
|
|
|
|
// Act
|
|
var result = await _settingsService.GetCachedRepoSettingsAsync();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.NotEmpty(result.AccountTypes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldSaveAccountType()
|
|
{
|
|
// Arrange
|
|
var accountType = new AccountType { ID = 1, AccountTypeCode = "AT1" };
|
|
|
|
// Act
|
|
var result = await _settingsService.SaveAsync(accountType);
|
|
|
|
// Assert
|
|
_mockAccountTypeRepository.Verify(repo => repo.AddAsync(accountType), Times.Once);
|
|
_mockUnitOfWork.Verify(uow => uow.CompleteAsync(), Times.Once);
|
|
Assert.Equal(accountType, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldThrowException_WhenRepositoryFails()
|
|
{
|
|
// Arrange
|
|
var accountType = new AccountType { ID = 1, AccountTypeCode = "AT1" };
|
|
_mockAccountTypeRepository.Setup(repo => repo.AddAsync(accountType))
|
|
.ThrowsAsync(new Exception("Repository failure"));
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<RepoApi.Old_Repo_Exceptions.ServerErrorException>(() => _settingsService.SaveAsync(accountType));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_ShouldUpdateAccountType()
|
|
{
|
|
// Arrange
|
|
var accountType = new AccountType { ID = 1, AccountTypeCode = "AT1", Description = "Updated" };
|
|
_mockAccountTypeRepository.Setup(repo => repo.FindByIdAsync(accountType.ID))
|
|
.ReturnsAsync(new AccountType { ID = 1, AccountTypeCode = "AT1" });
|
|
|
|
// Act
|
|
var result = await _settingsService.UpdateAsync(accountType);
|
|
|
|
// Assert
|
|
_mockAccountTypeRepository.Verify(repo => repo.Update(It.IsAny<AccountType>()), Times.Once);
|
|
_mockUnitOfWork.Verify(uow => uow.CompleteAsync(), Times.Once);
|
|
Assert.Equal(accountType.Description, result.Description);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_ShouldThrowNotFoundException_WhenAccountTypeDoesNotExist()
|
|
{
|
|
// Arrange
|
|
var accountType = new AccountType { ID = 1, AccountTypeCode = "AT1" };
|
|
_mockAccountTypeRepository.Setup(repo => repo.FindByIdAsync(accountType.ID))
|
|
.ReturnsAsync((AccountType)null);
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<RepoApi.Old_Repo_Exceptions.NotFoundException> (() => _settingsService.UpdateAsync(accountType));
|
|
}
|
|
}
|
|
}
|