Ajout des TU pour l'api formation

develop
jboinembalome 4 years ago
parent db7eff40a8
commit 23f8b69940
  1. 524
      EPAServeur.Tests/Controllers/FormationApiTests.cs

@ -13,6 +13,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; 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 namespace EPAServeur.Tests.Controllers
{ {
@ -21,9 +26,9 @@ namespace EPAServeur.Tests.Controllers
{ {
#region Variables #region Variables
private FormationService formationService; private IFormationService formationService;
private readonly ILogger<FormationsApiController> logger; private Mock<IWebHostEnvironment> mockEnvironment;
private EpContext epContext;
#endregion #endregion
#region Setup #region Setup
@ -31,12 +36,17 @@ namespace EPAServeur.Tests.Controllers
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
// Création d'une collection de services pour l'injection de dépendance
var services = new ServiceCollection();
// Utilisation d'une base de données en mémoire // Utilisation d'une base de données en mémoire
var optionBuider = new DbContextOptionsBuilder<EpContext>() var optionBuider = new DbContextOptionsBuilder<EpContext>()
.UseInMemoryDatabase("server_ep_test") .UseInMemoryDatabase("server_ep_test")
.Options; .Options;
EpContext epContext = new EpContext(optionBuider); services.AddDbContext<EpContext>(b => b.UseInMemoryDatabase("server_ep_test"));
epContext = new EpContext(optionBuider);
epContext.Database.EnsureDeleted(); epContext.Database.EnsureDeleted();
epContext.Database.EnsureCreated(); epContext.Database.EnsureCreated();
@ -52,8 +62,20 @@ namespace EPAServeur.Tests.Controllers
entity.State = EntityState.Detached; entity.State = EntityState.Detached;
} }
// Instanciation du service qui sera utilisé dans le controleur
formationService = new FormationService(epContext); services.AddScoped<IFormationService, FormationService>();
// Récupère le service qui sera utilsé pour tester le contrô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 #endregion
@ -61,127 +83,521 @@ namespace EPAServeur.Tests.Controllers
#region Tests GetFormationById #region Tests GetFormationById
[Test] [Test]
public void GetFormationById_PasseEnParamUnIdPresentDansLaBDD_RetourneUnObjetOkResult() public void GetById_PasseEnParamUnIdInconnu_RetourneUnObjetNotFoundResult()
{ {
//// Arrange // Arrange
//FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>()); FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// // Act // Act
// var okResult = formationsApiController.GetFormationById(1); var notFoundResult = formationsApiController.GetFormationById(99999);
//// Assert // Assert
//Assert.IsInstanceOf<OkObjectResult>(okResult.Result); Assert.IsInstanceOf<NotFoundObjectResult>(notFoundResult.Result);
} }
[Test]
public void GetFormationById_PasseEnParamUnIdConnu_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
#endregion // Act
var okResult = formationsApiController.GetFormationById(1);
#region Tests GetFormations // Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
// Arrange
// Act [Test]
public void GetFormationById_PasseEnParamUnIdConnu_RetourneLaBonneFormation()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
long idFormation = 1;
// Assert // Act
var okResult = formationsApiController.GetFormationById(idFormation).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<FormationDTO>(okResult.Value);
Assert.AreEqual(idFormation, (okResult.Value as FormationDTO).Id);
}
#endregion #endregion
#region Tests GetFormationAnnulees #region Tests GetFormations
// Arrange
// Act [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 };
// Assert // Act
var okResult = formationsApiController.GetFormations(1, idStatuts, true, 1, 5, "formation", null, null, null);
#endregion // Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
#region Tests GetFormationRealisee [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 = 1;
int idLastFormation = 5;
// 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);
}
// Arrange #endregion
// Act #region Tests GetFormationsCount
// Assert [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 };
#endregion // Act
var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null);
#region Tests GetProchainesFormation // Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
// Arrange [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 // Act
var okResult = formationsApiController.GetFormationsCount(1, idStatuts, 1, 5, "formation", null, null).Result as OkObjectResult;
// Assert // Assert
Assert.IsInstanceOf<long>(okResult.Value);
Assert.AreEqual(nbFormation, (long)okResult.Value);
}
#endregion #endregion
#region Tests GetModesFormation #region Tests GetModesFormation
// Arrange
// Act [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);
}
// Assert [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 #endregion
#region Tests GetOriginesFormation #region Tests GetOriginesFormation
// Arrange [Test]
public void GetOriginesFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act // Act
var okResult = formationsApiController.GetOriginesFormation();
// Assert // 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 #endregion
#region Tests GetStatutsFormation #region Tests GetStatutsFormation
// Arrange [Test]
public void GetStatusFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act
var okResult = formationsApiController.GetStatutsFormation();
// Act // Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
// Assert [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 #endregion
#region Tests GetTypesFormation #region Tests GetTypesFormation
// Arrange [Test]
public void GetTypesFormation_RetourneUnObjetOkResult()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Act // Act
var okResult = formationsApiController.GetTypesFormation();
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
// Assert [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 #endregion
#region Tests AddFormation #region Tests AddFormation
// Arrange [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é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é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);
}
// Act [Test]
public void AddFormation_AjouteUneFormationValide_RetourneLaFormationCreee()
{
// Arrange
FormationsApiController formationsApiController = new FormationsApiController(formationService, new NullLogger<FormationsApiController>(), mockEnvironment.Object);
// Assert ModeFormationDTO modeExterne = new ModeFormationDTO { Id = 1, Libelle = "Externe" };
StatutFormationDTO statutPlanifie = new StatutFormationDTO { Id = 1, Libelle = "Planifié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 #endregion
#region Tests UpdateFormation #region Tests UpdateFormation
// Arrange [Test]
public async Task UpdateFormation_ModifieUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResultDansCatchFormationInvalidException()
{
// Arrange
long idFormation = 1;
string nouvelleIntitule = "Formation modifié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 // Act
var objectResult = formationsApiController.UpdateFormation(formation, idFormation);
// Assert // Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public async Task UpdateFormation_ModifieUneFormationAvecDesObjetsEnfantsInvalides_RetourneUnObjetObjectResultDansCatchFormationIncompatibleIdException()
{
// Arrange
long idFormation = 1;
string nouvelleIntitule = "Formation modifié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é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é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é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 #endregion
#region Tests DeleteFormationById #region Tests DeleteFormationById
// Arrange [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 // 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);
}
// Assert
#endregion #endregion
} }

Loading…
Cancel
Save