From f2b9db54e8fee622862465db29b77adbd5014d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Mon, 28 Dec 2020 11:38:41 +0100 Subject: [PATCH] Ajout des tests unitaires pour le UpdateNote et le SupprimerNote de NoteService --- EPAServeur.Tests/Services/NoteServiceTests.cs | 128 +++++++++++++++++- EPAServeur/Controllers/NotesApi.cs | 9 ++ EPAServeur/IServices/INoteService.cs | 2 +- EPAServeur/Services/NoteService.cs | 10 +- 4 files changed, 143 insertions(+), 6 deletions(-) diff --git a/EPAServeur.Tests/Services/NoteServiceTests.cs b/EPAServeur.Tests/Services/NoteServiceTests.cs index 9aa7ea3..c2992f5 100644 --- a/EPAServeur.Tests/Services/NoteServiceTests.cs +++ b/EPAServeur.Tests/Services/NoteServiceTests.cs @@ -370,14 +370,140 @@ namespace EPAServeur.Tests.Services #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); + + Assert.IsNotNull(noteUpdate); + Assert.AreEqual(note.Id, noteUpdate.Id); + Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur); + Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id); + Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation); + Assert.AreEqual(note.Titre, noteUpdate.Titre); + Assert.AreEqual(note.Texte, noteUpdate.Texte); + Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.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); + + Assert.IsNotNull(noteUpdate); + Assert.AreEqual(note.Id, noteUpdate.Id); + Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur); + Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id); + Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation); + Assert.AreEqual(note.Titre, noteUpdate.Titre); + Assert.AreEqual(note.Texte, noteUpdate.Texte); + Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.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); + + Assert.IsNotNull(noteUpdate); + Assert.AreEqual(note.Id, noteUpdate.Id); + Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur); + Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id); + Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation); + Assert.AreEqual(note.Titre, noteUpdate.Titre); + Assert.AreEqual(note.Texte, noteUpdate.Texte); + Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.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 + #endregion. } diff --git a/EPAServeur/Controllers/NotesApi.cs b/EPAServeur/Controllers/NotesApi.cs index ed822a5..af6a614 100644 --- a/EPAServeur/Controllers/NotesApi.cs +++ b/EPAServeur/Controllers/NotesApi.cs @@ -186,6 +186,15 @@ namespace IO.Swagger.Controllers { noteService.SupprimerNoteAsync(idNote); } + catch(NoteNotFoundException e) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = 404, + Message = e.Message + }; + return NotFound(erreur); + } catch(DbUpdateException e) { logger.LogError(e.Message); diff --git a/EPAServeur/IServices/INoteService.cs b/EPAServeur/IServices/INoteService.cs index 668af87..d63515d 100644 --- a/EPAServeur/IServices/INoteService.cs +++ b/EPAServeur/IServices/INoteService.cs @@ -11,7 +11,7 @@ namespace EPAServeur.IServices public interface INoteService { Task AjouterNoteAsync(DetailsNoteDTO note); - void SupprimerNoteAsync(long? idNote); + Task SupprimerNoteAsync(long? idNote); Task GetNoteByIdAsync(long? idNote); Task> GetNotesByAuteurAsync(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri); Task GetNotesByAuteurCountAsync(Guid? idAuteur, string texte); diff --git a/EPAServeur/Services/NoteService.cs b/EPAServeur/Services/NoteService.cs index 0e380e3..86c12fe 100644 --- a/EPAServeur/Services/NoteService.cs +++ b/EPAServeur/Services/NoteService.cs @@ -76,14 +76,16 @@ namespace EPAServeur.Services /// Supprimer une note en fonction de son Id de manière async /// /// Id de la note à supprimer - public async void SupprimerNoteAsync(long? idNote) + public async Task SupprimerNoteAsync(long? idNote) { Note note = await context.Note.FindAsync(idNote); - if (note != null) + if (note == null) { - context.Remove(note); - await context.SaveChangesAsync(); + throw new NoteNotFoundException("Impossible de supprimer la note car elle n'a pas été trouvée"); } + context.Remove(note); + await context.SaveChangesAsync(); + return true; }