252 lines
8.4 KiB
C#
252 lines
8.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Collections.RepoApi.Controllers;
|
|
using LCU.API.Collections.RepoApi.Services;
|
|
using LCU.Shared.Collections.RepoApiLib.Exceptions;
|
|
using LCU.Shared.Collections.RepoApiLib.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace LCU.API.Collections.RepoApiTests.Controllers
|
|
{
|
|
public class EventAlertControllerTest
|
|
{
|
|
private readonly EventAlertController controller;
|
|
private readonly Mock<IEventAlertService> mockEventAlertService;
|
|
private readonly Mock<IRepoAccountService> mockRepoService;
|
|
private readonly Mock<IHttpContextAccessor> mockHttpContextAccessor;
|
|
|
|
public EventAlertControllerTest()
|
|
{
|
|
mockEventAlertService = new Mock<IEventAlertService>();
|
|
mockRepoService = new Mock<IRepoAccountService>();
|
|
mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
|
|
controller = new EventAlertController(mockEventAlertService.Object,
|
|
mockRepoService.Object, mockHttpContextAccessor.Object);
|
|
}
|
|
|
|
// Get tests
|
|
[Fact]
|
|
public async void Get_OK()
|
|
{
|
|
// arrange
|
|
var alerts = new List<EventAlert>
|
|
{
|
|
new EventAlert()
|
|
{
|
|
AccountNo = "1234567-98",
|
|
Message = "Status is O4R"
|
|
}
|
|
};
|
|
mockEventAlertService.Setup(x => x.ListAll()).ReturnsAsync(alerts);
|
|
|
|
// act
|
|
var result = await controller.Get();
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
var okValue = okResult.Value as IEnumerable<EventAlert>;
|
|
Assert.NotNull(okValue);
|
|
Assert.True(alerts.SequenceEqual(okValue));
|
|
}
|
|
|
|
[Fact]
|
|
public async void Get_UnreadOnly_OK()
|
|
{
|
|
// arrange
|
|
var expected = new EventAlert()
|
|
{
|
|
AccountNo = "1234567-99",
|
|
Message = "Status is O4R"
|
|
};
|
|
mockEventAlertService.Setup(x => x.ListUnread()).ReturnsAsync(new List<EventAlert> {expected});
|
|
|
|
// act
|
|
var result = await controller.Get(unreadOnly: true);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
var okValue = okResult.Value as IEnumerable<EventAlert>;
|
|
Assert.NotNull(okValue);
|
|
Assert.Single(okValue);
|
|
Assert.Equal(expected, okValue.First());
|
|
}
|
|
|
|
[Fact]
|
|
public async void Get_ActionNeeded_OK()
|
|
{
|
|
// arrange
|
|
var expected = new EventAlert()
|
|
{
|
|
AccountNo = "1234567-90",
|
|
Message = "Status is O4R"
|
|
};
|
|
mockEventAlertService.Setup(x => x.ListActionNeeded()).ReturnsAsync(new List<EventAlert> {expected});
|
|
|
|
// act
|
|
var result = await controller.Get(actionNeeded: true);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
var okValue = okResult.Value as IEnumerable<EventAlert>;
|
|
Assert.NotNull(okValue);
|
|
Assert.Single(okValue);
|
|
Assert.Equal(expected, okValue.First());
|
|
}
|
|
|
|
[Fact]
|
|
public async void Get_WithinDateRange_OK()
|
|
{
|
|
// arrange
|
|
var startDate = DateTime.Now.AddSeconds(-5);
|
|
var endDate = DateTime.Now.AddSeconds(5);
|
|
var expected = new EventAlert()
|
|
{
|
|
DateAdded = DateTime.Now,
|
|
AccountNo = "1234567-99",
|
|
Message = "Status is O4R"
|
|
};
|
|
mockEventAlertService.Setup(x => x.ListWithinDateRange(startDate, endDate))
|
|
.ReturnsAsync(new List<EventAlert> {expected});
|
|
|
|
// act
|
|
var result = await controller.GetWithinDateRange(startDate, endDate);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
var okValue = okResult.Value as IEnumerable<EventAlert>;
|
|
Assert.NotNull(okValue);
|
|
Assert.Single(okValue);
|
|
Assert.Equal(expected, okValue.First());
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async void Post_OK()
|
|
{
|
|
// arrange
|
|
var postAlert = new EventAlert()
|
|
{
|
|
AccountNo = "1234567-98",
|
|
Message = "Status is O4R"
|
|
};
|
|
|
|
// act
|
|
var result = await controller.Post(postAlert);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Post_By_Auction_ID_OK()
|
|
{
|
|
// arrange
|
|
mockRepoService.Setup(x => x.FindByAuctionAssignmentId(5)).ReturnsAsync(new RepoAccount()
|
|
{
|
|
RepoId = 10,
|
|
AccountNo = "12345"
|
|
});
|
|
|
|
var postAlert = new EventAlert()
|
|
{
|
|
AccountNo = "1234567-98",
|
|
Message = "Status is O4R"
|
|
};
|
|
|
|
// act
|
|
var result = await controller.CreateByAuctionAssignmentId(5, postAlert);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
mockRepoService.Verify(x => x.FindByAuctionAssignmentId(5), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Post_Bad_Request()
|
|
{
|
|
// arrange
|
|
var postAlert = new EventAlert()
|
|
{
|
|
AccountNo = "1234567-98",
|
|
};
|
|
controller.ModelState.AddModelError("Message", "The message field is required.");
|
|
|
|
// act
|
|
var result = await controller.Post(postAlert);
|
|
Assert.NotNull(result);
|
|
var badRequestResult = result as BadRequestObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(badRequestResult);
|
|
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Post_By_AuctionAssignment_Bad_Request()
|
|
{
|
|
// arrange
|
|
mockEventAlertService.Setup(x => x.SaveAsync(It.IsAny<EventAlert>())).Verifiable();
|
|
mockRepoService.Setup(x => x.FindByAuctionAssignmentId(5)).Returns(Task.FromResult<RepoAccount>(null));
|
|
var postAlert = new EventAlert()
|
|
{
|
|
AccountNo = "1234567-98",
|
|
};
|
|
|
|
// act
|
|
var result = await controller.CreateByAuctionAssignmentId(5, postAlert);
|
|
Assert.NotNull(result);
|
|
var badRequestResult = result as BadRequestObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(badRequestResult);
|
|
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
|
|
mockRepoService.Verify(x => x.FindByAuctionAssignmentId(5), Times.Once);
|
|
mockEventAlertService.Verify(x => x.SaveAsync(It.IsAny<EventAlert>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Duplicate_Add_Bad_Request()
|
|
{
|
|
// arrange
|
|
mockEventAlertService.Setup(x => x.SaveAsync(It.IsAny<EventAlert>())).ThrowsAsync(
|
|
new DuplicateResourceException("EventAlert"));
|
|
|
|
// act
|
|
var result = await controller.Post(new EventAlert());
|
|
Assert.NotNull(result);
|
|
var requestResult = result as ObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(requestResult);
|
|
Assert.Equal(StatusCodes.Status409Conflict, requestResult.StatusCode);
|
|
Assert.Equal("A Duplicate Event Alert already exists.", requestResult.Value);
|
|
}
|
|
}
|
|
} |