DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Employees.Test/EmployeeServiceTest.cs

164 lines
7.7 KiB
C#
Raw Permalink Normal View History

2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (OkObjectResult)await EmployeeProvider.GetEmployeeByIdAsync(1);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (NotFoundResult)await EmployeeProvider.GetEmployeeByIdAsync(99999);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
var mockInputEmployee = await MockData.getInputEmployeeData();
mockEmployeeService.Setup(service => service.UpdateEmployeeAsync(1,mockInputEmployee)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (OkObjectResult)await EmployeeProvider.UpdateEmployee(1,mockInputEmployee);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (NotFoundObjectResult)await EmployeeProvider.UpdateEmployee(1, mockInputEmployee);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (BadRequestObjectResult)await EmployeeProvider.UpdateEmployee(1, mockInputEmployee);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
mockEmployeeService.Setup(service => service.DeleteEmployeeAsync(1)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (OkObjectResult)await EmployeeProvider.DeleteEmployee(1);
2023-08-15 22:52:30 -05:00
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);
2023-08-15 22:52:30 -05:00
var EmployeeProvider = new EmployeesController(mockEmployeeService.Object);
var result = (NotFoundResult)await EmployeeProvider.DeleteEmployee(1);
2023-08-15 22:52:30 -05:00
Assert.Equal(404, result.StatusCode);
}
}
}