DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/SurveyServiceTest.cs
2023-08-15 23:52:30 -04:00

161 lines
7.3 KiB
C#

using DamageAssesment.Api.Survey.Test;
using DamageAssesment.Api.Surveys.Controllers;
using DamageAssesment.Api.Surveys.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
namespace DamageAssesment.Api.Surveys.Test
{
public class SurveyServiceTest
{
[Fact(DisplayName = "Get Surveys - Ok case")]
public async Task GetSurveysAsync_ShouldReturnStatusCode200()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse();
mockSurveyService.Setup(service => service.GetSurveysAsync()).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult) await surveyProvider.GetSurveysAsync();
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Get Surveys - NoContent Case")]
public async Task GetSurveysAsync_ShouldReturnStatusCode204()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getNoContentResponse();
mockSurveyService.Setup(service => service.GetSurveysAsync()).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (NoContentResult)await surveyProvider.GetSurveysAsync();
Assert.Equal(204, result.StatusCode);
}
[Fact(DisplayName = "Get Survey by Id - Ok case")]
public async Task GetSurveyAsync_ShouldReturnStatusCode200()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse(1);
mockSurveyService.Setup(service => service.GetSurveysAsync(1)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult)await surveyProvider.GetSurveysAsync(1);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Get Survey by Id - NotFound case")]
public async Task GetSurveyAsync_ShouldReturnStatusCode404()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getNotFoundResponse();
mockSurveyService.Setup(service => service.GetSurveysAsync(99)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (NotFoundResult)await surveyProvider.GetSurveysAsync(99);
Assert.Equal(404, result.StatusCode);
}
[Fact(DisplayName = "Post Survey - Ok case")]
public async Task PostSurveyAsync_ShouldReturnStatusCode200()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse(1);
var mockInputSurvey = await MockData.getInputSurveyData();
mockSurveyService.Setup(service => service.PostSurveyAsync(mockInputSurvey)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult)await surveyProvider.PostSurveysAsync(mockInputSurvey);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Post Survey - BadRequest case")]
public async Task PostSurveyAsync_ShouldReturnStatusCode400()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockInputSurvey = await MockData.getInputSurveyData();
var mockResponse = await MockData.getBadRequestResponse();
mockSurveyService.Setup(service => service.PostSurveyAsync(mockInputSurvey)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (BadRequestObjectResult)await surveyProvider.PostSurveysAsync(mockInputSurvey);
Assert.Equal(400, result.StatusCode);
}
[Fact(DisplayName = "Put Survey - Ok case")]
public async Task PutSurveyAsync_ShouldReturnStatusCode200()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse(1);
var mockInputSurvey = await MockData.getInputSurveyData();
mockSurveyService.Setup(service => service.PutSurveyAsync(1,mockInputSurvey)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult)await surveyProvider.PutSurveysAsync(1,mockInputSurvey);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Put Survey - NotFound case")]
public async Task PutSurveyAsync_ShouldReturnStatusCode404()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getNotFoundResponse();
var mockInputSurvey = await MockData.getInputSurveyData();
mockSurveyService.Setup(service => service.PutSurveyAsync(1,mockInputSurvey)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (NotFoundObjectResult)await surveyProvider.PutSurveysAsync(1,mockInputSurvey);
Assert.Equal(404, result.StatusCode);
}
[Fact(DisplayName = "Put Survey - BadRequest case")]
public async Task PutSurveyAsync_ShouldReturnStatusCode400()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getBadRequestResponse();
var mockInputSurvey = await MockData.getInputSurveyData();
mockSurveyService.Setup(service => service.PutSurveyAsync(1,mockInputSurvey)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (BadRequestObjectResult)await surveyProvider.PutSurveysAsync(1,mockInputSurvey);
Assert.Equal(400, result.StatusCode);
}
[Fact(DisplayName = "Delete Survey - Ok case")]
public async Task DeleteSurveyAsync_ShouldReturnStatusCode200()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse(1);
mockSurveyService.Setup(service => service.DeleteSurveyAsync(1)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult)await surveyProvider.DeleteSurveysAsync(1);
Assert.Equal(200, result.StatusCode);
}
[Fact(DisplayName = "Delete Survey - NotFound case")]
public async Task DeleteSurveyAsync_ShouldReturnStatusCode404()
{
var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getNotFoundResponse();
mockSurveyService.Setup(service => service.DeleteSurveyAsync(1)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (NotFoundResult)await surveyProvider.DeleteSurveysAsync(1);
Assert.Equal(404, result.StatusCode);
}
}
}