You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.7 KiB
180 lines
6.7 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using Server.Controller.v1;
|
|
using Server.Dto.Request;
|
|
using Server.Dto.Response;
|
|
using Server.IServices;
|
|
using Shared.Criteria;
|
|
|
|
namespace TestAPI.ControllerTests
|
|
{
|
|
public class FormationControllerTest
|
|
{
|
|
private readonly ILogger<FormationsController> _loggerMock = Mock.Of<ILogger<FormationsController>>();
|
|
private readonly Mock<IFormationsService> _servicesMock = new Mock<IFormationsService>();
|
|
|
|
[Fact]
|
|
public async Task GetFormations()
|
|
{
|
|
// Arrange
|
|
var expectedFormations = new List<ResponseFormationDto>
|
|
{
|
|
new ResponseFormationDto { Id = "1", Name = "Computer Science" },
|
|
new ResponseFormationDto { Id = "2", Name = "Data Science" }
|
|
};
|
|
_servicesMock.Setup(s => s.GetFormations(null, 0, 10, FormationOrderCriteria.StartDate, true))
|
|
.ReturnsAsync((expectedFormations.Count, expectedFormations));
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = await formationsController.GetFormations(null, 0, 10);
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result);
|
|
var formationList = Assert.IsAssignableFrom<IEnumerable<ResponseFormationDto>>(okResult.Value);
|
|
Assert.Equal(expectedFormations.Count, formationList.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormationById_ValidId()
|
|
{
|
|
// Arrange
|
|
var expectedFormation = new ResponseFormationDto { Id = "1", Name = "Computer Science" };
|
|
_servicesMock.Setup(s => s.GetFormationById("1")).ReturnsAsync(expectedFormation);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.GetFormationById("1");
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result);
|
|
var formationItem = Assert.IsType<ResponseFormationDto>(okResult.Value);
|
|
Assert.Equal(expectedFormation.Id, formationItem.Id);
|
|
Assert.Equal(expectedFormation.Name, formationItem.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormationById_InvalidId()
|
|
{
|
|
// Arrange
|
|
_servicesMock.Setup(s => s.GetFormationById("InvalidId")).ReturnsAsync((ResponseFormationDto)null);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.GetFormationById("InvalidId");
|
|
|
|
// Assert
|
|
Assert.IsType<NotFoundResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFormation()
|
|
{
|
|
// Arrange
|
|
var newFormation = new RequestFormationDto
|
|
{
|
|
AlumniId = "A1",
|
|
Name = "Software Engineering",
|
|
StartingDate = DateTime.Now.AddYears(-2),
|
|
SchoolName = "University XYZ",
|
|
CurrentFormation = true
|
|
};
|
|
var expectedFormation = new ResponseFormationDto { Id = "3", Name = "Software Engineering" };
|
|
_servicesMock.Setup(s => s.CreateFormation(newFormation)).ReturnsAsync(expectedFormation);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.CreateFormation(newFormation);
|
|
|
|
// Assert
|
|
var createdAtResult = Assert.IsType<CreatedAtActionResult>(result);
|
|
var createdFormation = Assert.IsType<ResponseFormationDto>(createdAtResult.Value);
|
|
Assert.Equal(expectedFormation.Id, createdFormation.Id);
|
|
Assert.Equal(expectedFormation.Name, createdFormation.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFormation_ValidId()
|
|
{
|
|
// Arrange
|
|
var updatedFormation = new RequestFormationDto
|
|
{
|
|
Name = "Updated Data Science",
|
|
StartingDate = DateTime.Now.AddYears(-2),
|
|
SchoolName = "University XYZ",
|
|
CurrentFormation = false
|
|
};
|
|
var expectedFormation = new ResponseFormationDto { Id = "2", Name = "Updated Data Science" };
|
|
_servicesMock.Setup(s => s.UpdateFormation("2", updatedFormation)).ReturnsAsync(expectedFormation);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.UpdateFormation("2", updatedFormation);
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result);
|
|
var updatedFormationResult = Assert.IsType<ResponseFormationDto>(okResult.Value);
|
|
Assert.Equal(expectedFormation.Id, updatedFormationResult.Id);
|
|
Assert.Equal(expectedFormation.Name, updatedFormationResult.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFormation_InvalidId()
|
|
{
|
|
// Arrange
|
|
var updatedFormation = new RequestFormationDto
|
|
{
|
|
Name = "Updated Data Science",
|
|
StartingDate = DateTime.Now.AddYears(-2),
|
|
SchoolName = "University XYZ",
|
|
CurrentFormation = false
|
|
};
|
|
_servicesMock.Setup(s => s.UpdateFormation("InvalidId", updatedFormation)).ReturnsAsync((ResponseFormationDto)null);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.UpdateFormation("InvalidId", updatedFormation);
|
|
|
|
// Assert
|
|
Assert.IsType<BadRequestResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFormation_ValidId()
|
|
{
|
|
// Arrange
|
|
_servicesMock.Setup(s => s.DeleteFormation("1")).ReturnsAsync(true);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.DeleteFormation("1");
|
|
|
|
// Assert
|
|
Assert.IsType<OkResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFormation_InvalidId()
|
|
{
|
|
// Arrange
|
|
_servicesMock.Setup(s => s.DeleteFormation("InvalidId")).ReturnsAsync(false);
|
|
|
|
var formationsController = new FormationsController(_loggerMock, _servicesMock.Object);
|
|
|
|
// Act
|
|
var result = formationsController.DeleteFormation("InvalidId");
|
|
|
|
// Assert
|
|
Assert.IsType<NotFoundResult>(result);
|
|
}
|
|
}
|
|
}
|