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/EngagementsApiTests.cs

340 lines
15 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;
using IO.Swagger.ApiCollaborateur;
using IO.Swagger.Enum;
namespace EPAServeur.Tests.Controllers
{
[TestFixture]
public class EngagementsApiTests
{
#region Variables
private IEngagementService engagementService;
private Mock<IWebHostEnvironment> mockEnvironment;
private EpContext epContext;
#endregion
#region Setup
[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
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ées pour les tests
DataSeeder.AddEngagements(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;
}
services.AddScoped<ICollaborateurApi, CollaborateurApi>();
services.AddScoped<ICollaborateurService, CollaborateurService>();
services.AddScoped<IEngagementService, EngagementService>();
// Récupère le service qui sera utilsé pour tester le contrôleur
var serviceProvider = services.BuildServiceProvider();
engagementService = serviceProvider.GetService<IEngagementService>();
// Simule l'interface IWebHostEnvironment avec Moq
mockEnvironment = new Mock<IWebHostEnvironment>();
mockEnvironment
.Setup(m => m.EnvironmentName)
.Returns("Development");
}
#endregion
#region Tests GetEngagements
[Test]
public void GetEngagements_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult()
{
// Arrange
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
List<long> idBUs = new List<long> { 1, 2 };
// Act
var okResult = engagementsApiController.GetEngagements(idBUs, null, true, 1, 5, null, null);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetEngagements_PasseDesParamsPresentsDansLaBDD_RetourneLesCinqPremiersEngagements()
{
// Arrange
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
List<long> idBUs = new List<long> { 1, 2 };
int nbEngagement = 5;
int idFirstEngagement = 2;
int idLastEngagement = 10;
// Act
var okResult = engagementsApiController.GetEngagements(idBUs, null, true, 1, 5, null, null).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<IEnumerable<EngagementDTO>>(okResult.Value);
Assert.AreEqual(nbEngagement, (okResult.Value as IEnumerable<EngagementDTO>).Count());
Assert.AreEqual(idFirstEngagement, (okResult.Value as IEnumerable<EngagementDTO>).First().Id);
Assert.AreEqual(idLastEngagement, (okResult.Value as IEnumerable<EngagementDTO>).Last().Id);
}
#endregion
#region Tests GetEngagementsCount
[Test]
public void GetEngagementsCount_PasseDesParamsPresentsDansLaBDD_RetourneUnObjetOkResult()
{
// Arrange
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
List<long> idBUs = new List<long> { 1, 2 };
// Act
var okResult = engagementsApiController.GetEngagementsCount(idBUs, null, true, 1, 5, null, null);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okResult.Result);
}
[Test]
public void GetEngagementsCount_PasseDesParamsPresentsDansLaBDD_RetourneLeBonNombreDEngagement()
{
// Arrange
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
List<long> idBUs = new List<long> { 1, 2 };
int nbEngagement = 5;
// Act
var okResult = engagementsApiController.GetEngagementsCount(idBUs, null, true, 1, 5, null, null).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<long>(okResult.Value);
Assert.AreEqual(nbEngagement, (long)okResult.Value);
}
#endregion
#region Tests UpdateEngagement
[Test]
public void UpdateEngagement_ModifieUnEngagementAvecUnEPInvalide_RetourneUnObjetObjectResultDansCatchEngagementInvalidException()
{
// Arrange
long idEngagement = 1;
EtatEngagement reponse = EtatEngagement.Respecte;
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
EpInformationDTO epInformationDTO = null;
EngagementDTO engagementDTO = new EngagementDTO
{
Id = idEngagement,
Action = "Je m'engage à trouver une formation sur l'Asp.Net Core.",
Dispositif = "Demande de formation RH.",
Modalite = "Sur temps de travail",
DateLimite = new DateTime(2020, 9, 1),
EtatEngagement = reponse,
Ep = epInformationDTO
};
// Act
var objectResult = engagementsApiController.UpdateEngagement(engagementDTO, idEngagement);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public void UpdateEngagement_ModifieUnEngagementAvecUnIdEngagementInvalide_RetourneUnObjetObjectResultDansCatchEngagementIncompatibleIdException()
{
// Arrange
long idEngagement = 1;
long idEngagementIncorrecte = 2;
long idEp = 9;
EtatEngagement reponse = EtatEngagement.Respecte;
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
EpInformationDTO epInformationDTO = epContext.Ep.Where(ep => ep.IdEP == idEp)
.Select(ep => new EpInformationDTO
{
Id = ep.IdEP,
Type = ep.TypeEP,
Statut = ep.Statut,
DateDisponibilite = ep.DateDisponibilite,
DatePrevisionnelle = ep.DatePrevisionnelle,
Obligatoire = ep.Obligatoire
}).FirstOrDefault();
EngagementDTO engagementDTO = new EngagementDTO
{
Id = idEngagementIncorrecte,
Action = "Je m'engage à trouver une formation sur l'Asp.Net Core.",
Dispositif = "Demande de formation RH.",
Modalite = "Sur temps de travail",
DateLimite = new DateTime(2020, 9, 1),
EtatEngagement = reponse,
Ep = epInformationDTO
};
// Act
var objectResult = engagementsApiController.UpdateEngagement(engagementDTO, idEngagement);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public void UpdateEngagement_ModifieUnEngagementInexistant_RetourneUnObjetObjectResultDansCatchEngagementNotFoundExceptionn()
{
// Arrange
long idEngagementInexistant = 999;
long idEp = 9;
EtatEngagement reponse = EtatEngagement.Respecte;
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
EpInformationDTO epInformationDTO = epContext.Ep.Where(ep => ep.IdEP == idEp)
.Select(ep => new EpInformationDTO
{
Id = ep.IdEP,
Type = ep.TypeEP,
Statut = ep.Statut,
DateDisponibilite = ep.DateDisponibilite,
DatePrevisionnelle = ep.DatePrevisionnelle,
Obligatoire = ep.Obligatoire
}).FirstOrDefault();
EngagementDTO engagementDTO = new EngagementDTO
{
Id = idEngagementInexistant,
Action = "Toto.",
Dispositif = "Tata.",
Modalite = "Titi",
DateLimite = new DateTime(2020, 9, 1),
EtatEngagement = reponse,
Ep = epInformationDTO
};
// Act
var objectResult = engagementsApiController.UpdateEngagement(engagementDTO, idEngagementInexistant);
// Assert
Assert.IsInstanceOf<ObjectResult>(objectResult.Result);
}
[Test]
public void UpdateEngagement_ModifieUnEngagementValide_RetourneUnObjetOkObjectResult()
{
// Arrange
long idEngagement = 1;
long idEp = 9;
EtatEngagement reponse = EtatEngagement.Respecte;
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
EpInformationDTO epInformationDTO = epContext.Ep.Where(ep => ep.IdEP == idEp)
.Select(ep => new EpInformationDTO
{
Id = ep.IdEP,
Type = ep.TypeEP,
Statut = ep.Statut,
DateDisponibilite = ep.DateDisponibilite,
DatePrevisionnelle = ep.DatePrevisionnelle,
Obligatoire = ep.Obligatoire
}).FirstOrDefault();
EngagementDTO engagementDTO = new EngagementDTO
{
Id = idEngagement,
Action = "Je m'engage à trouver une formation sur l'Asp.Net Core.",
Dispositif = "Demande de formation RH.",
Modalite = "Sur temps de travail",
DateLimite = new DateTime(2020, 9, 1),
EtatEngagement = reponse,
Ep = epInformationDTO
};
// Act
var okObjectResult = engagementsApiController.UpdateEngagement(engagementDTO, idEngagement);
// Assert
Assert.IsInstanceOf<OkObjectResult>(okObjectResult.Result);
}
[Test]
public void UpdateEngagement_ModifieUnEngagementValide_RetourneLEngagementModifie()
{
// Arrange
long idEngagement = 1;
long idEp = 9;
EtatEngagement reponse = EtatEngagement.Respecte;
EngagementsApiController engagementsApiController = new EngagementsApiController(engagementService, new NullLogger<EngagementsApiController>(), mockEnvironment.Object);
EpInformationDTO epInformationDTO = epContext.Ep.Where(ep => ep.IdEP == idEp)
.Select(ep => new EpInformationDTO
{
Id = ep.IdEP,
Type = ep.TypeEP,
Statut = ep.Statut,
DateDisponibilite = ep.DateDisponibilite,
DatePrevisionnelle = ep.DatePrevisionnelle,
Obligatoire = ep.Obligatoire
}).FirstOrDefault();
EngagementDTO engagementDTO = new EngagementDTO
{
Id = idEngagement,
Action = "Je m'engage à trouver une formation sur l'Asp.Net Core.",
Dispositif = "Demande de formation RH.",
Modalite = "Sur temps de travail",
DateLimite = new DateTime(2020, 9, 1),
EtatEngagement = reponse,
Ep = epInformationDTO
};
// Act
var okObjectResult = engagementsApiController.UpdateEngagement(engagementDTO, idEngagement).Result as OkObjectResult;
// Assert
Assert.IsInstanceOf<EngagementDTO>(okObjectResult.Value);
Assert.AreEqual(reponse, (okObjectResult.Value as EngagementDTO).EtatEngagement);
}
#endregion
}
}