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

385 lines
12 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("ecf528c3-e509-402f-87bb-c8821467e350");
auteur2 = new Guid("6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb");
auteur3 = new Guid("571463f3-b286-4a21-9eab-0707dc506dec");
collaborateur1 = new Guid("92b29b9c-40a4-4aa0-9412-bc97a379e52f");
collaborateur2 = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04");
collaborateur3 = new Guid("e5d36da4-df16-4d19-8a11-1ba2f6efc80c");
auteurNonExistant = collaborateurNonExistant = new Guid("397f616e-44ff-11eb-b378-0242ac130002");
collaborateurParti = new Guid("3c99214d-0a5e-4bb6-b7b2-7d9bb8143c50");
referentParti = new Guid("0021430f-9cb5-4d7e-a395-904c95421920");
}
#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
#endregion
#region Tests UpdateNote
#endregion
#region Tests GetNoteByAuteur
#endregion
}
}