diff --git a/EPAServeur.Tests/Services/ParticipationFormationServiceTests.cs b/EPAServeur.Tests/Services/ParticipationFormationServiceTests.cs new file mode 100644 index 0000000..cbe581c --- /dev/null +++ b/EPAServeur.Tests/Services/ParticipationFormationServiceTests.cs @@ -0,0 +1,310 @@ +using EPAServeur.Context; +using EPAServeur.Exceptions; +using EPAServeur.IServices; +using EPAServeur.Models.Formation; +using EPAServeur.Services; +using IO.Swagger.ApiCollaborateur; +using IO.Swagger.DTO; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Tests.Services +{ + [TestFixture] + public class ParticipationFormationServiceTests + { + #region Variables + + private EpContext epContext; + private ICollaborateurApi collaborateurApi; + private ICollaborateurService collaborateurService; + #endregion + + #region Setup + + [SetUp] + public void Setup() + { + // Utilisation d'une base de données en mémoire + var optionBuider = new DbContextOptionsBuilder() + .UseInMemoryDatabase("server_ep_test") + .Options; + + epContext = new EpContext(optionBuider); + collaborateurApi = new CollaborateurApi(); + collaborateurService = new CollaborateurService(collaborateurApi, epContext); + 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; + } + } + + #endregion + + #region Tests GetEvaluationCollaborateurAsync + [Test] + public async Task GetEvaluationCollaborateurAsync_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictif_RetourneUneEvaluation() + { + // Arrange + ParticipationFormationService participationFormationService = new ParticipationFormationService(epContext, collaborateurService); + int idParticipationFormation = 5; + // Act + EvaluationDTO evaluationDTO = await participationFormationService.GetEvaluationCollaborateurAsync(idParticipationFormation); + + // Assert + Assert.AreEqual(idParticipationFormation, evaluationDTO.Id); + Assert.AreEqual(new DateTime(2020, 5, 25, 14, 0, 0), evaluationDTO.DateDebut); + Assert.True(evaluationDTO.EstCertifie); + Assert.AreEqual("Apprendre C# et le développement de logiciels avec WPF", evaluationDTO.Intitule); + } + + [TestCase(-1)] + [TestCase(0)] + [TestCase(999999)] + public void GetEvaluationCollaborateurAsync_PasseEnParamUnIdInexistantDansLeJeuDeDonneesFictif_LeveUneParticipationFormationNotFoundException(long idParticipationFormation) + { + // Arrange + ParticipationFormationService participationFormationService = new ParticipationFormationService(epContext, collaborateurService); + + // Act + AsyncTestDelegate throwException = () => participationFormationService.GetEvaluationCollaborateurAsync(idParticipationFormation); + + // Assert + Assert.ThrowsAsync(typeof(ParticipationFormationNotFoundException), throwException); + } + + #endregion + + #region Tests GetParticipationByCollaborateur + + [TestCase("842650db-a548-4472-a3af-4c5fff3c1ab8")] + [TestCase("4f3fcd23-a1e4-4c9e-afa2-d06ca9216491")] + public async Task GetParticipationByCollaborateur_PasseDesParamsValides_RetourneDesParticipationsFormations(Guid idCollaborateur) + { + // Arrange + ParticipationFormationService participationFormationService = new ParticipationFormationService(epContext, collaborateurService); + + // Act + IEnumerable participationFormationDTOs = await participationFormationService.GetParticipationByCollaborateurAsync(idCollaborateur); + + // Assert + Assert.Less(0, participationFormationDTOs.Count()); + } + + [TestCase("e7820f92-eab1-42f5-ae96-5c16e71ff1e6")] + [TestCase("b5254c6c-7caa-435f-a4bb-e0cf92559832")] + public async Task GetFormationsAsync_PasseDesParamsInvalides_RetourneZeroFormation(Guid idCollaborateur) + { + // Arrange + ParticipationFormationService participationFormationService = new ParticipationFormationService(epContext, collaborateurService); + + // Act + IEnumerable participationFormationDTOs = await participationFormationService.GetParticipationByCollaborateurAsync(idCollaborateur); + + // Assert + Assert.AreEqual(0, participationFormationDTOs.Count()); + } + + #endregion + /* + #region Tests UpdateFormationAsync + + [TestCase(1, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + public async Task UpdateFormationAsync_ModifieUneFormationValide_FormationModifieeAvecSucces(long idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin) + { + // Arrange + ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType) + .Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext, collaborateurService); + + FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation); + + formation.Intitule = intitule; + formation.IdAgence = idAgence; + formation.DateDebut = dateDebut; + formation.DateFin = dateFin; + formation.Heure = 2; + formation.Jour = 1; + formation.Mode = modeExterne; + formation.Type = typeELearning; + formation.Organisme = organisme; + formation.Origine = origineFormationCollaborateur; + formation.Statut = statutPlanifie; + formation.EstCertifiee = false; + + + // Act + FormationDTO formationModifie = await formationService.UpdateFormationAsync(idFormation, formation); + + // Assert + Assert.IsNotNull(formationModifie); + Assert.AreEqual(idFormation, formationModifie.Id); + Assert.AreEqual(formation.Intitule, formationModifie.Intitule); + Assert.AreEqual(formation.IdAgence, formationModifie.IdAgence); + Assert.AreEqual(formation.DateDebut, formationModifie.DateDebut); + Assert.AreEqual(formation.DateFin, formationModifie.DateFin); + Assert.AreEqual(formation.Heure, formationModifie.Heure); + Assert.AreEqual(formation.Jour, formationModifie.Jour); + Assert.AreEqual(formation.Mode, formationModifie.Mode); + Assert.AreEqual(formation.Type, formationModifie.Type); + Assert.AreEqual(formation.Organisme, formationModifie.Organisme); + Assert.AreEqual(formation.Origine, formationModifie.Origine); + Assert.AreEqual(formation.Statut, formationModifie.Statut); + Assert.AreEqual(formation.EstCertifiee, formationModifie.EstCertifiee); + } + + [TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-10-30")] + [TestCase(1, 0, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 0, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 0, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 0, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 0, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 1, "", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, null, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 1, null, "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 1, "Test Formation", null, "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "Apside", null, "2020-11-02")] + [TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", null)] + public async Task UpdateFormationAsync_ModifieUneFormationAvecDesProprietesInvalides_LeveUneFormationInvalidException(long idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin) + { + + // Arrange + ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType) + .Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext, collaborateurService); + + FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation); + + formation.Intitule = intitule; + formation.IdAgence = idAgence; + formation.DateDebut = dateDebut; + formation.DateFin = dateFin; + formation.Heure = 2; + formation.Jour = 1; + formation.Mode = modeExterne; + formation.Type = typeELearning; + formation.Organisme = organisme; + formation.Origine = origineFormationCollaborateur; + formation.Statut = statutPlanifie; + formation.EstCertifiee = false; + + // Act + AsyncTestDelegate throwException = () => formationService.UpdateFormationAsync(idFormation, formation); + + // Assert + Assert.ThrowsAsync(typeof(FormationInvalidException), throwException); + } + + [TestCase(2, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + [TestCase(0, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + [TestCase(null, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + public async Task UpdateFormationAsync_ModifieUneFormationAvecUnIdIncorrecte_LeveUneFormationIncompatibleIdException(long? idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin) + { + // Arrange + ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType) + .Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext, collaborateurService); + + FormationDTO formation = await formationService.GetFormationByIdAsync(1); + + formation.Id = idFormation; + formation.Intitule = intitule; + formation.IdAgence = idAgence; + formation.DateDebut = dateDebut; + formation.DateFin = dateFin; + formation.Heure = 2; + formation.Jour = 1; + formation.Mode = modeExterne; + formation.Type = typeELearning; + formation.Organisme = organisme; + formation.Origine = origineFormationCollaborateur; + formation.Statut = statutPlanifie; + formation.EstCertifiee = false; + + // Act + AsyncTestDelegate throwException = () => formationService.UpdateFormationAsync(1, formation); + + // Assert + Assert.ThrowsAsync(typeof(FormationIncompatibleIdException), throwException); + } + + [TestCase(0, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + public void UpdateFormationAsync_ModifieUneFormationAvecUnIdInexistant_RetourneUnObjetNull(long idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin) + { + // Arrange + ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType) + .Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext, collaborateurService); + + FormationDTO formation = new FormationDTO + { + Id = idFormation, + Intitule = intitule, + IdAgence = idAgence, + DateDebut = dateDebut, + DateFin = dateFin, + Heure = 2, + Jour = 1, + Mode = modeExterne, + Type = typeELearning, + Organisme = organisme, + Origine = origineFormationCollaborateur, + Statut = statutPlanifie, + EstCertifiee = false + }; + + // Act + AsyncTestDelegate throwException = () => formationService.UpdateFormationAsync(idFormation, formation); + + // Assert + Assert.ThrowsAsync(typeof(FormationNotFoundException), throwException); + } + + + #endregion + */ + + } +} \ No newline at end of file