DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Answers.Test/AnswersServiceTest.cs

189 lines
8.6 KiB
C#
Raw Normal View History

2023-08-15 22:52:30 -05:00
using AutoMapper;
using DamageAssesment.Api.Answers.Controllers;
using DamageAssesment.Api.Answers.Db;
using DamageAssesment.Api.Answers.Interfaces;
using DamageAssesment.Api.Answers.Profiles;
using DamageAssesment.Api.Answers.Providers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Moq;
using Xunit;
namespace DamageAssesment.Api.Answers.Test
{
public class AnswersServiceTest
{
[Fact(DisplayName = "Get Answers - Ok case")]
public async Task GetAnswersAsync_ShouldReturnStatusCode200()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getOkResponse();
mockAnswerService.Setup(service => service.GetAnswersAsync()).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (OkObjectResult)await AnswerProvider.GetAnswersAsync();
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Get Answers - NoContent Case")]
public async Task GetAnswersAsync_ShouldReturnStatusCode204()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getNoContentResponse();
mockAnswerService.Setup(service => service.GetAnswersAsync()).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (NoContentResult)await AnswerProvider.GetAnswersAsync();
Assert.Equal(204, result.StatusCode);
}
[Fact(DisplayName = "Get Answer by Id - Ok case")]
public async Task GetAnswerAsync_ShouldReturnStatusCode200()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getOkResponse(1);
mockAnswerService.Setup(service => service.GetAnswerByIdAsync(1)).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (OkObjectResult)await AnswerProvider.GetAnswerByIdAsync(1);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Get Answer by Id - NotFound case")]
public async Task GetAnswerAsync_ShouldReturnStatusCode404()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getNotFoundResponse();
mockAnswerService.Setup(service => service.GetAnswerByIdAsync(99)).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (NotFoundResult)await AnswerProvider.GetAnswerByIdAsync(99);
Assert.Equal(404, result.StatusCode);
}
[Fact(DisplayName = "Get Answers by Survey resopnse id - Ok case")]
public async Task GetAnswersByResponseIdAsync_ShouldReturnStatusCode200()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getOkResponse();
mockAnswerService.Setup(service => service.GetAnswersAsync(1)).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (OkObjectResult)await AnswerProvider.GetAnswersByResponseId(1);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Get Answers by Survey resopnse id - NoContent Case")]
public async Task GetAnswersByResponseIdAsync_ShouldReturnStatusCode204()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getNoContentResponse();
mockAnswerService.Setup(service => service.GetAnswersAsync(99)).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (NoContentResult)await AnswerProvider.GetAnswersByResponseId(99);
Assert.Equal(204, result.StatusCode);
}
[Fact(DisplayName = "Post Answer - Ok case")]
public async Task PostAnswerAsync_ShouldReturnStatusCode200()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getOkResponse(1);
var mockInputAnswer = await MockData.getInputAnswerData();
mockAnswerService.Setup(service => service.PostAnswerAsync(mockInputAnswer)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (OkObjectResult) await AnswerProvider.CreateAnswer(mockInputAnswer);
2023-08-15 22:52:30 -05:00
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Post Answer - BadRequest case")]
public async Task PostAnswerAsync_ShouldReturnStatusCode400()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockInputAnswer = await MockData.getInputAnswerData();
var mockResponse = await MockData.getBadRequestResponse();
mockAnswerService.Setup(service => service.PostAnswerAsync(mockInputAnswer)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (BadRequestObjectResult) await AnswerProvider.CreateAnswer(mockInputAnswer);
2023-08-15 22:52:30 -05:00
Assert.Equal(400, result.StatusCode);
}
[Fact(DisplayName = "Put Answer - Ok case")]
public async Task PutAnswerAsync_ShouldReturnStatusCode200()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getOkResponse(1);
var mockInputAnswer = await MockData.getInputAnswerData();
mockAnswerService.Setup(service => service.UpdateAnswerAsync(mockInputAnswer)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (OkObjectResult) await AnswerProvider.UpdateAnswer(mockInputAnswer);
2023-08-15 22:52:30 -05:00
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Put Answer - NotFound case")]
public async Task PutAnswerAsync_ShouldReturnStatusCode404()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getNotFoundResponse();
var mockInputAnswer = await MockData.getInputAnswerData();
mockAnswerService.Setup(service => service.UpdateAnswerAsync(mockInputAnswer)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (NotFoundObjectResult) await AnswerProvider.UpdateAnswer(mockInputAnswer);
2023-08-15 22:52:30 -05:00
Assert.Equal(404, result.StatusCode);
}
[Fact(DisplayName = "Put Answer - BadRequest case")]
public async Task PutAnswerAsync_ShouldReturnStatusCode400()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getBadRequestResponse();
var mockInputAnswer = await MockData.getInputAnswerData();
mockAnswerService.Setup(service => service.UpdateAnswerAsync(mockInputAnswer)).ReturnsAsync(mockResponse);
2023-08-15 22:52:30 -05:00
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (BadRequestObjectResult) await AnswerProvider.UpdateAnswer(mockInputAnswer);
2023-08-15 22:52:30 -05:00
Assert.Equal(400, result.StatusCode);
}
[Fact(DisplayName = "Delete Answer - Ok case")]
public async Task DeleteAnswerAsync_ShouldReturnStatusCode200()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getOkResponse(1);
mockAnswerService.Setup(service => service.DeleteAnswerAsync(1)).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (OkObjectResult)await AnswerProvider.DeleteAnswer(1);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Delete Answer - NotFound case")]
public async Task DeleteAnswerAsync_ShouldReturnStatusCode404()
{
var mockAnswerService = new Mock<IAnswersProvider>();
var mockResponse = await MockData.getNotFoundResponse();
mockAnswerService.Setup(service => service.DeleteAnswerAsync(1)).ReturnsAsync(mockResponse);
var AnswerProvider = new AnswersController(mockAnswerService.Object);
var result = (NotFoundResult)await AnswerProvider.DeleteAnswer(1);
Assert.Equal(404, result.StatusCode);
}
}
}