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/Controllers/FormationApiTests.cs

604 lines
26 KiB

using EPAServeur.Context;
using EPAServeur.Exceptions;
using EPAServeur.Models.Formation;
using EPAServeur.Services;
using IO.Swagger.Controllers;
using IO.Swagger.DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using EPAServeur.IServices;
using Moq;
namespace EPAServeur.Tests.Controllers
{
[TestFixture]
public class FormationApiTests
{
#region Variables
private IFormationService formationService;
private Mock<IWebHostEnvironment> mockEnvironment;
private EpContext epContext;
#endregion
#region Setup
[SetUp]
public void Setup()
{
// Cr<EFBFBD>ation d'une collection de services pour l'injection de d<EFBFBD>pendance
var services = new ServiceCollection();
// Utilisation d'une base de donn<EFBFBD>es en m<EFBFBD>moire
var optionBuider = new DbContextOptionsBuilder<EpContext>()
.UseInMemoryDatabase("server_ep_test")
.Options;
services.AddDbContext<EpContext>(b => b.UseInMemoryDatabase("server_ep_test"));
epContext = new EpContext(optionBuider);
epContext.Database.EnsureDeleted();
epContext.Database.EnsureCreated();
epContext.SaveChanges();
// Ajout du jeu de donn<EFBFBD>es pour les tests
DataSeeder.AddFormations(epContext);
// D<EFBFBD>tache les entit<EFBFBD>s du context car la base de donn<EFBFBD>es InMemory cr<EFBFBD><EFBFBD> des conflits
// entre les cl<EFBFBD>s primaires lors d'un Update ou d'un Insert
foreach (var entity in epContext.ChangeTracker.Entries())
{
entity.State = EntityState.Detached;
}
services.AddScoped<IFormationService, FormationService>();
// R<EFBFBD>cup<EFBFBD>re le service qui sera utils<EFBFBD> pour tester le contr<EFBFBD>leur
var serviceProvider = services.BuildServiceProvider();
formationService = serviceProvider.GetService<IFormationService>();
// Simule l'interface IWebHostEnvironment avec Moq
mockEnvironment = new Mock<IWebHostEnvironment>();
mockEnvironment
.Setup(m => m.EnvironmentName)
.Returns("Development");
}
#endregion
#region Tests GetFormationById
[Test]
public void GetById_PasseEnParamUnIdInconnu_RetourneUnObjetNotFoundResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var notFoundResult = formationsApiController.GetFormationById(99999);
// Assert
Assert.IsInstanceOf<NotFoundObjectResult>(notFoundResult.Result);
}
[Test]
public void GetFormationById_PasseEnParamUnIdConnu_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var okResult = formationsApiController.GetFormationById(1);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetFormationById_PasseEnParamUnIdConnu_RetourneLaBonneFormation()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
long idFormation = 1;
// Act
var okResult = formationsApiController.GetFormationById(idFormation).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<FormationDTO>(okResult.Value);
Assert.AreEqual(idFormation, (okResult.Value as FormationDTO).Id);
}
#endregion
#region Tests GetFormations
[Test]
public void GetFormations_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
List<int?> idStatuts = new List<int?> { 1, 2, 3 };
// Act
var okResult = formationsApiController.GetFormations(1, idStatuts, true, 1, 5, "formation", null, null, null);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetFormations_PasseDesParamsPresentsDansLaBDD_RetourneLesCinqPremieresFormations()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
List<int?> idStatuts = new List<int?> { 1, 2, 3 };
int nbFormation = 5;
int idFirstFormation = 5;
int idLastFormation = 1;
// Act
var okResult = formationsApiController.GetFormations(1, idStatuts, true, 1, 5, "formation", null, null, null).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<IEnumerable<FormationDTO>>(okResult.Value);
Assert.AreEqual(nbFormation, (okResult.Value as IEnumerable<FormationDTO>).Count());
Assert.AreEqual(idFirstFormation, (okResult.Value as IEnumerable<FormationDTO>).First().Id);
Assert.AreEqual(idLastFormation, (okResult.Value as IEnumerable<FormationDTO>).Last().Id);
}
#endregion
#region Tests GetFormationsCount
[Test]
public void GetFormationsCount_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
List<int?> idStatuts = new List<int?> { 1, 2, 3 };
// Act
var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetFormationsCount_PasseDesParamsPresentsDansLaBDD_RetourneLeBonNombreDeFormation()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
List<int?> idStatuts = new List<int?> { 1, 2, 3 };
int nbFormation = 5;
// Act
var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<long>(okResult.Value);
Assert.AreEqual(nbFormation, (long)okResult.Value);
}
#endregion
#region Tests GetModesFormation
[Test]
public void GetModesFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var okResult = formationsApiController.GetModesFormation();
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetModesFormation_RetourneTousLesModes()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int nbModeFormation = 4;
// Act
var okResult = formationsApiController.GetModesFormation().Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<IEnumerable<ModeFormationDTO>>(okResult.Value);
Assert.AreEqual(nbModeFormation, (okResult.Value as IEnumerable<ModeFormationDTO>).Count());
}
#endregion
#region Tests GetOriginesFormation
[Test]
public void GetOriginesFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var okResult = formationsApiController.GetOriginesFormation();
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetOriginesFormation_RetourneToutesLesOrigines()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int nbOrigineFormation = 4;
// Act
var okResult = formationsApiController.GetOriginesFormation().Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<IEnumerable<OrigineFormationDTO>>(okResult.Value);
Assert.AreEqual(nbOrigineFormation, (okResult.Value as IEnumerable<OrigineFormationDTO>).Count());
}
#endregion
#region Tests GetStatutsFormation
[Test]
public void GetStatusFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var okResult = formationsApiController.GetStatutsFormation();
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetStatutsFormation_RetourneTousLesStatuts()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int nbStatutFormation = 4;
// Act
var okResult = formationsApiController.GetStatutsFormation().Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<IEnumerable<StatutFormationDTO>>(okResult.Value);
Assert.AreEqual(nbStatutFormation, (okResult.Value as IEnumerable<StatutFormationDTO>).Count());
}
#endregion
#region Tests GetTypesFormation
[Test]
public void GetTypesFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var okResult = formationsApiController.GetTypesFormation();
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetTypesFormation_RetourneTousLesTypes()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int nbTypeFormation = 4;
// Act
var okResult = formationsApiController.GetTypesFormation().Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<IEnumerable<TypeFormationDTO>>(okResult.Value);
Assert.AreEqual(nbTypeFormation, (okResult.Value as IEnumerable<TypeFormationDTO>).Count());
}
#endregion
#region Tests AddFormation
[Test]
public void AddFormation_AjouteUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 0, Libelle = "Externe" };
StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifi<EFBFBD>e" };
TypeFormationDTO typeELearning = new TypeFormationDTO { Id = 3, Libelle = "E-learning" };
OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = 1, Libelle = "Demande collaborateur" };
FormationDTO formation = new FormationDTO
{
Intitule = "Test Formation",
IdAgence = 1,
DateDebut = new DateTime(2020, 10, 31),
DateFin = new DateTime(2020, 11, 02),
Heure = 2,
Jour = 1,
Mode = modeExterne,
Type = typeELearning,
Organisme = "Apside",
Origine = origineFormationCollaborateur,
Statut = statutPlanifie,
EstCertifiee = false
};
// Act
var objectResult = formationsApiController.AddFormation(formation);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public void AddFormation_AjouteUneFormationValide_RetourneUnObjetCreatedResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 1, Libelle = "Externe" };
StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifi<EFBFBD>e" };
TypeFormationDTO typeELearning = new TypeFormationDTO { Id = 3, Libelle = "E-learning" };
OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = 1, Libelle = "Demande collaborateur" };
FormationDTO formation = new FormationDTO
{
Intitule = "Test Formation",
IdAgence = 1,
DateDebut = new DateTime(2020, 10, 31),
DateFin = new DateTime(2020, 11, 02),
Heure = 2,
Jour = 1,
Mode = modeExterne,
Type = typeELearning,
Organisme = "Apside",
Origine = origineFormationCollaborateur,
Statut = statutPlanifie,
EstCertifiee = false
};
// Act
var createdResult = formationsApiController.AddFormation(formation);
// Assert
Assert.IsInstanceOf<CreatedResult>(createdResult.Result);
}
[Test]
public void AddFormation_AjouteUneFormationValide_RetourneLaFormationCreee()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 1, Libelle = "Externe" };
StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifi<EFBFBD>e" };
TypeFormationDTO typeELearning = new TypeFormationDTO { Id = 3, Libelle = "E-learning" };
OrigineFormationDTO origineFormationCollaborateur = new OrigineFormationDTO { Id = 1, Libelle = "Demande collaborateur" };
FormationDTO formation = new FormationDTO
{
Intitule = "Test Formation",
IdAgence = 1,
DateDebut = new DateTime(2020, 10, 31),
DateFin = new DateTime(2020, 11, 02),
Heure = 2,
Jour = 1,
Mode = modeExterne,
Type = typeELearning,
Organisme = "Apside",
Origine = origineFormationCollaborateur,
Statut = statutPlanifie,
EstCertifiee = false
};
// Act
var createdResult = formationsApiController.AddFormation(formation).Result as CreatedResult;
// Assert
Assert.IsInstanceOf<FormationDTO>(createdResult.Value);
Assert.AreEqual("Test Formation", (createdResult.Value as FormationDTO).Intitule);
}
#endregion
#region Tests UpdateFormation
[Test]
public async Task UpdateFormation_ModifieUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResultDansCatchFormationInvalidException()
{
// Arrange
long idFormation = 1;
string nouvelleIntitule = "Formation modifi<EFBFBD>e";
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation);
formation.Intitule = nouvelleIntitule;
formation.Mode.Id = 0;
formation.Statut.Id = 0;
// Act
var objectResult = formationsApiController.UpdateFormation(formation, idFormation);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public async Task UpdateFormation_ModifieUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResultDansCatchFormationIncompatibleIdException()
{
// Arrange
long idFormation = 1;
string nouvelleIntitule = "Formation modifi<EFBFBD>e";
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation);
idFormation = 2;
formation.Intitule = nouvelleIntitule;
// Act
var objectResult = formationsApiController.UpdateFormation(formation, idFormation);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public async Task UpdateFormation_ModifieUneFormatioInexistante_RetourneUnObjetObjectResultDansCatchFormationNotFoundExceptionn()
{
// Arrange
long idFormationExistant = 1;
long idFormationInexistant = 999;
string nouvelleIntitule = "Formation modifi<EFBFBD>e";
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormationExistant);
formation.Id = idFormationInexistant;
formation.Intitule = nouvelleIntitule;
// Act
var objectResult = formationsApiController.UpdateFormation(formation, idFormationInexistant);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public async Task UpdateFormation_ModifieUneFormationValide_RetourneUnObjetOkObjectResult()
{
// Arrange
long idFormation = 1;
string nouvelleIntitule = "Formation modifi<EFBFBD>e";
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object); ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == 2)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == 2)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == 1)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == 1)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation);
formation.Intitule = nouvelleIntitule;
formation.Mode = modeExterne;
formation.Type = typeELearning;
formation.Origine = origineFormationCollaborateur;
formation.Statut = statutPlanifie;
// Act
var okObjectResult = formationsApiController.UpdateFormation(formation, idFormation);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okObjectResult.Result);
}
[Test]
public async Task UpdateFormation_ModifieUneFormationValide_RetourneLaFormationModifiee()
{
// Arrange
long idFormation = 1;
string nouvelleIntitule = "Formation modifi<EFBFBD>e";
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
ModeFormationDTO modeExterne = epContext.ModeFormation.Where(mode => mode.IdModeFormation == 2)
.Select(mode => new ModeFormationDTO { Id = mode.IdModeFormation, Libelle = mode.Libelle }).FirstOrDefault();
StatutFormationDTO statutPlanifie = epContext.StatutFormation.Where(mode => mode.IdStatutFormation == 2)
.Select(mode => new StatutFormationDTO { Id = mode.IdStatutFormation, Libelle = mode.Libelle }).FirstOrDefault();
TypeFormationDTO typeELearning = epContext.TypeFormation.Where(mode => mode.IdTypeFormation == 1)
.Select(mode => new TypeFormationDTO { Id = mode.IdTypeFormation, Libelle = mode.Libelle }).FirstOrDefault();
OrigineFormationDTO origineFormationCollaborateur = epContext.OrigineFormation.Where(mode => mode.IdOrigineFormation == 1)
.Select(mode => new OrigineFormationDTO { Id = mode.IdOrigineFormation, Libelle = mode.Libelle }).FirstOrDefault();
FormationDTO formation = await formationService.GetFormationByIdAsync(idFormation);
formation.Intitule = nouvelleIntitule;
formation.Mode = modeExterne;
formation.Type = typeELearning;
formation.Origine = origineFormationCollaborateur;
formation.Statut = statutPlanifie;
// Act
var okObjectResult = formationsApiController.UpdateFormation(formation, idFormation).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<FormationDTO>(okObjectResult.Value);
Assert.AreEqual(nouvelleIntitule, (okObjectResult.Value as FormationDTO).Intitule);
}
#endregion
#region Tests DeleteFormationById
[Test]
public void DeleteFormation_PasseEnParamUnIdFormationInexistant_RetourneUnObjetNotFoundObjectResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int idFormation = 999;
// Act
var notFoundObjectResult = formationsApiController.DeleteFormation(idFormation);
// Assert
Assert.IsInstanceOf<NotFoundObjectResult>(notFoundObjectResult.Result);
}
[Test]
public void DeleteFormation_PasseEnParamUnIdFormationExistant_RetourneUnObjetNoContentResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int idFormation = 1;
// Act
var noContentResult = formationsApiController.DeleteFormation(idFormation);
// Assert
Assert.IsInstanceOf<NoContentResult>(noContentResult.Result);
}
[Test]
public async Task DeleteFormation_PasseEnParamUnIdFormationExistant_SupprimeBienUneFormation()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
int idFormation = 1;
long nbFormationAvantSuppression = await formationService.GetFormationsCountAsync(null, null, null, null, null, null, null);
// Act
var noContentResult = formationsApiController.DeleteFormation(idFormation).Result as NoContentResult;
// Assert
long nbFormationApresSuppression = await formationService.GetFormationsCountAsync(null, null, null, null, null, null, null);
Assert.AreEqual(nbFormationAvantSuppression -1, nbFormationApresSuppression);
}
#endregion
}
}