188 lines
6.9 KiB
C#
188 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Internal.RepoApi.Models.Old_Repo_Models;
|
|
using LCU.API.Internal.RepoApi.Old_Repo_Exceptions;
|
|
using LCU.API.Internal.RepoApi.Repos.Exceptions;
|
|
using LCU.API.Internal.RepoApi.Repos.Models;
|
|
using LCU.API.Internal.RepoApi.Repos.Repositories;
|
|
using LCU.API.Internal.RepoApi.Old_Repo_Services;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace LCU.API.Internal.RepoApiTests.ServiceTests
|
|
{
|
|
public class EventAlertServiceTests
|
|
{
|
|
private readonly Mock<IEventAlertRepository> _eventAlertRepositoryMock;
|
|
private readonly Mock<IUnitOfWork> _unitOfWorkMock;
|
|
private readonly Mock<IRepoAccountRepository> _accountRepositoryMock;
|
|
private readonly EventAlertService _service;
|
|
|
|
public EventAlertServiceTests()
|
|
{
|
|
_eventAlertRepositoryMock = new Mock<IEventAlertRepository>();
|
|
_unitOfWorkMock = new Mock<IUnitOfWork>();
|
|
_accountRepositoryMock = new Mock<IRepoAccountRepository>();
|
|
_service = new EventAlertService(
|
|
_eventAlertRepositoryMock.Object,
|
|
_unitOfWorkMock.Object,
|
|
_accountRepositoryMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListAll_ShouldReturnAllEventAlerts()
|
|
{
|
|
// Arrange
|
|
var eventAlerts = new List<EventAlert> { new EventAlert { Id = 1 }, new EventAlert { Id = 2 } };
|
|
_eventAlertRepositoryMock.Setup(repo => repo.ListAllAsync()).ReturnsAsync(eventAlerts);
|
|
|
|
// Act
|
|
var result = await _service.ListAll();
|
|
|
|
// Assert
|
|
Assert.Equal(eventAlerts, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListAll_ShouldThrowServerErrorException_OnException()
|
|
{
|
|
// Arrange
|
|
_eventAlertRepositoryMock.Setup(repo => repo.ListAllAsync()).ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.ListAll());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListUnread_ShouldReturnUnreadEventAlerts()
|
|
{
|
|
// Arrange
|
|
var unreadAlerts = new List<EventAlert> { new EventAlert { Id = 1 } };
|
|
_eventAlertRepositoryMock.Setup(repo => repo.ListUnreadAsync()).ReturnsAsync(unreadAlerts);
|
|
|
|
// Act
|
|
var result = await _service.ListUnread();
|
|
|
|
// Assert
|
|
Assert.Equal(unreadAlerts, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListUnread_ShouldThrowServerErrorException_OnException()
|
|
{
|
|
// Arrange
|
|
_eventAlertRepositoryMock.Setup(repo => repo.ListUnreadAsync()).ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.ListUnread());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListWithinDateRange_ShouldReturnAlertsWithinDateRange()
|
|
{
|
|
// Arrange
|
|
var start = DateTime.Now.AddDays(-1);
|
|
var end = DateTime.Now;
|
|
var alerts = new List<EventAlert> { new EventAlert { Id = 1 } };
|
|
_eventAlertRepositoryMock.Setup(repo => repo.ListWithinDateRangeAsync(start, end)).ReturnsAsync(alerts);
|
|
|
|
// Act
|
|
var result = await _service.ListWithinDateRange(start, end);
|
|
|
|
// Assert
|
|
Assert.Equal(alerts, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListWithinDateRange_ShouldThrowServerErrorException_OnException()
|
|
{
|
|
// Arrange
|
|
var start = DateTime.Now.AddDays(-1);
|
|
var end = DateTime.Now;
|
|
_eventAlertRepositoryMock.Setup(repo => repo.ListWithinDateRangeAsync(start, end)).ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.ListWithinDateRange(start, end));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldSaveEventAlert()
|
|
{
|
|
// Arrange
|
|
var eventAlert = new EventAlert { RepoId = 1, AccountNo = "123" };
|
|
_accountRepositoryMock.Setup(repo => repo.FindByIdAsync(eventAlert.RepoId.Value))
|
|
.ReturnsAsync(new RepoAccount { Status = "ACTIVE" });
|
|
|
|
// Act
|
|
var result = await _service.SaveAsync(eventAlert);
|
|
|
|
// Assert
|
|
_eventAlertRepositoryMock.Verify(repo => repo.AddAsync(eventAlert), Times.Once);
|
|
_unitOfWorkMock.Verify(uow => uow.CompleteAsync(), Times.Once);
|
|
Assert.Equal(eventAlert, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldThrowUnwantedStatusException_WhenStatusIsCancelledOrRedeemed()
|
|
{
|
|
// Arrange
|
|
var eventAlert = new EventAlert { RepoId = 1, AccountNo = "123" };
|
|
_accountRepositoryMock.Setup(repo => repo.FindByIdAsync(eventAlert.RepoId.Value))
|
|
.ReturnsAsync(new RepoAccount { Status = "REDEEMED" });
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<UnwantedStatusException>(() => _service.SaveAsync(eventAlert));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_ShouldThrowServerErrorException_OnException()
|
|
{
|
|
// Arrange
|
|
var eventAlert = new EventAlert { RepoId = 1, AccountNo = "123" };
|
|
_accountRepositoryMock.Setup(repo => repo.FindByIdAsync(eventAlert.RepoId.Value))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.SaveAsync(eventAlert));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAsync_ShouldDeleteEventAlert()
|
|
{
|
|
// Arrange
|
|
var eventAlert = new EventAlert { Id = 1 };
|
|
_eventAlertRepositoryMock.Setup(repo => repo.FindByIdAsync(eventAlert.Id)).ReturnsAsync(eventAlert);
|
|
|
|
// Act
|
|
var result = await _service.DeleteAsync(eventAlert.Id);
|
|
|
|
// Assert
|
|
_eventAlertRepositoryMock.Verify(repo => repo.Remove(eventAlert), Times.Once);
|
|
_unitOfWorkMock.Verify(uow => uow.CompleteAsync(), Times.Once);
|
|
Assert.Equal(eventAlert, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAsync_ShouldThrowNotFoundException_WhenEventAlertNotFound()
|
|
{
|
|
// Arrange
|
|
_eventAlertRepositoryMock.Setup(repo => repo.FindByIdAsync(It.IsAny<int>())).ReturnsAsync((EventAlert)null);
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<NotFoundException>(() => _service.DeleteAsync(1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAsync_ShouldThrowServerErrorException_OnException()
|
|
{
|
|
// Arrange
|
|
_eventAlertRepositoryMock.Setup(repo => repo.FindByIdAsync(It.IsAny<int>())).ThrowsAsync(new Exception());
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<ServerErrorException>(() => _service.DeleteAsync(1));
|
|
}
|
|
}
|
|
}
|