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

341 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<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.AddEngagements(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<ITransformDTO, TransformDTO>();
services.AddScoped<ICollaborateurApi, CollaborateurApi>();
services.AddScoped<ICollaborateurService, CollaborateurService>();
services.AddScoped<IEngagementService, EngagementService>();
// R<EFBFBD>cup<EFBFBD>re le service qui sera utils<EFBFBD> pour tester le contr<EFBFBD>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 = 8;
int idLastEngagement = 4;
// 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, 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 = 12;
// Act
var okResult = engagementsApiController.GetEngagementsCount(idBUs, 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 <EFBFBD> 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 <EFBFBD> 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 <EFBFBD> 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 <EFBFBD> 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
}
}