189 lines
6.9 KiB
C#
189 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Internal.RepoApi.Old_Repo_Services;
|
|
using LCU.API.Internal.RepoApi.Repos.Exceptions;
|
|
using LCU.API.Internal.RepoApi.Repos.Models;
|
|
using LCU.API.Internal.RepoApi.Repos.Models.Results;
|
|
using LCU.API.Internal.RepoApi.Repos.Repositories;
|
|
using LCU.API.Internal.RepoApi.Models.Old_Repo_Models.Filters;
|
|
using LCU.API.Internal.RepoApi.Models.Old_Repo_Models.Requests;
|
|
using Moq;
|
|
using Xunit;
|
|
using LCU.API.Internal.RepoApi.Old_Repo_Exceptions;
|
|
|
|
namespace LCU.API.Internal.RepoApiTests.ServiceTests
|
|
{
|
|
public class RepoAccountServiceTests
|
|
{
|
|
private readonly Mock<IRepoAccountRepository> _repoAccountRepositoryMock;
|
|
private readonly Mock<IRepoStatusChangeRepository> _repoStatusChangeRepositoryMock;
|
|
private readonly Mock<IRepoManualStatusRepository> _repoManualStatusRepositoryMock;
|
|
private readonly Mock<ICollateralRepository> _collateralRepositoryMock;
|
|
private readonly Mock<IUnitOfWork> _unitOfWorkMock;
|
|
private readonly RepoAccountService _service;
|
|
|
|
public RepoAccountServiceTests()
|
|
{
|
|
_repoAccountRepositoryMock = new Mock<IRepoAccountRepository>();
|
|
_repoStatusChangeRepositoryMock = new Mock<IRepoStatusChangeRepository>();
|
|
_repoManualStatusRepositoryMock = new Mock<IRepoManualStatusRepository>();
|
|
_collateralRepositoryMock = new Mock<ICollateralRepository>();
|
|
_unitOfWorkMock = new Mock<IUnitOfWork>();
|
|
|
|
_service = new RepoAccountService(
|
|
_repoAccountRepositoryMock.Object,
|
|
_repoStatusChangeRepositoryMock.Object,
|
|
_repoManualStatusRepositoryMock.Object,
|
|
_collateralRepositoryMock.Object,
|
|
_unitOfWorkMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListAll_ShouldReturnPagedResult_WhenCalled()
|
|
{
|
|
// Arrange
|
|
var pageFilter = new PageFilter { Page = 1, Limit = 10 };
|
|
var repoAccountFilter = new RepoAccountPostFilter { RepoIdList = new List<int?> { 1, 2 }, Status = "Open" };
|
|
var expectedResult = new PagedResult<RepoAccount>();
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.ListAsync(It.IsAny<List<int?>>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string?>(), It.IsAny<int?>(), It.IsAny<bool?>()))
|
|
.ReturnsAsync(expectedResult);
|
|
|
|
// Act
|
|
var result = await _service.ListAll(pageFilter, repoAccountFilter);
|
|
|
|
// Assert
|
|
Assert.Equal(expectedResult, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListAll_ShouldThrowServerErrorException_WhenExceptionOccurs()
|
|
{
|
|
// Arrange
|
|
var pageFilter = new PageFilter { Page = 1, Limit = 10 };
|
|
var repoAccountFilter = new RepoAccountPostFilter();
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.ListAsync(It.IsAny<List<int?>>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string?>(), It.IsAny<int?>(), It.IsAny<bool?>()))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.ListAll(pageFilter, repoAccountFilter));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindByAuctionAssignmentId_ShouldReturnRepoAccount_WhenFound()
|
|
{
|
|
// Arrange
|
|
var auctionAssignmentId = 1;
|
|
var expectedRepoAccount = new RepoAccount();
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.FindByAuctionAssignmentIdAsync(auctionAssignmentId))
|
|
.ReturnsAsync(expectedRepoAccount);
|
|
|
|
// Act
|
|
var result = await _service.FindByAuctionAssignmentId(auctionAssignmentId);
|
|
|
|
// Assert
|
|
Assert.Equal(expectedRepoAccount, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindByAuctionAssignmentId_ShouldThrowServerErrorException_WhenExceptionOccurs()
|
|
{
|
|
// Arrange
|
|
var auctionAssignmentId = 1;
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.FindByAuctionAssignmentIdAsync(auctionAssignmentId))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.FindByAuctionAssignmentId(auctionAssignmentId));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldSaveRepoAccountAndAddStatusChange()
|
|
{
|
|
// Arrange
|
|
var repoAccount = new RepoAccount { RepoId = 1, Status = "Open" };
|
|
var savedRepoAccount = new RepoAccount { RepoId = 1, Status = "Open" };
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.AddAsync(repoAccount))
|
|
.ReturnsAsync(savedRepoAccount);
|
|
|
|
// Act
|
|
var result = await _service.SaveAsync(repoAccount);
|
|
|
|
// Assert
|
|
Assert.Equal(repoAccount, result);
|
|
_unitOfWorkMock.Verify(uow => uow.CompleteAsync(), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldThrowServerErrorException_WhenExceptionOccurs()
|
|
{
|
|
// Arrange
|
|
var repoAccount = new RepoAccount { RepoId = 1, Status = "Open" };
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.AddAsync(repoAccount))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<RepoApi.Old_Repo_Exceptions.ServerErrorException>(() => _service.SaveAsync(repoAccount));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_ShouldReturnRepoAccount_WhenFound()
|
|
{
|
|
// Arrange
|
|
var id = 1;
|
|
var expectedRepoAccount = new RepoAccount();
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.FindByIdAsync(id))
|
|
.ReturnsAsync(expectedRepoAccount);
|
|
|
|
// Act
|
|
var result = await _service.Get(id);
|
|
|
|
// Assert
|
|
Assert.Equal(expectedRepoAccount, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_ShouldThrowNotFoundException_WhenRepoAccountNotFound()
|
|
{
|
|
// Arrange
|
|
var id = 1;
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.FindByIdAsync(id))
|
|
.ReturnsAsync((RepoAccount)null);
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<NotFoundException>(() => _service.Get(id));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_ShouldThrowServerErrorException_WhenExceptionOccurs()
|
|
{
|
|
// Arrange
|
|
var id = 1;
|
|
|
|
_repoAccountRepositoryMock
|
|
.Setup(repo => repo.FindByIdAsync(id))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.Get(id));
|
|
}
|
|
}
|
|
}
|