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

139 lines
3.9 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, collaborateur1;
#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("aa36f34c-9041-42f5-9db3-6536fe7f1696");
collaborateur1 = new Guid("842650db-a548-4472-a3af-4c5fff3c1ab8");
}
#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);
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
}
}