PAN-15 Finished Unit Tests

This commit is contained in:
2019-04-07 15:18:19 -06:00
parent adbcd786f0
commit 36036b710d
3 changed files with 136 additions and 142 deletions
@@ -33,14 +33,14 @@ public class UserServiceIntegrationTest {
assertEquals("test_password", returnedAccount.getPassword()); assertEquals("test_password", returnedAccount.getPassword());
assertFalse(returnedAccount.isAdministratorStatus()); assertFalse(returnedAccount.isAdministratorStatus());
final Optional<Account> getAccountById = userService.getAccountById(returnedAccount.getId()); final Optional<Account> getAccountById = userService.findAccountById(returnedAccount.getId());
assertTrue(getAccountById.isPresent()); assertTrue(getAccountById.isPresent());
final Account returnedAccountById = getAccountById.get(); final Account returnedAccountById = getAccountById.get();
assertEquals("test_user", returnedAccountById.getUsername()); assertEquals("test_user", returnedAccountById.getUsername());
assertEquals("test_password", returnedAccountById.getPassword()); assertEquals("test_password", returnedAccountById.getPassword());
assertFalse(returnedAccountById.isAdministratorStatus()); assertFalse(returnedAccountById.isAdministratorStatus());
final Optional<Account> getAccountByUsername = userService.getAccountByUsername(returnedAccount.getUsername()); final Optional<Account> getAccountByUsername = userService.findAccountByUsername(returnedAccount.getUsername());
assertTrue(getAccountByUsername.isPresent()); assertTrue(getAccountByUsername.isPresent());
final Account returnedAccountByUsername = getAccountByUsername.get(); final Account returnedAccountByUsername = getAccountByUsername.get();
assertEquals("test_user", returnedAccountByUsername.getUsername()); assertEquals("test_user", returnedAccountByUsername.getUsername());
@@ -11,6 +11,7 @@ import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Slf4j @Slf4j
@@ -26,41 +27,39 @@ public class UserService {
this.restService = restService; this.restService = restService;
} }
public Optional<Account> getListOfAccount(){ public Optional<List<Account>> getListOfAccounts() {
final String auth = null;
final Instant start = Instant.now(); final Instant start = Instant.now();
try { try {
final TypeToken<Account> typeToken = new TypeToken<Account>() {}; final TypeToken<List<Account>> typeToken = new TypeToken<List<Account>>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", final Optional<List<Account>> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/",
typeToken, typeToken,
connectionTimeoutMilliseconds, connectionTimeoutMilliseconds,
socketTimeoutMilliseconds, socketTimeoutMilliseconds,
auth); null);
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to get the list of accounts"); LOG.warn("Unable to get the list of accounts");
} }
return persistenceApiResponse; return persistenceApiResponse;
} catch (final Exception e) { } catch (final Exception e) {
LOG.error("Error getting the list of accounts {}", e); LOG.error("Error getting the list of accounts", e);
return Optional.empty(); return Optional.empty();
} finally { } finally {
LOG.info("Getting the list of accounts request took {} ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Get the list of accounts request took {}ms", Duration.between(start, Instant.now()).toMillis());
} }
} }
public Optional<Account> getAccountById(final int id) { public Optional<Account> findAccountById(final int id) {
if (id == 0) { if (id == 0) {
LOG.error("No user ID specified! Returning {}"); LOG.error("No user ID specified! Returning {}");
return Optional.empty(); return Optional.empty();
} }
final String auth = null;
final Instant start = Instant.now(); final Instant start = Instant.now();
try { try {
@@ -69,30 +68,29 @@ public class UserService {
typeToken, typeToken,
connectionTimeoutMilliseconds, connectionTimeoutMilliseconds,
socketTimeoutMilliseconds, socketTimeoutMilliseconds,
auth); null);
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to find account with id {}", id); LOG.warn("Unable to find account with id {}", id);
} }
return persistenceApiResponse; return persistenceApiResponse;
} catch (final Exception e) { } catch (final Exception e) {
LOG.error("Error getting account by id {}", e); LOG.error("Error finding account by id", e);
return Optional.empty(); return Optional.empty();
} finally { } finally {
LOG.info("Getting account by ID request took {} ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Find account by ID request took {}ms", Duration.between(start, Instant.now()).toMillis());
} }
} }
public Optional<Account> getAccountByUsername(final String username) { public Optional<Account> findAccountByUsername(final String username) {
if (username == null) { if (username == null) {
LOG.error("No username specified! Returning {}"); LOG.error("No username specified! Returning {}");
return Optional.empty(); return Optional.empty();
} }
final String auth = null;
final Instant start = Instant.now(); final Instant start = Instant.now();
try { try {
@@ -101,20 +99,20 @@ public class UserService {
typeToken, typeToken,
connectionTimeoutMilliseconds, connectionTimeoutMilliseconds,
socketTimeoutMilliseconds, socketTimeoutMilliseconds,
auth); null);
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to GET account with username{}", username); LOG.warn("Unable to GET account with username {}", username);
} }
return persistenceApiResponse; return persistenceApiResponse;
} catch (final Exception e) { } catch (final Exception e) {
LOG.error("Error getting account by username {}", e); LOG.error("Error finding account by username", e);
return Optional.empty(); return Optional.empty();
} finally { } finally {
LOG.info("Getting account by username request took {} ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Find account by username request took {}ms", Duration.between(start, Instant.now()).toMillis());
} }
} }
@@ -136,12 +134,12 @@ public class UserService {
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to create new account {}", account.toString()); LOG.warn("Unable to create new account {}", account.toString());
} }
return persistenceApiResponse; return persistenceApiResponse;
} catch (final Exception e) { } catch (final Exception e) {
LOG.error("Error creating new account {}", e); LOG.error("Error creating new account", e);
return Optional.empty(); return Optional.empty();
} finally { } finally {
LOG.info("Create new account request took {}ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Create new account request took {}ms", Duration.between(start, Instant.now()).toMillis());
@@ -173,12 +171,12 @@ public class UserService {
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to update user with id {}", account.getId()); LOG.warn("Unable to update user with id {}", account.getId());
} }
return persistenceApiResponse; return persistenceApiResponse;
} catch (final Exception e) { } catch (final Exception e) {
LOG.error("Error updating user {}", e); LOG.error("Error updating user", e);
return Optional.empty(); return Optional.empty();
} finally { } finally {
LOG.info("Update user request took {}ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Update user request took {}ms", Duration.between(start, Instant.now()).toMillis());
@@ -196,6 +194,7 @@ public class UserService {
LOG.error("No user ID specified! Returning {}"); LOG.error("No user ID specified! Returning {}");
return false; return false;
} }
final int id = account.getId(); final int id = account.getId();
final Instant start = Instant.now(); final Instant start = Instant.now();
@@ -208,12 +207,12 @@ public class UserService {
LOG.info("Returning {}", persistenceApiResponse); LOG.info("Returning {}", persistenceApiResponse);
} }
else { else {
LOG.info("Unable to delete user {}"); LOG.error("Unable to delete user {}", account);
} }
return persistenceApiResponse; return persistenceApiResponse;
}catch (final Exception e) { }catch (final Exception e) {
LOG.error("Error deleting user {}", e); LOG.error("Error deleting user", e);
return false; return false;
} finally { } finally {
LOG.info("Delete user request took {}ms", Duration.between(start, Instant.now()).toMillis()); LOG.info("Delete user request took {}ms", Duration.between(start, Instant.now()).toMillis());
@@ -23,59 +23,46 @@ public class UserServiceTest {
@Test @Test
public void testGetListOfAccounts() { public void testGetListOfAccounts() {
final Account account1 = createAccount();
final Account account2 = createAccount();
final List<Account> accountList = new ArrayList<>(); final List<Account> accountList = new ArrayList<>();
accountList.add(account1); accountList.add(createAccount());
accountList.add(account2); accountList.add(createAccount());
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(), anyString())).thenReturn(Optional.of(accountList));
.thenReturn(Optional.of(accountList));
final Optional<Account> response = userService.getListOfAccount(); final Optional<List<Account>> response = userService.getListOfAccounts();
assertTrue(response.isPresent()); assertTrue(response.isPresent());
assertEquals(accountList, response.get()); assertEquals(accountList, response.get());
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
} }
@Test @Test
public void testGetListOfAccounts_persistenceApiResponseIsNotPresent(){ public void testGetListOfAccounts_PersistenceApiResponseIsEmpty() {
final Account account1 = createAccount(); when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
final Account account2 = createAccount();
final List<Account> accountList = new ArrayList<>();
accountList.add(account1);
accountList.add(account2);
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) final Optional<List<Account>> response = userService.getListOfAccounts();
.thenReturn(Optional.empty());
final Optional<Account> response = userService.getListOfAccount();
assertFalse(response.isPresent()); assertFalse(response.isPresent());
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
} }
@Test @Test
public void testGetListOfAccounts_catchException(){ public void testGetListOfAccounts_RestServiceThrowsException() {
final Account account1 = createAccount(); when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class);
final Account account2 = createAccount();
final List<Account> accountList = new ArrayList<>();
accountList.add(account1);
accountList.add(account2);
final Exception e = new Exception();
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) final Optional<List<Account>> response = userService.getListOfAccounts();
.thenThrow(Exception.class);
final Optional<Account> response = userService.getListOfAccount();
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
} }
@Test @Test
public void testGetAccountById(){ public void testFindAccountById() {
final Account account = createAccount(); final Account account = createAccount();
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account));
.thenReturn(Optional.of(account));
final Optional<Account> response = userService.getAccountById(1); final Optional<Account> response = userService.findAccountById(1);
assertTrue(response.isPresent()); assertTrue(response.isPresent());
assertEquals(account, response.get()); assertEquals(account, response.get());
@@ -83,8 +70,8 @@ public class UserServiceTest {
} }
@Test @Test
public void testGetAccountById_nullId() { public void testFindAccountById_IdEqualsZero() {
final Optional<Account> response = userService.getAccountById(0); final Optional<Account> response = userService.findAccountById(0);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); assertEquals(Optional.empty(), response);
@@ -92,34 +79,32 @@ public class UserServiceTest {
} }
@Test @Test
public void testGetAccountById_persistenceApiResponseIsNotPresent(){ public void testFindAccountById_PersistenceApiResponseIsEmpty() {
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) final Optional<Account> response = userService.findAccountById(1);
.thenReturn(Optional.empty());
final Optional<Account> response = userService.getAccountById(1);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString());
} }
@Test @Test
public void testGetAccountById_catchException(){ public void testFindAccountById_RestServiceThrowsException() {
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class);
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) final Optional<Account> response = userService.findAccountById(1);
.thenThrow(Exception.class);
final Optional<Account> response = userService.getAccountById(1);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
} }
@Test @Test
public void testGetAccountByUsername(){ public void testFindAccountByUsername() {
final Account account = createAccount(); final Account account = createAccount();
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account));
.thenReturn(Optional.of(account));
final Optional<Account> response = userService.getAccountByUsername(account.getUsername()); final Optional<Account> response = userService.findAccountByUsername(account.getUsername());
assertTrue(response.isPresent()); assertTrue(response.isPresent());
assertEquals(account, response.get()); assertEquals(account, response.get());
@@ -127,8 +112,8 @@ public class UserServiceTest {
} }
@Test @Test
public void testGetAccountByUsername_nullUsername(){ public void testFindAccountByUsername_NullUsername() {
final Optional<Account> response = userService.getAccountByUsername(null); final Optional<Account> response = userService.findAccountByUsername(null);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); assertEquals(Optional.empty(), response);
@@ -136,12 +121,22 @@ public class UserServiceTest {
} }
@Test @Test
public void testGetAccountByUsername_persistenceApiResponseIsNotPresent(){ public void testFindAccountByUsername_PersistenceApiResponseIsEmpty() {
final Account account = createAccount(); final Account account = createAccount();
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
.thenReturn(Optional.empty());
final Optional<Account> response = userService.getAccountByUsername(account.getUsername()); final Optional<Account> response = userService.findAccountByUsername(account.getUsername());
assertFalse(response.isPresent());
verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString());
}
@Test
public void testFindAccountByUsername_RestServiceThrowsException() {
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())).thenThrow(Exception.class);
final Optional<Account> response = userService.findAccountByUsername("test");
assertFalse(response.isPresent()); assertFalse(response.isPresent());
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
@@ -151,8 +146,8 @@ public class UserServiceTest {
public void testCreateAccount() { public void testCreateAccount() {
final Account account = createAccount(); final Account account = createAccount();
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.of(account));
.thenReturn(Optional.of(account));
final Optional<Account> response = userService.createAccount(account); final Optional<Account> response = userService.createAccount(account);
assertTrue(response.isPresent()); assertTrue(response.isPresent());
@@ -161,7 +156,7 @@ public class UserServiceTest {
} }
@Test @Test
public void testCreateAccount_NoAccountFound() { public void testCreateAccount_NullAccount() {
final Optional<Account> response = userService.createAccount(null); final Optional<Account> response = userService.createAccount(null);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
@@ -170,14 +165,23 @@ public class UserServiceTest {
} }
@Test @Test
public void testCreateAccount_catchException(){ public void testCreateAccount_AccountCouldNotBeCreated() {
final Account account = createAccount(); when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty());
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); final Optional<Account> response = userService.createAccount(createAccount());
final Optional<Account> response = userService.createAccount(account);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
}
@Test
public void testCreateAccount_RestServiceThrowsException() {
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class);
final Optional<Account> response = userService.createAccount(createAccount());
assertFalse(response.isPresent());
verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
} }
@Test @Test
@@ -185,8 +189,8 @@ public class UserServiceTest {
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.of(account));
.thenReturn(Optional.of(account));
final Optional<Account> response = userService.updateAccount(account); final Optional<Account> response = userService.updateAccount(account);
assertTrue(response.isPresent()); assertTrue(response.isPresent());
@@ -195,115 +199,106 @@ public class UserServiceTest {
} }
@Test @Test
public void testUpdateAccount_nullAccount(){ public void testUpdateAccount_NullAccount() {
final Optional<Account> response = userService.updateAccount(null); final Optional<Account> response = userService.updateAccount(null);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verifyZeroInteractions(restService);
verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
} }
@Test @Test
public void testUpdateAccount_nullId(){ public void testUpdateAccount_IdEqualsZero() {
final Account account = createAccount(); final Account account = createAccount();
account.setId(0); account.setId(0);
final Optional<Account> response = userService.updateAccount(account); final Optional<Account> response = userService.updateAccount(account);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verifyZeroInteractions(restService);
verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
} }
@Test @Test
public void testUpdateAccount_PersistenceApiResponseIsNotPresent(){ public void testUpdateAccount_PersistenceApiResponseIsEmpty() {
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty());
.thenReturn(Optional.empty());
final Optional<Account> response = userService.updateAccount(account); final Optional<Account> response = userService.updateAccount(account);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
} }
@Test @Test
public void testUpdateAccount_catchException(){ public void testUpdateAccount_RestServiceThrowsException() {
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class);
.thenThrow(Exception.class);
final Optional<Account> response = userService.updateAccount(account); final Optional<Account> response = userService.updateAccount(account);
assertFalse(response.isPresent()); assertFalse(response.isPresent());
assertEquals(Optional.empty(), response); verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
} }
@Test @Test
public void testDeleteAccount() { public void testDeleteAccount() {
final boolean response= true;
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.delete(anyString(), anyInt(), anyInt(), any())) when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(true);
.thenReturn(response);
final boolean persistenceApiResponse = userService.deleteAccount(account);
assertNotNull(persistenceApiResponse); final boolean isDeleteSuccessful = userService.deleteAccount(account);
assertTrue(persistenceApiResponse);
assertEquals(response, persistenceApiResponse); assertTrue(isDeleteSuccessful);
verify(restService).delete(anyString(), anyInt(), anyInt(), any()); verify(restService).delete(anyString(), anyInt(), anyInt(), any());
} }
@Test @Test
public void testDeleteAccount_nullAccount(){ public void testDeleteAccount_NullAccount() {
final boolean persistenceApiResponse = userService.deleteAccount(null); final boolean isDeleteSuccessful = userService.deleteAccount(null);
assertNotNull(persistenceApiResponse); assertFalse(isDeleteSuccessful);
assertFalse(persistenceApiResponse); verifyZeroInteractions(restService);
verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any());
} }
@Test @Test
public void testDeleteAccount_nullId(){ public void testDeleteAccount_IdEqualsZero() {
final Account account = createAccount(); final Account account = createAccount();
account.setId(0); account.setId(0);
final boolean persistenceApiResponse = userService.deleteAccount(account); final boolean isDeleteSuccessful = userService.deleteAccount(account);
assertNotNull(persistenceApiResponse); assertFalse(isDeleteSuccessful);
assertFalse(persistenceApiResponse); verifyZeroInteractions(restService);
verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any());
} }
@Test @Test
public void testDeleteAccount_persistenceApiResponseIsNotPresent(){ public void testDeleteAccount_PersistenceApiResponseIsEmpty() {
final boolean response= false;
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.delete(anyString(), anyInt(), anyInt(), any())) when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(false);
.thenReturn(response);
final boolean persistenceApiResponse = userService.deleteAccount(account);
assertNotNull(persistenceApiResponse); final boolean isDeleteSuccessful = userService.deleteAccount(account);
assertFalse(persistenceApiResponse);
assertEquals(response, persistenceApiResponse); assertFalse(isDeleteSuccessful);
verify(restService).delete(anyString(), anyInt(), anyInt(), any()); verify(restService).delete(anyString(), anyInt(), anyInt(), any());
} }
@Test @Test
public void testDeleteAccount_catchException(){ public void testDeleteAccount_RestServiceThrowsException() {
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class); when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class);
final boolean persistenceApiResponse = userService.deleteAccount(account); final boolean persistenceApiResponse = userService.deleteAccount(account);
assertNotNull(persistenceApiResponse);
assertFalse(persistenceApiResponse); assertFalse(persistenceApiResponse);
verify(restService).delete(anyString(), anyInt(), anyInt(), any());
} }