using EPAServeur.Context; using EPAServeur.Exceptions; using EPAServeur.Models.Formation; using EPAServeur.Services; using IO.Swagger.Controllers; using IO.Swagger.DTO; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.DependencyInjection; using EPAServeur.IServices; using Moq; using IO.Swagger.ApiCollaborateur; namespace EPAServeur.Tests.Controllers { [TestFixture] public class FormationApiTests { #region Variables private IFormationService formationService; private Mock mockEnvironment; private EpContext epContext; #endregion #region Setup [SetUp] public void Setup() { // Création d'une collection de services pour l'injection de dépendance var services = new ServiceCollection(); // Utilisation d'une base de données en mémoire var optionBuider = new DbContextOptionsBuilder() .UseInMemoryDatabase("server_ep_test") .Options; services.AddDbContext(b => b.UseInMemoryDatabase("server_ep_test")); epContext = new EpContext(optionBuider); epContext.Database.EnsureDeleted(); epContext.Database.EnsureCreated(); epContext.SaveChanges(); // Ajout du jeu de données pour les tests DataSeeder.AddFormations(epContext); // Détache les entités du context car la base de données InMemory créé des conflits // entre les clés primaires lors d'un Update ou d'un Insert foreach (var entity in epContext.ChangeTracker.Entries()) { entity.State = EntityState.Detached; } services.AddScoped(); services.AddScoped(); services.AddScoped(); // Récupère le service qui sera utilsé pour tester le contrôleur var serviceProvider = services.BuildServiceProvider(); formationService = serviceProvider.GetService(); // Simule l'interface IWebHostEnvironment avec Moq mockEnvironment = new Mock(); mockEnvironment .Setup(m => m.EnvironmentName) .Returns("Development"); } #endregion #region Tests GetFormationById [Test] public void GetById_PasseEnParamUnIdInconnu_RetourneUnObjetNotFoundResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); // Act var notFoundResult = formationsApiController.GetFormationById(99999); // Assert Assert.IsInstanceOf(notFoundResult.Result); } [Test] public void GetFormationById_PasseEnParamUnIdConnu_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = formationsApiController.GetFormationById(1); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetFormationById_PasseEnParamUnIdConnu_RetourneLaBonneFormation() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); long idFormation = 1; // Act var okResult = formationsApiController.GetFormationById(idFormation).Result as OkObjectResult; // Assert Assert.IsInstanceOf(okResult.Value); Assert.AreEqual(idFormation, (okResult.Value as FormationDTO).Id); } #endregion #region Tests GetFormations [Test] public void GetFormations_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); List idStatuts = new List { 1, 2, 3 }; // Act var okResult = formationsApiController.GetFormations(1, idStatuts, true, 1, 5, "formation", null, null, null); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetFormations_PasseDesParamsPresentsDansLaBDD_RetourneLesCinqPremieresFormations() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); List idStatuts = new List { 1, 2, 3 }; int nbFormation = 5; int idFirstFormation = 5; int idLastFormation = 1; // Act var okResult = formationsApiController.GetFormations(1, idStatuts, true, 1, 5, "formation", null, null, null).Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbFormation, (okResult.Value as IEnumerable).Count()); Assert.AreEqual(idFirstFormation, (okResult.Value as IEnumerable).First().Id); Assert.AreEqual(idLastFormation, (okResult.Value as IEnumerable).Last().Id); } #endregion #region Tests GetFormationsCount [Test] public void GetFormationsCount_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); List idStatuts = new List { 1, 2, 3 }; // Act var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetFormationsCount_PasseDesParamsPresentsDansLaBDD_RetourneLeBonNombreDeFormation() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); List idStatuts = new List { 1, 2, 3 }; int nbFormation = 5; // Act var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null).Result as OkObjectResult; // Assert Assert.IsInstanceOf(okResult.Value); Assert.AreEqual(nbFormation, (long)okResult.Value); } #endregion #region Tests GetModesFormation [Test] public void GetModesFormation_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = formationsApiController.GetModesFormation(); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetModesFormation_RetourneTousLesModes() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int nbModeFormation = 4; // Act var okResult = formationsApiController.GetModesFormation().Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbModeFormation, (okResult.Value as IEnumerable).Count()); } #endregion #region Tests GetOriginesFormation [Test] public void GetOriginesFormation_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = formationsApiController.GetOriginesFormation(); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetOriginesFormation_RetourneToutesLesOrigines() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int nbOrigineFormation = 4; // Act var okResult = formationsApiController.GetOriginesFormation().Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbOrigineFormation, (okResult.Value as IEnumerable).Count()); } #endregion #region Tests GetStatutsFormation [Test] public void GetStatusFormation_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = formationsApiController.GetStatutsFormation(); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetStatutsFormation_RetourneTousLesStatuts() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int nbStatutFormation = 4; // Act var okResult = formationsApiController.GetStatutsFormation().Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbStatutFormation, (okResult.Value as IEnumerable).Count()); } #endregion #region Tests GetTypesFormation [Test] public void GetTypesFormation_RetourneUnObjetOkResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = formationsApiController.GetTypesFormation(); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetTypesFormation_RetourneTousLesTypes() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int nbTypeFormation = 4; // Act var okResult = formationsApiController.GetTypesFormation().Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbTypeFormation, (okResult.Value as IEnumerable).Count()); } #endregion #region Tests AddFormation [Test] public void AddFormation_AjouteUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 0, Libelle = "Externe" }; StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }; TypeFormationDTO typeELearning = new TypeFormationDTO { Id = 3, Libelle = "E-learning" }; OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = 1, Libelle = "Demande collaborateur" }; FormationDTO formation = new FormationDTO { Intitule = "Test Formation", IdAgence = 1, DateDebut = new DateTime(2020, 10, 31), DateFin = new DateTime(2020, 11, 02), Heure = 2, Jour = 1, Mode = modeExterne, Type = typeELearning, Organisme = "Apside", Origine = origineFormationCollaborateur, Statut = statutPlanifie, EstCertifiee = false }; // Act var objectResult = formationsApiController.AddFormation(formation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public void AddFormation_AjouteUneFormationValide_RetourneUnObjetCreatedResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 1, Libelle = "Externe" }; StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }; TypeFormationDTO typeELearning = new TypeFormationDTO { Id = 3, Libelle = "E-learning" }; OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = 1, Libelle = "Demande collaborateur" }; FormationDTO formation = new FormationDTO { Intitule = "Test Formation", IdAgence = 1, DateDebut = new DateTime(2020, 10, 31), DateFin = new DateTime(2020, 11, 02), Heure = 2, Jour = 1, Mode = modeExterne, Type = typeELearning, Organisme = "Apside", Origine = origineFormationCollaborateur, Statut = statutPlanifie, EstCertifiee = false }; // Act var createdResult = formationsApiController.AddFormation(formation); // Assert Assert.IsInstanceOf(createdResult.Result); } [Test] public void AddFormation_AjouteUneFormationValide_RetourneLaFormationCreee() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 1, Libelle = "Externe" }; StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }; TypeFormationDTO typeELearning = new TypeFormationDTO { Id = 3, Libelle = "E-learning" }; OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = 1, Libelle = "Demande collaborateur" }; FormationDTO formation = new FormationDTO { Intitule = "Test Formation", IdAgence = 1, DateDebut = new DateTime(2020, 10, 31), DateFin = new DateTime(2020, 11, 02), Heure = 2, Jour = 1, Mode = modeExterne, Type = typeELearning, Organisme = "Apside", Origine = origineFormationCollaborateur, Statut = statutPlanifie, EstCertifiee = false }; // Act var createdResult = formationsApiController.AddFormation(formation).Result as CreatedResult; // Assert Assert.IsInstanceOf(createdResult.Value); Assert.AreEqual("Test Formation", (createdResult.Value as FormationDTO).Intitule); } #endregion #region Tests UpdateFormation [Test] public async Task UpdateFormation_ModifieUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResultDansCatchFormationInvalidException() { // Arrange long idFormation = 1; string nouvelleIntitule = "Formation modifiée"; FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation); formation.Intitule = nouvelleIntitule; formation.Mode.Id = 0; formation.Statut.Id = 0; // Act var objectResult = formationsApiController.UpdateFormation(formation, idFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public async Task UpdateFormation_ModifieUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResultDansCatchFormationIncompatibleIdException() { // Arrange long idFormation = 1; string nouvelleIntitule = "Formation modifiée"; FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation); idFormation = 2; formation.Intitule = nouvelleIntitule; // Act var objectResult = formationsApiController.UpdateFormation(formation, idFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public async Task UpdateFormation_ModifieUneFormatioInexistante_RetourneUnObjetObjectResultDansCatchFormationNotFoundExceptionn() { // Arrange long idFormationExistant = 1; long idFormationInexistant = 999; string nouvelleIntitule = "Formation modifiée"; FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); FormationDTO formation = await formationService.GetFormationByIdAsync(idFormationExistant); formation.Id = idFormationInexistant; formation.Intitule = nouvelleIntitule; // Act var objectResult = formationsApiController.UpdateFormation(formation, idFormationInexistant); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public async Task UpdateFormation_ModifieUneFormationValide_RetourneUnObjetOkObjectResult() { // Arrange long idFormation = 1; string nouvelleIntitule = "Formation modifiée"; FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == 2) .Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault(); StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == 2) .Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault(); TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == 1) .Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault(); OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == 1) .Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault(); FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation); formation.Intitule = nouvelleIntitule; formation.Mode = modeExterne; formation.Type = typeELearning; formation.Origine = origineFormationCollaborateur; formation.Statut = statutPlanifie; // Act var okObjectResult = formationsApiController.UpdateFormation(formation, idFormation); // Assert Assert.IsInstanceOf(okObjectResult.Result); } [Test] public async Task UpdateFormation_ModifieUneFormationValide_RetourneLaFormationModifiee() { // Arrange long idFormation = 1; string nouvelleIntitule = "Formation modifiée"; FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == 2) .Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault(); StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == 2) .Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault(); TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == 1) .Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault(); OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == 1) .Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault(); FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation); formation.Intitule = nouvelleIntitule; formation.Mode = modeExterne; formation.Type = typeELearning; formation.Origine = origineFormationCollaborateur; formation.Statut = statutPlanifie; // Act var okObjectResult = formationsApiController.UpdateFormation(formation, idFormation).Result as OkObjectResult; // Assert Assert.IsInstanceOf(okObjectResult.Value); Assert.AreEqual(nouvelleIntitule, (okObjectResult.Value as FormationDTO).Intitule); } #endregion #region Tests DeleteFormationById [Test] public void DeleteFormation_PasseEnParamUnIdFormationInexistant_RetourneUnObjetNotFoundObjectResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int idFormation = 999; // Act var notFoundObjectResult = formationsApiController.DeleteFormation(idFormation); // Assert Assert.IsInstanceOf(notFoundObjectResult.Result); } [Test] public void DeleteFormation_PasseEnParamUnIdFormationExistant_RetourneUnObjetNoContentResult() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int idFormation = 1; // Act var noContentResult = formationsApiController.DeleteFormation(idFormation); // Assert Assert.IsInstanceOf(noContentResult.Result); } [Test] public async Task DeleteFormation_PasseEnParamUnIdFormationExistant_SupprimeBienUneFormation() { // Arrange FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); int idFormation = 1; long nbFormationAvantSuppression = await formationService.GetFormationsCountAsync(null, null, null, null, null, null, null); // Act var noContentResult = formationsApiController.DeleteFormation(idFormation).Result as NoContentResult; // Assert long nbFormationApresSuppression = await formationService.GetFormationsCountAsync(null, null, null, null, null, null, null); Assert.AreEqual(nbFormationAvantSuppression -1, nbFormationApresSuppression); } #endregion } }