diff --git a/src/Backend/UserTransactions.API/Controllers/v1/UserController.cs b/src/Backend/UserTransactions.API/Controllers/v1/UserController.cs index 61475a3..51c373b 100644 --- a/src/Backend/UserTransactions.API/Controllers/v1/UserController.cs +++ b/src/Backend/UserTransactions.API/Controllers/v1/UserController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using System.Diagnostics.CodeAnalysis; using UserTransactions.Application.UseCases.User.Create; +using UserTransactions.Application.UseCases.User.ListAll; using UserTransactions.Application.UseCases.User.ListTotal; using UserTransactions.Communication.Dtos.Errors.Response; using UserTransactions.Communication.Dtos.User.Request; @@ -23,10 +24,18 @@ public class UserController : ControllerBase public async Task RegisterUser([FromBody] RequestCreateUserDto request, [FromServices] ICreateUserUseCase useCase) { var result = await useCase.ExecuteAsync(request); - return CreatedAtAction(nameof(RegisterUser), new { id = result.Id }, result); } + [HttpGet("list-all")] + [ProducesResponseType(typeof(ResponseListAllUsersDto), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ResponseErrorDto), StatusCodes.Status400BadRequest)] + public async Task ListAllUsers([FromServices] IListAllUsersUseCase useCase) + { + var result = await useCase.ExecuteAsync(); + return Ok(result); + } + [HttpGet("list-total-quantity")] [ProducesResponseType(typeof(ResponseListTotalQuantityUserDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ResponseErrorDto), StatusCodes.Status400BadRequest)] diff --git a/src/Backend/UserTransactions.API/Controllers/v1/WalletController.cs b/src/Backend/UserTransactions.API/Controllers/v1/WalletController.cs index 84447fd..026f5cb 100644 --- a/src/Backend/UserTransactions.API/Controllers/v1/WalletController.cs +++ b/src/Backend/UserTransactions.API/Controllers/v1/WalletController.cs @@ -2,9 +2,9 @@ using Microsoft.AspNetCore.Mvc; using System.Diagnostics.CodeAnalysis; using UserTransactions.Application.UseCases.Wallet.Create; +using UserTransactions.Application.UseCases.Wallet.ListAll; using UserTransactions.Application.UseCases.Wallet.ListTotal; using UserTransactions.Communication.Dtos.Errors.Response; -using UserTransactions.Communication.Dtos.User.Response; using UserTransactions.Communication.Dtos.Wallet.Request; using UserTransactions.Communication.Dtos.Wallet.Response; @@ -28,6 +28,15 @@ public async Task RegisterWallet([FromBody] RequestCreateWalletDt return CreatedAtAction(nameof(RegisterWallet), new { id = result.Id }, result); } + [HttpGet("list-all")] + [ProducesResponseType(typeof(ResponseListAllWalletsDto), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ResponseErrorDto), StatusCodes.Status400BadRequest)] + public async Task ListAllWallets([FromServices] IListAllWalletsUseCase useCase) + { + var result = await useCase.ExecuteAsync(); + return Ok(result); + } + [HttpGet("list-total-quantity")] [ProducesResponseType(typeof(ResponseListTotalQuantityWalletDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ResponseErrorDto), StatusCodes.Status400BadRequest)] diff --git a/src/Backend/UserTransactions.Application/DI/DependencyInjection.cs b/src/Backend/UserTransactions.Application/DI/DependencyInjection.cs index a813bdc..f37167b 100644 --- a/src/Backend/UserTransactions.Application/DI/DependencyInjection.cs +++ b/src/Backend/UserTransactions.Application/DI/DependencyInjection.cs @@ -11,8 +11,10 @@ using UserTransactions.Application.UseCases.Transaction.ListTotal; using UserTransactions.Application.UseCases.Transaction.ListTotalAmount; using UserTransactions.Application.UseCases.User.Create; +using UserTransactions.Application.UseCases.User.ListAll; using UserTransactions.Application.UseCases.User.ListTotal; using UserTransactions.Application.UseCases.Wallet.Create; +using UserTransactions.Application.UseCases.Wallet.ListAll; using UserTransactions.Application.UseCases.Wallet.ListTotal; using UserTransactions.Communication.Dtos.Errors.Response; @@ -41,9 +43,11 @@ public static void AddUseCases(this IServiceCollection services) services.AddScoped(); + services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); } diff --git a/src/Backend/UserTransactions.Application/Mappers/User/UserMapper.cs b/src/Backend/UserTransactions.Application/Mappers/User/UserMapper.cs index 4dc2e73..9231d67 100644 --- a/src/Backend/UserTransactions.Application/Mappers/User/UserMapper.cs +++ b/src/Backend/UserTransactions.Application/Mappers/User/UserMapper.cs @@ -20,5 +20,16 @@ public static ResponseCreateUserDto MapFromUser(this UserEntity user) Email = user.Email, }; } + + public static IList MapListAllFromUser(this IList users) + { + return users.Select(user => new ResponseListAllUsersDto + { + FullName = user.FullName, + Email = user.Email, + CPF = user.CPF, + UserType = user.UserType + }).ToList(); + } } } diff --git a/src/Backend/UserTransactions.Application/Mappers/Wallet/WalletMapper.cs b/src/Backend/UserTransactions.Application/Mappers/Wallet/WalletMapper.cs index 485ebbc..eb1c921 100644 --- a/src/Backend/UserTransactions.Application/Mappers/Wallet/WalletMapper.cs +++ b/src/Backend/UserTransactions.Application/Mappers/Wallet/WalletMapper.cs @@ -19,5 +19,16 @@ public static ResponseCreateWalletDto MapFromWallet(this WalletEntity wallet) Balance = wallet.Balance }; } + + public static IList MapListAllFromWallet(this IList wallets) + { + return wallets.Select(wallet => new ResponseListAllWalletsDto + { + FullName = wallet.User!.FullName, + Email = wallet.User!.Email, + UserType = wallet.User.UserType, + Balance = wallet.Balance + }).ToList(); + } } } \ No newline at end of file diff --git a/src/Backend/UserTransactions.Application/UseCases/Transaction/ListAll/ListAllTransactionUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/Transaction/ListAll/ListAllTransactionUseCase.cs index f3f3ba7..0f54847 100644 --- a/src/Backend/UserTransactions.Application/UseCases/Transaction/ListAll/ListAllTransactionUseCase.cs +++ b/src/Backend/UserTransactions.Application/UseCases/Transaction/ListAll/ListAllTransactionUseCase.cs @@ -17,9 +17,9 @@ public async Task> ExecuteAsync() { var transactions = await _transactionRepository.ListAllAsync(); - var ResponseListTransactionsDto = transactions.MapListAllFromTransactions(); + var responseListTransactionsDto = transactions.MapListAllFromTransactions(); - return ResponseListTransactionsDto; + return responseListTransactionsDto; } } } diff --git a/src/Backend/UserTransactions.Application/UseCases/User/ListAll/IListAllUsersUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/User/ListAll/IListAllUsersUseCase.cs new file mode 100644 index 0000000..bf7f4df --- /dev/null +++ b/src/Backend/UserTransactions.Application/UseCases/User/ListAll/IListAllUsersUseCase.cs @@ -0,0 +1,9 @@ +using UserTransactions.Communication.Dtos.User.Response; + +namespace UserTransactions.Application.UseCases.User.ListAll +{ + public interface IListAllUsersUseCase + { + Task> ExecuteAsync(); + } +} diff --git a/src/Backend/UserTransactions.Application/UseCases/User/ListAll/ListAllUsersUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/User/ListAll/ListAllUsersUseCase.cs new file mode 100644 index 0000000..7a01f85 --- /dev/null +++ b/src/Backend/UserTransactions.Application/UseCases/User/ListAll/ListAllUsersUseCase.cs @@ -0,0 +1,25 @@ +using UserTransactions.Application.Mappers.User; +using UserTransactions.Communication.Dtos.User.Response; +using UserTransactions.Domain.Repositories.User; + +namespace UserTransactions.Application.UseCases.User.ListAll +{ + public class ListAllUsersUseCase : IListAllUsersUseCase + { + private readonly IUserRepository _userRepository; + + public ListAllUsersUseCase(IUserRepository userRepository) + { + _userRepository = userRepository; + } + + public async Task> ExecuteAsync() + { + var users = await _userRepository.ListAllAsync(); + + var responseListAllUsersDto = users.MapListAllFromUser(); + + return responseListAllUsersDto; + } + } +} diff --git a/src/Backend/UserTransactions.Application/UseCases/Wallet/ListAll/IListAllWalletsUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListAll/IListAllWalletsUseCase.cs new file mode 100644 index 0000000..e04480d --- /dev/null +++ b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListAll/IListAllWalletsUseCase.cs @@ -0,0 +1,9 @@ +using UserTransactions.Communication.Dtos.Wallet.Response; + +namespace UserTransactions.Application.UseCases.Wallet.ListAll +{ + public interface IListAllWalletsUseCase + { + Task> ExecuteAsync(); + } +} diff --git a/src/Backend/UserTransactions.Application/UseCases/Wallet/ListAll/ListAllWalletsUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListAll/ListAllWalletsUseCase.cs new file mode 100644 index 0000000..08995c5 --- /dev/null +++ b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListAll/ListAllWalletsUseCase.cs @@ -0,0 +1,25 @@ +using UserTransactions.Application.Mappers.Wallet; +using UserTransactions.Communication.Dtos.Wallet.Response; +using UserTransactions.Domain.Repositories.Wallet; + +namespace UserTransactions.Application.UseCases.Wallet.ListAll +{ + public class ListAllWalletsUseCase : IListAllWalletsUseCase + { + private readonly IWalletRepository _walletRepository; + + public ListAllWalletsUseCase(IWalletRepository walletRepository) + { + _walletRepository = walletRepository; + } + + public async Task> ExecuteAsync() + { + var wallets = await _walletRepository.ListAllAsync(); + + var responseListAllWalletsDto = wallets.MapListAllFromWallet(); + + return responseListAllWalletsDto; + } + } +} diff --git a/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/IListTotalQuantityWalletUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/IListTotalQuantityWalletUseCase.cs index fbf3166..a47c9a5 100644 --- a/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/IListTotalQuantityWalletUseCase.cs +++ b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/IListTotalQuantityWalletUseCase.cs @@ -1,4 +1,4 @@ -using UserTransactions.Communication.Dtos.User.Response; +using UserTransactions.Communication.Dtos.Wallet.Response; namespace UserTransactions.Application.UseCases.Wallet.ListTotal { diff --git a/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/ListTotalQuantityWalletUseCase.cs b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/ListTotalQuantityWalletUseCase.cs index ec9bf5d..bdf0843 100644 --- a/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/ListTotalQuantityWalletUseCase.cs +++ b/src/Backend/UserTransactions.Application/UseCases/Wallet/ListTotal/ListTotalQuantityWalletUseCase.cs @@ -1,4 +1,4 @@ -using UserTransactions.Communication.Dtos.User.Response; +using UserTransactions.Communication.Dtos.Wallet.Response; using UserTransactions.Domain.Repositories.Wallet; namespace UserTransactions.Application.UseCases.Wallet.ListTotal diff --git a/src/Backend/UserTransactions.Domain/Repositories/User/IUserRepository.cs b/src/Backend/UserTransactions.Domain/Repositories/User/IUserRepository.cs index 9b758bd..85f9db3 100644 --- a/src/Backend/UserTransactions.Domain/Repositories/User/IUserRepository.cs +++ b/src/Backend/UserTransactions.Domain/Repositories/User/IUserRepository.cs @@ -9,5 +9,6 @@ public interface IUserRepository Task IsCpfAlreadyRegistered(string cpf); Task ExistsAndIsActiveAsync(Guid userId); Task ListTotalQuantityAsync(); + Task> ListAllAsync(); } } diff --git a/src/Backend/UserTransactions.Domain/Repositories/Wallet/IWalletRepository.cs b/src/Backend/UserTransactions.Domain/Repositories/Wallet/IWalletRepository.cs index af18e18..54b4bff 100644 --- a/src/Backend/UserTransactions.Domain/Repositories/Wallet/IWalletRepository.cs +++ b/src/Backend/UserTransactions.Domain/Repositories/Wallet/IWalletRepository.cs @@ -10,5 +10,6 @@ public interface IWalletRepository Task GetByIdAsync(Guid id); Task UpdateAsync(WalletEntity wallet); Task ListTotalQuantityAsync(); + Task> ListAllAsync(); } } diff --git a/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/TransactionRepository.cs b/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/TransactionRepository.cs index e59cb78..bb6b11c 100644 --- a/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/TransactionRepository.cs +++ b/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/TransactionRepository.cs @@ -31,15 +31,9 @@ public async Task> ListAllAsync() .ToListAsync(); } - public async Task ListTotalAsync() - { - return await _dbContext.Transactions.CountAsync(); - } + public async Task ListTotalAsync() => await _dbContext.Transactions.CountAsync(); - public async Task ListTotalAmountAsync() - { - return await _dbContext.Transactions.SumAsync(t => t.Amount); - } + public async Task ListTotalAmountAsync() => await _dbContext.Transactions.SumAsync(t => t.Amount); public async Task> ListLatestFourAsync() { diff --git a/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/UserRepository.cs b/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/UserRepository.cs index da0a616..651ab43 100644 --- a/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/UserRepository.cs +++ b/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/UserRepository.cs @@ -22,24 +22,14 @@ public async Task AddAsync(User user) await _dbContext.SaveChangesAsync(); } - public async Task IsCpfAlreadyRegistered(string cpf) - { - return await _dbContext.Users.AnyAsync(u => u.CPF == cpf); - } + public async Task IsCpfAlreadyRegistered(string cpf) => await _dbContext.Users.AnyAsync(u => u.CPF == cpf); - public async Task IsEmailAlreadyRegistered(string email) - { - return await _dbContext.Users.AnyAsync(u => u.Email == email); - } + public async Task IsEmailAlreadyRegistered(string email) => await _dbContext.Users.AnyAsync(u => u.Email == email); - public async Task ExistsAndIsActiveAsync(Guid userId) - { - return await _dbContext.Users.AnyAsync(u => u.Id == userId && u.IsActive); - } + public async Task ExistsAndIsActiveAsync(Guid userId) => await _dbContext.Users.AnyAsync(u => u.Id == userId && u.IsActive); - public async Task ListTotalQuantityAsync() - { - return await _dbContext.Users.CountAsync(); - } + public async Task ListTotalQuantityAsync() => await _dbContext.Users.CountAsync(); + + public async Task> ListAllAsync() => await _dbContext.Users.ToListAsync(); } } diff --git a/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/WalletRepository.cs b/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/WalletRepository.cs index 2639d54..6ede81a 100644 --- a/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/WalletRepository.cs +++ b/src/Backend/UserTransactions.Infrastructure/Persistance/Repositories/WalletRepository.cs @@ -22,17 +22,9 @@ public async Task AddAsync(Wallet wallet) await _dbContext.SaveChangesAsync(); } - public async Task ExistsByUserIdAsync(Guid userId) - { - return await _dbContext.Wallets - .AnyAsync(w => w.UserId == userId && w.IsActive); - } + public async Task ExistsByUserIdAsync(Guid userId) => await _dbContext.Wallets.AnyAsync(w => w.UserId == userId && w.IsActive); - public async Task ExistsByIdAsync(Guid id) - { - return await _dbContext.Wallets - .AnyAsync(w => w.Id == id && w.IsActive); - } + public async Task ExistsByIdAsync(Guid id) => await _dbContext.Wallets.AnyAsync(w => w.Id == id && w.IsActive); public async Task GetByIdAsync(Guid id) { @@ -48,9 +40,8 @@ public async Task UpdateAsync(Wallet wallet) await _dbContext.SaveChangesAsync(); } - public async Task ListTotalQuantityAsync() - { - return await _dbContext.Wallets.CountAsync(); - } + public async Task ListTotalQuantityAsync() => await _dbContext.Wallets.CountAsync(); + + public async Task> ListAllAsync() => await _dbContext.Wallets.Include(w => w.User).ToListAsync(); } } \ No newline at end of file diff --git a/src/Shared/UserTransactions.Communication/Dtos/User/Response/ResponseListAllUsersDto.cs b/src/Shared/UserTransactions.Communication/Dtos/User/Response/ResponseListAllUsersDto.cs new file mode 100644 index 0000000..ba5e9f5 --- /dev/null +++ b/src/Shared/UserTransactions.Communication/Dtos/User/Response/ResponseListAllUsersDto.cs @@ -0,0 +1,12 @@ +using UserTransactions.Domain.Enum; + +namespace UserTransactions.Communication.Dtos.User.Response +{ + public class ResponseListAllUsersDto + { + public string FullName { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string CPF { get; set; } = string.Empty; + public UserType UserType { get; set; } + } +} diff --git a/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListAllWalletsDto.cs b/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListAllWalletsDto.cs new file mode 100644 index 0000000..f32f5d4 --- /dev/null +++ b/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListAllWalletsDto.cs @@ -0,0 +1,12 @@ +using UserTransactions.Domain.Enum; + +namespace UserTransactions.Communication.Dtos.Wallet.Response +{ + public class ResponseListAllWalletsDto + { + public string FullName { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public decimal Balance { get; set; } + public UserType UserType { get; set; } + } +} diff --git a/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListTotalQuantityWalletDto.cs b/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListTotalQuantityWalletDto.cs index e3f1a37..1cbff58 100644 --- a/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListTotalQuantityWalletDto.cs +++ b/src/Shared/UserTransactions.Communication/Dtos/Wallet/Response/ResponseListTotalQuantityWalletDto.cs @@ -1,4 +1,4 @@ -namespace UserTransactions.Communication.Dtos.User.Response +namespace UserTransactions.Communication.Dtos.Wallet.Response { public class ResponseListTotalQuantityWalletDto { diff --git a/tests/UserTransactions.Tests/Application/User/UseCases/ListAllUsersUseCaseTest.cs b/tests/UserTransactions.Tests/Application/User/UseCases/ListAllUsersUseCaseTest.cs new file mode 100644 index 0000000..f5847e6 --- /dev/null +++ b/tests/UserTransactions.Tests/Application/User/UseCases/ListAllUsersUseCaseTest.cs @@ -0,0 +1,106 @@ +using FluentAssertions; +using UserTransactions.Application.UseCases.User.ListAll; +using UserTransactions.Domain.Enum; +using UserTransactions.Domain.Repositories.User; +using UserTransactions.Tests.Shared.Builders.Entities; +using UserTransactions.Tests.Shared.Builders.Repositories; +using UserEntity = UserTransactions.Domain.Entities.User; + +namespace UserTransactions.Tests.Application.User.UseCases +{ + public class ListAllUsersUseCaseTest + { + private readonly IUserRepository _userRepository; + private readonly IListAllUsersUseCase _sut; + + public ListAllUsersUseCaseTest() + { + _userRepository = UserRepositoryBuilder.Build(); + _sut = new ListAllUsersUseCase(_userRepository); + } + + [Fact] + public async Task Given_ValidRequest_When_ExecuteAsyncIsCalled_Then_ShouldReturnValidResult() + { + // Arrange + var users = new List(); + for (int i = 0; i < 3; i++) + { + users.Add(UserEntityBuilder.Build()); + } + UserRepositoryBuilder.SetupListAllAsync(users); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().HaveCount(3); + result.Should().AllSatisfy(user => + { + user.FullName.Should().NotBeNullOrEmpty(); + user.Email.Should().NotBeNullOrEmpty(); + user.CPF.Should().NotBeNullOrEmpty(); + user.UserType.Should().BeDefined(); + }); + } + + [Fact] + public async Task Given_NoUsers_When_ExecuteAsyncIsCalled_Then_ShouldReturnEmptyList() + { + // Arrange + var users = new List(); + UserRepositoryBuilder.SetupListAllAsync(users); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEmpty(); + } + + [Theory] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + public async Task Given_SpecificNumberOfUsers_When_ExecuteAsyncIsCalled_Then_ShouldReturnCorrectCount(int userCount) + { + // Arrange + var users = new List(); + for (int i = 0; i < userCount; i++) + { + users.Add(UserEntityBuilder.Build()); + } + UserRepositoryBuilder.SetupListAllAsync(users); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().HaveCount(userCount); + } + + [Fact] + public async Task Given_UsersWithDifferentTypes_When_ExecuteAsyncIsCalled_Then_ShouldReturnAllUsers() + { + // Arrange + var users = new List + { + UserEntityBuilder.BuildUser(), + UserEntityBuilder.BuildMerchant() + }; + UserRepositoryBuilder.SetupListAllAsync(users); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().HaveCount(2); + result.Should().Contain(u => u.UserType == UserType.User); + result.Should().Contain(u => u.UserType == UserType.Merchant); + } + } +} \ No newline at end of file diff --git a/tests/UserTransactions.Tests/Application/Wallet/UseCases/ListAllWalletsUseCaseTest.cs b/tests/UserTransactions.Tests/Application/Wallet/UseCases/ListAllWalletsUseCaseTest.cs new file mode 100644 index 0000000..77c97cc --- /dev/null +++ b/tests/UserTransactions.Tests/Application/Wallet/UseCases/ListAllWalletsUseCaseTest.cs @@ -0,0 +1,113 @@ +using FluentAssertions; +using UserTransactions.Application.UseCases.Wallet.ListAll; +using UserTransactions.Domain.Enum; +using UserTransactions.Domain.Repositories.Wallet; +using UserTransactions.Tests.Shared.Builders.Entities; +using UserTransactions.Tests.Shared.Builders.Repositories; +using WalletEntity = UserTransactions.Domain.Entities.Wallet; + +namespace UserTransactions.Tests.Application.Wallet.UseCases +{ + public class ListAllWalletsUseCaseTest + { + private readonly IWalletRepository _walletRepository; + private readonly IListAllWalletsUseCase _sut; + + public ListAllWalletsUseCaseTest() + { + _walletRepository = WalletRepositoryBuilder.Build(); + _sut = new ListAllWalletsUseCase(_walletRepository); + } + + [Fact] + public async Task Given_ValidRequest_When_ExecuteAsyncIsCalled_Then_ShouldReturnValidResult() + { + // Arrange + var wallets = new List(); + for (int i = 0; i < 3; i++) + { + var user = UserEntityBuilder.BuildUser(); + var wallet = WalletEntityBuilder.BuildWithUser(user); + wallets.Add(wallet); + } + WalletRepositoryBuilder.SetupListAllAsync(wallets); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().HaveCount(3); + result.Should().AllSatisfy(wallet => + { + wallet.FullName.Should().NotBeNullOrEmpty(); + wallet.Email.Should().NotBeNullOrEmpty(); + wallet.Balance.Should().BeGreaterThanOrEqualTo(0); + wallet.UserType.Should().BeDefined(); + }); + } + + [Fact] + public async Task Given_NoWallets_When_ExecuteAsyncIsCalled_Then_ShouldReturnEmptyList() + { + // Arrange + var wallets = new List(); + WalletRepositoryBuilder.SetupListAllAsync(wallets); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEmpty(); + } + + [Theory] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + public async Task Given_SpecificNumberOfWallets_When_ExecuteAsyncIsCalled_Then_ShouldReturnCorrectCount(int walletCount) + { + // Arrange + var wallets = new List(); + for (int i = 0; i < walletCount; i++) + { + var user = UserEntityBuilder.BuildUser(); + var wallet = WalletEntityBuilder.BuildWithUser(user); + wallets.Add(wallet); + } + WalletRepositoryBuilder.SetupListAllAsync(wallets); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().HaveCount(walletCount); + } + + [Fact] + public async Task Given_WalletsWithDifferentUserTypes_When_ExecuteAsyncIsCalled_Then_ShouldReturnAllWallets() + { + // Arrange + var wallets = new List(); + + var userWallet = WalletEntityBuilder.BuildWithUser(UserEntityBuilder.BuildUser()); + var merchantWallet = WalletEntityBuilder.BuildWithUser(UserEntityBuilder.BuildMerchant()); + + wallets.Add(userWallet); + wallets.Add(merchantWallet); + + WalletRepositoryBuilder.SetupListAllAsync(wallets); + + // Act + var result = await _sut.ExecuteAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().HaveCount(2); + result.Should().Contain(w => w.UserType == UserType.User); + result.Should().Contain(w => w.UserType == UserType.Merchant); + } + } +} \ No newline at end of file diff --git a/tests/UserTransactions.Tests/Shared/Builders/Dtos/Response/Wallet/ResponseListTotalQuantityWalletDtoBuilder.cs b/tests/UserTransactions.Tests/Shared/Builders/Dtos/Response/Wallet/ResponseListTotalQuantityWalletDtoBuilder.cs index f3a807a..165a153 100644 --- a/tests/UserTransactions.Tests/Shared/Builders/Dtos/Response/Wallet/ResponseListTotalQuantityWalletDtoBuilder.cs +++ b/tests/UserTransactions.Tests/Shared/Builders/Dtos/Response/Wallet/ResponseListTotalQuantityWalletDtoBuilder.cs @@ -1,5 +1,5 @@ using Bogus; -using UserTransactions.Communication.Dtos.User.Response; +using UserTransactions.Communication.Dtos.Wallet.Response; namespace UserTransactions.Tests.Shared.Builders.Dtos.Response.Wallet { diff --git a/tests/UserTransactions.Tests/Shared/Builders/Repositories/UserRepositoryBuilder.cs b/tests/UserTransactions.Tests/Shared/Builders/Repositories/UserRepositoryBuilder.cs index 4b4a0fd..44e1769 100644 --- a/tests/UserTransactions.Tests/Shared/Builders/Repositories/UserRepositoryBuilder.cs +++ b/tests/UserTransactions.Tests/Shared/Builders/Repositories/UserRepositoryBuilder.cs @@ -1,5 +1,6 @@ using Moq; using UserTransactions.Domain.Repositories.User; +using UserEntity = UserTransactions.Domain.Entities.User; namespace UserTransactions.Tests.Shared.Builders.Repositories { @@ -28,5 +29,10 @@ public static void SetupListTotalQuantityAsync(int totalQuantity) { _mock.Setup(x => x.ListTotalQuantityAsync()).ReturnsAsync(totalQuantity); } + + public static void SetupListAllAsync(IList users) + { + _mock.Setup(x => x.ListAllAsync()).ReturnsAsync(users); + } } } diff --git a/tests/UserTransactions.Tests/Shared/Builders/Repositories/WalletRepositoryBuilder.cs b/tests/UserTransactions.Tests/Shared/Builders/Repositories/WalletRepositoryBuilder.cs index 410e95a..cc044b0 100644 --- a/tests/UserTransactions.Tests/Shared/Builders/Repositories/WalletRepositoryBuilder.cs +++ b/tests/UserTransactions.Tests/Shared/Builders/Repositories/WalletRepositoryBuilder.cs @@ -49,5 +49,10 @@ public static void SetupListTotalQuantityAsync(int totalQuantity) { _mock.Setup(x => x.ListTotalQuantityAsync()).ReturnsAsync(totalQuantity); } + + public static void SetupListAllAsync(IList wallets) + { + _mock.Setup(x => x.ListAllAsync()).ReturnsAsync(wallets); + } } }