164 lines
7.7 KiB
C#
164 lines
7.7 KiB
C#
using AutoMapper;
|
|
using DamageAssesment.Api.Employees.Controllers;
|
|
using DamageAssesment.Api.Employees.Db;
|
|
using DamageAssesment.Api.Employees.Interfaces;
|
|
using DamageAssesment.Api.Employees.Profiles;
|
|
using DamageAssesment.Api.Employees.Providers;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace DamageAssesment.Api.Employees.Test
|
|
{
|
|
public class EmployeeServiceTest
|
|
{
|
|
[Fact(DisplayName = "Get Employees - Ok case")]
|
|
public async Task GetEmployeesAsync_ShouldReturnStatusCode200()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getOkResponse();
|
|
mockEmployeeService.Setup(service => service.GetEmployeesAsync()).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (OkObjectResult)await EmployeeProvider.GetEmployeesAsync();
|
|
|
|
Assert.Equal(200, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Get Employees - NoContent Case")]
|
|
public async Task GetEmployeesAsync_ShouldReturnStatusCode204()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getNoContentResponse();
|
|
mockEmployeeService.Setup(service => service.GetEmployeesAsync()).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (NoContentResult)await EmployeeProvider.GetEmployeesAsync();
|
|
|
|
Assert.Equal(204, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Get Employee by Id - Ok case")]
|
|
public async Task GetEmployeeAsync_ShouldReturnStatusCode200()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getOkResponse(1);
|
|
mockEmployeeService.Setup(service => service.GetEmployeeByIdAsync(1)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (OkObjectResult)await EmployeeProvider.GetEmployeeByIdAsync(1);
|
|
|
|
Assert.Equal(200, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Get Employee by Id - NotFound case")]
|
|
public async Task GetEmployeeAsync_ShouldReturnStatusCode404()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getNotFoundResponse();
|
|
mockEmployeeService.Setup(service => service.GetEmployeeByIdAsync(99999)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (NotFoundResult)await EmployeeProvider.GetEmployeeByIdAsync(99999);
|
|
Assert.Equal(404, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Post Employee - Ok case")]
|
|
public async Task PostEmployeeAsync_ShouldReturnStatusCode200()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getOkResponse(1);
|
|
var mockInputEmployee = await MockData.getInputEmployeeData();
|
|
mockEmployeeService.Setup(service => service.PostEmployeeAsync(mockInputEmployee)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (OkObjectResult)await EmployeeProvider.CreateEmployee(mockInputEmployee);
|
|
|
|
Assert.Equal(200, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Post Employee - BadRequest case")]
|
|
public async Task PostEmployeeAsync_ShouldReturnStatusCode400()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockInputEmployee = await MockData.getInputEmployeeData();
|
|
var mockResponse = await MockData.getBadRequestResponse();
|
|
mockEmployeeService.Setup(service => service.PostEmployeeAsync(mockInputEmployee)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (BadRequestObjectResult)await EmployeeProvider.CreateEmployee(mockInputEmployee);
|
|
|
|
Assert.Equal(400, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Put Employee - Ok case")]
|
|
public async Task PutEmployeeAsync_ShouldReturnStatusCode200()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getOkResponse(1);
|
|
var mockInputEmployee = await MockData.getInputEmployeeData();
|
|
mockEmployeeService.Setup(service => service.UpdateEmployeeAsync(1,mockInputEmployee)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (OkObjectResult)await EmployeeProvider.UpdateEmployee(1,mockInputEmployee);
|
|
|
|
Assert.Equal(200, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Put Employee - NotFound case")]
|
|
public async Task PutEmployeeAsync_ShouldReturnStatusCode404()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getNotFoundResponse();
|
|
var mockInputEmployee = await MockData.getInputEmployeeData();
|
|
mockEmployeeService.Setup(service => service.UpdateEmployeeAsync(1, mockInputEmployee)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (NotFoundObjectResult)await EmployeeProvider.UpdateEmployee(1, mockInputEmployee);
|
|
|
|
Assert.Equal(404, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Put Employee - BadRequest case")]
|
|
public async Task PutEmployeeAsync_ShouldReturnStatusCode400()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getBadRequestResponse();
|
|
var mockInputEmployee = await MockData.getInputEmployeeData();
|
|
mockEmployeeService.Setup(service => service.UpdateEmployeeAsync(1, mockInputEmployee)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (BadRequestObjectResult)await EmployeeProvider.UpdateEmployee(1, mockInputEmployee);
|
|
|
|
Assert.Equal(400, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Delete Employee - Ok case")]
|
|
public async Task DeleteEmployeeAsync_ShouldReturnStatusCode200()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getOkResponse(1);
|
|
|
|
mockEmployeeService.Setup(service => service.DeleteEmployeeAsync(1)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (OkObjectResult)await EmployeeProvider.DeleteEmployee(1);
|
|
|
|
Assert.Equal(200, result.StatusCode);
|
|
}
|
|
|
|
[Fact(DisplayName = "Delete Employee - NotFound case")]
|
|
public async Task DeleteEmployeeAsync_ShouldReturnStatusCode404()
|
|
{
|
|
var mockEmployeeService = new Mock<IEmployeesProvider>();
|
|
var mockResponse = await MockData.getNotFoundResponse();
|
|
mockEmployeeService.Setup(service => service.DeleteEmployeeAsync(1)).ReturnsAsync(mockResponse);
|
|
|
|
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
|
|
var result = (NotFoundResult)await EmployeeProvider.DeleteEmployee(1);
|
|
|
|
Assert.Equal(404, result.StatusCode);
|
|
}
|
|
}
|
|
} |