diff --git a/test/Application.UnitTests/Services/DomainService/AccountServiceTests.cs b/test/Application.UnitTests/Services/DomainService/AccountServiceTests.cs new file mode 100644 index 0000000..d1e01fd --- /dev/null +++ b/test/Application.UnitTests/Services/DomainService/AccountServiceTests.cs @@ -0,0 +1,165 @@ +using Application.DTOs.Account; +using Application.Interfaces.Repositories; +using Application.Interfaces.Services; +using Application.Mappers; +using Application.Services.DomainService; +using Application.Services.SharedService; +using Domain.Entities; +using NSubstitute; + +namespace test.Application.UnitTests.Services.DomainService; + +public class AccountServiceTests +{ + private readonly IAccountRepository _accountRepository; + private readonly AccountService _accountService; + private readonly ICsvReaderService _csvReaderService; + + public AccountServiceTests() + { + _accountRepository = Substitute.For(); + _csvReaderService = Substitute.For(); + _accountService = new AccountService(_accountRepository, _csvReaderService); + } + + [Fact] + public async Task AddAccountsFromCsvAsync_WhenCsvIsValid_ReturnsOk() + { + // Arrange + var filePath = "dummy.csv"; + var accountCsvModels = new List + { + new AccountCsvModel + { + AccountID = 1, + CardID = 100, + IBAN = "IBAN1", + AccountType = "Savings", + BranchTelephone = "1234567890", + BranchAdress = "Main Street", + BranchName = "Main Branch", + OwnerName = "Mobin", + OwnerLastName = "Barfi", + OwnerID = 101 + }, + new AccountCsvModel + { + AccountID = 2, + CardID = 200, + IBAN = "IBAN2", + AccountType = "Checking", + BranchTelephone = "0987654321", + BranchAdress = "Some Street", + BranchName = "Some Branch", + OwnerName = "Mohammad", + OwnerLastName = "Mohammadi", + OwnerID = 102 + } + }; + var existingAccountIds = new List { 1 }; + + _csvReaderService.ReadFromCsv(filePath).Returns(accountCsvModels); + _accountRepository.GetAllIdsAsync().Returns(existingAccountIds); + + // Act + var result = await _accountService.AddAccountsFromCsvAsync(filePath); + + // Assert + Assert.True(result.Succeed); + await _accountRepository + .Received(1) + .CreateBulkAsync(Arg.Is>(a => + a.Count == 1 + && a.First().AccountId == 2 + && a.First().OwnerName == "Mohammad")); + } + + [Fact] + public async Task AddAccountsFromCsvAsync_WhenExceptionIsThrown_ReturnsFail() + { + // Arrange + var filePath = "dummy.csv"; + _csvReaderService + .When(x => x.ReadFromCsv(filePath)) + .Do(x => { throw new Exception("CSV read error"); }); + + // Act + var result = await _accountService.AddAccountsFromCsvAsync(filePath); + + // Assert + Assert.False(result.Succeed); + Assert.Contains("An unexpected error occurred: CSV read error", result.Message); + } + + [Fact] + public async Task GetAccountByIdAsync_WhenAccountExists_ReturnsAccount() + { + // Arrange + var accountId = 1; + var account = new Account { AccountId = accountId, OwnerName = "John Doe" }; + + _accountRepository.GetByIdAsync(accountId).Returns(account); + + // Act + var result = await _accountService.GetAccountByIdAsync(accountId); + + // Assert + Assert.NotNull(result); + Assert.Equal(accountId, result.Value!.AccountId); + Assert.Equal("John Doe", result.Value!.OwnerName); + } + + [Fact] + public async Task GetAccountByIdAsync_WhenAccountDoesNotExist_ReturnsNull() + { + // Arrange + var accountId = 1; + _accountRepository.GetByIdAsync(accountId).Returns((Account?)null); + + // Act + var result = await _accountService.GetAccountByIdAsync(accountId); + + // Assert + Assert.False(result.Succeed); + Assert.Null(result.Value); + Assert.Equal("Account not found", result.Message); + } + + [Fact] + public async Task GetAllAccountsAsync_WhenAccountsExist_ReturnsListOfAccounts() + { + // Arrange + var accounts = new List + { + new Account { AccountId = 1, OwnerName = "Mobin Barfi" }, + new Account { AccountId = 2, OwnerName = "Mohammad Mohammadi" } + }; + + _accountRepository.GetAllAccounts().Returns(accounts); + + // Act + var result = await _accountService.GetAllAccountsAsync(); + + // Assert + Assert.True(result.Succeed); + Assert.Equal(2, result.Value.Count); + Assert.Equal("Mobin Barfi", result.Value[0].OwnerName); + Assert.Equal("Mohammad Mohammadi", result.Value[1].OwnerName); + } + + [Fact] + public async Task GetAllAccountsAsync_WhenExceptionIsThrown_ReturnsFail() + { + // Arrange + _accountRepository + .When(x => x.GetAllAccounts()) + .Do(x => throw new Exception("Database error")); + + // Act + var result = await _accountService.GetAllAccountsAsync(); + + // Assert + Assert.False(result.Succeed); + Assert.Equal("An unexpected error occurred: Database error", result.Message); + } +} \ No newline at end of file