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/NoteServiceTests.cs

513 lines
17 KiB

using EPAServeur.Context;
using EPAServeur.Exceptions;
using EPAServeur.IServices;
using EPAServeur.Models.Notes;
using EPAServeur.Services;
using IO.Swagger.ApiCollaborateur;
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 NoteServiceTests
{
#region Variables
private EpContext context;
private ICollaborateurApi collaborateurApi;
private ICollaborateurService collaborateurService;
private Guid? auteur1, auteur2, auteur3;
private Guid? collaborateur1, collaborateur2, collaborateur3;
private Guid? auteurNonExistant, collaborateurNonExistant, collaborateurParti, referentParti;
#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();
// Ajout du jeu de données pour les tests
DataSeeder.AddNotes(context);
// 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();
collaborateurService = new CollaborateurService(collaborateurApi, context);
auteur1 = new Guid("aa36f34c-9041-42f5-9db3-6536fe7f1696");
auteur2 = new Guid("01ee85ff-d7f3-494b-b1de-26ced8fbfa0d");
auteur3 = new Guid("eb8b0f33-f529-4985-861e-1207f3312bb5");
collaborateur1 = new Guid("842650db-a548-4472-a3af-4c5fff3c1ab8");
collaborateur2 = new Guid("b5254c6c-7caa-435f-a4bb-e0cf92559832");
collaborateur3 = new Guid("de98a866-736f-4295-a669-92a8694e2ee3");
auteurNonExistant = collaborateurNonExistant = new Guid("397f616e-44ff-11eb-b378-0242ac130002");
collaborateurParti = new Guid("13fbe621-1bc9-4f04-afde-b54ca076e239");
referentParti = new Guid("ea027734-ff0f-4308-8879-133a09fb3c46");
}
#endregion
#region Test AjouterNote
[Test]
public async Task AjouterNote_Ajouter4NoteAvecSucces()
{
// récupérer les collaborateurs liés aux notes
CollaborateurDTO collaborateur1DTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
CollaborateurDTO collaborateur2DTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur2);
CollaborateurDTO collaborateur3DTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur3);
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DateTime date = DateTime.Now;
DetailsNoteDTO noteAjoutee1, noteAjoutee2;
Note noteAjoutee3, noteAjoutee4;
DetailsNoteDTO nouvelleNote1 = new DetailsNoteDTO()
{
Collaborateur = collaborateur1DTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur1,
Texte = "Texte nouvelle note 11",
Titre = "Titre nouvelle note 11"
};
noteAjoutee1 = await noteService.AjouterNoteAsync(nouvelleNote1);
DetailsNoteDTO nouvelleNote2 = new DetailsNoteDTO()
{
Collaborateur = collaborateur2DTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur2,
Texte = "Texte nouvelle note 12",
Titre = "Titre nouvelle note 12"
};
noteAjoutee2 = await noteService.AjouterNoteAsync(nouvelleNote2);
DetailsNoteDTO nouvelleNote3 = new DetailsNoteDTO()
{
Collaborateur = collaborateur3DTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur3,
Texte = "Texte nouvelle note 13",
Titre = "Titre nouvelle note 13",
};
await noteService.AjouterNoteAsync(nouvelleNote3);
noteAjoutee3 = await context.Note.FindAsync((long)13);
DetailsNoteDTO nouvelleNote4 = new DetailsNoteDTO()
{
Collaborateur = collaborateur3DTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur1,
Texte = "Texte nouvelle note 14",
Titre = "Titre nouvelle note 14"
};
await noteService.AjouterNoteAsync(nouvelleNote4);
noteAjoutee4 = await context.Note.FindAsync((long)14);
//Assert note 1
Assert.IsNotNull(noteAjoutee1);
Assert.AreEqual(11, noteAjoutee1.Id);
Assert.AreEqual(nouvelleNote1.IdAuteur, noteAjoutee1.IdAuteur);
Assert.AreEqual(nouvelleNote1.DateCreation, noteAjoutee1.DateCreation);
Assert.AreEqual(nouvelleNote1.Collaborateur.Id, noteAjoutee1.Collaborateur.Id);
Assert.AreEqual(nouvelleNote1.Titre, noteAjoutee1.Titre);
Assert.AreEqual(nouvelleNote1.Texte, noteAjoutee1.Texte);
Assert.AreEqual(nouvelleNote1.DateMiseAjour, noteAjoutee1.DateMiseAjour);
Assert.AreEqual(nouvelleNote1.DateCreation, noteAjoutee1.DateCreation);
//Assert note 2
Assert.IsNotNull(noteAjoutee2);
Assert.AreEqual(12, noteAjoutee2.Id);
Assert.AreEqual(nouvelleNote2.IdAuteur, noteAjoutee2.IdAuteur);
Assert.AreEqual(nouvelleNote2.DateCreation, noteAjoutee2.DateCreation);
Assert.AreEqual(nouvelleNote2.Collaborateur.Id, noteAjoutee2.Collaborateur.Id);
Assert.AreEqual(nouvelleNote2.Titre, noteAjoutee2.Titre);
Assert.AreEqual(nouvelleNote2.Texte, noteAjoutee2.Texte);
Assert.AreEqual(nouvelleNote2.DateMiseAjour, noteAjoutee2.DateMiseAjour);
Assert.AreEqual(nouvelleNote2.DateCreation, noteAjoutee2.DateCreation);
//Assert note 3
Assert.IsNotNull(noteAjoutee3);
Assert.AreEqual(13, noteAjoutee3.IdNote);
Assert.AreEqual(nouvelleNote3.IdAuteur, noteAjoutee3.IdAuteur);
Assert.AreEqual(nouvelleNote3.DateCreation, noteAjoutee3.DateCreation);
Assert.AreEqual(nouvelleNote3.Collaborateur.Id, noteAjoutee3.IdCollaborateur);
Assert.AreEqual(nouvelleNote3.Titre, noteAjoutee3.Titre);
Assert.AreEqual(nouvelleNote3.Texte, noteAjoutee3.Texte);
Assert.AreEqual(nouvelleNote3.DateMiseAjour, noteAjoutee3.DateMiseAJour);
Assert.AreEqual(nouvelleNote3.DateCreation, noteAjoutee3.DateCreation);
//Assert note 4
Assert.IsNotNull(noteAjoutee4);
Assert.AreEqual(14, noteAjoutee4.IdNote);
Assert.AreEqual(nouvelleNote4.IdAuteur, noteAjoutee4.IdAuteur);
Assert.AreEqual(nouvelleNote4.DateCreation, noteAjoutee4.DateCreation);
Assert.AreEqual(nouvelleNote4.Collaborateur.Id, noteAjoutee4.IdCollaborateur);
Assert.AreEqual(nouvelleNote4.Titre, noteAjoutee4.Titre);
Assert.AreEqual(nouvelleNote4.Texte, noteAjoutee4.Texte);
Assert.AreEqual(nouvelleNote4.DateMiseAjour, noteAjoutee4.DateMiseAJour);
Assert.AreEqual(nouvelleNote4.DateCreation, noteAjoutee4.DateCreation);
}
[TestCase("", "")]
[TestCase("Titre 2", "")]
[TestCase("", "Texte 3")]
[TestCase("Titre 3", "Te")]
[TestCase("T", "Texte 4")]
[TestCase("T", "T")]
public async Task AjouterNote_NoteInvalideException(string titre, string texte)
{
CollaborateurDTO collaborateurDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DateTime date = DateTime.Now;
DetailsNoteDTO noteAjoutee1 = new DetailsNoteDTO()
{
Collaborateur = collaborateurDTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur1,
Texte = texte,
Titre = titre,
};
AsyncTestDelegate exception = () => noteService.AjouterNoteAsync(noteAjoutee1);
Assert.ThrowsAsync(typeof(NoteInvalideException), exception);
}
[Test]
public async Task AjouterNote_ReferentNotFoundException()
{
CollaborateurDTO collaborateurDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DateTime date = DateTime.Now;
DetailsNoteDTO note = new DetailsNoteDTO()
{
Collaborateur = collaborateurDTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteurNonExistant,
Texte = "Texte nouvelle note",
Titre = "Titre nouvelle note"
};
AsyncTestDelegate exception = () => noteService.AjouterNoteAsync(note);
Assert.ThrowsAsync(typeof(ReferentNotFoundException), exception);
}
[Test]
public async Task AjouterNote_CollaborateurNotFoundException()
{
CollaborateurDTO collaborateurDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
collaborateurDTO.Id = collaborateurNonExistant;
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DateTime date = DateTime.Now;
DetailsNoteDTO note = new DetailsNoteDTO()
{
Collaborateur = collaborateurDTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur1,
Texte = "Texte nouvelle note",
Titre = "Titre nouvelle note"
};
AsyncTestDelegate exception = () => noteService.AjouterNoteAsync(note);
Assert.ThrowsAsync(typeof(CollaborateurNotFoundException), exception);
}
//[Test]
[Test]
public async Task AjouterNote_CollaborateurPartiException()
{
CollaborateurDTO collaborateurDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
CollaborateurDTO collaborateurPartiDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateurParti);
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DateTime date = DateTime.Now;
DetailsNoteDTO note1 = new DetailsNoteDTO()
{
Collaborateur = collaborateurDTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = referentParti,
Texte = "Texte note 1",
Titre = "Titre note 1"
};
AsyncTestDelegate exception1 = () => noteService.AjouterNoteAsync(note1);
DetailsNoteDTO note2 = new DetailsNoteDTO()
{
Collaborateur = collaborateurPartiDTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur2,
Texte = "Texte note 2",
Titre = "Titre note 2"
};
AsyncTestDelegate exception2 = () => noteService.AjouterNoteAsync(note2);
Assert.ThrowsAsync(typeof(CollaborateurPartiException), exception1);
Assert.ThrowsAsync(typeof(CollaborateurPartiException), exception2);
}
#endregion
#region Tests GetNoteById
[TestCase(1)]
[TestCase(2)]
[TestCase(4)]
[TestCase(5)]
[TestCase(6)]
[TestCase(9)]
public async Task GetNote_NoteNotNull(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO detailsNoteDTO = await noteService.GetNoteByIdAsync(id);
Assert.NotNull(detailsNoteDTO);
Assert.NotNull(detailsNoteDTO.Titre);
Assert.NotNull(detailsNoteDTO.Texte);
Assert.NotNull(detailsNoteDTO.IdAuteur);
Assert.NotNull(detailsNoteDTO.Collaborateur);
Assert.NotNull(detailsNoteDTO.DateCreation);
Assert.NotNull(detailsNoteDTO.DateMiseAjour);
Assert.AreEqual(id, detailsNoteDTO.Id);
}
[TestCase(-999)]
[TestCase(-1)]
[TestCase(0)]
[TestCase(11)]
[TestCase(20)]
[TestCase(30)]
[TestCase(100)]
public void GetNote_NoteNotFoundException(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
AsyncTestDelegate exception = () => noteService.GetNoteByIdAsync(id);
Assert.ThrowsAsync(typeof(NoteNotFoundException), exception);
}
[TestCase(3)]
[TestCase(7)]
public void GetNote_ReferentNotFoundException(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
AsyncTestDelegate exception = () => noteService.GetNoteByIdAsync(id);
Assert.ThrowsAsync(typeof(ReferentNotFoundException), exception);
}
[TestCase(8)]
[TestCase(10)]
public void GetNote_CollaborateurNotFoundException(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
AsyncTestDelegate exception = () => noteService.GetNoteByIdAsync(id);
Assert.ThrowsAsync(typeof(CollaborateurNotFoundException), exception);
}
#endregion
#region Test Supprimer Note
[TestCase(5)]
[TestCase(7)]
[TestCase(8)]
public async Task SupprimerNote_Succes(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
await noteService.SupprimerNoteAsync(id);
Note note = await context.Note.FindAsync(id);
Assert.IsNull(note);
}
[TestCase(0)]
[TestCase(-1)]
[TestCase(-999)]
[TestCase(20)]
[TestCase(30)]
[TestCase(100)]
public void SupprimerNote_NoteNotFoundException(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
AsyncTestDelegate exception = () => noteService.SupprimerNoteAsync(id);
Assert.ThrowsAsync(typeof(NoteNotFoundException), exception);
}
#endregion
#region Tests UpdateNote
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_UpdateTitreEtTexte(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
note.Texte= "Texte à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Note noteUpdated = context.Note.Find(noteUpdate.Id);
Assert.IsNotNull(noteUpdate);
Assert.IsNotNull(noteUpdated);
Assert.AreEqual(noteUpdated.IdNote, noteUpdate.Id);
Assert.AreEqual(noteUpdated.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(noteUpdated.IdCollaborateur, noteUpdate.Collaborateur.Id);
Assert.AreEqual(noteUpdated.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(noteUpdated.Titre, noteUpdate.Titre);
Assert.AreEqual(noteUpdated.Texte, noteUpdate.Texte);
Assert.AreEqual(noteUpdated.DateMiseAJour, noteUpdated.DateMiseAJour);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_UpdateTexte(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Texte = "Texte à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Note noteUpdated = context.Note.Find(noteUpdate.Id);
Assert.IsNotNull(noteUpdate);
Assert.IsNotNull(noteUpdated);
Assert.AreEqual(noteUpdated.IdNote, noteUpdate.Id);
Assert.AreEqual(noteUpdated.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(noteUpdated.IdCollaborateur, noteUpdate.Collaborateur.Id);
Assert.AreEqual(noteUpdated.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(noteUpdated.Titre, noteUpdate.Titre);
Assert.AreEqual(noteUpdated.Texte, noteUpdate.Texte);
Assert.AreEqual(noteUpdated.DateMiseAJour, noteUpdated.DateMiseAJour);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_UpdateTitre(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Note noteUpdated = context.Note.Find(noteUpdate.Id);
Assert.IsNotNull(noteUpdate);
Assert.IsNotNull(noteUpdated);
Assert.AreEqual(noteUpdated.IdNote, noteUpdate.Id);
Assert.AreEqual(noteUpdated.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(noteUpdated.IdCollaborateur, noteUpdate.Collaborateur.Id);
Assert.AreEqual(noteUpdated.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(noteUpdated.Titre, noteUpdate.Titre);
Assert.AreEqual(noteUpdated.Texte, noteUpdate.Texte);
Assert.AreEqual(noteUpdated.DateMiseAJour, noteUpdated.DateMiseAJour);
}
[TestCase(1,2)]
[TestCase(1,3)]
[TestCase(2,4)]
[TestCase(2,5)]
[TestCase(9,6)]
[TestCase(9,7)]
public async Task UpdateNote_NoteIdImcompatibleExceptionId(long id, long mauvaisId)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
note.Texte = "Texte à jour " + id;
AsyncTestDelegate exception = () => noteService.UpdateNoteAsync(mauvaisId, note);
Assert.ThrowsAsync(typeof(NoteIdImcompatibleException), exception);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_NoteIdImcompatibleExceptionDate(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
note.Texte = "Texte à jour " + id;
note.DateCreation = DateTime.Now;
AsyncTestDelegate exception = () => noteService.UpdateNoteAsync(id, note);
Assert.ThrowsAsync(typeof(NoteIdImcompatibleException), exception);
}
#endregion
#region Tests GetNoteByAuteur
#endregion.
}
}