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 _loggerMock = Mock.Of>(); private readonly Mock _servicesMock = new Mock(); [Fact] public async Task GetFormations() { // Arrange var expectedFormations = new List { 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(result); var formationList = Assert.IsAssignableFrom>(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(result); var formationItem = Assert.IsType(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(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(result); var createdFormation = Assert.IsType(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(result); var updatedFormationResult = Assert.IsType(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(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(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(result); } } }