### Question:
AccountServiceImpl implements AccountService { @Override public Optional<AccountDto> getAccount(final String username) { try { final Optional<Account> account = accountRepository.findById(username); return account.isPresent() ? Optional.of(accountMapper.accountToAccountDto(account.get())) : Optional.empty(); } catch (final Exception ex) { final String errorMessage = String.format("An error occured while reading account %s: %s", username, ex.getMessage()); log.error(errorMessage, ex); throw new AccountPersistenceException(errorMessage, ex); } }  @Override Optional<AccountDto> getAccount(final String username); @Override Optional<AccountDto> getAccount(final String username, final String password); @Override AccountDto createAccount(final AccountDto accountDto); @Override AccountDto updateAccount(final AccountDto accountDto); @Override void deleteAccount(final String username);  }### Answer:
@Test public void test_getAccount_onlyWithUsername_found() { final Account account = new Account(); final AccountDto accountDto = new AccountDto(); when(accountRepository.findById(TEST_USERNAME)).thenReturn(Optional.of(account)); when(accountMapper.accountToAccountDto(account)).thenReturn(accountDto); final Optional<AccountDto> result = accountService.getAccount(TEST_USERNAME); assertTrue(result.isPresent()); assertEquals(accountDto, result.get()); }
@Test public void test_getAccount_onlyWithUsername_notFound() { when(accountRepository.findById(TEST_USERNAME)).thenReturn(Optional.empty()); final Optional<AccountDto> result = accountService.getAccount(TEST_USERNAME); assertFalse(result.isPresent()); }
@Test(expected = AccountPersistenceException.class) public void test_getAccount_onlyWithUsername_runtimeException() { when(accountRepository.findById(TEST_USERNAME)).thenThrow(new RuntimeException()); accountService.getAccount(TEST_USERNAME); }
@Test public void test_getAccount_withAuth_found() { final Account account = new Account(); account.setUsername(TEST_USERNAME); account.setPasswordSalt(BCrypt.gensalt()); account.setPassword(BCrypt.hashpw(TEST_PASSWORD, account.getPasswordSalt())); final AccountDto accountDto = new AccountDto(); when(accountRepository.findById(TEST_USERNAME)).thenReturn(Optional.of(account)); when(accountMapper.accountToAccountDto(account)).thenReturn(accountDto); final Optional<AccountDto> result = accountService.getAccount(TEST_USERNAME, TEST_PASSWORD); assertTrue(result.isPresent()); assertEquals(accountDto, result.get()); }
@Test public void test_getAccount_withAuth_notFound() { when(accountRepository.findById(TEST_USERNAME)).thenReturn(Optional.empty()); final Optional<AccountDto> result = accountService.getAccount(TEST_USERNAME, TEST_PASSWORD); assertFalse(result.isPresent()); }
@Test public void test_getAccount_withAuth_invalidPassword() { final Account account = new Account(); account.setUsername(TEST_USERNAME); account.setPasswordSalt(BCrypt.gensalt()); account.setPassword(BCrypt.hashpw(TEST_PASSWORD, account.getPasswordSalt())); final AccountDto accountDto = new AccountDto(); when(accountRepository.findById(TEST_USERNAME)).thenReturn(Optional.of(account)); when(accountMapper.accountToAccountDto(account)).thenReturn(accountDto); final Optional<AccountDto> result = accountService.getAccount(TEST_USERNAME, "12345"); assertFalse(result.isPresent()); }
@Test(expected = AccountPersistenceException.class) public void test_getAccount_withAuth_runtimeException() { when(accountRepository.findById(TEST_USERNAME)).thenThrow(new RuntimeException()); accountService.getAccount(TEST_USERNAME, TEST_PASSWORD); }