From 205919227c53ded0eb63798d703f19dfeeaec9d3 Mon Sep 17 00:00:00 2001 From: tonyfages Date: Sat, 16 Mar 2024 16:04:15 +0100 Subject: [PATCH] Add some Tests --- .../API_UnitTest_Mapper.csproj | 29 +++++ .../API_UnitTest_Mapper/GlobalUsings.cs | 1 + .../API_Unit_Test/API_Unit_Test.csproj | 4 + .../API_Unit_Test/ArticleControllerTests.cs | 111 ++++++++++++++++ .../API_Unit_Test/ArticleDTOTests.cs | 35 +++++ .../API_Unit_Test/ArticleMapperTests.cs | 72 +++++++++++ .../API_Unit_Test/DbManagerTests.cs | 35 +++++ .../API_Unit_Test/ExtensionsTests.cs | 63 +++++++++ .../API_Unit_Test/FormMapperTests.cs | 70 ++++++++++ .../FormulaireControllerTests.cs | 111 ++++++++++++++++ .../API_Unit_Test/FormulaireDTOTests.cs | 27 ++++ ...est_Article.cs => IArticleServiceTests.cs} | 2 +- ...{UnitTest_Form.cs => IFormServiceTests.cs} | 2 +- ...{UnitTest_User.cs => IUserServiceTests.cs} | 2 +- .../API_Unit_Test/UserControllerTests.cs | 95 ++++++++++++++ .../API_Unit_Test/UserDTOTests.cs | 29 +++++ .../API_Unit_Test/UserMappingTests.cs | 120 ++++++++++++++++++ 17 files changed, 805 insertions(+), 3 deletions(-) create mode 100644 Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/API_UnitTest_Mapper.csproj create mode 100644 Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/GlobalUsings.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleControllerTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleDTOTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleMapperTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/DbManagerTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/ExtensionsTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/FormMapperTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireControllerTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireDTOTests.cs rename Verax_API_EF/Verax_API_EF/API_Unit_Test/{UnitTest_Article.cs => IArticleServiceTests.cs} (99%) rename Verax_API_EF/Verax_API_EF/API_Unit_Test/{UnitTest_Form.cs => IFormServiceTests.cs} (98%) rename Verax_API_EF/Verax_API_EF/API_Unit_Test/{UnitTest_User.cs => IUserServiceTests.cs} (99%) create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/UserControllerTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/UserDTOTests.cs create mode 100644 Verax_API_EF/Verax_API_EF/API_Unit_Test/UserMappingTests.cs diff --git a/Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/API_UnitTest_Mapper.csproj b/Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/API_UnitTest_Mapper.csproj new file mode 100644 index 0000000..7b96d38 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/API_UnitTest_Mapper.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/GlobalUsings.cs b/Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/GlobalUsings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_UnitTest_Mapper/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/API_Unit_Test.csproj b/Verax_API_EF/Verax_API_EF/API_Unit_Test/API_Unit_Test.csproj index bf9fa2b..baf95c8 100644 --- a/Verax_API_EF/Verax_API_EF/API_Unit_Test/API_Unit_Test.csproj +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/API_Unit_Test.csproj @@ -24,7 +24,11 @@ + + + + diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleControllerTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleControllerTests.cs new file mode 100644 index 0000000..abdef65 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleControllerTests.cs @@ -0,0 +1,111 @@ +using API.Controllers; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Model; +using Moq; + +namespace API_Unit_Test; + +public class ArticleControllerTests + { + private readonly Mock _mockDataManager; + private readonly Mock> _mockLogger; + private readonly ArticleController _controller; + + public ArticleControllerTests() + { + _mockDataManager = new Mock(); + _mockLogger = new Mock>(); + _controller = new ArticleController(_mockDataManager.Object, _mockLogger.Object); + } + + [Fact] + public async Task GetAllArticles_ReturnsOk() + { + // Arrange + _mockDataManager.Setup(dm => dm.ArticleService.GetAllArticles(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new List
()); + + // Act + var result = await _controller.GetAllArticles(); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task GetArticleById_ReturnsOk() + { + // Arrange + var testArticleId = 1; + _mockDataManager.Setup(dm => dm.ArticleService.GetArticleById(testArticleId)) + .ReturnsAsync(new Article { Id = testArticleId }); + + // Act + var result = await _controller.GetArticleById(testArticleId); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task CreateArticle_ReturnsOk() + { + // Arrange + var article = new Article { Title = "Test" }; + _mockDataManager.Setup(dm => dm.ArticleService.CreateArticle(article)) + .ReturnsAsync(article); + + // Act + var result = await _controller.CreateArticle(article); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task DeleteArticle_ReturnsOk() + { + // Arrange + var testArticleId = 1; + _mockDataManager.Setup(dm => dm.ArticleService.DeleteArticle(testArticleId)) + .ReturnsAsync(new Article { Id = testArticleId }); + + // Act + var result = await _controller.DeleteArticle(testArticleId); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task UpdateArticle_ReturnsOk() + { + // Arrange + var testArticleId = 1; + var article = new Article { Title = "Updated" }; + _mockDataManager.Setup(dm => dm.ArticleService.UpdateArticle(testArticleId, article)) + .ReturnsAsync(article); + + // Act + var result = await _controller.UpdateArticle(testArticleId, article); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task GetAllArticles_ThrowsException_ReturnsBadRequest() + { + // Arrange + _mockDataManager.Setup(dm => dm.ArticleService.GetAllArticles(It.IsAny(), It.IsAny(), It.IsAny())) + .ThrowsAsync(new Exception("Test exception")); + + // Act + var result = await _controller.GetAllArticles(); + + // Assert + Assert.IsType(result); + } + + } \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleDTOTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleDTOTests.cs new file mode 100644 index 0000000..9ea390b --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleDTOTests.cs @@ -0,0 +1,35 @@ +using Web_API.Model; + +namespace API_Unit_Test; + +public class ArticleDTOTests +{ + [Fact] + public void ArticleDTOPropertiesTest() + { + // Arrange + var articleDTO = new ArticleDTO(); + var testId = 1L; + var testTitle = "Test Title"; + var testDescription = "Test Description"; + var testDatePublished = "2024-03-16"; + var testLectureTime = 5; + var testAuthor = "Test Author"; + + // Act + articleDTO.Id = testId; + articleDTO.Title = testTitle; + articleDTO.Description = testDescription; + articleDTO.DatePublished = testDatePublished; + articleDTO.LectureTime = testLectureTime; + articleDTO.Author = testAuthor; + + // Assert + Assert.Equal(testId, articleDTO.Id); + Assert.Equal(testTitle, articleDTO.Title); + Assert.Equal(testDescription, articleDTO.Description); + Assert.Equal(testDatePublished, articleDTO.DatePublished); + Assert.Equal(testLectureTime, articleDTO.LectureTime); + Assert.Equal(testAuthor, articleDTO.Author); + } +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleMapperTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleMapperTests.cs new file mode 100644 index 0000000..a93cc7c --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ArticleMapperTests.cs @@ -0,0 +1,72 @@ +using API_Mapping; +using Model; +using Web_API.Model; + +namespace API_UnitTest_Mapper; + +public class ArticleMapperTests +{ + [Fact] + public void ToDTOMapsCorrectly() + { + var article = new Article + { + Id = 1, + Title = "Test Article", + Description = "Test Description", + DatePublished = "2021-01-01", + LectureTime = 5, + Author = "Test Author" + }; + var dto = ArticleMapper.ToDTO(article); + + Assert.NotNull(dto); + Assert.Equal(article.Id, dto.Id); + Assert.Equal(article.Title, dto.Title); + Assert.Equal(article.Description, dto.Description); + Assert.Equal(article.DatePublished, dto.DatePublished); + Assert.Equal(article.LectureTime, dto.LectureTime); + Assert.Equal(article.Author, dto.Author); + } + + [Fact] + public void ToModelMapsCorrectly() + { + var dto = new ArticleDTO + { + Id = 2, + Title = "Another Test Article", + Description = "Another Test Description", + DatePublished = "2021-01-02", + LectureTime = 10, + Author = "Another Test Author" + }; + + var article = ArticleMapper.ToModel(dto); + + Assert.NotNull(article); + Assert.Equal(dto.Id, article.Id); + Assert.Equal(dto.Title, article.Title); + Assert.Equal(dto.Description, article.Description); + Assert.Equal(dto.DatePublished, article.DatePublished); + Assert.Equal(dto.LectureTime, article.LectureTime); + Assert.Equal(dto.Author, article.Author); + } + + [Fact] + public void ToDTONullArticleThrowsNullReferenceException() + { + Article article = null; + + Assert.Throws(() => ArticleMapper.ToDTO(article)); + } + + [Fact] + public void ToModelNullArticleDTOThrowsNullReferenceException() + { + ArticleDTO dto = null; + + Assert.Throws(() => ArticleMapper.ToModel(dto)); + } + +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/DbManagerTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/DbManagerTests.cs new file mode 100644 index 0000000..ccaee1d --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/DbManagerTests.cs @@ -0,0 +1,35 @@ +using DbContextLib; +using DbDataManager; +using Moq; + +namespace API_Unit_Test; + +public class DbManagerTests +{ + [Fact] + public void Constructor_WithoutParameters_InitializesServices() + { + // Arrange & Act + var dbManager = new DbManager(); + + // Assert + Assert.NotNull(dbManager.ArticleService); + Assert.NotNull(dbManager.UserService); + Assert.NotNull(dbManager.FormulaireService); + } + + [Fact] + public void Constructor_WithLibraryContextParameter_InitializesServicesWithGivenContext() + { + // Arrange + var context = new Mock().Object; + + // Act + var dbManager = new DbManager(context); + + // Assert + Assert.NotNull(dbManager.ArticleService); + Assert.NotNull(dbManager.UserService); + Assert.NotNull(dbManager.FormulaireService); + } +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/ExtensionsTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ExtensionsTests.cs new file mode 100644 index 0000000..541c726 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/ExtensionsTests.cs @@ -0,0 +1,63 @@ +using DbDataManager; +using Entities; +using Model; + +namespace API_Unit_Test; + +public class ExtensionsTests +{ + [Fact] + public void ArticleToEntityMapsCorrectly() + { + // Arrange + var article = new Article + { + Id = 1, + Author = "Author", + Description = "Description", + Title = "Title", + DatePublished = "2021-01-01", + LectureTime = 10 + }; + + // Act + var entity = article.ToEntity(); + + // Assert + Assert.NotNull(entity); + Assert.Equal(article.Id, entity.Id); + Assert.Equal(article.Author, entity.Author); + Assert.Equal(article.Description, entity.Description); + Assert.Equal(article.Title, entity.Title); + Assert.Equal(article.DatePublished, entity.DatePublished); + Assert.Equal(article.LectureTime, entity.LectureTime); + } + + [Fact] + public void ArticleEntityToModelMapsCorrectly() + { + // Arrange + var entity = new ArticleEntity + { + Id = 1, + Author = "Author", + Description = "Description", + Title = "Title", + DatePublished = "2021-01-01", + LectureTime = 10 + }; + + // Act + var model = entity.ToModel(); + + // Assert + Assert.NotNull(model); + Assert.Equal(entity.Id, model.Id); + Assert.Equal(entity.Author, model.Author); + Assert.Equal(entity.Description, model.Description); + Assert.Equal(entity.Title, model.Title); + Assert.Equal(entity.DatePublished, model.DatePublished); + Assert.Equal(entity.LectureTime, model.LectureTime); + } + +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormMapperTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormMapperTests.cs new file mode 100644 index 0000000..4a33e9a --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormMapperTests.cs @@ -0,0 +1,70 @@ +using API_Mapping; +using Model; +using Web_API.Model; + +namespace API_UnitTest_Mapper; + +public class FormMapperTests +{ + [Fact] + public void ToDTOMapsCorrectly() + { + var formulaire = new Formulaire + { + Id = 1, + Theme = "Test Theme", + Date = "2021-01-01", + Lien = "http://example.com", + UserPseudo = "TestUser" + }; + + var dto = FormulaireMapping.ToDTO(formulaire); + + Assert.NotNull(dto); + Assert.Equal(formulaire.Id, dto.Id); + Assert.Equal(formulaire.Theme, dto.Theme); + Assert.Equal(formulaire.Date, dto.Date); + Assert.Equal(formulaire.Lien, dto.Lien); + Assert.Equal(formulaire.UserPseudo, dto.UserPseudo); + } + + [Fact] + public void ToModelMapsCorrectly() + { + var dto = new FormulaireDTO + { + Id = 2, + Theme = "Another Test Theme", + Date = "2021-01-02", + Lien = "http://anotherexample.com", + UserPseudo = "AnotherTestUser" + }; + + var formulaire = FormulaireMapping.ToModel(dto); + + Assert.NotNull(formulaire); + Assert.Equal(dto.Id, formulaire.Id); + Assert.Equal(dto.Theme, formulaire.Theme); + Assert.Equal(dto.Date, formulaire.Date); + Assert.Equal(dto.Lien, formulaire.Lien); + Assert.Equal(dto.UserPseudo, formulaire.UserPseudo); + } + + [Fact] + public void ToDTONullFormulaireThrowsNullReferenceException() + { + Formulaire formulaire = null; + + Assert.Throws(() => FormulaireMapping.ToDTO(formulaire)); + } + + [Fact] + public void ToModelNullFormulaireDTOThrowsNullReferenceException() + { + FormulaireDTO dto = null; + + Assert.Throws(() => FormulaireMapping.ToModel(dto)); + } + + +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireControllerTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireControllerTests.cs new file mode 100644 index 0000000..d891f5f --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireControllerTests.cs @@ -0,0 +1,111 @@ +using API.Controllers; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Model; +using Moq; + +namespace API_Unit_Test; + +public class FormulaireControllerTests + { + private readonly Mock _mockDataManager; + private readonly Mock> _mockLogger; + private readonly FormulaireController _controller; + + public FormulaireControllerTests() + { + _mockDataManager = new Mock(); + _mockLogger = new Mock>(); + _controller = new FormulaireController(_mockDataManager.Object, _mockLogger.Object); + } + + [Fact] + public async Task GetAllForm_ReturnsOk() + { + // Arrange + _mockDataManager.Setup(dm => dm.FormulaireService.GetAllForm(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new List()); + + // Act + var result = await _controller.GetAllForm(); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task GetById_ReturnsOk() + { + // Arrange + var testFormId = 1L; + _mockDataManager.Setup(dm => dm.FormulaireService.GetById(testFormId)) + .ReturnsAsync(new Formulaire { Id = testFormId }); + + // Act + var result = await _controller.GetById(testFormId); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task CreateForm_ReturnsOk() + { + // Arrange + var form = new Formulaire { Theme = "Test" }; + _mockDataManager.Setup(dm => dm.FormulaireService.CreateForm(form)) + .ReturnsAsync(form); + + // Act + var result = await _controller.CreateForm(form); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task DeleteForm_ReturnsOk() + { + // Arrange + var testFormId = 1L; + _mockDataManager.Setup(dm => dm.FormulaireService.DeleteForm(testFormId)) + .ReturnsAsync(new Formulaire { Id = testFormId }); + + // Act + var result = await _controller.DeleteForm(testFormId); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task UpdateForm_ReturnsOk() + { + // Arrange + var testFormId = 1L; + var form = new Formulaire { Theme = "Updated" }; + _mockDataManager.Setup(dm => dm.FormulaireService.UpdateForm(testFormId, form)) + .ReturnsAsync(form); + + // Act + var result = await _controller.UpdateForm(testFormId, form); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async Task GetAllForm_ThrowsException_ReturnsBadRequest() + { + // Arrange + _mockDataManager.Setup(dm => dm.FormulaireService.GetAllForm(It.IsAny(), It.IsAny(), It.IsAny())) + .ThrowsAsync(new Exception("Test exception")); + + // Act + var result = await _controller.GetAllForm(); + + // Assert + Assert.IsType(result); + } + + } \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireDTOTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireDTOTests.cs new file mode 100644 index 0000000..d7c1753 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/FormulaireDTOTests.cs @@ -0,0 +1,27 @@ +using Web_API.Model; + +namespace API_Unit_Test; + +public class FormulaireDTOTests +{ + [Fact] + public void PropertiesTest() + { + // Arrange + var formulaire = new FormulaireDTO + { + Id = 1, + Theme = "Test Theme", + Date = "2024-03-16", + Lien = "http://example.com", + UserPseudo = "TestUser" + }; + + // Act & Assert + Assert.Equal(1, formulaire.Id); + Assert.Equal("Test Theme", formulaire.Theme); + Assert.Equal("2024-03-16", formulaire.Date); + Assert.Equal("http://example.com", formulaire.Lien); + Assert.Equal("TestUser", formulaire.UserPseudo); + } +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_Article.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/IArticleServiceTests.cs similarity index 99% rename from Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_Article.cs rename to Verax_API_EF/Verax_API_EF/API_Unit_Test/IArticleServiceTests.cs index 0219dbd..33d085d 100644 --- a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_Article.cs +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/IArticleServiceTests.cs @@ -4,7 +4,7 @@ using Moq; namespace API_Unit_Test; -public class UnitTest_Article +public class IArticleServiceTests { diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_Form.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/IFormServiceTests.cs similarity index 98% rename from Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_Form.cs rename to Verax_API_EF/Verax_API_EF/API_Unit_Test/IFormServiceTests.cs index bc9b615..9b5c8d4 100644 --- a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_Form.cs +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/IFormServiceTests.cs @@ -4,7 +4,7 @@ using Moq; namespace API_Unit_Test; -public class UnitTest_Form +public class IFormServiceTests { [Fact] public void TestGetAllForm() diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_User.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/IUserServiceTests.cs similarity index 99% rename from Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_User.cs rename to Verax_API_EF/Verax_API_EF/API_Unit_Test/IUserServiceTests.cs index 9634bcd..363a324 100644 --- a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UnitTest_User.cs +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/IUserServiceTests.cs @@ -5,7 +5,7 @@ using Moq; namespace API_Unit_Test; -public class UnitTest_User +public class IUserServiceTests { [Fact] diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserControllerTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserControllerTests.cs new file mode 100644 index 0000000..8acc072 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserControllerTests.cs @@ -0,0 +1,95 @@ +using API.Controllers; +using Entities; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Model; +using Moq; + +namespace API_Unit_Test; + +public class UserControllerTests + { + private readonly Mock _mockDataManager; + private readonly Mock> _mockLogger; + private readonly UserController _controller; + + public UserControllerTests() + { + _mockDataManager = new Mock(); + _mockLogger = new Mock>(); + _controller = new UserController(_mockDataManager.Object, _mockLogger.Object); + } + + [Fact] + public async Task GetAll_ReturnsOk() + { + _mockDataManager.Setup(dm => dm.UserService.GetAll(It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new List()); + + var result = await _controller.GetAll(); + + Assert.IsType(result); + } + + [Fact] + public async Task GetByPseudo_ReturnsOk() + { + var pseudo = "testUser"; + _mockDataManager.Setup(dm => dm.UserService.GetByPseudo(pseudo)) + .ReturnsAsync(new User { Pseudo = pseudo }); + + var result = await _controller.GetByPseudo(pseudo); + + Assert.IsType(result); + } + + [Fact] + public async Task Create_ReturnsOk() + { + var user = new User { Pseudo = "newUser" }; + _mockDataManager.Setup(dm => dm.UserService.Create(user)) + .ReturnsAsync(user); + + var result = await _controller.Create(user); + + Assert.IsType(result); + } + + [Fact] + public async Task Update_ReturnsOk() + { + var pseudo = "existingUser"; + var user = new User { Pseudo = pseudo }; + _mockDataManager.Setup(dm => dm.UserService.Update(user, pseudo)) + .ReturnsAsync(user); + + var result = await _controller.Update(user, pseudo); + + Assert.IsType(result); + } + + [Fact] + public async Task Delete_ReturnsOk() + { + var pseudo = "deleteUser"; + _mockDataManager.Setup(dm => dm.UserService.Delete(pseudo)) + .ReturnsAsync(new User { Pseudo = pseudo }); + + var result = await _controller.Delete(pseudo); + + Assert.IsType(result); + } + + + [Fact] + public async Task Create_ThrowsException_ReturnsBadRequest() + { + var user = new User { Pseudo = "errorUser" }; + _mockDataManager.Setup(dm => dm.UserService.Create(user)) + .ThrowsAsync(new Exception("Test exception")); + + var result = await _controller.Create(user); + + Assert.IsType(result); + } + } \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserDTOTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserDTOTests.cs new file mode 100644 index 0000000..7a02534 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserDTOTests.cs @@ -0,0 +1,29 @@ +using Web_API.Model; + +namespace API_Unit_Test; + +public class UserDTOTests +{ + [Fact] + public void UserDTOPropertiesTest() + { + // Arrange + var user = new UserDTO + { + Pseudo = "user1", + Mdp = "password", + Nom = "Doe", + Prenom = "John", + Mail = "john.doe@example.com", + Role = "Admin" + }; + + // Act & Assert + Assert.Equal("user1", user.Pseudo); + Assert.Equal("password", user.Mdp); + Assert.Equal("Doe", user.Nom); + Assert.Equal("John", user.Prenom); + Assert.Equal("john.doe@example.com", user.Mail); + Assert.Equal("Admin", user.Role); + } +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserMappingTests.cs b/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserMappingTests.cs new file mode 100644 index 0000000..ef01c12 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/API_Unit_Test/UserMappingTests.cs @@ -0,0 +1,120 @@ +using API_Mapping; +using Model; +using Web_API.Model; + +namespace API_UnitTest_Mapper; + +public class UserMappingTests +{ + [Fact] + public void ToDTOMapsCorrectly() + { + // Arrange + var user = new User + { + Pseudo = "testUser", + Mdp = "testPassword", + Nom = "Doe", + Prenom = "John", + Mail = "john.doe@example.com", + Role = "User" + }; + + // Act + var dto = UserMapping.ToDTO(user); + + // Assert + Assert.NotNull(dto); + Assert.Equal(user.Pseudo, dto.Pseudo); + Assert.Equal(user.Mdp, dto.Mdp); + Assert.Equal(user.Nom, dto.Nom); + Assert.Equal(user.Prenom, dto.Prenom); + Assert.Equal(user.Mail, dto.Mail); + Assert.Equal(user.Role, dto.Role); + } + + [Fact] + public void ToModelMapsCorrectly() + { + // Arrange + var dto = new UserDTO + { + Pseudo = "anotherTestUser", + Mdp = "anotherTestPassword", + Nom = "Smith", + Prenom = "Jane", + Mail = "jane.smith@example.com", + Role = "Admin" + }; + + // Act + var user = UserMapping.ToModel(dto); + + // Assert + Assert.NotNull(user); + Assert.Equal(dto.Pseudo, user.Pseudo); + Assert.Equal(dto.Mdp, user.Mdp); + Assert.Equal(dto.Nom, user.Nom); + Assert.Equal(dto.Prenom, user.Prenom); + Assert.Equal(dto.Mail, user.Mail); + Assert.Equal(dto.Role, user.Role); + } + + [Fact] + public void ToDTONullUserThrowsNullReferenceException() + { + // Arrange + User user = null; + + // Act & Assert + Assert.Throws(() => UserMapping.ToDTO(user)); + } + + [Fact] + public void ToModelNullUserDTOThrowsNullReferenceException() + { + // Arrange + UserDTO dto = null; + + // Act & Assert + Assert.Throws(() => UserMapping.ToModel(dto)); + } + + [Fact] + public void ToDTOMapsCorrectlyWithEmptyUser() + { + // Arrange + var user = new User(); + + // Act + var dto = UserMapping.ToDTO(user); + + // Assert + Assert.NotNull(dto); + Assert.Equal(user.Pseudo, dto.Pseudo); + Assert.Equal(user.Mdp, dto.Mdp); + Assert.Equal(user.Nom, dto.Nom); + Assert.Equal(user.Prenom, dto.Prenom); + Assert.Equal(user.Mail, dto.Mail); + Assert.Equal(user.Role, dto.Role); + } + + [Fact] + public void ToModelMapsCorrectlyWithEmptyUserDTO() + { + // Arrange + var dto = new UserDTO(); + + // Act + var user = UserMapping.ToModel(dto); + + // Assert + Assert.NotNull(user); + Assert.Equal(dto.Pseudo, user.Pseudo); + Assert.Equal(dto.Mdp, user.Mdp); + Assert.Equal(dto.Nom, user.Nom); + Assert.Equal(dto.Prenom, user.Prenom); + Assert.Equal(dto.Mail, user.Mail); + Assert.Equal(dto.Role, user.Role); + } +} \ No newline at end of file