using EPAServeur.Context; using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Models.EP; 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.Text; using System.Threading.Tasks; namespace EPAServeur.Tests.Services { [TestFixture] public class DemandeDelegationServiceTests { #region variables private EpContext context; private ICollaborateurApi collaborateurApi; #endregion #region Setup [SetUp] public void Setup() { // Utilisation d'une base de données en mémoire var optionBuider = new DbContextOptionsBuilder() .UseInMemoryDatabase("server_ep_test") .Options; context = new EpContext(optionBuider); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.SaveChanges(); DataSeeder.AddDemandeDelegationEP(context); // Ajout du jeu de données pour les tests //TO DO // 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 context.ChangeTracker.Entries()) { entity.State = EntityState.Detached; } collaborateurApi = new CollaborateurApi(); } #endregion #region Test RecuperationDemandeDelegation [TestCase("771f9129-5610-4f4b-a8eb-902a98d722be", 0)] [TestCase("01ee85ff-d7f3-494b-b1de-26ced8fbfa0d", 3)] [TestCase("0afd1573-de75-4e5b-bfcb-1985259d8370", 1)] [TestCase("de98a866-736f-4295-a669-92a8694e2ee3", 1)] [TestCase("17b87130-0e9d-4b78-b0e3-a11e5f70318d", 1)] public async Task RecupererDemandesDelegation_OK(Guid? idReferent, int count) { IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); IEnumerable demandesDelegation = await demandeDelegationService.RecupererDemandesDelegation(idReferent); foreach (DemandeDelegationDTO demandeDelegation in demandesDelegation) { Assert.AreEqual(idReferent, demandeDelegation.Referent.Id); Assert.AreEqual(demandeDelegation.EtatDemande, EtatDemande.EnAttente); } Assert.AreEqual(count, demandesDelegation.Count()); } [Test] public void RecupererDemandeDelegation_ReferentNotFound() { Guid? idReferent = new Guid(); IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); AsyncTestDelegate exception = () => demandeDelegationService.RecupererDemandesDelegation(idReferent); Assert.ThrowsAsync(typeof(ReferentNotFoundException), exception); } #endregion #region Reponse demande delegation [TestCase(1)] [TestCase(2)] [TestCase(5)] public async Task UpdateDemandeDelegation_ReponsePositive(long id) { IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); DemandeDelegation demandeDelegation = await context.DemandeDelegations.FindAsync(id); long idEp = demandeDelegation.IdEP; Ep ep = await context.Ep.FindAsync(idEp); Guid idReferent = demandeDelegation.IdReferent; Assert.AreNotEqual(ep.IdReferent, idReferent); DemandeDelegationDTO demandeDTO = new DemandeDelegationDTO() { Id = id, DateDemande = demandeDelegation.DateDemande, RaisonDemande = demandeDelegation.RaisonDemande, DateReponse = DateTime.Now, EtatDemande = EtatDemande.Validee }; DemandeDelegationDTO reponse = await demandeDelegationService.UpdateDemandeDelegation(id, demandeDTO); demandeDelegation = await context.DemandeDelegations.FindAsync(id); Assert.IsNull(reponse); Assert.IsNull(demandeDelegation); ep = await context.Ep.FindAsync(idEp); Assert.AreEqual(ep.IdReferent, idReferent); } [TestCase(3)] [TestCase(4)] [TestCase(6)] public async Task UpdateDemandeDelegation_ReponseNegative(long id) { IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); DemandeDelegation demandeDelegation = await context.DemandeDelegations.FindAsync(id); long idEp = demandeDelegation.IdEP; Ep ep = await context.Ep.FindAsync(idEp); Guid idReferent = demandeDelegation.IdReferent; Assert.AreNotEqual(ep.IdReferent, idReferent); DemandeDelegationDTO demandeDTO = new DemandeDelegationDTO() { Id = id, DateDemande = demandeDelegation.DateDemande, RaisonDemande = demandeDelegation.RaisonDemande, DateReponse = DateTime.Now, EtatDemande = EtatDemande.Rejetee, RaisonRefus = "exemple raison refus" }; DemandeDelegationDTO reponse = await demandeDelegationService.UpdateDemandeDelegation(id, demandeDTO); Assert.IsNotNull(reponse); Assert.AreEqual(reponse.EtatDemande, EtatDemande.Rejetee); ep = await context.Ep.FindAsync(idEp); Assert.AreNotEqual(ep.IdReferent, idReferent); } [TestCase(-5, EtatDemande.Validee, null)] [TestCase(-5, EtatDemande.Rejetee, "exemple refus")] [TestCase(0, EtatDemande.Validee, null)] [TestCase(0, EtatDemande.Rejetee, "exemple refus")] [TestCase(10, EtatDemande.Validee, null)] [TestCase(10, EtatDemande.Rejetee, "exemple refus")] public void UpdateDemandeDelegation_DemandeNonExistante(long id, EtatDemande etatDemande, string raisonRefus) { IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); DemandeDelegationDTO demandeDTO = new DemandeDelegationDTO() { Id = id, DateDemande = DateTime.Now, RaisonDemande = "exemple raison demande", DateReponse = DateTime.Now, EtatDemande = etatDemande, RaisonRefus = raisonRefus }; AsyncTestDelegate exception = () => demandeDelegationService.UpdateDemandeDelegation(id, demandeDTO); Assert.ThrowsAsync(typeof(DemandeDelegationNotFoundException), exception); } [TestCase(1, 4,EtatDemande.Validee, null)] [TestCase(2, 3,EtatDemande.Rejetee, "exemple refus")] [TestCase(3, 2,EtatDemande.Validee, null)] [TestCase(4, 1,EtatDemande.Rejetee, "exemple refus")] public void UpdateDemandeDelegation_DemandeIncompatible(long id, long idIncompatible, EtatDemande etatDemande, string raisonRefus) { IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); DemandeDelegationDTO demandeDTO = new DemandeDelegationDTO() { Id = id, DateDemande = DateTime.Now, RaisonDemande = "exemple raison demande", DateReponse = DateTime.Now, EtatDemande = etatDemande, RaisonRefus = raisonRefus }; AsyncTestDelegate exception = () => demandeDelegationService.UpdateDemandeDelegation(idIncompatible, demandeDTO); Assert.ThrowsAsync(typeof(DemandeDelegationIncompatibleException), exception); } [TestCase(7, EtatDemande.EnAttente, null)] [TestCase(7, EtatDemande.EnAttente, "raison")] [TestCase(7, EtatDemande.EnAttente, "")] [TestCase(7, EtatDemande.Rejetee, null)] [TestCase(7, EtatDemande.Rejetee, "")] public async Task UpdateDemandeDelegation_DemandeInvalid(long id, EtatDemande etatDemande, string raisonRefus) { IDemandeDelegationService demandeDelegationService = new DemandeDelegationService(collaborateurApi, context); DemandeDelegation demandeDelegation = await context.DemandeDelegations.FindAsync(id); long idEp = demandeDelegation.IdEP; Ep ep = await context.Ep.FindAsync(idEp); Guid idReferent = demandeDelegation.IdReferent; Assert.AreNotEqual(ep.IdReferent, idReferent); DemandeDelegationDTO demandeDTO = new DemandeDelegationDTO() { Id = id, DateDemande = demandeDelegation.DateDemande, RaisonDemande = demandeDelegation.RaisonDemande, DateReponse = DateTime.Now, EtatDemande = etatDemande, RaisonRefus = raisonRefus }; AsyncTestDelegate exception = () => demandeDelegationService.UpdateDemandeDelegation(id, demandeDTO); Assert.ThrowsAsync(typeof(DemandeDelegationInvalidException), exception); } #endregion } }