diff --git a/CommonTestUtilities/Entities/ClientBuilder.cs b/CommonTestUtilities/Entities/ClientBuilder.cs index 96df670..3f78044 100644 --- a/CommonTestUtilities/Entities/ClientBuilder.cs +++ b/CommonTestUtilities/Entities/ClientBuilder.cs @@ -16,7 +16,8 @@ public static (Client client, string password) Build() .RuleFor(client => client.Id, () => Guid.NewGuid()) .RuleFor(client => client.Name, (f) => f.Person.FirstName) .RuleFor(client => client.Email, (f, user) => f.Internet.Email(user.Name)) - .RuleFor(client => client.Password, (f) => passwordEncripter.Encrypt(password)); + .RuleFor(client => client.Password, (f) => passwordEncripter.Encrypt(password)) + .RuleFor(client => client.Products, () => new List()); return (client, password); } diff --git a/CommonTestUtilities/Entities/ProductBuilder.cs b/CommonTestUtilities/Entities/ProductBuilder.cs index 78c6c65..bfc3b80 100644 --- a/CommonTestUtilities/Entities/ProductBuilder.cs +++ b/CommonTestUtilities/Entities/ProductBuilder.cs @@ -20,6 +20,9 @@ public static IList Collection(Client client, uint count = 2) list.Add(fakeProduct); } + foreach (var product in list) + client.Products.Add(product); + return list; } diff --git a/CommonTestUtilities/LoggedUser/LoggedUserBuilder.cs b/CommonTestUtilities/LoggedUser/LoggedUserBuilder.cs index 8596660..d051785 100644 --- a/CommonTestUtilities/LoggedUser/LoggedUserBuilder.cs +++ b/CommonTestUtilities/LoggedUser/LoggedUserBuilder.cs @@ -1,14 +1,14 @@ using Moq; using ProductClientHub.Domain.Entities; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; -namespace CommonTestUtilities.LoggedUser; +namespace CommonTestUtilities.loggedClient; -public class LoggedUserBuilder +public class LoggedClientBuilder { - public static ILoggedUser Build(Client client) + public static ILoggedClient Build(Client client) { - var mock = new Mock(); + var mock = new Mock(); mock.Setup(c => c.User()).ReturnsAsync(client); diff --git a/CommonTestUtilities/Repositories/ClientReadOnlyRepositoryBuilder.cs b/CommonTestUtilities/Repositories/ClientReadOnlyRepositoryBuilder.cs index 6bb1fe1..6148d7f 100644 --- a/CommonTestUtilities/Repositories/ClientReadOnlyRepositoryBuilder.cs +++ b/CommonTestUtilities/Repositories/ClientReadOnlyRepositoryBuilder.cs @@ -15,12 +15,20 @@ public ClientReadOnlyRepositoryBuilder() public ClientReadOnlyRepositoryBuilder EmailAlreadyExists(Client? client) { - if(client is not null) + if (client is not null) _repository.Setup(r => r.EmailAlreadyExists(client.Email)).ReturnsAsync(client); return this; } + public ClientReadOnlyRepositoryBuilder EmailAlreadyExists(Client? clientEmail, Client? clientReturned) + { + if (clientEmail is not null && clientReturned is not null) + _repository.Setup(r => r.EmailAlreadyExists(clientEmail.Email)).ReturnsAsync(clientReturned); + + return this; + } + public ClientReadOnlyRepositoryBuilder GetAll(IList clients) { _repository.Setup(r => r.GetAll()).ReturnsAsync(clients); diff --git a/CommonTestUtilities/Repositories/ProductsReadOnlyRepositoryBuild.cs b/CommonTestUtilities/Repositories/ProductsReadOnlyRepositoryBuild.cs new file mode 100644 index 0000000..570c948 --- /dev/null +++ b/CommonTestUtilities/Repositories/ProductsReadOnlyRepositoryBuild.cs @@ -0,0 +1,33 @@ +using Moq; +using ProductClientHub.Domain.Repositories.Product; + +namespace CommonTestUtilities.Repositories; + +public class ProductsReadOnlyRepositoryBuild +{ + private readonly Mock _repository; + + public ProductsReadOnlyRepositoryBuild() + { + _repository = new Mock(); + } + + public ProductsReadOnlyRepositoryBuild GetAll(IList products) + { + _repository.Setup(r => r.GetAll()).ReturnsAsync(products); + return this; + } + + public ProductsReadOnlyRepositoryBuild GetById(ProductClientHub.Domain.Entities.Product? product) + { + if(product is not null) + _repository.Setup(r => r.GetById(It.IsAny())).ReturnsAsync(product); + + return this; + } + + public IProductsReadOnlyRepository Build() + { + return _repository.Object; + } +} diff --git a/ProductClientHub.Application/UseCases/Clients/Update/UpdateClientUseCase.cs b/ProductClientHub.Application/UseCases/Clients/Update/UpdateClientUseCase.cs index 9ca9d96..888da9e 100644 --- a/ProductClientHub.Application/UseCases/Clients/Update/UpdateClientUseCase.cs +++ b/ProductClientHub.Application/UseCases/Clients/Update/UpdateClientUseCase.cs @@ -5,7 +5,7 @@ using ProductClientHub.Domain.Extensions; using ProductClientHub.Domain.Repositories.Client; using ProductClientHub.Domain.Repositories.UnitOfWork; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Exceptions.ExceptionsBase; namespace ProductClientHub.Application.UseCases.Users.Update; @@ -15,24 +15,24 @@ public class UpdateClientUseCase : IUpdateClientUseCase private readonly IClientWriteOnlyRepository _clientWriteOnlyRepository; private readonly IClientReadOnlyRepository _clientReadOnlyRepository; private readonly IUnitOfWork _unitOfWork; - private readonly ILoggedUser _loggedUser; + private readonly ILoggedClient _loggedClient; public UpdateClientUseCase(IClientWriteOnlyRepository clientWriteOnlyRepository, IClientReadOnlyRepository clientReadOnlyRepository, IUnitOfWork unitOfWork, - ILoggedUser loggedUser) + ILoggedClient loggedClient) { _clientWriteOnlyRepository = clientWriteOnlyRepository; _clientReadOnlyRepository = clientReadOnlyRepository; _unitOfWork = unitOfWork; - _loggedUser = loggedUser; + _loggedClient = loggedClient; } public async Task Execute(RequestShortClientJson request) { Validate(request); - var userLogged = await _loggedUser.User(); + var userLogged = await _loggedClient.User(); var client = await _clientReadOnlyRepository.GetById(userLogged.Id) ?? throw new NotFoundException(ResourceMessagesExceptions.CLIENT_NOCONTENT); diff --git a/ProductClientHub.Application/UseCases/Products/Delete/DeleteProductUseCase.cs b/ProductClientHub.Application/UseCases/Products/Delete/DeleteProductUseCase.cs index eaee1d1..fe6c158 100644 --- a/ProductClientHub.Application/UseCases/Products/Delete/DeleteProductUseCase.cs +++ b/ProductClientHub.Application/UseCases/Products/Delete/DeleteProductUseCase.cs @@ -1,7 +1,7 @@ using ProductClientHub.Domain.Extensions; using ProductClientHub.Domain.Repositories.Product; using ProductClientHub.Domain.Repositories.UnitOfWork; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Exceptions.ExceptionsBase; namespace ProductClientHub.Application.UseCases.Products.Delete; @@ -10,20 +10,20 @@ public class DeleteProductUseCase : IDeleteProductUseCase { private readonly IDeleteProductOnlyRepository _productsWriteOnlyRepository; private readonly IUnitOfWork _unitOfWork; - private readonly ILoggedUser _loggedUser; + private readonly ILoggedClient _loggedClient; public DeleteProductUseCase(IDeleteProductOnlyRepository productsWriteOnlyRepository, IUnitOfWork unitOfWork, - ILoggedUser loggedUser) + ILoggedClient loggedClient) { _productsWriteOnlyRepository = productsWriteOnlyRepository; _unitOfWork = unitOfWork; - _loggedUser = loggedUser; + _loggedClient = loggedClient; } public async Task Execute(Guid productId) { - var client = await _loggedUser.User(); + var client = await _loggedClient.User(); var productExist = await _productsWriteOnlyRepository.Delete(client.Id, productId); diff --git a/ProductClientHub.Application/UseCases/Products/GetAll/GetAllProductsUseCase.cs b/ProductClientHub.Application/UseCases/Products/GetAll/GetAllProductsUseCase.cs index 7af7735..5f9014f 100644 --- a/ProductClientHub.Application/UseCases/Products/GetAll/GetAllProductsUseCase.cs +++ b/ProductClientHub.Application/UseCases/Products/GetAll/GetAllProductsUseCase.cs @@ -2,7 +2,7 @@ using ProductClientHub.Communication.Responses; using ProductClientHub.Domain.Extensions; using ProductClientHub.Domain.Repositories.Product; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Exceptions.ExceptionsBase; namespace ProductClientHub.Application.UseCases.Products.GetAll; @@ -10,17 +10,17 @@ namespace ProductClientHub.Application.UseCases.Products.GetAll; public class GetAllProductsUseCase : IGetAllProductsUseCase { private readonly IProductsReadOnlyRepository _productReadOnlyRepository; - private readonly ILoggedUser _loggedUser; + private readonly ILoggedClient _loggedClient; - public GetAllProductsUseCase(IProductsReadOnlyRepository productReadOnlyRepository, ILoggedUser loggedUser) + public GetAllProductsUseCase(IProductsReadOnlyRepository productReadOnlyRepository, ILoggedClient loggedClient) { _productReadOnlyRepository = productReadOnlyRepository; - _loggedUser = loggedUser; + _loggedClient = loggedClient; } public async Task Execute() { - var user = await _loggedUser.User(); + var user = await _loggedClient.User(); if (user is null) throw new UserNotLoggedException(ResourceMessagesExceptions.NOT_LOGGED); diff --git a/ProductClientHub.Application/UseCases/Products/Register/RegisterProductUseCase.cs b/ProductClientHub.Application/UseCases/Products/Register/RegisterProductUseCase.cs index e4296fc..93ec2fc 100644 --- a/ProductClientHub.Application/UseCases/Products/Register/RegisterProductUseCase.cs +++ b/ProductClientHub.Application/UseCases/Products/Register/RegisterProductUseCase.cs @@ -6,7 +6,7 @@ using ProductClientHub.Domain.Repositories.Client; using ProductClientHub.Domain.Repositories.Product; using ProductClientHub.Domain.Repositories.UnitOfWork; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Exceptions.ExceptionsBase; namespace ProductClientHub.Application.UseCases.Products.Register; @@ -16,22 +16,22 @@ public class RegisterProductUseCase : IRegisterProductUseCase private readonly IProductsWriteOnlyRepository _productsWriteOnlyRepository; private readonly IClientReadOnlyRepository _clientReadOnlyRepository; private readonly IUnitOfWork _unitOfWork; - private readonly ILoggedUser _loggedUser; + private readonly ILoggedClient _loggedClient; public RegisterProductUseCase(IProductsWriteOnlyRepository productsWriteOnlyRepository, IUnitOfWork unitOfWork, IClientReadOnlyRepository clientReadOnlyRepository, - ILoggedUser loggedUser) + ILoggedClient loggedClient) { _productsWriteOnlyRepository = productsWriteOnlyRepository; _unitOfWork = unitOfWork; _clientReadOnlyRepository = clientReadOnlyRepository; - _loggedUser = loggedUser; + _loggedClient = loggedClient; } public async Task Execute(RequestProductJson request) { - var user = await _loggedUser.User(); + var user = await _loggedClient.User(); await Validate(user.Id, request); diff --git a/ProductClientHub.Application/UseCases/Products/Update/UploadProductUseCase.cs b/ProductClientHub.Application/UseCases/Products/Update/UploadProductUseCase.cs index 471784b..e83d55d 100644 --- a/ProductClientHub.Application/UseCases/Products/Update/UploadProductUseCase.cs +++ b/ProductClientHub.Application/UseCases/Products/Update/UploadProductUseCase.cs @@ -6,7 +6,7 @@ using ProductClientHub.Domain.Repositories.Client; using ProductClientHub.Domain.Repositories.Product; using ProductClientHub.Domain.Repositories.UnitOfWork; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Exceptions.ExceptionsBase; namespace ProductClientHub.Application.UseCases.Products.Update; @@ -17,24 +17,24 @@ public class UploadProductUseCase : IUploadProductUseCase private readonly IClientReadOnlyRepository _clientReadOnlyRepository; private readonly IProductsReadOnlyRepository _productsReadOnlyRepository; private readonly IUploadProductOnlyRepository _productWriteOnlyRepository; - private readonly ILoggedUser _loggedUser; + private readonly ILoggedClient _loggedClient; public UploadProductUseCase(IUnitOfWork unitOfWork, IUploadProductOnlyRepository productWriteOnlyRepository, IClientReadOnlyRepository clientReadOnlyRepository, IProductsReadOnlyRepository productsReadOnlyRepository, - ILoggedUser loggedUser) + ILoggedClient loggedClient) { _unitOfWork = unitOfWork; _productWriteOnlyRepository = productWriteOnlyRepository; _clientReadOnlyRepository = clientReadOnlyRepository; _productsReadOnlyRepository = productsReadOnlyRepository; - _loggedUser = loggedUser; + _loggedClient = loggedClient; } public async Task Execute(Guid productId, RequestProductJson request) { - var client = await _loggedUser.User(); + var client = await _loggedClient.User(); await Validate(client.Id, request); var product = await _productsReadOnlyRepository.GetById(productId) ?? throw new NotFoundException(ResourceMessagesExceptions.PRODUCT_NOTFOUND); diff --git a/ProductClientHub.Domain/Services/LoggedUser/ILoggedUser.cs b/ProductClientHub.Domain/Services/LoggedUser/ILoggedUser.cs index b89c5a5..de0203f 100644 --- a/ProductClientHub.Domain/Services/LoggedUser/ILoggedUser.cs +++ b/ProductClientHub.Domain/Services/LoggedUser/ILoggedUser.cs @@ -1,8 +1,8 @@ using ProductClientHub.Domain.Entities; -namespace ProductClientHub.Domain.Services.LoggedUser; +namespace ProductClientHub.Domain.Services.loggedClient; -public interface ILoggedUser +public interface ILoggedClient { public Task User(); } diff --git a/ProductClientHub.Infrastructure/DependencyInjectionExtension.cs b/ProductClientHub.Infrastructure/DependencyInjectionExtension.cs index 00da013..2e1e9c3 100644 --- a/ProductClientHub.Infrastructure/DependencyInjectionExtension.cs +++ b/ProductClientHub.Infrastructure/DependencyInjectionExtension.cs @@ -21,7 +21,7 @@ using ProductClientHub.Domain.Security.Tokens; using ProductClientHub.Infrastructure.Security.Tokens.Acess.Generator; using ProductClientHub.Infrastructure.Security.Tokens.Acess.Validator; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Domain.Services.Messaging; using ProductClientHub.Infrastructure.Services; using ProductClientHub.Infrastructure.Messaging.RabbitMq; @@ -37,7 +37,7 @@ public static void AddInfrastructure(this IServiceCollection services, IConfigur AddFluentMigrator_PostgreSql(services, configuration); AddTokens(services, configuration); AddPasswordEncrpter(services); - AddLoggedUser(services); + AddloggedClient(services); AddMessaging(services, configuration); } @@ -78,7 +78,7 @@ private static void AddTokens(IServiceCollection services, IConfiguration config //services.AddScoped(); } - private static void AddLoggedUser(IServiceCollection services) => services.AddScoped(); + private static void AddloggedClient(IServiceCollection services) => services.AddScoped(); private static void AddMessaging(IServiceCollection services, IConfiguration configuration) { diff --git a/ProductClientHub.Infrastructure/Services/LoggedUser.cs b/ProductClientHub.Infrastructure/Services/LoggedUser.cs index caad32e..6b1edc8 100644 --- a/ProductClientHub.Infrastructure/Services/LoggedUser.cs +++ b/ProductClientHub.Infrastructure/Services/LoggedUser.cs @@ -1,19 +1,19 @@ using Microsoft.EntityFrameworkCore; using ProductClientHub.Domain.Entities; using ProductClientHub.Domain.Security.Tokens; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ProductClientHub.Infrastructure.Database; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; namespace ProductClientHub.Infrastructure.Services; -public class LoggedUser : ILoggedUser +public class loggedClient : ILoggedClient { private readonly ProductClientHubDbContext _dbContext; private readonly ITokenProvider _tokenProvider; - public LoggedUser(ProductClientHubDbContext dbContext, ITokenProvider tokenProvider) + public loggedClient(ProductClientHubDbContext dbContext, ITokenProvider tokenProvider) { _dbContext = dbContext; _tokenProvider = tokenProvider; diff --git a/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs b/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs index 1aa8e57..e879596 100644 --- a/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs +++ b/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs @@ -1,6 +1,5 @@ -using CommonTestUtilities.Cryptografhy; -using CommonTestUtilities.Entities; -using CommonTestUtilities.LoggedUser; +using CommonTestUtilities.Entities; +using CommonTestUtilities.loggedClient; using CommonTestUtilities.Repositories; using CommonTestUtilities.Requests; using ProductClientHub.Application.UseCases.Users.Update; @@ -20,7 +19,7 @@ public async Task UpdateClient_Sucess() var clientRequest = RequestShortClientJsonBuilder.Build(client.Name, client.Email); - var useCase = CreateUseCase(client, emailExistsTest: false, clientExist: true); + var useCase = CreateUseCase(client, emailExistClient: null, clientExist: true); var result = await useCase.Execute(clientRequest); @@ -38,7 +37,7 @@ public async Task UpdateClient_Error_ClientNotExists() var clientRequest = RequestShortClientJsonBuilder.Build(client.Name, client.Email); - var useCase = CreateUseCase(client, emailExistsTest: false, clientExist: false); + var useCase = CreateUseCase(client, emailExistClient: null, clientExist: false); var resultException = await Should.ThrowAsync(async () => await useCase.Execute(clientRequest)); @@ -50,10 +49,11 @@ public async Task UpdateClient_Error_ClientNotExists() public async Task UpdateClient_Error_EmailExist() { (var client, _) = ClientBuilder.Build(); + (var anotherClient, _) = ClientBuilder.Build(); var clientRequest = RequestShortClientJsonBuilder.Build(client.Name, client.Email); - var useCase = CreateUseCase(client, emailExistsTest: true, clientExist: true); + var useCase = CreateUseCase(client, emailExistClient: anotherClient, clientExist: true); var resultException = await Should.ThrowAsync(async () => await useCase.Execute(clientRequest)); @@ -61,24 +61,22 @@ public async Task UpdateClient_Error_EmailExist() resultException.ShouldSatisfyAllConditions(() => resultException.Message.ShouldBe(ResourceMessagesExceptions.EMAIL_INVALID)); } - private static UpdateClientUseCase CreateUseCase(ProductClientHub.Domain.Entities.Client? client, bool emailExistsTest, bool clientExist) + private static UpdateClientUseCase CreateUseCase( + ProductClientHub.Domain.Entities.Client? client, + ProductClientHub.Domain.Entities.Client? emailExistClient = null, + bool clientExist = false) { var clientWriteOnlyRepository = ClientWriteOnlyRepositoryBuilder.Build(); var clientReadOnlyRepository = new ClientReadOnlyRepositoryBuilder(); var unitOfWork = UnitOfWorkBuilder.Build(); - var loggedUser = LoggedUserBuilder.Build(client!); + var loggedClient = LoggedClientBuilder.Build(client!); if (client is not null && clientExist.IsTrue()) - clientReadOnlyRepository.GetById(client); + clientReadOnlyRepository.GetById(client); - if(emailExistsTest.IsTrue()) - { - var (clientWithSameEmail, _) = ClientBuilder.Build(); - clientWithSameEmail.Email = client!.Email; + if (emailExistClient is not null) + clientReadOnlyRepository.EmailAlreadyExists(client, emailExistClient); - clientReadOnlyRepository.EmailAlreadyExists(clientWithSameEmail); - } - - return new UpdateClientUseCase(clientWriteOnlyRepository, clientReadOnlyRepository.Build(), unitOfWork, loggedUser); + return new UpdateClientUseCase(clientWriteOnlyRepository, clientReadOnlyRepository.Build(), unitOfWork, loggedClient); } } diff --git a/UseCase.Test/Product/GetAll/GetAllProductsUseCaseTest.cs b/UseCase.Test/Product/GetAll/GetAllProductsUseCaseTest.cs new file mode 100644 index 0000000..b7b32e6 --- /dev/null +++ b/UseCase.Test/Product/GetAll/GetAllProductsUseCaseTest.cs @@ -0,0 +1,38 @@ +using CommonTestUtilities.Entities; +using CommonTestUtilities.loggedClient; +using CommonTestUtilities.Repositories; +using ProductClientHub.Application.UseCases.Products.GetAll; +using ProductClientHub.Domain.Extensions; +using Shouldly; + +namespace UseCase.Test.Product.GetAll; + +public class GetAllProductsUseCaseTest +{ + [Fact] + public async Task GetAllProductsTest_Success() + { + (var client, _) = ClientBuilder.Build(); + + var useCase = CreateUseCase(client, clientIsNull: false); + + var result = await useCase.Execute(); + + result.ShouldNotBeNull(); + result.Products.ShouldNotBeNull(); + result.Products.Count.ShouldBe(client.Products.Count); + } + private static GetAllProductsUseCase CreateUseCase(ProductClientHub.Domain.Entities.Client client, bool clientIsNull) + { + var repositoryReadOnly = new ProductsReadOnlyRepositoryBuild(); + var userLogged = LoggedClientBuilder.Build(client!); + + if(client is not null && clientIsNull.IsFalse()) + { + var products = ProductBuilder.Collection(client); + repositoryReadOnly.GetAll(products); + } + + return new GetAllProductsUseCase(repositoryReadOnly.Build(), userLogged); + } +} diff --git a/WebApi.Test/CustomWebApplicationFactory.cs b/WebApi.Test/CustomWebApplicationFactory.cs index f8c1096..413ae11 100644 --- a/WebApi.Test/CustomWebApplicationFactory.cs +++ b/WebApi.Test/CustomWebApplicationFactory.cs @@ -7,7 +7,7 @@ using ProductClientHub.Domain.Repositories.Client; using ProductClientHub.Domain.Repositories.UnitOfWork; using ProductClientHub.Domain.Security.Tokens; -using ProductClientHub.Domain.Services.LoggedUser; +using ProductClientHub.Domain.Services.loggedClient; using ClientEntity = ProductClientHub.Domain.Entities.Client; namespace WebApi.Test; @@ -46,7 +46,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) services.RemoveAll(); services.RemoveAll(); services.RemoveAll(); - services.RemoveAll(); + services.RemoveAll(); // Remove all hosted services to prevent background services from running during tests services.RemoveAll(typeof(IHostedService)); @@ -56,7 +56,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); }); } @@ -140,11 +140,11 @@ public Task Add(ClientEntity client) } } - private sealed class FakeLoggedUser : ILoggedUser + private sealed class FakeloggedClient : ILoggedClient { private readonly TestClientStore _clientStore; - public FakeLoggedUser(TestClientStore clientStore) + public FakeloggedClient(TestClientStore clientStore) { _clientStore = clientStore; }