From 56c878e0f87b1e868592673c07bf607ee62c5f49 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 3 Nov 2020 14:10:20 +0100 Subject: [PATCH 01/12] =?UTF-8?q?FormationService=20tests=20unitaires=20te?= =?UTF-8?q?rmin=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FormationServiceTests.cs | 1230 ++++++++++++++++- 1 file changed, 1202 insertions(+), 28 deletions(-) diff --git a/EPAServeur.Tests/Services/FormationServiceTests.cs b/EPAServeur.Tests/Services/FormationServiceTests.cs index 52de24a..d99c6a6 100644 --- a/EPAServeur.Tests/Services/FormationServiceTests.cs +++ b/EPAServeur.Tests/Services/FormationServiceTests.cs @@ -19,21 +19,32 @@ namespace EPAServeur.Tests [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); - epContext.Database.EnsureDeleted(); + 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; + } } + #region Tests GetFormationById et GetFormationByIdAsync + [Test] - public void GetFormationById_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictifs_RetourneUneFormation() + public void GetFormationById_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictif_RetourneUneFormation() { // Arrange FormationService formationService = new FormationService(epContext); @@ -61,7 +72,7 @@ namespace EPAServeur.Tests [TestCase(0)] [TestCase(999999)] [TestCase(null)] - public void GetFormationById_PasseEnParamUnIdInexistantDansLeJeuDeDonneesFictifs_LeveUneFormationNotFoundException(long? value) + public void GetFormationById_PasseEnParamUnIdInexistantDansLeJeuDeDonneesFictif_LeveUneFormationNotFoundException(long? value) { // Arrange FormationService formationService = new FormationService(epContext); @@ -74,7 +85,7 @@ namespace EPAServeur.Tests } [Test] - public async Task GetFormationByIdAsync_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictifs_RetourneUneFormation() + public async Task GetFormationByIdAsync_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictif_RetourneUneFormation() { // Arrange FormationService formationService = new FormationService(epContext); @@ -102,44 +113,1207 @@ namespace EPAServeur.Tests [TestCase(0)] [TestCase(999999)] [TestCase(null)] - public void GetFormationByIdAsync_PasseEnParamUnIdInexistantDansLeJeuDeDonneesFictifs_LeveUneFormationNotFoundException(long? value) + public void GetFormationByIdAsync_PasseEnParamUnIdInexistantDansLeJeuDeDonneesFictif_LeveUneFormationNotFoundException(long? value) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + AsyncTestDelegate throwException = () => formationService.GetFormationByIdAsync(value); + + // Assert + Assert.ThrowsAsync(typeof(FormationNotFoundException), throwException); + } + + #endregion + + #region Tests GetFormations et GetFormationsAsync + + [TestCase(true, 1, 5, 1, 1, "formation", null)] + [TestCase(false, 1, 5, 1, 1, "formation1", null)] + [TestCase(null, 2, 10, null, null, null, null)] + [TestCase(null, 1, 10, null, 1, null, null)] + [TestCase(null, 1, 10, 1, null, "for", null)] + public void GetFormations_PasseDesParamsValides_RetourneDesFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetFormations(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, 0, null, null)] + public void GetFormations_PasseDesParamsInvalides_RetourneZeroFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetFormations(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 5, 1, 1, "formation", null)] + [TestCase(false, 1, 5, 1, 1, "formation1", null)] + [TestCase(null, 2, 10, null, null, null, null)] + [TestCase(null, 1, 10, null, 1, null, null)] + [TestCase(null, 1, 10, 1, null, "for", null)] + public async Task GetFormationsAsync_PasseDesParamsValides_RetourneDesFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, 0, null, null)] + public async Task GetFormationsAsync_PasseDesParamsInvalides_RetourneZeroFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + #endregion + + #region Tests GetFormationAnnulees et GetFormationAnnuleesAsync + + [TestCase(true, 1, 5, 1, "formation", null)] + [TestCase(false, 1, 5, 1, "formation10", null)] + [TestCase(null, 2, 1, null, null, null)] + [TestCase(null, 1, 10, null, null, null)] + [TestCase(null, 1, 10, 1, "for", null)] + public void GetFormationAnnulees_PasseDesParamsValides_RetourneDesFormationsAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetFormationAnnulees(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, null, null)] + public void GetFormationAnnulees_PasseDesParamsInvalides_RetourneZeroFormationAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetFormationAnnulees(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 5, 1, "formation", null)] + [TestCase(false, 1, 5, 1, "formation1", null)] + [TestCase(null, 2, 1, null, null, null)] + [TestCase(null, 1, 10, null, null, null)] + [TestCase(null, 1, 10, 1, "for", null)] + public async Task GetFormationAnnuleesAsync_PasseDesParamsValides_RetourneDesFormationsAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetFormationAnnuleesAsync(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, null, null)] + public async Task GetFormationAnnuleesAsync_PasseDesParamsInvalides_RetourneZeroFormationAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetFormationAnnuleesAsync(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + #endregion + + #region Tests GetFormationRealisee et GetFormationRealiseeAsync + + [TestCase(true, 1, 5, 1, "formation", null)] + [TestCase(false, 1, 5, 1, "formation4", null)] + [TestCase(null, 2, 1, null, null, null)] + [TestCase(null, 1, 10, null, null, null)] + [TestCase(null, 1, 10, 1, "for", null)] + public void GetFormationRealisee_PasseDesParamsValides_RetourneDesFormationsRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetFormationRealisees(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, null, null)] + public void GetFormationRealisee_PasseDesParamsInvalides_RetourneZeroFormationRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetFormationRealisees(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 5, 1, "formation", null)] + [TestCase(false, 1, 5, 1, "formation4", null)] + [TestCase(null, 2, 1, null, null, null)] + [TestCase(null, 1, 10, null, null, null)] + [TestCase(null, 1, 10, 1, "for", null)] + public async Task GetFormationRealiseeAsync_PasseDesParamsValides_RetourneDesFormationsRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetFormationRealiseesAsync(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, null, null)] + public async Task GetFormationRealiseeAsync_PasseDesParamsInvalides_RetourneZeroFormationRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetFormationRealiseesAsync(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + #endregion + + #region Tests GetProchainesFormation et GetProchainesFormationAsync + + [TestCase(true, 1, 5, 1, "formation", null)] + [TestCase(false, 1, 5, 1, "formation1", null)] + [TestCase(null, 2, 1, null, null, null)] + [TestCase(null, 1, 10, null, null, null)] + [TestCase(null, 1, 10, 1, "for", null)] + public void GetProchainesFormation_PasseDesParamsValides_RetourneDesProchainesFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetProchainesFormation(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, null, null)] + public void GetProchainesFormation_PasseDesParamsInvalides_RetourneZeroProchaineFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = formationService.GetProchainesFormation(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 5, 1, "formation", null)] + [TestCase(false, 1, 5, 1, "formation1", null)] + [TestCase(null, 2, 1, null, null, null)] + [TestCase(null, 1, 10, null, null, null)] + [TestCase(null, 1, 10, 1, "for", null)] + public async Task GetProchainesFormationAsync_PasseDesParamsValides_RetourneDesProchainesFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetProchainesFormationAsync(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.Less(0, formationDTOs.Count()); + } + + [TestCase(true, 1, 15, 1, "azerty", null)] + [TestCase(null, -1, -1, -1, "azerty", null)] + [TestCase(null, 0, 0, 0, null, null)] + public async Task GetProchainesFormationAsync_PasseDesParamsInvalides_RetourneZeroProchaineFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable formationDTOs = await formationService.GetProchainesFormationAsync(asc, numPage, parPAge, idAgence, texte, tri); + + // Assert + Assert.AreEqual(0, formationDTOs.Count()); + } + + #endregion + + #region Tests GetModesFormation et GetModesFormationAsync + + [Test] + public void GetModesFormation_RetourneTousLesModesDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable modeFormationDTOs = formationService.GetModesFormation(); + + // Assert + Assert.IsNotNull(modeFormationDTOs); + Assert.AreEqual(4, modeFormationDTOs.Count()); // Nombre total de mode de formation dans la classe DataSeeder le 2020-10-31 + } + + [Test] + public async Task GetModesFormationAsync_RetourneTousLesModesDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable modeFormationDTOs = await formationService.GetModesFormationAsync(); + + // Assert + Assert.IsNotNull(modeFormationDTOs); + Assert.AreEqual(4, modeFormationDTOs.Count()); // Nombre total de mode de formation dans la classe DataSeeder le 2020-10-31 + } + + #endregion + + #region Tests GetOriginesFormation et GetOriginesFormationAsync + + [Test] + public void GetOriginesFormation_RetourneToutesLesOriginesDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable origineFormationDTOs = formationService.GetOriginesFormation(); + + // Assert + Assert.IsNotNull(origineFormationDTOs); + Assert.AreEqual(4, origineFormationDTOs.Count()); // Nombre total d'origine de formation dans la classe DataSeeder le 2020-10-31 + } + + [Test] + public async Task GetOriginesAsyncFormation_RetourneToutesLesOriginesDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable origineFormationDTOs = await formationService.GetOriginesFormationAsync(); + + // Assert + Assert.IsNotNull(origineFormationDTOs); + Assert.AreEqual(4, origineFormationDTOs.Count()); // Nombre total d'origine de formation dans la classe DataSeeder le 2020-10-31 + } + + #endregion + + #region Tests GetStatutsFormation et GetStatutsFormationAsync + + [Test] + public void GetStatutsFormation_RetourneTousLesStatutsDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable statutFormationDTOs = formationService.GetStatutsFormation(); + + // Assert + Assert.IsNotNull(statutFormationDTOs); + Assert.AreEqual(4, statutFormationDTOs.Count()); // Nombre total de statut de formation dans la classe DataSeeder le 2020-10-31 + } + + [Test] + public async Task GetStatutsFormationAsyncFormation_RetourneTousLesStatutsDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable statutFormationDTOs = await formationService.GetStatutsFormationAsync(); + + // Assert + Assert.IsNotNull(statutFormationDTOs); + Assert.AreEqual(4, statutFormationDTOs.Count()); // Nombre total de statut de formation dans la classe DataSeeder le 2020-10-31 + } + + #endregion + + #region Tests GetTypesFormation et GetTypesFormationAsync + + [Test] + public void GetTypesFormation_RetourneTousLesTypesDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable typeFormationDTOs = formationService.GetTypesFormation(); + + // Assert + Assert.IsNotNull(typeFormationDTOs); + Assert.AreEqual(4, typeFormationDTOs.Count()); // Nombre total de type de formation dans la classe DataSeeder le 2020-10-31 + } + + [Test] + public async Task GetTypesFormationAsync_RetourneTousLesTypesDeFormation() + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + IEnumerable typeFormationDTOs = await formationService.GetTypesFormationAsync(); + + // Assert + Assert.IsNotNull(typeFormationDTOs); + Assert.AreEqual(4, typeFormationDTOs.Count()); // Nombre total de type de formation dans la classe DataSeeder le 2020-10-31 + } + + #endregion + + #region Tests AddFormation et AddFormationAsync + + [TestCase(1, 1, 3, 1, 1, "Test ajout formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test ajout formation", "Apside", "2020-10-31", "2020-10-31")] + [TestCase(1, 1, 3, 1, 1, "Test ajout formation", "Apside", "2020-11-02", "2020-11-02")] + public void AddFormation_AjouteUneFormationValide_FormationAjouteeAvecSucces(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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationDTO formation = new FormationDTO + { + Intitule = intitule, + IdAgence = idAgence, + DateDebut = dateDebut, + DateFin = dateFin, + Heure = 2, + Jour = 1, + Mode = modeExterne, + Type = typeELearning, + Organisme = organisme, + Origine = origineFormationCollaborateur, + Statut = statutPlanifie, + EstCertifie = false + }; + + FormationService formationService = new FormationService(epContext); + + // Act + FormationDTO formationAjoute = formationService.AddFormation(formation); + + // Assert + Assert.IsNotNull(formationAjoute); + Assert.AreEqual(12, formationAjoute.Id); + Assert.AreEqual(formation.Intitule, formationAjoute.Intitule); + Assert.AreEqual(formation.IdAgence, formationAjoute.IdAgence); + Assert.AreEqual(formation.DateDebut, formationAjoute.DateDebut); + Assert.AreEqual(formation.DateFin, formationAjoute.DateFin); + Assert.AreEqual(formation.Heure, formationAjoute.Heure); + Assert.AreEqual(formation.Jour, formationAjoute.Jour); + Assert.AreEqual(formation.Mode, formationAjoute.Mode); + Assert.AreEqual(formation.Type, formationAjoute.Type); + Assert.AreEqual(formation.Organisme, formationAjoute.Organisme); + Assert.AreEqual(formation.Origine, formationAjoute.Origine); + Assert.AreEqual(formation.Statut, formationAjoute.Statut); + Assert.AreEqual(formation.EstCertifie, formationAjoute.EstCertifie); + } + + [TestCase(1, 1, 3, 1, 1, "Test ajout formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test ajout formation", "Apside", "2020-10-31", "2020-10-31")] + [TestCase(1, 1, 3, 1, 1, "Test ajout formation", "Apside", "2020-11-02", "2020-11-02")] + public async Task AddFormationAsync_AjouteUneFormationValide_FormationAjouteeAvecSucces(int? idMode, int? idStatut, int? idType, long? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin) + { + // Arrange + ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationDTO formation = new FormationDTO + { + Intitule = intitule, + IdAgence = idAgence, + DateDebut = dateDebut, + DateFin = dateFin, + Heure = 2, + Jour = 1, + Mode = modeExterne, + Type = typeELearning, + Organisme = organisme, + Origine = origineFormationCollaborateur, + Statut = statutPlanifie, + EstCertifie = false + }; + + FormationService formationService = new FormationService(epContext); + + // Act + FormationDTO formationAjoute = await formationService.AddFormationAsync(formation); + + // Assert + Assert.IsNotNull(formationAjoute); + Assert.AreEqual(12, formationAjoute.Id); + Assert.AreEqual(formation.Intitule, formationAjoute.Intitule); + Assert.AreEqual(formation.IdAgence, formationAjoute.IdAgence); + Assert.AreEqual(formation.DateDebut, formationAjoute.DateDebut); + Assert.AreEqual(formation.DateFin, formationAjoute.DateFin); + Assert.AreEqual(formation.Heure, formationAjoute.Heure); + Assert.AreEqual(formation.Jour, formationAjoute.Jour); + Assert.AreEqual(formation.Mode, formationAjoute.Mode); + Assert.AreEqual(formation.Type, formationAjoute.Type); + Assert.AreEqual(formation.Organisme, formationAjoute.Organisme); + Assert.AreEqual(formation.Origine, formationAjoute.Origine); + Assert.AreEqual(formation.Statut, formationAjoute.Statut); + Assert.AreEqual(formation.EstCertifie, formationAjoute.EstCertifie); + } + + [Test] + public void AddFormation_AjouteUneFormationNull_LeveUneFormationInvalidException() + { + + // Arrange + FormationDTO formation = null; + + FormationService formationService = new FormationService(epContext); + + // Act + TestDelegate throwException = () => formationService.AddFormation(formation); + + // Assert + Assert.Throws(typeof(FormationInvalidException), throwException); + } + + [Test] + public void AddFormationAsync_AjouteUneFormationNull_LeveUneFormationInvalidException() + { + + // Arrange + FormationDTO formation = null; + + FormationService formationService = new FormationService(epContext); + + // Act + AsyncTestDelegate throwException = () => formationService.AddFormationAsync(formation); + + // Assert + Assert.ThrowsAsync(typeof(FormationInvalidException), throwException); + } + + [TestCase(1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-10-30")] + [TestCase(0, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 0, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 0, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 0, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 0, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test Formation", "", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, null, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, null, "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test Formation", null, "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test Formation", "Apside", null, "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", null)] + public void AddFormation_AjouteUneFormationAvecDesProprietesInvalides_LeveUneFormationInvalidException(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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationDTO formation = new FormationDTO + { + Intitule = intitule, + IdAgence = idAgence, + DateDebut = dateDebut, + DateFin = dateFin, + Heure = 2, + Jour = 1, + Mode = modeExterne, + Type = typeELearning, + Organisme = organisme, + Origine = origineFormationCollaborateur, + Statut = statutPlanifie, + EstCertifie = false + }; + + FormationService formationService = new FormationService(epContext); + + // Act + TestDelegate throwException = () => formationService.AddFormation(formation); + + // Assert + Assert.Throws(typeof(FormationInvalidException), throwException); + } + + [TestCase(1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-10-30")] + [TestCase(0, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 0, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 0, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 0, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 0, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test Formation", "", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, null, "Test Formation", "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, null, "Apside", "2020-10-31", "2020-11-02")] + [TestCase(1, 1, 3, 1, 1, "Test Formation", null, "2020-10-31", "2020-11-02")] + public void AddFormationAsync_AjouteUneFormationAvecDesProprietesInvalides_LeveUneFormationInvalidException(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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationDTO formation = new FormationDTO + { + Intitule = intitule, + IdAgence = idAgence, + DateDebut = dateDebut, + DateFin = dateFin, + Heure = 2, + Jour = 1, + Mode = modeExterne, + Type = typeELearning, + Organisme = organisme, + Origine = origineFormationCollaborateur, + Statut = statutPlanifie, + EstCertifie = false + }; + + FormationService formationService = new FormationService(epContext); + + // Act + AsyncTestDelegate throwException = () => formationService.AddFormationAsync(formation); + + // Assert + Assert.ThrowsAsync(typeof(FormationInvalidException), throwException); + } + + [TestCase(0, 1, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 0, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 0, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 0, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(null, 1, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, null, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, null, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, null, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externez", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externe", "Planifiées", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externe", "Planifiée", "E-learnings", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateurs")] + public void AddFormation_AjouteUneFormationAvecDesObjetsEnfantsInvalides_LeveUneFormationInvalidException(int? idMode, int? idStatut, int? idType, int? idOrigine, string libelleMode, string libelleStatut, string libelleType, string libelleOrigine) + { + + // Arrange + ModeFormationDTO modeExterne = new ModeFormationDTO { Id = idMode, Libelle = libelleMode }; + StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = idStatut, Libelle = libelleStatut }; + TypeFormationDTO typeELearning = new TypeFormationDTO { Id = idType, Libelle = libelleType }; + OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = idOrigine, Libelle = libelleOrigine }; + + 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, + EstCertifie = false + }; + + FormationService formationService = new FormationService(epContext); + + // Act + TestDelegate throwException = () => formationService.AddFormation(formation); + + // Assert + Assert.Throws(typeof(FormationInvalidException), throwException); + } + + [TestCase(0, 1, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 0, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 0, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 0, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(null, 1, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, null, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, null, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, null, "Externe", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externez", "Planifiée", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externe", "Planifiées", "E-learning", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externe", "Planifiée", "E-learnings", "Demande collaborateur")] + [TestCase(1, 1, 3, 1, "Externe", "Planifiée", "E-learning", "Demande collaborateurs")] + public void AddFormationAsync_AjouteUneFormationAvecDesObjetsEnfantsInvalides_LeveUneFormationInvalidException(int? idMode, int? idStatut, int? idType, int? idOrigine, string libelleMode, string libelleStatut, string libelleType, string libelleOrigine) + { + + // Arrange + ModeFormationDTO modeExterne = new ModeFormationDTO { Id = idMode, Libelle = libelleMode }; + StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = idStatut, Libelle = libelleStatut }; + TypeFormationDTO typeELearning = new TypeFormationDTO { Id = idType, Libelle = libelleType }; + OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = idOrigine, Libelle = libelleOrigine }; + + 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, + EstCertifie = false + }; + + FormationService formationService = new FormationService(epContext); + + // Act + AsyncTestDelegate throwException = () => formationService.AddFormationAsync(formation); + + // Assert + Assert.ThrowsAsync(typeof(FormationInvalidException), throwException); + } + + #endregion + + #region Tests UpdateFormation et UpdateFormationAsync + + [TestCase(1, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + public void UpdateFormation_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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + FormationDTO formation = formationService.GetFormationById(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.EstCertifie = false; + + + // Act + FormationDTO formationModifie = formationService.UpdateFormation(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.EstCertifie, formationModifie.EstCertifie); + } + + [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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + 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.EstCertifie = 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.EstCertifie, formationModifie.EstCertifie); + } + + [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 void UpdateFormation_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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + FormationDTO formation = formationService.GetFormationById(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.EstCertifie = false; + + // Act + TestDelegate throwException = () => formationService.UpdateFormation(idFormation, formation); + + // Assert + Assert.Throws(typeof(FormationInvalidException), throwException); + } + + [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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + 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.EstCertifie = 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(null, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + public void UpdateFormation_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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + FormationService formationService = new FormationService(epContext); + FormationDTO formation = formationService.GetFormationById(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.EstCertifie = false; + // Act - AsyncTestDelegate throwException = () => formationService.GetFormationByIdAsync(value); + TestDelegate throwException = () => formationService.UpdateFormation(1, formation); + + // Assert + Assert.Throws(typeof(FormationIncompatibleIdException), throwException); + } + + [TestCase(2, 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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + 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.EstCertifie = 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 UpdateFormation_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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + 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, + EstCertifie = false + }; + + // Act + FormationDTO formationModifie = formationService.UpdateFormation(idFormation, formation); + + // Assert + Assert.IsNull(formationModifie); + } + + [TestCase(0, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")] + public async Task 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.Id == idMode) + .Select(mode => new ModeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.Id == idStatut) + .Select(mode => new StatutFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.Id == idType) + .Select(mode => new TypeFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.Id == idOrigine) + .Select(mode => new OrigineFormationDTO { Id = mode.Id, Libelle = mode.Libelle }).FirstOrDefault(); + + FormationService formationService = new FormationService(epContext); + + 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, + EstCertifie = false + }; + + // Act + FormationDTO formationModifie = await formationService.UpdateFormationAsync(idFormation, formation); + + // Assert + Assert.IsNull(formationModifie); + } + + + #endregion + + #region Tests DeleteFormationById et DeleteFormationByIdAsync + + [TestCase(1)] + [TestCase(2)] + public void DeleteFormationById_SupprimeUneFormationAvecUnIdValide_FormationSupprimeAvecSucces(long? idFormation) + { + // Arrange + FormationService formationService = new FormationService(epContext); + bool existFormation = true; + + // Act + FormationDTO formationSupprime = formationService.DeleteFormationById(idFormation); + + existFormation = epContext.Formation.Any(formation => formation.Id == formationSupprime.Id.Value); + + // Assert + Assert.IsFalse(existFormation); + } + + [TestCase(1)] + [TestCase(2)] + public async Task DeleteFormationByIdAsync_SupprimeUneFormationAvecUnIdValide_FormationSupprimeAvecSucces(long? idFormation) + { + // Arrange + FormationService formationService = new FormationService(epContext); + bool existFormation = true; + + // Act + FormationDTO formationSupprime = await formationService.DeleteFormationByIdAsync(idFormation); + + existFormation = await epContext.Formation.AnyAsync(formation => formation.Id == formationSupprime.Id.Value); + + // Assert + Assert.IsFalse(existFormation); + } + + [TestCase(0)] + [TestCase(-1)] + public void DeleteFormationById_SupprimeUneFormationAvecUnIdInvalide_LeveUneFormationNotFoundException(long? idFormation) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + TestDelegate throwException = () => formationService.DeleteFormationById(idFormation); + + // Assert + Assert.Throws(typeof(FormationNotFoundException), throwException); + } + + [TestCase(0)] + [TestCase(-1)] + public void DeleteFormationByIdAsync_SupprimeUneFormationAvecUnIdInvalide_LeveUneFormationNotFoundException(long? idFormation) + { + // Arrange + FormationService formationService = new FormationService(epContext); + + // Act + AsyncTestDelegate throwException = () => formationService.DeleteFormationByIdAsync(idFormation); // Assert Assert.ThrowsAsync(typeof(FormationNotFoundException), throwException); } - //[TestCase(true, 1, 2, 1, 1, "f", null)] - //[TestCase(null, null, null, null, null, null, null)] - //public void GetFormations_PasseDesParamsQuiVont_RetourneDesFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) - //{ - // // Arrange - // FormationService formationService = new FormationService(epContext); + [Test] + public void DeleteFormationById_SupprimeUneFormationAvecUnIdNull_LeveUneFormationIncompatibleIdException() + { + // Arrange + FormationService formationService = new FormationService(epContext); - // // Act - // IEnumerable formationDTOs = formationService.GetFormations(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + // Act + TestDelegate throwException = () => formationService.DeleteFormationById(null); - // // Assert - // Assert.Less(0, formationDTOs.Count()); - //} + // Assert + Assert.Throws(typeof(FormationIncompatibleIdException), throwException); + } - //[TestCase(null,-1,-1,-1,-1,"azerty",null)] - //public void GetFormations_PasseDesParamsInvalides_RetourneZeroFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) - //{ - // // Arrange - // FormationService formationService = new FormationService(epContext); + [Test] + public void DeleteFormationByIdAsync_SupprimeUneFormationAvecUnIdNull_LeveUneFormationIncompatibleIdException() + { + // Arrange + FormationService formationService = new FormationService(epContext); - // // Act - // IEnumerable formationDTOs = formationService.GetFormations(asc, numPage, parPAge, idAgence, statutFormation,texte,tri); + // Act + AsyncTestDelegate throwException = () => formationService.DeleteFormationByIdAsync(null); + + // Assert + Assert.ThrowsAsync(typeof(FormationIncompatibleIdException), throwException); + } - // // Assert - // Assert.AreEqual(0, formationDTOs.Count()); - //} - + #endregion } } \ No newline at end of file From cc984a67718868f2b9709c1b37003dad39965563 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 3 Nov 2020 14:12:23 +0100 Subject: [PATCH 02/12] =?UTF-8?q?Classe=20contenant=20les=20jeux=20de=20do?= =?UTF-8?q?nn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EPAServeur/Context/DataSeeder.cs | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/EPAServeur/Context/DataSeeder.cs b/EPAServeur/Context/DataSeeder.cs index e0a3b4d..279d787 100644 --- a/EPAServeur/Context/DataSeeder.cs +++ b/EPAServeur/Context/DataSeeder.cs @@ -383,65 +383,65 @@ namespace EPAServeur.Context //ModeFormation ModeFormation modeExterne, modeInterne, modePresentiel, modeELearning; - modeExterne = new ModeFormation { Libelle = "Externe" }; + modeExterne = new ModeFormation { Id = 1, Libelle = "Externe" }; epContext.ModeFormation.Add(modeExterne); - modeInterne = new ModeFormation { Libelle = "Interne" }; + modeInterne = new ModeFormation { Id = 2, Libelle = "Interne" }; epContext.ModeFormation.Add(modeInterne); - modePresentiel = new ModeFormation { Libelle = "Présentiel" }; + modePresentiel = new ModeFormation { Id = 3, Libelle = "Présentiel" }; epContext.ModeFormation.Add(modePresentiel); - modeELearning = new ModeFormation { Libelle = "E-learning" }; + modeELearning = new ModeFormation { Id = 4, Libelle = "E-learning" }; epContext.ModeFormation.Add(modeELearning); //TypeFormation TypeFormation typeExterne, typeInterne, typeELearning, typeAcademy; - typeExterne = new TypeFormation { Libelle = "Externe" }; + typeExterne = new TypeFormation { Id = 1, Libelle = "Externe" }; epContext.TypeFormation.Add(typeExterne); - typeInterne = new TypeFormation { Libelle = "Interne" }; + typeInterne = new TypeFormation { Id = 2, Libelle = "Interne" }; epContext.TypeFormation.Add(typeInterne); - typeELearning = new TypeFormation { Libelle = "E-learning" }; + typeELearning = new TypeFormation { Id = 3, Libelle = "E-learning" }; epContext.TypeFormation.Add(typeELearning); - typeAcademy = new TypeFormation { Libelle = "Academy by Apside" }; + typeAcademy = new TypeFormation { Id = 4, Libelle = "Academy by Apside" }; epContext.TypeFormation.Add(typeAcademy); //OrigineFormation OrigineFormation origineFormationCollaborateur, origineFormationClient, origineFormationApside, origineFormationReglementaire; - origineFormationCollaborateur = new OrigineFormation { Libelle = "Demande collaborateur" }; + origineFormationCollaborateur = new OrigineFormation { Id = 1, Libelle = "Demande collaborateur" }; epContext.OrigineFormation.Add(origineFormationCollaborateur); - origineFormationClient = new OrigineFormation { Libelle = "Exigence client" }; + origineFormationClient = new OrigineFormation { Id = 2, Libelle = "Exigence client" }; epContext.OrigineFormation.Add(origineFormationClient); - origineFormationApside = new OrigineFormation { Libelle = "Exigence Apside" }; + origineFormationApside = new OrigineFormation { Id = 3, Libelle = "Exigence Apside" }; epContext.OrigineFormation.Add(origineFormationApside); - origineFormationReglementaire = new OrigineFormation { Libelle = "Formation réglementaire" }; + origineFormationReglementaire = new OrigineFormation { Id = 4, Libelle = "Formation réglementaire" }; epContext.OrigineFormation.Add(origineFormationReglementaire); //OrigineDemandeFormation OrigineDemandeFormation origineDemandeFormationCollaborateur, origineDemandeFormationEP, origineDemandeFormationClient, origineDemandeFormationReglement, origineDemandeFormationApside; - origineDemandeFormationCollaborateur = new OrigineDemandeFormation { Libelle = "Demande collaborateur" }; + origineDemandeFormationCollaborateur = new OrigineDemandeFormation { Id = 1, Libelle = "Demande collaborateur" }; epContext.OrigineDemandeFormation.Add(origineDemandeFormationCollaborateur); - origineDemandeFormationEP = new OrigineDemandeFormation { Libelle = "Demande EP" }; + origineDemandeFormationEP = new OrigineDemandeFormation { Id = 2, Libelle = "Demande EP" }; epContext.OrigineDemandeFormation.Add(origineDemandeFormationEP); - origineDemandeFormationClient = new OrigineDemandeFormation { Libelle = "Exigence Client" }; + origineDemandeFormationClient = new OrigineDemandeFormation { Id = 3, Libelle = "Exigence Client" }; epContext.OrigineDemandeFormation.Add(origineDemandeFormationClient); - origineDemandeFormationReglement = new OrigineDemandeFormation { Libelle = "Formation réglementaire" }; + origineDemandeFormationReglement = new OrigineDemandeFormation { Id = 4, Libelle = "Formation réglementaire" }; epContext.OrigineDemandeFormation.Add(origineDemandeFormationReglement); - origineDemandeFormationApside = new OrigineDemandeFormation { Libelle = "Demande Apside" }; + origineDemandeFormationApside = new OrigineDemandeFormation { Id = 5, Libelle = "Demande Apside" }; epContext.OrigineDemandeFormation.Add(origineDemandeFormationApside); //Formation From 04aac9898911f2d6ed89b59533b658272a45d5d1 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 3 Nov 2020 14:18:11 +0100 Subject: [PATCH 03/12] =?UTF-8?q?Ajout=20d'une=20exception=20suppl=C3=A9me?= =?UTF-8?q?ntaire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EPAServeur/Controllers/FormationsApi.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/EPAServeur/Controllers/FormationsApi.cs b/EPAServeur/Controllers/FormationsApi.cs index 4ed3f3a..b9fe56e 100644 --- a/EPAServeur/Controllers/FormationsApi.cs +++ b/EPAServeur/Controllers/FormationsApi.cs @@ -101,6 +101,10 @@ namespace IO.Swagger.Controllers FormationDTO formation = await formationService.DeleteFormationByIdAsync(idFormation); } + catch (FormationIncompatibleIdException) + { + logger.LogError("Impossible de supprimer la formation car l'id de la formation passé en paramètre est null."); + } catch (FormationNotFoundException) { logger.LogError("Impossible de supprimer la formation d'id {idFormation} car elle n'existe pas.", idFormation); @@ -272,7 +276,7 @@ namespace IO.Swagger.Controllers try { - formations = await formationService.GetFormationRealiseeAsync(asc, numPage, parPAge, idAgence, texte, tri); + formations = await formationService.GetFormationRealiseesAsync(asc, numPage, parPAge, idAgence, texte, tri); } catch (ArgumentNullException) { @@ -622,7 +626,7 @@ namespace IO.Swagger.Controllers logger.LogInformation("Mise à jour de la formation d'id {idFormation}.", idFormation); FormationDTO formation = null; - + try { formation = await formationService.UpdateFormationAsync(idFormation, body); From b81852a4cba854d71b7aca083319583cead49e94 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 3 Nov 2020 14:24:17 +0100 Subject: [PATCH 04/12] Renommage des membres --- EPAServeur/IServices/IFormationService.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/EPAServeur/IServices/IFormationService.cs b/EPAServeur/IServices/IFormationService.cs index 6b455c8..4a5e172 100644 --- a/EPAServeur/IServices/IFormationService.cs +++ b/EPAServeur/IServices/IFormationService.cs @@ -12,14 +12,14 @@ namespace EPAServeur.IServices { FormationDTO GetFormationById(long? idFormation); Task GetFormationByIdAsync(long? idFormation); - IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri); - Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri); - IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); - Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); - IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); - Task> GetFormationRealiseeAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); - IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); - Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri); + Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri); + IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri); + Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri); + IEnumerable GetFormationRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri); + Task> GetFormationRealiseesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri); + IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri); + Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri); IEnumerable GetModesFormation(); Task> GetModesFormationAsync(); IEnumerable GetOriginesFormation(); From b2bea37630773c8e64add2c139f1dfa7fb4a1aef Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 3 Nov 2020 14:25:43 +0100 Subject: [PATCH 05/12] MAJ du service formation --- EPAServeur/Services/FormationService.cs | 136 +++++++++--------------- 1 file changed, 52 insertions(+), 84 deletions(-) diff --git a/EPAServeur/Services/FormationService.cs b/EPAServeur/Services/FormationService.cs index 67c4c95..5040dc2 100644 --- a/EPAServeur/Services/FormationService.cs +++ b/EPAServeur/Services/FormationService.cs @@ -82,7 +82,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) + public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -129,10 +129,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take); } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -148,7 +144,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) + public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -199,10 +195,6 @@ namespace EPAServeur.Services } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -218,7 +210,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -244,9 +236,6 @@ namespace EPAServeur.Services .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -262,7 +251,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -288,9 +277,6 @@ namespace EPAServeur.Services .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -306,7 +292,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public IEnumerable GetFormationRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -340,10 +326,6 @@ namespace EPAServeur.Services } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -359,7 +341,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationRealiseeAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetFormationRealiseesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -392,10 +374,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -411,7 +389,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -442,9 +420,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take); } - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -460,7 +435,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -492,9 +467,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); } - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -511,9 +483,6 @@ namespace EPAServeur.Services modeFormations = epContext.ModeFormation; - if (modeFormations == null) - return null; - modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; @@ -529,9 +498,6 @@ namespace EPAServeur.Services IEnumerable modeFormationDTOs; modeFormations = await epContext.ModeFormation.ToListAsync(); - if (modeFormations == null) - return null; - modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; @@ -548,9 +514,6 @@ namespace EPAServeur.Services origineFormations = epContext.OrigineFormation; - if (origineFormations == null) - return null; - origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; @@ -567,9 +530,6 @@ namespace EPAServeur.Services origineFormations = await epContext.OrigineFormation.ToListAsync(); - if (origineFormations == null) - return null; - origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; @@ -586,9 +546,6 @@ namespace EPAServeur.Services statutFormations = epContext.StatutFormation; - if (statutFormations == null) - return null; - statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; @@ -605,9 +562,6 @@ namespace EPAServeur.Services statutFormations = await epContext.StatutFormation.ToListAsync(); - if (statutFormations == null) - return null; - statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; @@ -624,9 +578,6 @@ namespace EPAServeur.Services typeFormations = epContext.TypeFormation; - if (typeFormations == null) - return null; - typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; @@ -643,9 +594,6 @@ namespace EPAServeur.Services typeFormations = await epContext.TypeFormation.ToListAsync(); - if (typeFormations == null) - return null; - typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; @@ -664,9 +612,7 @@ namespace EPAServeur.Services Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); - if (formation.Statut != null) - epContext.StatutFormation.Attach(formation.Statut); - + epContext.StatutFormation.Attach(formation.Statut); epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); @@ -684,15 +630,13 @@ namespace EPAServeur.Services /// public async Task AddFormationAsync(FormationDTO formationDTO) { - if (!IsFormationValide(formationDTO)) + if (!await IsFormationValideAsync(formationDTO)) throw new FormationInvalidException(); Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); - if (formation.Statut != null) - epContext.StatutFormation.Attach(formation.Statut); - + epContext.StatutFormation.Attach(formation.Statut); epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); @@ -714,10 +658,10 @@ namespace EPAServeur.Services if (!IsFormationValide(formationDTO)) throw new FormationInvalidException(); - if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) + if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) throw new FormationIncompatibleIdException(); - Formation formation = epContext.Formation.Find(idFormation); + Formation formation = epContext.Formation.Find(idFormation.Value); if (formation == null) return null; @@ -740,10 +684,10 @@ namespace EPAServeur.Services if (!IsFormationValide(formationDTO)) throw new FormationInvalidException(); - if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) + if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) throw new FormationIncompatibleIdException(); - Formation formation = await epContext.Formation.FindAsync(idFormation); + Formation formation = await epContext.Formation.FindAsync(idFormation.Value); if (formation == null) return null; @@ -761,14 +705,17 @@ namespace EPAServeur.Services /// public FormationDTO DeleteFormationById(long? idFormation) { - Formation formation = epContext.Formation.Find(idFormation); + if (!idFormation.HasValue) + throw new FormationIncompatibleIdException(); + + + Formation formation = epContext.Formation.Find(idFormation.Value); if (formation == null) throw new FormationNotFoundException(); epContext.Remove(formation); - epContext.SaveChanges(); return GetFormationDTO(formation); @@ -781,7 +728,10 @@ namespace EPAServeur.Services /// public async Task DeleteFormationByIdAsync(long? idFormation) { - Formation formation = await epContext.Formation.FindAsync(idFormation); + if (!idFormation.HasValue) + throw new FormationIncompatibleIdException(); + + Formation formation = await epContext.Formation.FindAsync(idFormation.Value); if (formation == null) throw new FormationNotFoundException(); @@ -798,14 +748,40 @@ namespace EPAServeur.Services /// /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour /// - /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null + /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null,si la date de début de la formation est inférieur à la date de fin et si le statut, l'origine,le mode et le type sont présents dans la base de données /// /// true si l'objet est valide, false sinon private bool IsFormationValide(FormationDTO formation) { - return !(formation == null || formation.IdAgence == null || formation.Intitule == null || formation.Organisme == null); + return !(formation == null || formation.Statut == null || formation.Mode == null || formation.Origine == null || formation.Type == null + || !formation.IdAgence.HasValue || formation.IdAgence == 0 + || string.IsNullOrWhiteSpace(formation.Intitule) || string.IsNullOrWhiteSpace(formation.Organisme) + || !formation.DateDebut.HasValue || !formation.DateFin.HasValue || formation.DateDebut.Value > formation.DateFin.Value + || !formation.Statut.Id.HasValue || !formation.Mode.Id.HasValue || !formation.Origine.Id.HasValue || !formation.Type.Id.HasValue + || !epContext.StatutFormation.Any(statut => statut.Id == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle) + || !epContext.ModeFormation.Any(mode => mode.Id == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle) + || !epContext.OrigineFormation.Any(origine => origine.Id == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle) + || !epContext.TypeFormation.Any(type => type.Id == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)); } + /// + /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour + /// + /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null,si la date de début de la formation est inférieur à la date de fin et si le statut, l'origine,le mode et le type sont présents dans la base de données + /// + /// true si l'objet est valide, false sinon + private async Task IsFormationValideAsync(FormationDTO formation) + { + return !(formation == null || formation.Statut == null || formation.Mode == null || formation.Origine == null || formation.Type == null + || !formation.IdAgence.HasValue || formation.IdAgence == 0 + || string.IsNullOrWhiteSpace(formation.Intitule) || string.IsNullOrWhiteSpace(formation.Organisme) + || !formation.DateDebut.HasValue || !formation.DateFin.HasValue || formation.DateDebut.Value > formation.DateFin.Value + || !formation.Statut.Id.HasValue || !formation.Mode.Id.HasValue || !formation.Origine.Id.HasValue || !formation.Type.Id.HasValue + || ! await epContext.StatutFormation.AnyAsync(statut => statut.Id == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle) + || ! await epContext.ModeFormation.AnyAsync(mode => mode.Id == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle) + || ! await epContext.OrigineFormation.AnyAsync(origine => origine.Id == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle) + || ! await epContext.TypeFormation.AnyAsync(type => type.Id == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)); + } #region Object to DTO /// /// Récupère un objet FormationDTO en fonction d'un objet Formation @@ -936,8 +912,6 @@ namespace EPAServeur.Services /// private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { - if (origineFormationDTO == null) - return null; OrigineFormation origineFormation = new OrigineFormation() { Id = origineFormationDTO.Id.Value, @@ -953,8 +927,6 @@ namespace EPAServeur.Services /// private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { - if (statutFormationDTO == null) - return null; StatutFormation statutFormation = new StatutFormation() { Id = statutFormationDTO.Id.Value, @@ -970,8 +942,6 @@ namespace EPAServeur.Services /// private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { - if (modeFormationDTO == null) - return null; ModeFormation modeFormation = new ModeFormation() { Id = modeFormationDTO.Id.Value, @@ -987,8 +957,6 @@ namespace EPAServeur.Services /// private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { - if (typeFormationDTO == null) - return null; TypeFormation typeFormation = new TypeFormation() { Id = typeFormationDTO.Id.Value, From b8072d67faa581d11680f662432238f8d647d978 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 3 Nov 2020 16:29:07 +0100 Subject: [PATCH 06/12] =?UTF-8?q?Ajout=20des=20r=C3=A9gions=20Variables=20?= =?UTF-8?q?et=20Setup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EPAServeur.Tests/Services/FormationServiceTests.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/EPAServeur.Tests/Services/FormationServiceTests.cs b/EPAServeur.Tests/Services/FormationServiceTests.cs index d99c6a6..e47ddba 100644 --- a/EPAServeur.Tests/Services/FormationServiceTests.cs +++ b/EPAServeur.Tests/Services/FormationServiceTests.cs @@ -14,8 +14,14 @@ namespace EPAServeur.Tests { public class FormationServiceTests { + #region Variables + private EpContext epContext; + #endregion + + #region Setup + [SetUp] public void Setup() { @@ -41,6 +47,8 @@ namespace EPAServeur.Tests } } + #endregion + #region Tests GetFormationById et GetFormationByIdAsync [Test] From c6ccba8babe0ffb63e0bfd100f0ed993761f982d Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Wed, 4 Nov 2020 14:10:47 +0100 Subject: [PATCH 07/12] Ajout du rapport de la couverture de code --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c4077a4..031bf3b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ # LOGS EPA SERVEUR /EPAServeurLog +# RAPPORT COUVERTURE DE CODE TESTS UNITAIRES +EPAServeurCoverageReport/ + # User-specific files *.rsuser *.suo From a0feaefec0a8ffca1424dfdb9c35da1dd9d741a0 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Wed, 4 Nov 2020 14:11:53 +0100 Subject: [PATCH 08/12] Ajout du package coverlet.collector --- EPAServeur.Tests/EPAServeur.Tests.csproj | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/EPAServeur.Tests/EPAServeur.Tests.csproj b/EPAServeur.Tests/EPAServeur.Tests.csproj index 5efe526..50b948f 100644 --- a/EPAServeur.Tests/EPAServeur.Tests.csproj +++ b/EPAServeur.Tests/EPAServeur.Tests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + From 8f57dfcf12d81936446bbf70cf6537a679a39c14 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Mon, 9 Nov 2020 14:37:17 +0100 Subject: [PATCH 09/12] Ajout de l'attribut TestFixture --- EPAServeur.Tests/Services/FormationServiceTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/EPAServeur.Tests/Services/FormationServiceTests.cs b/EPAServeur.Tests/Services/FormationServiceTests.cs index e47ddba..3df9aee 100644 --- a/EPAServeur.Tests/Services/FormationServiceTests.cs +++ b/EPAServeur.Tests/Services/FormationServiceTests.cs @@ -10,8 +10,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace EPAServeur.Tests +namespace EPAServeur.Tests.Services { + [TestFixture] public class FormationServiceTests { #region Variables From 03dd6302b24201fb1996426f8665ef8aa3a5c69f Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Mon, 9 Nov 2020 14:37:52 +0100 Subject: [PATCH 10/12] MAJ des commentaires --- EPAServeur/Context/DataSeeder.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/EPAServeur/Context/DataSeeder.cs b/EPAServeur/Context/DataSeeder.cs index 279d787..218a1fc 100644 --- a/EPAServeur/Context/DataSeeder.cs +++ b/EPAServeur/Context/DataSeeder.cs @@ -8,12 +8,12 @@ using System.Collections.Generic; namespace EPAServeur.Context { /// - /// Classe permettant d'ajouter un jeu de données à un EpContext. + /// Classe permettant d'insérer des données dans la base de données. /// public class DataSeeder { /// - /// Ajout des informations qui pourront être conservées dans la base de données + /// Insère des informations qui pourront être conservées dans la base de données /// /// public static void AddInformationsDeBase(EpContext epContext) @@ -31,7 +31,7 @@ namespace EPAServeur.Context } /// - /// Ajout des thèmes qui pourront être ajoutés et conservés + /// Insère des thèmes qui pourront être ajoutés et conservés /// /// public static void AddThemes(EpContext epContext) @@ -54,7 +54,7 @@ namespace EPAServeur.Context } /// - /// Ajout d'un jeu de données fictif pour les types d'entretien + /// Insère un jeu de données fictif pour les types d'entretien /// /// public static void AddTypesEntretien(EpContext epContext) @@ -78,7 +78,7 @@ namespace EPAServeur.Context } /// - /// Ajout d'un jeu de données fictif pour les ep + /// Insère un jeu de données fictif pour les ep /// /// public static void AddEp(EpContext epContext) @@ -254,7 +254,7 @@ namespace EPAServeur.Context } /// - /// Ajout d'un jeu de données fictif pour les engagements + /// Insère un jeu de données fictif pour les engagements /// /// public static void AddEngagements(EpContext epContext) @@ -360,7 +360,7 @@ namespace EPAServeur.Context } /// - /// Ajout d'un jeu de données fictif pour les formations + /// Insère un jeu de données fictif pour les formations /// /// public static void AddFormations(EpContext epContext) @@ -661,7 +661,7 @@ namespace EPAServeur.Context } /// - /// Ajout d'un jeu de données fictif pour les champs + /// Insère un jeu de données fictif pour les champs /// /// public static void AddChamps(EpContext epContext) @@ -822,7 +822,7 @@ namespace EPAServeur.Context } /// - /// Ajout d'un jeu de données fictif pour les notes + /// Insère un jeu de données fictif pour les notes /// /// public static void AddNotes(EpContext epContext) From 71b2de41764df6f5fc071c81a3ecd750eda14ee2 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Mon, 9 Nov 2020 14:38:35 +0100 Subject: [PATCH 11/12] MAJ des exceptions --- EPAServeur/Controllers/FormationsApi.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/EPAServeur/Controllers/FormationsApi.cs b/EPAServeur/Controllers/FormationsApi.cs index b9fe56e..f9df2b3 100644 --- a/EPAServeur/Controllers/FormationsApi.cs +++ b/EPAServeur/Controllers/FormationsApi.cs @@ -217,6 +217,10 @@ namespace IO.Swagger.Controllers { formationDTO = await formationService.GetFormationByIdAsync(idFormation); } + catch (ArgumentNullException) + { + logger.LogError("L'argument passé dans la requête pour récupérer une formation est invalide."); + } catch (FormationNotFoundException) { logger.LogError("Aucune formation ne correspond à l'id {idFormation} recherchée.", idFormation); @@ -229,14 +233,6 @@ namespace IO.Swagger.Controllers return NotFound(erreurDTO); } - catch (DbUpdateConcurrencyException) - { - logger.LogWarning("La formation {idFormation} n'a pas pu être récupérée car elle est prise par une autre ressource.", idFormation); - } - catch (DbUpdateException) - { - logger.LogError("Une erreur a eu lieu, la formation {idFormation} n'a pas pu être récupérée.", idFormation); - } catch (Exception) { logger.LogError("Une erreur inconnue est survenue lors de la récupération de la formation {idFormation}.", idFormation); From efbfd193d745e3afd95aca25a8d431b60d28012f Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Mon, 9 Nov 2020 14:49:42 +0100 Subject: [PATCH 12/12] Creation de la classe FormationApiTests --- .../Controllers/FormationApiTests.cs | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 EPAServeur.Tests/Controllers/FormationApiTests.cs diff --git a/EPAServeur.Tests/Controllers/FormationApiTests.cs b/EPAServeur.Tests/Controllers/FormationApiTests.cs new file mode 100644 index 0000000..9347923 --- /dev/null +++ b/EPAServeur.Tests/Controllers/FormationApiTests.cs @@ -0,0 +1,188 @@ +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; + +namespace EPAServeur.Tests.Controllers +{ + [TestFixture] + public class FormationApiTests + { + #region Variables + + private FormationService formationService; + private readonly ILogger logger; + + #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 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; + } + + // Instanciation du service qui sera utilisé dans le controleur + formationService = new FormationService(epContext); + } + + #endregion + + #region Tests GetFormationById + + [Test] + public void GetFormationById_PasseEnParamUnIdPresentDansLaBDD_RetourneUnObjetOkResult() + { + // Arrange + FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger()); + + // Act + var okResult = formationsApiController.GetFormationById(1); + + // Assert + Assert.IsInstanceOf(okResult.Result); + } + + + #endregion + + #region Tests GetFormations + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetFormationAnnulees + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetFormationRealisee + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetProchainesFormation + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetModesFormation + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetOriginesFormation + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetStatutsFormation + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests GetTypesFormation + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests AddFormation + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests UpdateFormation + + // Arrange + + // Act + + // Assert + + #endregion + + #region Tests DeleteFormationById + + // Arrange + + // Act + + // Assert + + #endregion + } +} \ No newline at end of file