From 23f8b6994067324c3f61ed016adfb8e3dcfab822 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Thu, 14 Jan 2021 10:39:43 +0100 Subject: [PATCH] Ajout des TU pour l'api formation --- .../Controllers/FormationApiTests.cs | 526 ++++++++++++++++-- 1 file changed, 471 insertions(+), 55 deletions(-) diff --git a/EPAServeur.Tests/Controllers/FormationApiTests.cs b/EPAServeur.Tests/Controllers/FormationApiTests.cs index bf8a76d..199b129 100644 --- a/EPAServeur.Tests/Controllers/FormationApiTests.cs +++ b/EPAServeur.Tests/Controllers/FormationApiTests.cs @@ -13,6 +13,11 @@ 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; namespace EPAServeur.Tests.Controllers { @@ -21,9 +26,9 @@ namespace EPAServeur.Tests.Controllers { #region Variables - private FormationService formationService; - private readonly ILogger logger; - + private IFormationService formationService; + private Mock mockEnvironment; + private EpContext epContext; #endregion #region Setup @@ -31,12 +36,17 @@ namespace EPAServeur.Tests.Controllers [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; - EpContext epContext = new EpContext(optionBuider); + services.AddDbContext(b => b.UseInMemoryDatabase("server_ep_test")); + + epContext = new EpContext(optionBuider); epContext.Database.EnsureDeleted(); epContext.Database.EnsureCreated(); @@ -52,8 +62,20 @@ namespace EPAServeur.Tests.Controllers entity.State = EntityState.Detached; } - // Instanciation du service qui sera utilisé dans le controleur - formationService = new FormationService(epContext); + + 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 @@ -61,127 +83,521 @@ namespace EPAServeur.Tests.Controllers #region Tests GetFormationById [Test] - public void GetFormationById_PasseEnParamUnIdPresentDansLaBDD_RetourneUnObjetOkResult() + public void GetById_PasseEnParamUnIdInconnu_RetourneUnObjetNotFoundResult() { - //// Arrange - //FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger()); + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); - // // Act - // var okResult = formationsApiController.GetFormationById(1); + // Act + var notFoundResult = formationsApiController.GetFormationById(99999); - //// Assert - //Assert.IsInstanceOf(okResult.Result); + // Assert + Assert.IsInstanceOf(notFoundResult.Result); } - - #endregion + [Test] + public void GetFormationById_PasseEnParamUnIdConnu_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); - #region Tests GetFormations + // Act + var okResult = formationsApiController.GetFormationById(1); - // Arrange + // Assert + Assert.IsInstanceOf(okResult.Result); + } - // Act - // Assert + [Test] + public void GetFormationById_PasseEnParamUnIdConnu_RetourneLaBonneFormation() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); + long idFormation = 1; - #endregion + // Act + var okResult = formationsApiController.GetFormationById(idFormation).Result as OkObjectResult; - #region Tests GetFormationAnnulees + // Assert + Assert.IsInstanceOf(okResult.Value); + Assert.AreEqual(idFormation, (okResult.Value as FormationDTO).Id); + } + #endregion - // Arrange + #region Tests GetFormations - // Act + [Test] + public void GetFormations_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); + List idStatuts = new List { 1, 2, 3 }; - // Assert + // Act + var okResult = formationsApiController.GetFormations(1, idStatuts, true, 1, 5, "formation", null, null, null); - #endregion + // Assert + Assert.IsInstanceOf(okResult.Result); + } - #region Tests GetFormationRealisee + [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 = 1; + int idLastFormation = 5; + + // 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); + } - // Arrange + #endregion - // Act + #region Tests GetFormationsCount - // Assert + [Test] + public void GetFormationsCount_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); + List idStatuts = new List { 1, 2, 3 }; - #endregion + // Act + var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null); - #region Tests GetProchainesFormation + // Assert + Assert.IsInstanceOf(okResult.Result); + } - // Arrange + [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 + // Act + var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null).Result as OkObjectResult; - // Assert + // Assert + Assert.IsInstanceOf(okResult.Value); + Assert.AreEqual(nbFormation, (long)okResult.Value); + } #endregion #region Tests GetModesFormation - // Arrange - // Act + [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); + } - // Assert + [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 - // Arrange + [Test] + public void GetOriginesFormation_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); - // Act + // Act + var okResult = formationsApiController.GetOriginesFormation(); - // Assert + // 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 - // Arrange + [Test] + public void GetStatusFormation_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); + + // Act + var okResult = formationsApiController.GetStatutsFormation(); - // Act + // Assert + Assert.IsInstanceOf(okResult.Result); + } - // Assert + [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 - // Arrange + [Test] + public void GetTypesFormation_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); - // Act + // Act + var okResult = formationsApiController.GetTypesFormation(); + + // Assert + Assert.IsInstanceOf(okResult.Result); + } - // Assert + [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 - // Arrange + [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); + } - // Act + [Test] + public void AddFormation_AjouteUneFormationValide_RetourneLaFormationCreee() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger(), mockEnvironment.Object); - // Assert + 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 - // Arrange + [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 + // Act + var objectResult = formationsApiController.UpdateFormation(formation, idFormation); - // Assert + // 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 - // Arrange + [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 + // 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); + } - // Assert #endregion }