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/Controllers/NoteApiTests.cs

152 lines
4.7 KiB

using EPAServeur.Context;
using EPAServeur.IServices;
using EPAServeur.Services;
using IO.Swagger.ApiCollaborateur;
using IO.Swagger.Controllers;
using IO.Swagger.DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace EPAServeur.Tests.Controllers
{
[TestFixture]
public class NoteApiTests
{
#region Variables
private EpContext context;
private ICollaborateurApi collaborateurApi;
private ICollaborateurService collaborateurService;
private INoteService noteService;
private ILogger<NotesApiController> logger;
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);
noteService = new NoteService(collaborateurApi, collaborateurService, context);
logger = new NullLogger<NotesApiController>();
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 Tests
//AddNote
[Test]
public async Task AddNote()
{
NotesApiController notesApiController = new NotesApiController(noteService, logger);
CollaborateurDTO collaborateurDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
DateTime date = DateTime.Now;
DetailsNoteDTO nouvelleNote = new DetailsNoteDTO()
{
Collaborateur = collaborateurDTO,
DateCreation = date,
DateMiseAjour = date,
IdAuteur = auteur1,
Texte = "Texte nouvelle note",
Titre = "Titre nouvelle note"
};
Task<IActionResult> task = notesApiController.AddNote(nouvelleNote);
CreatedResult result = task.Result as CreatedResult;
Assert.AreEqual(201, result.StatusCode);
Assert.IsInstanceOf<DetailsNoteDTO>(result.Value);
}
//DeleteNote
[Test]
public void SupprimerNote()
{
NotesApiController notesApiController = new NotesApiController(noteService, logger);
Task<IActionResult> task = notesApiController.DeleteNote(1);
NoContentResult result = task.Result as NoContentResult;
Assert.AreEqual(204, result.StatusCode);
}
//GetNoteById
[Test]
public void GetNoteById()
{
NotesApiController notesApiController = new NotesApiController(noteService, logger);
Task<IActionResult> task = notesApiController.GetNoteById(1);
OkObjectResult result = task.Result as OkObjectResult;
Assert.AreEqual(200 ,result.StatusCode);
Assert.IsInstanceOf<DetailsNoteDTO>(result.Value);
}
//GetNotesAuteur
//GetNotesAuteurCount
//UpdateNote
[Test]
public async Task UpdateNote()
{
NotesApiController notesApiController = new NotesApiController(noteService, logger);
CollaborateurDTO collaborateurDTO = await collaborateurService.GetCollaborateurByIdAsync(collaborateur1);
DateTime date = DateTime.Now;
DetailsNoteDTO updatedNote = await noteService.GetNoteByIdAsync(2);
updatedNote.Texte = "Texte mise à jour";
updatedNote.Titre = "Titre mise à jour";
Task<IActionResult> task = notesApiController.UpdateNote(updatedNote, 2);
OkObjectResult result = task.Result as OkObjectResult;
Assert.AreEqual(200, result.StatusCode);
Assert.IsInstanceOf<DetailsNoteDTO>(result.Value);
}
#endregion
}
}