using EPAServeur.Context; using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Services; using IO.Swagger.ApiCollaborateur; using IO.Swagger.ClientCollaborateur; using IO.Swagger.DTO; using Microsoft.EntityFrameworkCore; using NUnit.Framework; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace EPAServeur.Tests.Services { [TestFixture] public class ReferentEPTests { #region Variables private EpContext context; private ICollaborateurApi collaborateurApi; private ICollaborateurService collaborateurService; private Guid? referent1, referent2, referent3; private Guid? collaborateur1, collaborateur2, collaborateur3; private Guid? collaborateurNonExistant, collaborateurParti; #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(); foreach (var entity in context.ChangeTracker.Entries()) { entity.State = EntityState.Detached; } collaborateurApi = new CollaborateurApi(); collaborateurService = new CollaborateurService(collaborateurApi, context); referent1 = new Guid("ecf528c3-e509-402f-87bb-c8821467e350"); referent2 = new Guid("6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb"); referent3 = new Guid("23d629cd-df09-404a-a6ff-81341db55f29"); collaborateur1 = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"); collaborateur2 = new Guid("9e1ee839-4477-4d64-9b4d-80654c97c39f"); collaborateur3 = new Guid("97e606a1-2fef-4cbf-b6a9-00366fabc412"); collaborateurNonExistant = new Guid(); collaborateurParti = new Guid("3c99214d-0a5e-4bb6-b7b2-7d9bb8143c50"); } #endregion #region Ajout pour un collaborateur [Test] public async Task UpdateReferentUnCollaborateur() { ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi); CollaborateurDTO collaborateurDTO1 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1); CollaborateurDTO collaborateurDTO2 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur2); CollaborateurDTO collaborateurDTO3 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur3); ReferentEPDTO referentEPDTO1 = new ReferentEPDTO() { IdReferent = referent1, IdsCollaborateur = new List(), }; referentEPDTO1.IdsCollaborateur.Add(collaborateur1); ReferentEPDTO referentEPDTO2 = new ReferentEPDTO() { IdReferent = referent2, IdsCollaborateur = new List(), }; referentEPDTO2.IdsCollaborateur.Add(collaborateur2); ReferentEPDTO referentEPDTO3 = new ReferentEPDTO() { IdReferent = referent3, IdsCollaborateur = new List(), }; referentEPDTO3.IdsCollaborateur.Add(collaborateur3); await referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO1, collaborateur1); await referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO2, collaborateur2); await referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO3, collaborateur3); Assert.IsNotNull(collaborateurDTO1); Assert.IsNull(collaborateurDTO1.Referent); Assert.IsNotNull(collaborateurDTO2); Assert.IsNull(collaborateurDTO2.Referent); Assert.IsNotNull(collaborateurDTO3); Assert.IsNull(collaborateurDTO3.Referent); collaborateurDTO1 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1); collaborateurDTO2 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur2); collaborateurDTO3 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur3); Assert.IsNotNull(collaborateurDTO1); Assert.IsNotNull(collaborateurDTO1.Referent); Assert.AreEqual(collaborateurDTO1.Referent.Id, referent1); Assert.IsNotNull(collaborateurDTO2); Assert.IsNotNull(collaborateurDTO2.Referent); Assert.AreEqual(collaborateurDTO2.Referent.Id, referent2); Assert.IsNotNull(collaborateurDTO3); Assert.IsNotNull(collaborateurDTO3.Referent); Assert.AreEqual(collaborateurDTO3.Referent.Id, referent3); } [Test] public void UpdateReferentCollaborateur_CollaborateurIncompatibleException() { ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi); ReferentEPDTO referentEPDTO1 = new ReferentEPDTO() { IdReferent = referent1, IdsCollaborateur = new List(), }; referentEPDTO1.IdsCollaborateur.Add(collaborateur1); ReferentEPDTO referentEPDTO2 = new ReferentEPDTO() { IdReferent = referent1, IdsCollaborateur = new List(), }; referentEPDTO2.IdsCollaborateur.Add(null); AsyncTestDelegate exception1 = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO1, collaborateur2); Assert.ThrowsAsync(typeof(CollaborateurIncompatibleException), exception1); AsyncTestDelegate exception2 = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO2, null); Assert.ThrowsAsync(typeof(CollaborateurIncompatibleException), exception2); } [Test] public void UpdateReferentCollaborateur_ReferentNotFoundException() { ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi); ReferentEPDTO referentEPDTO = new ReferentEPDTO() { IdReferent = collaborateurNonExistant, IdsCollaborateur = new List(), }; referentEPDTO.IdsCollaborateur.Add(collaborateur1); AsyncTestDelegate exception = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO, collaborateur1); Assert.ThrowsAsync(typeof(ReferentNotFoundException), exception); } [Test] public void UpdateReferentCollaborateur_CollaborateurPartiException() { ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi); ReferentEPDTO referentEPDTO1 = new ReferentEPDTO() { IdReferent = collaborateurParti, IdsCollaborateur = new List(), }; referentEPDTO1.IdsCollaborateur.Add(collaborateur1); ReferentEPDTO referentEPDTO2 = new ReferentEPDTO() { IdReferent = collaborateurParti, IdsCollaborateur = new List(), }; referentEPDTO2.IdsCollaborateur.Add(collaborateurParti); AsyncTestDelegate exception1 = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO1, collaborateur1); AsyncTestDelegate exception2 = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO2, collaborateurParti); Assert.ThrowsAsync(typeof(CollaborateurPartiException), exception1); Assert.ThrowsAsync(typeof(CollaborateurPartiException), exception2); } [Test] public void UpdateReferentCollaborateur_CollaborateurNotFoundException() { ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi); ReferentEPDTO referentEPDTO = new ReferentEPDTO() { IdReferent = referent1, IdsCollaborateur = new List(), }; referentEPDTO.IdsCollaborateur.Add(collaborateurNonExistant); AsyncTestDelegate exception = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO, collaborateurNonExistant); Assert.ThrowsAsync(typeof(CollaborateurNotFoundException), exception); } [Test] public void UpdateReferentCollaborateur_ApiException() { ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi); ReferentEPDTO referentEPDTO = new ReferentEPDTO() { IdReferent = null, IdsCollaborateur = new List(), }; referentEPDTO.IdsCollaborateur.Add(collaborateur1); AsyncTestDelegate exception = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO, collaborateur1); Assert.ThrowsAsync(typeof(ApiException), exception); } #endregion } }