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/Services/ReferentEPTests.cs

254 lines
8.4 KiB

using EPAServeur.Context;
using EPAServeur.Exceptions;
using EPAServeur.IServices;
using EPAServeur.Models.EP;
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;
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<EpContext>()
.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("aa36f34c-9041-42f5-9db3-6536fe7f1696");
collaborateur1 = new Guid("301ba7f3-095e-4912-8998-a7c942dc5f23");
collaborateur2 = new Guid("a43b6f4f-f199-4dd0-93b6-a1cb2c0a0d14");
collaborateur3 = new Guid("b5254c6c-7caa-435f-a4bb-e0cf92559832");
collaborateurNonExistant = new Guid();
collaborateurParti = new Guid("ea027734-ff0f-4308-8879-133a09fb3c46");
}
#endregion
#region Ajout pour un collaborateur
[Test]
public async Task UpdateReferentUnCollaborateur()
{
ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi);
CollaborateurDTO collaborateurDTO1 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
ReferentEPDTO referentEPDTO1 = new ReferentEPDTO()
{
IdReferent = referent1,
IdsCollaborateur = new List<Guid?>(),
};
referentEPDTO1.IdsCollaborateur.Add(collaborateur1);
await referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO1, collaborateur1);
Assert.IsNotNull(collaborateurDTO1);
Assert.IsNull(collaborateurDTO1.Referent);
collaborateurDTO1 = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
Assert.IsNotNull(collaborateurDTO1);
Assert.IsNotNull(collaborateurDTO1.Referent);
Assert.AreEqual(collaborateurDTO1.Referent.Id, referent1);
}
[Test]
public void UpdateReferentCollaborateur_CollaborateurIncompatibleException()
{
ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi);
ReferentEPDTO referentEPDTO1 = new ReferentEPDTO()
{
IdReferent = referent1,
IdsCollaborateur = new List<Guid?>(),
};
referentEPDTO1.IdsCollaborateur.Add(collaborateur1);
ReferentEPDTO referentEPDTO2 = new ReferentEPDTO()
{
IdReferent = referent1,
IdsCollaborateur = new List<Guid?>(),
};
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<Guid?>(),
};
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<Guid?>(),
};
referentEPDTO1.IdsCollaborateur.Add(collaborateur1);
ReferentEPDTO referentEPDTO2 = new ReferentEPDTO()
{
IdReferent = collaborateurParti,
IdsCollaborateur = new List<Guid?>(),
};
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<Guid?>(),
};
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<Guid?>(),
};
referentEPDTO.IdsCollaborateur.Add(collaborateur1);
AsyncTestDelegate exception = () => referentEPService.UpdateReferentCollaborateurAsync(referentEPDTO, collaborateur1);
Assert.ThrowsAsync(typeof(ApiException), exception);
}
#endregion
#region ajout de plusieurs collaborateur
[Test]
public async Task UpdateCollaborateursReferent()
{
ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi);
ReferentEPDTO referentEPDTO = new ReferentEPDTO()
{
IdReferent = referent1,
IdsCollaborateur = new List<Guid?>(),
};
referentEPDTO.IdsCollaborateur.Add(collaborateur1);
referentEPDTO.IdsCollaborateur.Add(collaborateur3);
referentEPDTO.IdsCollaborateur.Add(collaborateur2);
await referentEPService.UpdateCollaborateursReferentAsync(referentEPDTO, referent1);
foreach(Guid guid in referentEPDTO.IdsCollaborateur)
{
ReferentEP referentEP = await context.ReferentEP.FindAsync(guid);
Assert.AreEqual(referent1, referentEP.IdReferent);
}
}
[Test]
public void UpdateCollaborateursReferent_CollaborateurNotFound()
{
ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi);
ReferentEPDTO referentEPDTO = new ReferentEPDTO()
{
IdReferent = referent1,
IdsCollaborateur = new List<Guid?>(),
};
referentEPDTO.IdsCollaborateur.Add(collaborateur1);
referentEPDTO.IdsCollaborateur.Add(collaborateur3);
referentEPDTO.IdsCollaborateur.Add(collaborateurNonExistant);
referentEPDTO.IdsCollaborateur.Add(collaborateur2);
AsyncTestDelegate exception = () => referentEPService.UpdateCollaborateursReferentAsync(referentEPDTO, referent1);
Assert.ThrowsAsync(typeof(CollaborateurNotFoundException), exception);
}
[Test]
public void UpdateCollaborateursReferent_ReferentNotFound()
{
ReferentEPService referentEPService = new ReferentEPService(context, collaborateurApi);
ReferentEPDTO referentEPDTO = new ReferentEPDTO()
{
IdReferent = collaborateurNonExistant,
IdsCollaborateur = new List<Guid?>(),
};
referentEPDTO.IdsCollaborateur.Add(collaborateur1);
referentEPDTO.IdsCollaborateur.Add(collaborateur3);
referentEPDTO.IdsCollaborateur.Add(collaborateur2);
AsyncTestDelegate exception = () => referentEPService.UpdateCollaborateursReferentAsync(referentEPDTO, collaborateurNonExistant);
Assert.ThrowsAsync(typeof(ReferentNotFoundException), exception);
}
#endregion
}
}