From a5b1b6191c1ccb6e95787298f8c98e0abac7649b Mon Sep 17 00:00:00 2001 From: babisque Date: Wed, 5 Jun 2024 09:00:10 -0300 Subject: [PATCH] CategoryTests created and some fixings in controller --- .../Controllers/CategoryController.cs | 10 +- .../Catalog.Test/CategoryControllerTests.cs | 201 ++++++++++++++++++ 2 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 Catalog/Catalog.Test/CategoryControllerTests.cs diff --git a/Catalog/Catalog.API/Controllers/CategoryController.cs b/Catalog/Catalog.API/Controllers/CategoryController.cs index ce5c33b..fd8f141 100644 --- a/Catalog/Catalog.API/Controllers/CategoryController.cs +++ b/Catalog/Catalog.API/Controllers/CategoryController.cs @@ -77,9 +77,15 @@ public async Task GetCategoryById([FromRoute] int categoryId) { var category = await _categoryRepository.GetByIdAsync(categoryId); if (String.IsNullOrEmpty(category.Name)) - return NotFound($"Category not found for ID {categoryId}"); + return NotFound(); - return Ok(category); + var res = new CategoryGetRes + { + Id = category.Id, + Name = category.Name + }; + + return Ok(res); } catch (Exception e) { diff --git a/Catalog/Catalog.Test/CategoryControllerTests.cs b/Catalog/Catalog.Test/CategoryControllerTests.cs new file mode 100644 index 0000000..5ebb6a0 --- /dev/null +++ b/Catalog/Catalog.Test/CategoryControllerTests.cs @@ -0,0 +1,201 @@ +using Catalog.API.Controllers; +using Catalog.Core.DTO.Category; +using Catalog.Core.Entities; +using Catalog.Core.Repositories; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Moq; + +namespace Catalog.Test; + +public class CategoryControllerTests +{ + private readonly Mock _categoryRepositoryMock; + private readonly Mock> _loggerMock; + private readonly CategoryController _controller; + + public CategoryControllerTests() + { + _categoryRepositoryMock = new Mock(); + _loggerMock = new Mock>(); + + _controller = new CategoryController(_categoryRepositoryMock.Object, _loggerMock.Object); + } + + [Fact] + public async void Get_ReturnsOkWithCategories() + { + // Arrange + var categories = new List + { + new Category { Id = 1, Name = "Category 1" }, + new Category { Id = 2, Name = "Category 2" } + }; + + _categoryRepositoryMock.Setup(repo => repo.GetAllAsync()) + .ReturnsAsync(categories); + + // Act + var result = await _controller.Get(); + + // Assert + var okResult = Assert.IsType(result); + var returnValue = Assert.IsType>(okResult.Value); + Assert.Equal(2, returnValue.Count); + } + + [Fact] + public async void Get_ReturnsNoContent() + { + // Arrange + _categoryRepositoryMock.Setup(repo => repo.GetAllAsync()) + .ReturnsAsync(new List()); + + // Act + var result = await _controller.Get(); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async void GetCategory_NonExistingId_ReturnsNotFount() + { + // Arrange + _categoryRepositoryMock.Setup(repo => repo.GetByIdAsync(1)) + .ReturnsAsync(new Category()); + + // Act + var result = await _controller.GetCategoryById(1); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async void GetCategory_ExistingId_ReturnsOk() + { + // Arrange + var category = new Category { Id = 1, Name = "Category 1" }; + + _categoryRepositoryMock.Setup(repo => repo.GetByIdAsync(1)) + .ReturnsAsync(category); + + // Act + var result = await _controller.GetCategoryById(1); + + // Assert + var okResult = Assert.IsType(result); + var returnValue = Assert.IsType(okResult.Value); + Assert.Equal(category.Name, returnValue.Name); + } + + [Fact] + public async void Post_ValidCategory_ReturnsCreatedResult() + { + // Arrange + var categoryDtoReq = new CategoryPostReq + { + Name = "Category 1" + }; + + var category = new Category + { + Id = 1, + Name = categoryDtoReq.Name + }; + + _categoryRepositoryMock.Setup(repo => repo.CreateAsync(It.IsAny())) + .Returns(Task.CompletedTask); + + // Act + var result = await _controller.Post(categoryDtoReq); + + // Assert + var createdResult = Assert.IsType(result); + var returnValue = Assert.IsType(createdResult.Value); + Assert.Equal(category.Name, returnValue.Name); + _categoryRepositoryMock.Verify(repo => repo.CreateAsync(It.IsAny()), Times.Once); + } + + [Fact] + public async void Put_NonExistingId_ReturnsNotFound() + { + // Arrange + var category = new Category(); + var categoryReq = new CategoryUpdateReq + { + Name = "Category" + }; + + _categoryRepositoryMock.Setup(repo => repo.GetByIdAsync(1)) + .ReturnsAsync(category); + + // Act + var result = await _controller.Update(1, categoryReq); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async void Put_ValidId_ReturnsOk() + { + // Arrange + var category = new Category { Id = 1, Name = "Category Tesst" }; + var categoryReq = new CategoryUpdateReq { Name = "Category Test" }; + + _categoryRepositoryMock.Setup(repo => repo.GetByIdAsync(1)) + .ReturnsAsync(category); + + _categoryRepositoryMock.Setup(repo => repo.UpdateAsync(It.IsAny())) + .Returns(Task.CompletedTask); + + // Act + var result = await _controller.Update(1, categoryReq); + + // Assert + Assert.IsType(result); + _categoryRepositoryMock.Verify(repo => repo.UpdateAsync(It.IsAny()), Times.Once); + } + + [Fact] + public async void Delete_NonExistingId_ReturnsNotFount() + { + // Arrange + var category = new Category(); + var categoryReq = new CategoryUpdateReq + { + Name = "Category" + }; + + _categoryRepositoryMock.Setup(repo => repo.GetByIdAsync(1)) + .ReturnsAsync(category); + + // Act + var result = await _controller.Delete(1); + + // Assert + Assert.IsType(result); + } + + [Fact] + public async void Delete_ValidId_ReturnsOk() + { + // Arrange + var category = new Category { Id = 1, Name = "Category Tesst" }; + + _categoryRepositoryMock.Setup(repo => repo.GetByIdAsync(1)) + .ReturnsAsync(category); + + _categoryRepositoryMock.Setup(repo => repo.RemoveAsync(1)) + .Returns(Task.CompletedTask); + + // Act + var result = await _controller.Delete(1); + + // Assert + Assert.IsType(result); + _categoryRepositoryMock.Verify(repo => repo.RemoveAsync(1), Times.Once); + } +} \ No newline at end of file