255 lines
9.4 KiB
C#
255 lines
9.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Controllers;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Exceptions;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Models;
|
|
|
|
namespace Application.UnitTests.Controllers
|
|
{
|
|
public class DepartmentControllerTest
|
|
{
|
|
private DepartmentController _controller;
|
|
private IDepartmentService _mockDepartmentService;
|
|
private IUserService _userService;
|
|
private ICurrentUserService _currentUser;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_mockDepartmentService = Mock.Of<IDepartmentService>();
|
|
_userService = Mock.Of<IUserService>();
|
|
_currentUser = Mock.Of<ICurrentUserService>();
|
|
_controller = new DepartmentController(_mockDepartmentService, _userService, _currentUser);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetAll_OK()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetAllDepartmentsAsync()).ReturnsAsync(
|
|
new List<Department>()
|
|
{
|
|
new Department()
|
|
{
|
|
Id = 0
|
|
}
|
|
});
|
|
|
|
var result = await _controller.GetDepartments(null,null);
|
|
Assert.NotNull(result);
|
|
|
|
var okResult = result as OkObjectResult;
|
|
Assert.NotNull(okResult);
|
|
Assert.AreEqual(200, okResult.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetAll_Exception()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetAllDepartmentsAsync())
|
|
.ThrowsAsync(new Exception("Oops"));
|
|
|
|
var result = await _controller.GetDepartments(null, null);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as StatusCodeResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(500, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetById_OK()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetDepartmentByIdAsync(It.IsAny<int>()))
|
|
.ReturnsAsync(new Department() { Id = 5 });
|
|
|
|
var result = await _controller.GetDepartmentById(0);
|
|
Assert.NotNull(result);
|
|
|
|
var okResult = result as OkObjectResult;
|
|
Assert.NotNull(okResult);
|
|
Assert.AreEqual(200, okResult.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetById_NotFound()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetDepartmentByIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new NotFoundException("Oops"));
|
|
|
|
var result = await _controller.GetDepartmentById(5);
|
|
;
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as NotFoundObjectResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(404, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetById_Exception()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetDepartmentByIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Oops"));
|
|
|
|
var result = await _controller.GetDepartmentById(5);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as StatusCodeResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(500, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task CreateDepartment_OK()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.SaveDepartmentAsync(It.IsAny<Department>()))
|
|
.ReturnsAsync(new Department() {Id = 5});
|
|
|
|
var result = await _controller.CreateDepartment(new Department() {DepartmentName = "Test"});
|
|
Assert.NotNull(result);
|
|
|
|
var okResult = result as OkObjectResult;
|
|
Assert.NotNull(okResult);
|
|
Assert.AreEqual(200, okResult.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task CreateDepartment_Exception()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.SaveDepartmentAsync(It.IsAny<Department>()))
|
|
.ThrowsAsync(new Exception("Oops"));
|
|
|
|
var result = await _controller.CreateDepartment(new Department() {DepartmentName = "Test"});
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as StatusCodeResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(500, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task CreateDepartment_DuplicateName()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.SaveDepartmentAsync(It.IsAny<Department>()))
|
|
.ThrowsAsync(new DuplicateDataException("Oops"));
|
|
|
|
var result = await _controller.CreateDepartment(new Department() {DepartmentName = "Test"});
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as BadRequestObjectResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(400, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDepartment_OK()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.UpdateDepartmentAsync(It.IsAny<Department>(), It.IsAny<int>()))
|
|
.ReturnsAsync(new Department() {Id = 5});
|
|
|
|
var result = await _controller.UpdateDepartment(new Department() {DepartmentName = "Test"}, 3);
|
|
Assert.NotNull(result);
|
|
|
|
var okResult = result as OkObjectResult;
|
|
Assert.NotNull(okResult);
|
|
Assert.AreEqual(200, okResult.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDepartment_Exception()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.UpdateDepartmentAsync(It.IsAny<Department>(), It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Oops"));
|
|
|
|
var result = await _controller.UpdateDepartment(new Department() {DepartmentName = "Test"}, 3);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as StatusCodeResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(500, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDepartment_NotFound()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.UpdateDepartmentAsync(It.IsAny<Department>(), It.IsAny<int>()))
|
|
.ThrowsAsync(new NotFoundException("Oops"));
|
|
|
|
var result = await _controller.UpdateDepartment(new Department() {DepartmentName = "Test"}, 3);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as NotFoundObjectResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(404, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UpdateDepartment_DuplicateName()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.UpdateDepartmentAsync(It.IsAny<Department>(), It.IsAny<int>()))
|
|
.ThrowsAsync(new DuplicateDataException("Oops"));
|
|
|
|
var result = await _controller.UpdateDepartment(new Department() {DepartmentName = "Test"}, 3);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as BadRequestObjectResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(400, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetDepartmentDetails_OK()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetDepartmentDetailsAsync(It.IsAny<int>()))
|
|
.ReturnsAsync(new DepartmentDetails {
|
|
Department = new Department { Id = 3 },
|
|
Assignments = new List<UserDepartment> { new UserDepartment(), new UserDepartment() },
|
|
RegionalManager = new AppUser { EmployeeId = "C0000" }
|
|
});
|
|
|
|
var result = await _controller.GetDepartmentDetails(3);
|
|
Assert.NotNull(result);
|
|
|
|
var okResult = result as OkObjectResult;
|
|
Assert.NotNull(okResult);
|
|
Assert.AreEqual(200, okResult.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetDepartmentDetails_Exception()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetDepartmentDetailsAsync( It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Oops"));
|
|
|
|
var result = await _controller.GetDepartmentDetails(3);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as StatusCodeResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(500, statusRes.StatusCode);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetDepartmentDetails_NotFound()
|
|
{
|
|
Mock.Get(_mockDepartmentService).Setup(service => service.GetDepartmentDetailsAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new NotFoundException("Oops"));
|
|
|
|
var result = await _controller.GetDepartmentDetails(3);
|
|
Assert.NotNull(result);
|
|
|
|
var statusRes = result as NotFoundObjectResult;
|
|
Assert.NotNull(statusRes);
|
|
Assert.AreEqual(404, statusRes.StatusCode);
|
|
}
|
|
}
|
|
}
|