You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Digitalisation_EPA_Serveur/EPAServeur.Tests/Services/FormationServiceTests.cs

983 lines
50 KiB

using EPAServeur.Context;
using EPAServeur.Exceptions;
using EPAServeur.IServices;
using EPAServeur.Models.Formation;
using EPAServeur.Services;
using IO.Swagger.ApiCollaborateur;
using IO.Swagger.DTO;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Tests.Services
{
[TestFixture]
public class FormationServiceTests
{
#region Variables
private EpContext epContext;
private ICollaborateurApi collaborateurApi;
private ICollaborateurService collaborateurService;
private ITransformDTO transformDTO;
#endregion
#region Setup
[SetUp]
public void Setup()
{
// Utilisation d'une base de données en mémoire
var optionBuider = new DbContextOptionsBuilder<EpContext>()
.UseInMemoryDatabase("server_ep_test")
.Options;
epContext = new EpContext(optionBuider);
collaborateurApi = new CollaborateurApi();
transformDTO = new TransformDTO();
collaborateurService = new CollaborateurService(collaborateurApi, epContext, transformDTO);
epContext.Database.EnsureDeleted();
epContext.Database.EnsureCreated();
epContext.SaveChanges();
// Ajout du jeu de données pour les tests
DataSeeder.AddFormations(epContext);
// Détache les entités du context car la base de données InMemory créé des conflits
// entre les clés primaires lors d'un Update ou d'un Insert
foreach (var entity in epContext.ChangeTracker.Entries())
{
entity.State = EntityState.Detached;
}
}
#endregion
#region Tests GetFormationByIdAsync
[Test]
public async Task GetFormationByIdAsync_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictif_RetourneUneFormation()
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
FormationDTO formationDTO = await formationService.GetFormationByIdAsync(3);
// Assert
Assert.AreEqual(3, formationDTO.Id);
Assert.AreEqual("Apprendre C# et le développement de logiciels avec WPF", formationDTO.Intitule);
Assert.AreEqual(1, formationDTO.IdAgence);
Assert.AreEqual(new DateTime(2020, 5, 25, 14, 0, 0), formationDTO.DateDebut);
Assert.AreEqual(new DateTime(2020, 5, 27), formationDTO.DateFin);
Assert.AreEqual(4, formationDTO.Heure);
Assert.AreEqual(2, formationDTO.Jour);
Assert.AreEqual("Organisme2", formationDTO.Organisme);
Assert.True(formationDTO.EstCertifiee);
Assert.IsNotNull(formationDTO.Participations);
Assert.AreEqual(new OrigineFormationDTO { Id = 3, Libelle = "Exigence Apside" }, formationDTO.Origine);
Assert.AreEqual(new StatutFormationDTO { Id = 1, Libelle = "Planifiée" }, formationDTO.Statut);
Assert.AreEqual(new ModeFormationDTO { Id = 4, Libelle = "E-learning" }, formationDTO.Mode);
Assert.AreEqual(new TypeFormationDTO { Id = 3, Libelle = "E-learning" }, formationDTO.Type);
}
[Test]
public async Task GetFormationByIdAsync_PasseEnParamUnIdExistantDansLeJeuDeDonneesFictif_RetourneUneFormationSansParticipation()
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
long idFormation = 11;
// Act
FormationDTO formationDTO = await formationService.GetFormationByIdAsync(idFormation);
// Assert
Assert.AreEqual(idFormation, formationDTO.Id);
Assert.AreEqual("Formation Clean Architecture .Net Core", formationDTO.Intitule);
Assert.AreEqual(1, formationDTO.IdAgence);
Assert.AreEqual(new DateTime(2020, 04, 6, 9, 0, 0), formationDTO.DateDebut);
Assert.AreEqual(new DateTime(2020, 04, 11), formationDTO.DateFin);
Assert.AreEqual(15, formationDTO.Heure);
Assert.AreEqual(5, formationDTO.Jour);
Assert.AreEqual("Organisme1", formationDTO.Organisme);
Assert.False(formationDTO.EstCertifiee);
Assert.IsNull(formationDTO.Participations);
Assert.AreEqual(new OrigineFormationDTO { Id = 4, Libelle = "Formation réglementaire" }, formationDTO.Origine);
Assert.AreEqual(new StatutFormationDTO { Id = 4, Libelle = "Annulée" }, formationDTO.Statut);
Assert.AreEqual(new ModeFormationDTO { Id = 3, Libelle = "Présentiel" }, formationDTO.Mode);
Assert.AreEqual(new TypeFormationDTO { Id = 2, Libelle = "Interne" }, formationDTO.Type);
}
[TestCase(-1)]
[TestCase(0)]
[TestCase(999999)]
public void GetFormationByIdAsync_PasseEnParamUnIdInexistantDansLeJeuDeDonneesFictif_LeveUneFormationNotFoundException(long idFormation)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
AsyncTestDelegate throwException = () => formationService.GetFormationByIdAsync(idFormation);
// Assert
Assert.ThrowsAsync(typeof(FormationNotFoundException), throwException);
}
#endregion
#region Tests GetFormationsAsync
[TestCase(1, new int[] { 1, 2, 3 }, true, 1, 5, "formation", null, null, null)]
[TestCase(1, new int[] { 1 }, false, 1, 5, "Formation Mainframe Complète", null, null, null)]
[TestCase(null, null, null, 2, 10, null, null, null, null)]
[TestCase(null, new int[] { 1 }, null, 1, 10, null, null, null, null)]
[TestCase(1, null, null, 1, 10, "for", null, null, null)]
[TestCase(1, new int[] { }, null, 1, 10, "for", null, null, null)]
public async Task GetFormationsAsync_PasseDesParamsValides_RetourneDesFormations(long? idAgence, int[] arrIdStatuts, bool? asc, int? numPage, int? parPAge, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
List<int?> idStatuts;
if (arrIdStatuts != null)
idStatuts = arrIdStatuts.Select(x => (int?)x).ToList();
else
idStatuts = null;
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(idAgence, idStatuts, asc, numPage, parPAge, texte, tri, dateDebut, dateFin);
// Assert
Assert.Less(0, formationDTOs.Count());
}
[TestCase(1, 5)]
[TestCase(1, 10)]
public async Task GetFormationsAsync_PasseEnParamNumPageEtParPage_RetourneLaPremierePageDesFormations(int? numPage, int? parPAge)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, null, numPage, parPAge, null, null, null, null);
// Assert
Assert.AreEqual(parPAge, formationDTOs.Count());
}
[TestCase(2, 5)]
[TestCase(2, 6)]
[TestCase(2, 10)]
[TestCase(2, 15)]
public async Task GetFormationsAsync_PasseEnParamNumPageEtParPage_RetourneLaDeuxiemePageDesFormations(int? numPage, int? parPAge)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
int? nbFormationDeuxiemePage;
switch (parPAge)
{
case 5:
nbFormationDeuxiemePage = 5;
break;
case 6:
nbFormationDeuxiemePage = 5;
break;
case 10:
nbFormationDeuxiemePage = 1;
break;
default:
nbFormationDeuxiemePage = 0;
break;
}
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, null, numPage, parPAge, null, null, null, null);
// Assert
Assert.AreEqual(nbFormationDeuxiemePage, formationDTOs.Count());
}
[TestCase(true, "intitule")]
[TestCase(true, null)]
[TestCase(true, "toto")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParIntituleCroissant(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual("Apprendre C# et le développement de logiciels avec WPF", formationDTOs.First().Intitule);
Assert.AreEqual("Formation Xamarin, Développer des applications mobiles en C# pour iOS et Android", formationDTOs.Last().Intitule);
}
[TestCase(false, "intitule")]
[TestCase(false, null)]
[TestCase(false, "toto")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParIntituleDecroissant(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual("Formation Xamarin, Développer des applications mobiles en C# pour iOS et Android", formationDTOs.First().Intitule);
Assert.AreEqual("Apprendre C# et le développement de logiciels avec WPF", formationDTOs.Last().Intitule);
}
[TestCase(true, "statut")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParStatutCroissant(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual("Annulée", formationDTOs.First().Statut.Libelle);
Assert.AreEqual("Replanifiée", formationDTOs.Last().Statut.Libelle);
}
[TestCase(false, "statut")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParStatutDecroissant(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual("Replanifiée", formationDTOs.First().Statut.Libelle);
Assert.AreEqual("Annulée", formationDTOs.Last().Statut.Libelle);
}
[TestCase(true, "participants")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParNombreDeParticipantsCroissant(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual(0, formationDTOs.First().NbParticipations);
Assert.AreEqual(2, formationDTOs.Last().NbParticipations);
}
[TestCase(false, "participants")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParNombreDeParticipantsDecroissant(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual(2, formationDTOs.First().NbParticipations);
Assert.AreEqual(0, formationDTOs.Last().NbParticipations);
}
[TestCase(true, "origine")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParOrigineCroissante(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual("Demande collaborateur", formationDTOs.First().Origine.Libelle);
Assert.AreEqual("Formation réglementaire", formationDTOs.Last().Origine.Libelle);
}
[TestCase(false, "origine")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParOrigineDecroissante(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual("Formation réglementaire", formationDTOs.First().Origine.Libelle);
Assert.AreEqual("Demande collaborateur", formationDTOs.Last().Origine.Libelle);
}
[TestCase(true, "date")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParDateCroissante(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual(new DateTime(2020, 1, 25, 10, 0, 0), formationDTOs.First().DateDebut);
Assert.AreEqual(new DateTime(2020, 12, 25, 14, 0, 0), formationDTOs.Last().DateDebut);
}
[TestCase(false, "date")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParDateDecroissante(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual(new DateTime(2020, 12, 25, 14, 0, 0), formationDTOs.First().DateDebut);
Assert.AreEqual(new DateTime(2020, 1, 25, 10, 0, 0), formationDTOs.Last().DateDebut);
}
[TestCase(true, "certification")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParCertificationCroissante(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual(false, formationDTOs.First().EstCertifiee);
Assert.AreEqual(true, formationDTOs.Last().EstCertifiee);
}
[TestCase(false, "certification")]
public async Task GetFormationsAsync_PasseEnParamAscEtTri_RetourneDesFormationsOrdonnanceeParCertificationDecroissante(bool? asc, string tri)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, asc, null, null, null, tri, null, null);
// Assert
Assert.AreEqual(true, formationDTOs.First().EstCertifiee);
Assert.AreEqual(false, formationDTOs.Last().EstCertifiee);
}
[TestCase("2020-10-01")]
public async Task GetFormationsAsync_PasseEnParamUneDateDeDebut_RetourneDesFormationsAvecUneDateDeDebutSuperieur(DateTime dateDebut)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, null, null, null, null, null, dateDebut, null);
// Assert
Assert.Greater(formationDTOs.First().DateDebut, dateDebut);
Assert.GreaterOrEqual(formationDTOs.Last().DateDebut, dateDebut);
}
[TestCase("2020-10-01")]
public async Task GetFormationsAsync_PasseEnParamUneDateDeFin_RetourneDesFormationsAvecUneDateDeFinInferieur(DateTime dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, null, null, null, null, null, null, dateFin);
// Assert
Assert.Less(formationDTOs.First().DateFin, dateFin);
Assert.LessOrEqual(formationDTOs.Last().DateFin, dateFin);
}
[TestCase("2020-11-01", "2020-12-01")]
public async Task GetFormationsAsync_PasseEnParamUneDateDeDebutEtUneDateDeFin_RetourneDesFormationsAvecUneDateDeDebutSuperieurUneDateDeFinInferieur(DateTime dateDebut, DateTime dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, null, null, null, null, null, dateDebut, dateFin);
// Assert
Assert.Greater(formationDTOs.First().DateDebut, dateDebut);
Assert.Less(formationDTOs.First().DateFin, dateFin);
Assert.GreaterOrEqual(formationDTOs.Last().DateDebut, dateDebut);
Assert.LessOrEqual(formationDTOs.Last().DateFin, dateFin);
}
[TestCase("2020-11-01", "2020-10-01")]
public async Task GetFormationsAsync_PasseEnParamUneDateDeDebutSuperieurEtUneDateDeFinInferieur_RetourneZeroFormation(DateTime dateDebut, DateTime dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(null, null, null, null, null, null, null, dateDebut, dateFin);
// Assert
Assert.AreEqual(0, formationDTOs.Count());
}
[TestCase(1, new int[] { 1 }, true, 1, 15, "azerty", null, null, null)]
[TestCase(-1, new int[] { -1 }, null, -1, -1, "azerty", null, null, null)]
[TestCase(0, new int[] { 0 }, null, 0, 0, null, null, null, null)]
public async Task GetFormationsAsync_PasseDesParamsInvalides_RetourneZeroFormation(long? idAgence, int[] arrIdStatuts, bool? asc, int? numPage, int? parPAge, string texte, string tri, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
List<int?> idStatuts;
if (arrIdStatuts != null)
idStatuts = arrIdStatuts.Select(x => (int?)x).ToList();
else
idStatuts = null;
// Act
IEnumerable<FormationDetailsDTO> formationDTOs = await formationService.GetFormationsAsync(idAgence, idStatuts, asc, numPage, parPAge, texte, tri, dateDebut, dateFin);
// Assert
Assert.AreEqual(0, formationDTOs.Count());
}
#endregion
#region Tests GetFormationsCountAsync
[TestCase(1, new int[] { 1, 2, 3 }, "formation", "2020-09-30", "2020-11-30")]
[TestCase(1, new int[] { 1 }, "Formation Mainframe Complète", null, null)]
[TestCase(null, null, null, null, null)]
[TestCase(null, new int[] { 1 }, null, null, null)]
[TestCase(1, null, "for", null, null)]
[TestCase(1, new int[] { }, "for", null, null)]
public async Task GetFormationsCountAsync_PasseDesParamsValides_RetourneLeNombreTotalDeFormations(long? idAgence, int[] arrIdStatuts, string texte, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
List<int?> idStatuts;
if (arrIdStatuts != null)
idStatuts = arrIdStatuts.Select(x => (int?)x).ToList();
else
idStatuts = null;
// Act
long count = await formationService.GetFormationsCountAsync(idAgence, idStatuts, texte, dateDebut, dateFin);
// Assert
Assert.Less(0, count);
}
[TestCase(1, new int[] { 1 }, "azerty", null, null)]
[TestCase(-1, new int[] { -1 }, "azerty", null, null)]
[TestCase(0, new int[] { 0 }, null, null, null)]
public async Task GetFormationsCountAsync_PasseDesParamsInvalides_RetourneZero(long? idAgence, int[] arrIdStatuts, string texte, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
List<int?> idStatuts;
if (arrIdStatuts != null)
idStatuts = arrIdStatuts.Select(x => (int?)x).ToList();
else
idStatuts = null;
// Act
long count = await formationService.GetFormationsCountAsync(idAgence, idStatuts, texte, dateDebut, dateFin);
// Assert
Assert.AreEqual(0, count);
}
#endregion
#region Tests GetModesFormationAsync
[Test]
public async Task GetModesFormationAsync_RetourneTousLesModesDeFormation()
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<ModeFormationDTO> 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 GetOriginesFormationAsync
[Test]
public async Task GetOriginesAsyncFormation_RetourneToutesLesOriginesDeFormation()
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<OrigineFormationDTO> 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 GetStatutsFormationAsync
[Test]
public async Task GetStatutsFormationAsyncFormation_RetourneTousLesStatutsDeFormation()
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<StatutFormationDTO> 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 GetTypesFormationAsync
[Test]
public async Task GetTypesFormationAsync_RetourneTousLesTypesDeFormation()
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
IEnumerable<TypeFormationDTO> 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 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 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.IdModeFormation == idMode)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
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,
EstCertifiee = false
};
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// 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.EstCertifiee, formationAjoute.EstCertifiee);
}
[Test]
public void AddFormationAsync_AjouteUneFormationNull_LeveUneFormationInvalidException()
{
// Arrange
FormationDTO formation = null;
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// 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")]
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.IdModeFormation == idMode)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
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,
EstCertifiee = false
};
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// 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 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,
EstCertifiee = false
};
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
AsyncTestDelegate throwException = () => formationService.AddFormationAsync(formation);
// Assert
Assert.ThrowsAsync(typeof(FormationInvalidException), throwException);
}
#endregion
#region Tests UpdateFormationAsync
[TestCase(1, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")]
public async Task UpdateFormationAsync_ModifieUneFormationValide_FormationModifieeAvecSucces(long idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation);
formation.Intitule = intitule;
formation.IdAgence = idAgence;
formation.DateDebut = dateDebut;
formation.DateFin = dateFin;
formation.Heure = 2;
formation.Jour = 1;
formation.Mode = modeExterne;
formation.Type = typeELearning;
formation.Organisme = organisme;
formation.Origine = origineFormationCollaborateur;
formation.Statut = statutPlanifie;
formation.EstCertifiee = false;
// Act
FormationDTO formationModifie = await formationService.UpdateFormationAsync(idFormation, formation);
// Assert
Assert.IsNotNull(formationModifie);
Assert.AreEqual(idFormation, formationModifie.Id);
Assert.AreEqual(formation.Intitule, formationModifie.Intitule);
Assert.AreEqual(formation.IdAgence, formationModifie.IdAgence);
Assert.AreEqual(formation.DateDebut, formationModifie.DateDebut);
Assert.AreEqual(formation.DateFin, formationModifie.DateFin);
Assert.AreEqual(formation.Heure, formationModifie.Heure);
Assert.AreEqual(formation.Jour, formationModifie.Jour);
Assert.AreEqual(formation.Mode, formationModifie.Mode);
Assert.AreEqual(formation.Type, formationModifie.Type);
Assert.AreEqual(formation.Organisme, formationModifie.Organisme);
Assert.AreEqual(formation.Origine, formationModifie.Origine);
Assert.AreEqual(formation.Statut, formationModifie.Statut);
Assert.AreEqual(formation.EstCertifiee, formationModifie.EstCertifiee);
}
[TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-10-30")]
[TestCase(1, 0, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 0, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 0, 1, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 0, 1, "Test Formation", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 0, "Test Formation", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 1, "", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, null, "Test Formation", "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 1, null, "Apside", "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 1, "Test Formation", null, "2020-10-31", "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "Apside", null, "2020-11-02")]
[TestCase(1, 1, 1, 3, 1, 1, "Test Formation", "Apside", "2020-10-31", null)]
public async Task UpdateFormationAsync_ModifieUneFormationAvecDesProprietesInvalides_LeveUneFormationInvalidException(long idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation);
formation.Intitule = intitule;
formation.IdAgence = idAgence;
formation.DateDebut = dateDebut;
formation.DateFin = dateFin;
formation.Heure = 2;
formation.Jour = 1;
formation.Mode = modeExterne;
formation.Type = typeELearning;
formation.Organisme = organisme;
formation.Origine = origineFormationCollaborateur;
formation.Statut = statutPlanifie;
formation.EstCertifiee = false;
// Act
AsyncTestDelegate throwException = () => formationService.UpdateFormationAsync(idFormation, formation);
// Assert
Assert.ThrowsAsync(typeof(FormationInvalidException), throwException);
}
[TestCase(2, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")]
[TestCase(0, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")]
[TestCase(null, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")]
public async Task UpdateFormationAsync_ModifieUneFormationAvecUnIdIncorrecte_LeveUneFormationIncompatibleIdException(long? idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
FormationDTO formation = await formationService.GetFormationByIdAsync(1);
formation.Id = idFormation;
formation.Intitule = intitule;
formation.IdAgence = idAgence;
formation.DateDebut = dateDebut;
formation.DateFin = dateFin;
formation.Heure = 2;
formation.Jour = 1;
formation.Mode = modeExterne;
formation.Type = typeELearning;
formation.Organisme = organisme;
formation.Origine = origineFormationCollaborateur;
formation.Statut = statutPlanifie;
formation.EstCertifiee = false;
// Act
AsyncTestDelegate throwException = () => formationService.UpdateFormationAsync(1, formation);
// Assert
Assert.ThrowsAsync(typeof(FormationIncompatibleIdException), throwException);
}
[TestCase(0, 2, 2, 1, 1, 2, "Test modification formation", "Apside", "2020-10-31", "2020-11-04")]
public void UpdateFormationAsync_ModifieUneFormationAvecUnIdInexistant_RetourneUnObjetNull(long idFormation, int? idMode, int? idStatut, int? idType, int? idOrigine, long? idAgence, string intitule, string organisme, DateTime? dateDebut, DateTime? dateFin)
{
// Arrange
ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == idMode)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == idStatut)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == idType)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == idOrigine)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
FormationDTO formation = new FormationDTO
{
Id = idFormation,
Intitule = intitule,
IdAgence = idAgence,
DateDebut = dateDebut,
DateFin = dateFin,
Heure = 2,
Jour = 1,
Mode = modeExterne,
Type = typeELearning,
Organisme = organisme,
Origine = origineFormationCollaborateur,
Statut = statutPlanifie,
EstCertifiee = false
};
// Act
AsyncTestDelegate throwException = () => formationService.UpdateFormationAsync(idFormation, formation);
// Assert
Assert.ThrowsAsync(typeof(FormationNotFoundException), throwException);
}
#endregion
#region Tests DeleteFormationById et DeleteFormationByIdAsync
[TestCase(1)]
[TestCase(2)]
public async Task DeleteFormationByIdAsync_SupprimeUneFormationAvecUnIdValide_FormationSupprimeAvecSucces(long idFormation)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
bool existFormation = true;
// Act
FormationDTO formationSupprime = await formationService.DeleteFormationByIdAsync(idFormation);
existFormation = await epContext.Formation.AnyAsync(formation => formation.IdFormation == formationSupprime.Id.Value);
// Assert
Assert.IsFalse(existFormation);
}
[TestCase(0)]
[TestCase(-1)]
public void DeleteFormationByIdAsync_SupprimeUneFormationAvecUnIdInvalide_LeveUneFormationNotFoundException(long idFormation)
{
// Arrange
FormationService formationService = new FormationService(epContext, collaborateurService, transformDTO);
// Act
AsyncTestDelegate throwException = () => formationService.DeleteFormationByIdAsync(idFormation);
// Assert
Assert.ThrowsAsync(typeof(FormationNotFoundException), throwException);
}
#endregion
}
}