parent
a1ca683027
commit
74ca7f067b
@ -0,0 +1,340 @@ |
||||
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 |
||||
|
||||
} |
||||
} |
@ -0,0 +1,637 @@ |
||||
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 IO.Swagger.Enum; |
||||
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 EngagementServiceTests |
||||
{ |
||||
#region Variables |
||||
|
||||
private EpContext epContext; |
||||
private ICollaborateurApi collaborateurApi; |
||||
private ICollaborateurService collaborateurService; |
||||
#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(); |
||||
collaborateurService = new CollaborateurService(collaborateurApi, epContext); |
||||
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; |
||||
} |
||||
} |
||||
|
||||
#endregion |
||||
|
||||
#region Tests GetEngagementsAsync |
||||
|
||||
[TestCase(new long[] { 1, 2 })] |
||||
[TestCase(new long[] { 3 })] |
||||
public async Task GetEngagementsAsync_PasseDesParamsDesIdBUValides_RetourneDesEngagements(long[] arrIdBUs) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = arrIdBUs.Select(x => x).ToList(); |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, null, null, null, null, null); |
||||
|
||||
// Assert |
||||
Assert.Less(0, engagementDTOs.Count()); |
||||
} |
||||
|
||||
[TestCase(new EtatEngagement[] { EtatEngagement.EnAttente, EtatEngagement.Respecte })] |
||||
[TestCase(new EtatEngagement[] { EtatEngagement.DateLimitePassee })] |
||||
public async Task GetEngagementsAsync_PasseDesParamsDesEtatsDEngagementsValides_RetourneDesEngagements(EtatEngagement[] arrEtatsEngagement) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
List<EtatEngagement> etatEngagements = arrEtatsEngagement.Select(x => x).ToList(); |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, etatEngagements, null, null, null, null, null); |
||||
|
||||
// Assert |
||||
Assert.Less(0, engagementDTOs.Count()); |
||||
} |
||||
|
||||
[TestCase(1, 5)] |
||||
[TestCase(1, 10)] |
||||
public async Task GetEngagementsAsync_PasseEnParamNumPageEtParPage_RetourneLaPremierePageDesEngagements(int? numPage, int? parPage) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, null, numPage, parPage, null, null); |
||||
|
||||
// Assert |
||||
Assert.AreEqual(parPage, engagementDTOs.Count()); |
||||
} |
||||
|
||||
[TestCase(2, 5)] |
||||
[TestCase(2, 6)] |
||||
[TestCase(2, 10)] |
||||
[TestCase(2, 15)] |
||||
public async Task GetEngagementsAsync_PasseEnParamNumPageEtParPage_RetourneLaDeuxiemePageDesFormations(int? numPage, int? parPage) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
int? nbEngagementDeuxiemePage; |
||||
|
||||
switch (parPage) |
||||
{ |
||||
case 5: |
||||
nbEngagementDeuxiemePage = 5; |
||||
break; |
||||
case 6: |
||||
nbEngagementDeuxiemePage = 6; |
||||
break; |
||||
case 10: |
||||
nbEngagementDeuxiemePage = 2; |
||||
break; |
||||
default: |
||||
nbEngagementDeuxiemePage = 0; |
||||
break; |
||||
} |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, null, numPage, parPage, null, null); |
||||
|
||||
// Assert |
||||
Assert.AreEqual(nbEngagementDeuxiemePage, engagementDTOs.Count()); |
||||
} |
||||
|
||||
[TestCase(true, "action")] |
||||
[TestCase(true, null)] |
||||
[TestCase(true, "toto")] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParActionCroissant(bool? asc, string tri) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual("Je m'engage à attribuer 5 jours de congés supplémentaire.", engagementDTOs.First().Action); |
||||
Assert.AreEqual("Je m'engage à trouver une formation sur le Cobol.", engagementDTOs.Last().Action); |
||||
} |
||||
|
||||
[TestCase(false, "action")] |
||||
[TestCase(false, null)] |
||||
[TestCase(false, "toto")] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParActionDecroissant(bool? asc, string tri) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual("Je m'engage à trouver une formation sur le Cobol.", engagementDTOs.First().Action); |
||||
Assert.AreEqual("Je m'engage à attribuer 5 jours de congés supplémentaire.", engagementDTOs.Last().Action); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParDispositifCroissant() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
bool? asc = true; |
||||
string tri = "dispositif"; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual("Contacter le client pour planifier un rendez-vous.", engagementDTOs.First().Dispositif); |
||||
Assert.AreEqual("Planifier un point hebdomadaire sur Teams.", engagementDTOs.Last().Dispositif); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParDispositifDecroissant() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
bool? asc = false; |
||||
string tri = "dispositif"; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual("Planifier un point hebdomadaire sur Teams.", engagementDTOs.First().Dispositif); |
||||
Assert.AreEqual("Contacter le client pour planifier un rendez-vous.", engagementDTOs.Last().Dispositif); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParModaliteCroissante() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
bool? asc = true; |
||||
string tri = "modalite"; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual("Hors temps", engagementDTOs.First().Modalite); |
||||
Assert.AreEqual("Sur temps de travail", engagementDTOs.Last().Modalite); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParModaliteDecroissante() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
bool? asc = false; |
||||
string tri = "modalite"; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual("Sur temps de travail", engagementDTOs.First().Modalite); |
||||
Assert.AreEqual("Hors temps", engagementDTOs.Last().Modalite); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParDateCroissante() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
bool? asc = true; |
||||
string tri = "date"; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual(new DateTime(2020, 9, 1), engagementDTOs.First().DateLimite); |
||||
Assert.AreEqual(new DateTime(2021, 1, 31), engagementDTOs.Last().DateLimite); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task GetEngagementsAsync_PasseEnParamAscEtTri_RetourneDesEngagementsOrdonnancesParDateDecroissante() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>() { 1, 2, 3 }; |
||||
bool? asc = false; |
||||
string tri = "date"; |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, asc, null, null, null, tri); |
||||
|
||||
// Assert |
||||
Assert.AreEqual(new DateTime(2021, 1, 31), engagementDTOs.First().DateLimite); |
||||
Assert.AreEqual(new DateTime(2020, 9, 1), engagementDTOs.Last().DateLimite); |
||||
} |
||||
|
||||
[TestCase(new long[] { 0 }, null )] |
||||
[TestCase(new long[] { 4 }, null)] |
||||
[TestCase(new long[] { 1, 2 }, "azerty")] |
||||
|
||||
public async Task GetEngagementsAsync_PasseDesParamsInvalides_RetourneZeroEngagement(long[] arrIdBUs, string texte) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs; |
||||
|
||||
if (arrIdBUs != null) |
||||
idBUs = arrIdBUs.Select(x => x).ToList(); |
||||
else |
||||
idBUs = new List<long>(); |
||||
|
||||
// Act |
||||
IEnumerable<EngagementDTO> engagementDTOs = await engagementService.GetEngagementsAsync(idBUs, null, null, null, null, texte, null); |
||||
|
||||
// Assert |
||||
Assert.AreEqual(0, engagementDTOs.Count()); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetEngagementsAsync_PasseDesParamsUneListeDIdsBUVide_LeveUneEngagementInvalidException() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = new List<long>(); |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.GetEngagementsAsync(idBUs, null, null, null, null, null, null); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementInvalidException), throwException); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetEngagementsAsync_PasseDesParamsUneListeDIdsBUNull_LeveUneEngagementInvalidException() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs = null; |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.GetEngagementsAsync(idBUs, null, null, null, null, null, null); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementInvalidException), throwException); |
||||
} |
||||
|
||||
|
||||
#endregion |
||||
|
||||
#region Tests GetEngagementsCountAsync |
||||
|
||||
[TestCase(new long[] { 1, 2, 3 }, "formation")] |
||||
[TestCase(new long[] { 1 }, null)] |
||||
public async Task GetEngagementsCountAsync_PasseDesParamsValides_RetourneLeNombreTotalDEngagements(long[] arrIdBUs, string texte) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs; |
||||
|
||||
if (arrIdBUs != null) |
||||
idBUs = arrIdBUs.Select(x => x).ToList(); |
||||
else |
||||
idBUs = new List<long>(); |
||||
|
||||
// Act |
||||
long count = await engagementService.GetEngagementsCountAsync(idBUs, null, null, null, null, texte, null); |
||||
|
||||
// Assert |
||||
Assert.Less(0, count); |
||||
} |
||||
|
||||
[TestCase(new long[] { 0 }, null)] |
||||
[TestCase(new long[] { 4 }, null)] |
||||
[TestCase(new long[] { 1, 2 }, "azerty")] |
||||
public async Task GetEngagementsCountAsync_PasseDesParamsInvalides_RetourneZero(long[] arrIdBUs, string texte) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
List<long> idBUs; |
||||
|
||||
if (arrIdBUs != null) |
||||
idBUs = arrIdBUs.Select(x => x).ToList(); |
||||
else |
||||
idBUs = new List<long>(); |
||||
|
||||
// Act |
||||
long count = await engagementService.GetEngagementsCountAsync(idBUs, null, null, null, null, texte, null); |
||||
|
||||
// Assert |
||||
Assert.AreEqual(0, count); |
||||
} |
||||
|
||||
#endregion |
||||
|
||||
#region Tests RepondreEngagementAsync |
||||
|
||||
[TestCase(1, 9, EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 9, EtatEngagement.EnAttente, null)] |
||||
[TestCase(2, 9, EtatEngagement.NonRealisable, "Impossible de respecter cet engagement car hors budget.")] |
||||
public async Task RepondreEngagementAsync_ModifieUnEngagementValide_EngagementModifieAvecSucces(long idEngagement, long idEp, EtatEngagement etatEngagement, string raisonNonRealisable) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
|
||||
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 = etatEngagement, |
||||
RaisonNonRealisable = raisonNonRealisable, |
||||
Ep = epInformationDTO |
||||
}; |
||||
|
||||
// Act |
||||
EngagementDTO engagementModifie = await engagementService.RepondreEngagementAsync(engagementDTO, idEngagement); |
||||
|
||||
// Assert |
||||
Assert.IsNotNull(engagementModifie); |
||||
Assert.AreEqual(idEngagement, engagementModifie.Id); |
||||
Assert.AreEqual(engagementDTO.Id, engagementModifie.Id); |
||||
Assert.AreEqual(engagementDTO.EtatEngagement, engagementModifie.EtatEngagement); |
||||
Assert.AreEqual(engagementDTO.RaisonNonRealisable, engagementModifie.RaisonNonRealisable); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task RepondreEngagementAsync_ModifieUnEngagementAvecUneDateLimitePasseeValide_EngagementModifieAvecSucces() |
||||
{ |
||||
long idEngagement = 3; |
||||
long idEp = 9; |
||||
EtatEngagement etatEngagement = EtatEngagement.DateLimitePassee; |
||||
string raisonNonRealisableAvantUpdate = null; |
||||
string raisonNonRealisableApresUpdate = "La date limite pour respecter l'engagement est passée."; |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
|
||||
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 = etatEngagement, |
||||
RaisonNonRealisable = raisonNonRealisableAvantUpdate, |
||||
Ep = epInformationDTO |
||||
}; |
||||
|
||||
// Act |
||||
EngagementDTO engagementModifie = await engagementService.RepondreEngagementAsync(engagementDTO, idEngagement); |
||||
|
||||
// Assert |
||||
Assert.IsNotNull(engagementModifie); |
||||
Assert.AreEqual(idEngagement, engagementModifie.Id); |
||||
Assert.AreEqual(engagementDTO.Id, engagementModifie.Id); |
||||
Assert.AreEqual(engagementDTO.EtatEngagement, engagementModifie.EtatEngagement); |
||||
Assert.AreEqual(raisonNonRealisableApresUpdate, engagementModifie.RaisonNonRealisable); |
||||
} |
||||
|
||||
[TestCase(1, 0, "Je m'engage à trouver une formation sur l'Asp.Net Core.", "Demande de formation RH.", "Sur temps de travail", "2020-9-11", EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 11, "Je m'engage à trouver une formation sur l'Asp.Net Core.", "Demande de formation RH.", "Sur temps de travail", "2020-9-11", EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 9, "", "Demande de formation RH.", "Sur temps de travail", "2020-9-11", EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 9, "Je m'engage à trouver une formation sur l'Asp.Net Core.", "", "Sur temps de travail", "2020-9-11", EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 9, "Je m'engage à trouver une formation sur l'Asp.Net Core.", "Demande de formation RH.", "", "2020-9-11", EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 9, "Je m'engage à trouver une formation sur l'Asp.Net Core.", "Demande de formation RH.", "Sur temps de travail", null, EtatEngagement.Respecte, null)] |
||||
[TestCase(1, 9, "Je m'engage à trouver une formation sur l'Asp.Net Core.", "Demande de formation RH.", "Sur temps de travail", "2020-9-11", EtatEngagement.NonRealisable, null)] |
||||
public void RepondreEngagementAsync_ModifieUnEngagementAvecDesProprietesInvalide_LeveUneEngagementInvalidException(long idEngagement, long idEp, string action, string dispositif, string modalite, DateTime? dateLimite, EtatEngagement etatEngagement, string raisonNonRealisable) |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
|
||||
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 = action, |
||||
Dispositif = dispositif, |
||||
Modalite = modalite, |
||||
DateLimite = dateLimite, |
||||
EtatEngagement = etatEngagement, |
||||
RaisonNonRealisable = raisonNonRealisable, |
||||
Ep = epInformationDTO |
||||
}; |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.RepondreEngagementAsync(engagementDTO, idEngagement); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementInvalidException), throwException); |
||||
} |
||||
|
||||
[Test] |
||||
public void RepondreEngagementAsync_ModifieUnEngagementNull_LeveUneEngagementInvalidException() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
EngagementDTO engagementDTO = null; |
||||
long idEngagement = 1; |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.RepondreEngagementAsync(engagementDTO, idEngagement); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementInvalidException), throwException); |
||||
} |
||||
|
||||
[Test] |
||||
public void RepondreEngagementAsync_ModifieUnEngagementAvecUnEPInexistantDansLaBDD_LeveUneEngagementInvalidException() |
||||
{ |
||||
// Arrange |
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
long idEngagement = 1; |
||||
EpInformationDTO epInformationDTO = new EpInformationDTO |
||||
{ |
||||
Id = 999, |
||||
Type = TypeEp.EPA, |
||||
Statut = StatutEp.Signe, |
||||
DateDisponibilite = DateTime.Now, |
||||
DatePrevisionnelle = DateTime.Now, |
||||
Obligatoire = false |
||||
}; |
||||
|
||||
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 = EtatEngagement.Respecte, |
||||
RaisonNonRealisable = null, |
||||
Ep = epInformationDTO |
||||
}; |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.RepondreEngagementAsync(engagementDTO, idEngagement); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementInvalidException), throwException); |
||||
} |
||||
|
||||
|
||||
|
||||
[TestCase(1)] |
||||
[TestCase(null)] |
||||
public void RepondreEngagementAsync_ModifieUnEngagementAvecUnIdIncorrecte_LeveUneEngagementnIncompatibleIdException(long? idEngagement) |
||||
{ |
||||
// Arrange |
||||
long idEngagementIncorrecte = 2; |
||||
long idEp = 9; |
||||
|
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
|
||||
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 = EtatEngagement.Respecte, |
||||
RaisonNonRealisable = null, |
||||
Ep = epInformationDTO |
||||
}; |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.RepondreEngagementAsync(engagementDTO, idEngagementIncorrecte); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementIncompatibleIdException), throwException); |
||||
} |
||||
|
||||
[Test] |
||||
public void RepondreEngagementAsync_ModifieUnEngagementAvecUnIdInexistant_LeveUneEngagementNotFoundException() |
||||
{ |
||||
// Arrange |
||||
long idEngagement = 0; |
||||
long idEp = 9; |
||||
|
||||
EngagementService engagementService = new EngagementService(epContext, collaborateurService); |
||||
|
||||
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 = EtatEngagement.Respecte, |
||||
RaisonNonRealisable = null, |
||||
Ep = epInformationDTO |
||||
}; |
||||
|
||||
// Act |
||||
AsyncTestDelegate throwException = () => engagementService.RepondreEngagementAsync(engagementDTO, idEngagement); |
||||
|
||||
// Assert |
||||
Assert.ThrowsAsync(typeof(EngagementNotFoundException), throwException); |
||||
} |
||||
#endregion |
||||
} |
||||
} |
Loading…
Reference in new issue