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; using IO.Swagger.Enum; namespace EPAServeur.Tests.Controllers { [TestFixture] public class DemandeFormationApiTests { #region Variables private IDemandeFormationService demandeFormationService; 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(); services.AddScoped(); // Récupère le service qui sera utilsé pour tester le contrôleur var serviceProvider = services.BuildServiceProvider(); demandeFormationService = serviceProvider.GetService(); // Simule l'interface IWebHostEnvironment avec Moq mockEnvironment = new Mock(); mockEnvironment .Setup(m => m.EnvironmentName) .Returns("Development"); } #endregion #region Tests GetDemandesFormation [Test] public void GetDemandesFormation_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = DemandesFormationApiController.GetDemandesFormation(null, null, null, null, null, null, null, null, null); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetDemandesFormation_PasseDesParamsPresentsDansLaBDD_RetourneLesCinqPremieresDemandesDeFormations() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); int nbDemandeFormation = 5; int idFirstDemandeFormation = 1; int idLastDemandeFormation = 3; // Act var okResult = DemandesFormationApiController.GetDemandesFormation(null, null, null, 1, 5, null, null, null, null).Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbDemandeFormation, (okResult.Value as IEnumerable).Count()); Assert.AreEqual(idFirstDemandeFormation, (okResult.Value as IEnumerable).First().Id); Assert.AreEqual(idLastDemandeFormation, (okResult.Value as IEnumerable).Last().Id); } #endregion #region Tests GetDemandesFormationCount [Test] public void GetDemandesFormationCount_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = DemandesFormationApiController.GetDemandesFormationCount(null, null, null, null, null); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetDemandesFormationCount_PasseDesParamsPresentsDansLaBDD_RetourneLeBonNombreDeDemandeDeFormation() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); int nbDemandeFormation = 10; // Act var okResult = DemandesFormationApiController.GetDemandesFormationCount(null, null, null, null, null).Result as OkObjectResult; // Assert Assert.IsInstanceOf(okResult.Value); Assert.AreEqual(nbDemandeFormation, (long)okResult.Value); } #endregion #region Tests GetOriginesDemandeFormation [Test] public void GetOriginesDemandeFormation_RetourneUnObjetOkResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); // Act var okResult = DemandesFormationApiController.GetOriginesDemandeFormation(); // Assert Assert.IsInstanceOf(okResult.Result); } [Test] public void GetOriginesDemandeFormation_RetourneToutesLesOriginesDeDemande() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); int nbOrigineDemande = 5; // Act var okResult = DemandesFormationApiController.GetOriginesDemandeFormation().Result as OkObjectResult; // Assert Assert.IsInstanceOf>(okResult.Value); Assert.AreEqual(nbOrigineDemande, (okResult.Value as IEnumerable).Count()); } #endregion #region Tests AddDemandeFormation [Test] public void AddDemandeFormation_AjouteUneDemandeDeFormationAvecUnCollaborateurNull_RetourneUnObjetObjectResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); OrigineDemandeFormationDTO origineDemandeFormationApside = new OrigineDemandeFormationDTO { Id = 5, Libelle = "Demande Apside" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = null; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Libelle = "Formation React", Description = "Demande de formation React avec Redux", DemandeRH = true, DateDemande = DateTime.Now, EtatDemande = EtatDemande.EnAttente, Origine = origineDemandeFormationApside, Collaborateur = collaborateur, }; // Act var objectResult = DemandesFormationApiController.AddDemandeFormation(demandeFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public void AddDemandeFormation_AjouteUneDemandeDeFormationValide_RetourneUnObjetCreatedResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); OrigineDemandeFormationDTO origineDemandeFormationApside = new OrigineDemandeFormationDTO { Id = 5, Libelle = "Demande Apside" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("842650db-a548-4472-a3af-4c5fff3c1ab8"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Lemoine", Prenom = "Coty", MailApside = "coty.lemoine@apside-groupe.com", DateArrivee = new DateTime(2017, 2, 10, 20, 37, 58, 741), DateDepart = null, }; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Libelle = "Formation React", Description = "Demande de formation React avec Redux", DemandeRH = true, DateDemande = DateTime.Now, EtatDemande = EtatDemande.EnAttente, Origine = origineDemandeFormationApside, Collaborateur = collaborateur, }; // Act var createdResult = DemandesFormationApiController.AddDemandeFormation(demandeFormation); // Assert Assert.IsInstanceOf(createdResult.Result); } [Test] public void AddDemandeFormation_AjouteUneDemandeDeFormationValide_RetourneLaDemandeDeFormationCreee() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); OrigineDemandeFormationDTO origineDemandeFormationApside = new OrigineDemandeFormationDTO { Id = 5, Libelle = "Demande Apside" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("842650db-a548-4472-a3af-4c5fff3c1ab8"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Lemoine", Prenom = "Coty", MailApside = "coty.lemoine@apside-groupe.com", DateArrivee = new DateTime(2017, 2, 10, 20, 37, 58, 741), DateDepart = null, }; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Libelle = "Formation React", Description = "Demande de formation React avec Redux", DemandeRH = true, DateDemande = DateTime.Now, EtatDemande = EtatDemande.EnAttente, Origine = origineDemandeFormationApside, Collaborateur = collaborateur, }; // Act var createdResult = DemandesFormationApiController.AddDemandeFormation(demandeFormation).Result as CreatedResult; // Assert Assert.IsInstanceOf(createdResult.Value); Assert.AreEqual("Formation React", (createdResult.Value as DemandeFormationDTO).Libelle); } #endregion #region Tests UpdateDemandeFormation [Test] public async Task UpdateDemandeFormation_AccepteUneDemandeAvecUneFormationNull_RetourneUnObjetObjectResultDansCatchDemandeFormationInvalidException() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 3; OrigineDemandeFormationDTO origineDemandeFormationClient = new OrigineDemandeFormationDTO { Id = 3, Libelle = "Exigence Client" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("a0f40e2a-cc03-4032-a627-5389e1281c64"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Vasseur", Prenom = "Florencio", MailApside = "florencio.vasseur@apside-groupe.com", DateArrivee = new DateTime(2016, 1, 15, 10, 22, 30, 778), DateDepart = null, }; FormationDetailsDTO formation = null; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Id = idDemandeFormation, Libelle = "Formation C#", Description = "Demande de formation C# avec WPF", DemandeRH = false, DateDemande = new DateTime(2020, 3, 22, 9, 0, 0), EtatDemande = EtatDemande.Validee, Origine = origineDemandeFormationClient, Collaborateur = collaborateur, Formation = formation, }; // Act var objectResult = DemandesFormationApiController.UpdateDemandeFormation(demandeFormation, idDemandeFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public async Task UpdateDemandeFormation_AccepteUneDemandeAvecUnIdDemandeFormationNull_RetourneUnObjetObjectResultDansCatchDemandeFormationIncompatibleIdException() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 1; long? idDemandeFormationIncorrecte = null; int nbParticipant = 1; OrigineDemandeFormationDTO origineDemandeFormationClient = new OrigineDemandeFormationDTO { Id = 3, Libelle = "Exigence Client" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("a0f40e2a-cc03-4032-a627-5389e1281c64"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Vasseur", Prenom = "Florencio", MailApside = "florencio.vasseur@apside-groupe.com", DateArrivee = new DateTime(2016, 1, 15, 10, 22, 30, 778), DateDepart = null, }; FormationDetailsDTO formation = new FormationDetailsDTO { Id = 3, Intitule = "Apprendre C# et le développement de logiciels avec WPF", DateDebut = new DateTime(2020, 5, 25, 14, 0, 0), DateFin = new DateTime(2020, 5, 27), Organisme = "Organisme2", Origine = new OrigineFormationDTO { Id = 3, Libelle = "Exigence Apside" }, Statut = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }, EstCertifiee = true, NbParticipations = nbParticipant }; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Id = idDemandeFormationIncorrecte, Libelle = "Formation C#", Description = "Demande de formation C# avec WPF", DemandeRH = false, DateDemande = new DateTime(2020, 3, 22, 9, 0, 0), EtatDemande = EtatDemande.Validee, Origine = origineDemandeFormationClient, Collaborateur = collaborateur, Formation = formation, }; // Act var objectResult = DemandesFormationApiController.UpdateDemandeFormation(demandeFormation, idDemandeFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public async Task UpdateDemandeFormation_AccepteUneDemandeInexistante_RetourneUnObjetObjectResultDansCatchDemandeFormationNotFoundExceptionn() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 0; int nbParticipant = 1; OrigineDemandeFormationDTO origineDemandeFormationClient = new OrigineDemandeFormationDTO { Id = 3, Libelle = "Exigence Client" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("a0f40e2a-cc03-4032-a627-5389e1281c64"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Vasseur", Prenom = "Florencio", MailApside = "florencio.vasseur@apside-groupe.com", DateArrivee = new DateTime(2016, 1, 15, 10, 22, 30, 778), DateDepart = null, }; FormationDetailsDTO formation = new FormationDetailsDTO { Id = 3, Intitule = "Apprendre C# et le développement de logiciels avec WPF", DateDebut = new DateTime(2020, 5, 25, 14, 0, 0), DateFin = new DateTime(2020, 5, 27), Organisme = "Organisme2", Origine = new OrigineFormationDTO { Id = 3, Libelle = "Exigence Apside" }, Statut = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }, EstCertifiee = true, NbParticipations = nbParticipant }; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Id = idDemandeFormation, Libelle = "Formation C#", Description = "Demande de formation C# avec WPF", DemandeRH = false, DateDemande = new DateTime(2020, 3, 22, 9, 0, 0), EtatDemande = EtatDemande.Validee, Origine = origineDemandeFormationClient, Collaborateur = collaborateur, Formation = formation, }; // Act var objectResult = DemandesFormationApiController.UpdateDemandeFormation(demandeFormation, idDemandeFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public async Task UpdateDemandeFormation_AccepteUneDemandeDeDemandeFormation_RetourneUnObjetOkObjectResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 1; int nbParticipant = 0; OrigineDemandeFormationDTO origineDemandeFormationApside = new OrigineDemandeFormationDTO { Id = 5, Libelle = "Demande Apside" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("842650db-a548-4472-a3af-4c5fff3c1ab8"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Lemoine", Prenom = "Coty", MailApside = "coty.lemoine@apside-groupe.com", DateArrivee = new DateTime(2017, 2, 10, 20, 37, 58, 741), DateDepart = null, }; FormationDetailsDTO formation = new FormationDetailsDTO { Id = 1, Intitule = "Formation Mainframe Complète", DateDebut = new DateTime(2020, 1, 25, 10, 0, 0), DateFin = new DateTime(2020, 1, 27), Organisme = "Organisme1", Origine = new OrigineFormationDTO { Id = 2, Libelle = "Exigence client" }, Statut = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }, EstCertifiee = false, NbParticipations = nbParticipant }; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Id = idDemandeFormation, Libelle = "Formation Cobol", Description = "Demande de formation Cobol avec Mainframe", DemandeRH = false, DateDemande = new DateTime(2020, 1, 22, 9, 0, 0), EtatDemande = EtatDemande.Validee, Origine = origineDemandeFormationApside, Collaborateur = collaborateur, Formation = formation, }; // Act var okObjectResult = DemandesFormationApiController.UpdateDemandeFormation(demandeFormation, idDemandeFormation); // Assert Assert.IsInstanceOf(okObjectResult.Result); } [Test] public async Task UpdateDemandeFormation_AccepteUneDemandeDeDemandeFormation_RetourneLaDemandeDeFormationAcceptee() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 1; int nbParticipant = 0; OrigineDemandeFormationDTO origineDemandeFormationApside = new OrigineDemandeFormationDTO { Id = 5, Libelle = "Demande Apside" }; List Bus = new List() { new BusinessUnitDTO { Id = 1, Nom = "Tours" }, new BusinessUnitDTO { Id = 2, Nom = "Orléans" }, }; CollaborateurDTO collaborateur = new CollaborateurDTO { Id = Guid.Parse("842650db-a548-4472-a3af-4c5fff3c1ab8"), BusinessUnit = new BusinessUnitDTO { Id = 2, Nom = "Orléans", Agence = new AgenceDTO { Id = 1, Nom = "TOP", Bu = Bus } }, Nom = "Lemoine", Prenom = "Coty", MailApside = "coty.lemoine@apside-groupe.com", DateArrivee = new DateTime(2017, 2, 10, 20, 37, 58, 741), DateDepart = null, }; FormationDetailsDTO formation = new FormationDetailsDTO { Id = 1, Intitule = "Formation Mainframe Complète", DateDebut = new DateTime(2020, 1, 25, 10, 0, 0), DateFin = new DateTime(2020, 1, 27), Organisme = "Organisme1", Origine = new OrigineFormationDTO { Id = 2, Libelle = "Exigence client" }, Statut = new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }, EstCertifiee = false, NbParticipations = nbParticipant }; DemandeFormationDTO demandeFormation = new DemandeFormationDTO { Id = idDemandeFormation, Libelle = "Formation Cobol", Description = "Demande de formation Cobol avec Mainframe", DemandeRH = false, DateDemande = new DateTime(2020, 1, 22, 9, 0, 0), EtatDemande = EtatDemande.Validee, Origine = origineDemandeFormationApside, Collaborateur = collaborateur, Formation = formation, }; // Act var okObjectResult = DemandesFormationApiController.UpdateDemandeFormation(demandeFormation, idDemandeFormation).Result as OkObjectResult; // Assert Assert.IsInstanceOf(okObjectResult.Value); Assert.AreEqual("Formation Cobol", (okObjectResult.Value as DemandeFormationDTO).Libelle); } #endregion #region Tests DeleteDemandeFormation [Test] public void DeleteDemandeFormation_SupprimeUneDemandeDeFormationInexistante_RetourneUnObjetNotFoundObjectResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 0; // Act var notFoundObjectResult = DemandesFormationApiController.DeleteDemandeFormation(idDemandeFormation); // Assert Assert.IsInstanceOf(notFoundObjectResult.Result); } [Test] public void DeleteDemandeFormation_SupprimeUneDemandeDeFormationConcernantUnEpSigne_RetourneUnObjetObjectResultDansCatchDemandeFormationInvalidException() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 2; // Act var objectResult = DemandesFormationApiController.DeleteDemandeFormation(idDemandeFormation); // Assert Assert.IsInstanceOf(objectResult.Result); } [Test] public void DeleteDemandeFormation_SupprimeUneDemandeDeFormation_RetourneUnObjetNoContentResult() { // Arrange DemandesFormationApiController DemandesFormationApiController = new DemandesFormationApiController(demandeFormationService, new NullLogger(), mockEnvironment.Object); long idDemandeFormation = 5; // Act var noContentResult = DemandesFormationApiController.DeleteDemandeFormation(idDemandeFormation); // Assert Assert.IsInstanceOf(noContentResult.Result); } #endregion } }