From 36036b710de0ccc24e3977a11e1f677ce167231a Mon Sep 17 00:00:00 2001 From: atusa17 Date: Sun, 7 Apr 2019 15:18:19 -0600 Subject: [PATCH] PAN-15 Finished Unit Tests --- .../services/UserServiceIntegrationTest.java | 6 +- .../msudenver/tsp/services/UserService.java | 63 +++--- .../tsp/services/UserServiceTest.java | 209 +++++++++--------- 3 files changed, 136 insertions(+), 142 deletions(-) diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index b3a547d..be36c25 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -33,14 +33,14 @@ public class UserServiceIntegrationTest { assertEquals("test_password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); - final Optional getAccountById = userService.getAccountById(returnedAccount.getId()); + final Optional getAccountById = userService.findAccountById(returnedAccount.getId()); assertTrue(getAccountById.isPresent()); final Account returnedAccountById = getAccountById.get(); assertEquals("test_user", returnedAccountById.getUsername()); assertEquals("test_password", returnedAccountById.getPassword()); assertFalse(returnedAccountById.isAdministratorStatus()); - final Optional getAccountByUsername = userService.getAccountByUsername(returnedAccount.getUsername()); + final Optional getAccountByUsername = userService.findAccountByUsername(returnedAccount.getUsername()); assertTrue(getAccountByUsername.isPresent()); final Account returnedAccountByUsername = getAccountByUsername.get(); assertEquals("test_user", returnedAccountByUsername.getUsername()); @@ -61,7 +61,7 @@ public class UserServiceIntegrationTest { assertTrue(result); } - private Account createAccount(){ + private Account createAccount() { final Account testAccount = new Account(); testAccount.setUsername("test_user"); testAccount.setPassword("test_password"); diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index d5333f0..3125824 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import java.time.Duration; import java.time.Instant; +import java.util.List; import java.util.Optional; @Slf4j @@ -26,95 +27,92 @@ public class UserService { this.restService = restService; } - public Optional getListOfAccount(){ - final String auth = null; + public Optional> getListOfAccounts() { final Instant start = Instant.now(); try { - final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", + final TypeToken> typeToken = new TypeToken>() {}; + final Optional> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, - auth); + null); if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to get the list of accounts"); + LOG.warn("Unable to get the list of accounts"); } return persistenceApiResponse; } 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(); } 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 getAccountById(final int id) { + public Optional findAccountById(final int id) { if (id == 0) { LOG.error("No user ID specified! Returning {}"); return Optional.empty(); } - final String auth = null; final Instant start = Instant.now(); try { final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id="+id, + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id=" + id, typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, - auth); + null); if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to find account with id {}", id); + LOG.warn("Unable to find account with id {}", id); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error getting account by id {}", e); + LOG.error("Error finding account by id", e); return Optional.empty(); } 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 getAccountByUsername(final String username) { + public Optional findAccountByUsername(final String username) { if (username == null) { LOG.error("No username specified! Returning {}"); return Optional.empty(); } - final String auth = null; final Instant start = Instant.now(); try { final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username="+username, + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username=" + username, typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, - auth); + null); if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to GET account with username{}", username); + LOG.warn("Unable to GET account with username {}", username); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error getting account by username {}", e); + LOG.error("Error finding account by username", e); return Optional.empty(); } 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,15 +134,15 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to create new account {}", account.toString()); + LOG.warn("Unable to create new account {}", account.toString()); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error creating new account {}", e); + LOG.error("Error creating new account", e); return Optional.empty(); } 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,15 +171,15 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to update user with id {}", account.getId()); + LOG.warn("Unable to update user with id {}", account.getId()); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error updating user {}", e); + LOG.error("Error updating user", e); return Optional.empty(); } 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 {}"); return false; } + final int id = account.getId(); final Instant start = Instant.now(); @@ -204,19 +203,19 @@ public class UserService { final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, HttpStatus.NO_CONTENT); - if (persistenceApiResponse){ + if (persistenceApiResponse) { LOG.info("Returning {}", persistenceApiResponse); } else { - LOG.info("Unable to delete user {}"); + LOG.error("Unable to delete user {}", account); } return persistenceApiResponse; }catch (final Exception e) { - LOG.error("Error deleting user {}", e); + LOG.error("Error deleting user", e); return false; } 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()); } } } diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 6debc4a..1d42311 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -22,60 +22,47 @@ public class UserServiceTest { @InjectMocks private UserService userService; @Test - public void testGetListOfAccounts(){ - final Account account1 = createAccount(); - final Account account2 = createAccount(); + public void testGetListOfAccounts() { final List accountList = new ArrayList<>(); - accountList.add(account1); - accountList.add(account2); + accountList.add(createAccount()); + accountList.add(createAccount()); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.of(accountList)); - final Optional response = userService.getListOfAccount(); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(), anyString())).thenReturn(Optional.of(accountList)); + + final Optional> response = userService.getListOfAccounts(); assertTrue(response.isPresent()); assertEquals(accountList, response.get()); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetListOfAccounts_persistenceApiResponseIsNotPresent(){ - final Account account1 = createAccount(); - final Account account2 = createAccount(); - final List accountList = new ArrayList<>(); - accountList.add(account1); - accountList.add(account2); + public void testGetListOfAccounts_PersistenceApiResponseIsEmpty() { + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty()); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.empty()); - final Optional response = userService.getListOfAccount(); + final Optional> response = userService.getListOfAccounts(); assertFalse(response.isPresent()); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetListOfAccounts_catchException(){ - final Account account1 = createAccount(); - final Account account2 = createAccount(); - final List accountList = new ArrayList<>(); - accountList.add(account1); - accountList.add(account2); - final Exception e = new Exception(); + public void testGetListOfAccounts_RestServiceThrowsException() { + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenThrow(Exception.class); - final Optional response = userService.getListOfAccount(); + final Optional> response = userService.getListOfAccounts(); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetAccountById(){ + public void testFindAccountById() { final Account account = createAccount(); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.of(account)); - final Optional response = userService.getAccountById(1); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account)); + + final Optional response = userService.findAccountById(1); assertTrue(response.isPresent()); assertEquals(account, response.get()); @@ -83,8 +70,8 @@ public class UserServiceTest { } @Test - public void testGetAccountById_nullId() { - final Optional response = userService.getAccountById(0); + public void testFindAccountById_IdEqualsZero() { + final Optional response = userService.findAccountById(0); assertFalse(response.isPresent()); assertEquals(Optional.empty(), response); @@ -92,34 +79,32 @@ public class UserServiceTest { } @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())) - .thenReturn(Optional.empty()); - final Optional response = userService.getAccountById(1); + final Optional response = userService.findAccountById(1); assertFalse(response.isPresent()); verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); } @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())) - .thenThrow(Exception.class); - final Optional response = userService.getAccountById(1); + final Optional response = userService.findAccountById(1); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetAccountByUsername(){ + public void testFindAccountByUsername() { final Account account = createAccount(); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.of(account)); - final Optional response = userService.getAccountByUsername(account.getUsername()); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account)); + + final Optional response = userService.findAccountByUsername(account.getUsername()); assertTrue(response.isPresent()); assertEquals(account, response.get()); @@ -127,8 +112,8 @@ public class UserServiceTest { } @Test - public void testGetAccountByUsername_nullUsername(){ - final Optional response = userService.getAccountByUsername(null); + public void testFindAccountByUsername_NullUsername() { + final Optional response = userService.findAccountByUsername(null); assertFalse(response.isPresent()); assertEquals(Optional.empty(), response); @@ -136,23 +121,33 @@ public class UserServiceTest { } @Test - public void testGetAccountByUsername_persistenceApiResponseIsNotPresent(){ + public void testFindAccountByUsername_PersistenceApiResponseIsEmpty() { final Account account = createAccount(); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.empty()); - final Optional response = userService.getAccountByUsername(account.getUsername()); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty()); + + final Optional response = userService.findAccountByUsername(account.getUsername()); assertFalse(response.isPresent()); verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); } @Test - public void testCreateAccount(){ + public void testFindAccountByUsername_RestServiceThrowsException() { + when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())).thenThrow(Exception.class); + + final Optional response = userService.findAccountByUsername("test"); + + assertFalse(response.isPresent()); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); + } + + @Test + public void testCreateAccount() { final Account account = createAccount(); - when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenReturn(Optional.of(account)); + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.of(account)); + final Optional response = userService.createAccount(account); assertTrue(response.isPresent()); @@ -161,7 +156,7 @@ public class UserServiceTest { } @Test - public void testCreateAccount_NoAccountFound() { + public void testCreateAccount_NullAccount() { final Optional response = userService.createAccount(null); assertFalse(response.isPresent()); @@ -170,23 +165,32 @@ public class UserServiceTest { } @Test - public void testCreateAccount_catchException(){ - final Account account = createAccount(); + public void testCreateAccount_AccountCouldNotBeCreated() { + 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 response = userService.createAccount(account); + final Optional response = userService.createAccount(createAccount()); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); } @Test - public void testUpdateAccount(){ + public void testCreateAccount_RestServiceThrowsException() { + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); + + final Optional response = userService.createAccount(createAccount()); + + assertFalse(response.isPresent()); + verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount() { final Account account = createAccount(); account.setId(1); - when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenReturn(Optional.of(account)); + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.of(account)); + final Optional response = userService.updateAccount(account); assertTrue(response.isPresent()); @@ -195,115 +199,106 @@ public class UserServiceTest { } @Test - public void testUpdateAccount_nullAccount(){ + public void testUpdateAccount_NullAccount() { final Optional response = userService.updateAccount(null); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); - verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + verifyZeroInteractions(restService); } @Test - public void testUpdateAccount_nullId(){ + public void testUpdateAccount_IdEqualsZero() { final Account account = createAccount(); account.setId(0); final Optional response = userService.updateAccount(account); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); - verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + verifyZeroInteractions(restService); } @Test - public void testUpdateAccount_PersistenceApiResponseIsNotPresent(){ + public void testUpdateAccount_PersistenceApiResponseIsEmpty() { final Account account = createAccount(); account.setId(1); - when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenReturn(Optional.empty()); + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty()); + final Optional response = userService.updateAccount(account); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); } @Test - public void testUpdateAccount_catchException(){ + public void testUpdateAccount_RestServiceThrowsException() { final Account account = createAccount(); account.setId(1); - when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenThrow(Exception.class); + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); + final Optional response = userService.updateAccount(account); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); } @Test - public void testDeleteAccount(){ - final boolean response= true; + public void testDeleteAccount() { final Account account = createAccount(); account.setId(1); - when(restService.delete(anyString(), anyInt(), anyInt(), any())) - .thenReturn(response); - final boolean persistenceApiResponse = userService.deleteAccount(account); + when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(true); - assertNotNull(persistenceApiResponse); - assertTrue(persistenceApiResponse); - assertEquals(response, persistenceApiResponse); + final boolean isDeleteSuccessful = userService.deleteAccount(account); + + assertTrue(isDeleteSuccessful); verify(restService).delete(anyString(), anyInt(), anyInt(), any()); } @Test - public void testDeleteAccount_nullAccount(){ - final boolean persistenceApiResponse = userService.deleteAccount(null); + public void testDeleteAccount_NullAccount() { + final boolean isDeleteSuccessful = userService.deleteAccount(null); - assertNotNull(persistenceApiResponse); - assertFalse(persistenceApiResponse); - verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + assertFalse(isDeleteSuccessful); + verifyZeroInteractions(restService); } @Test - public void testDeleteAccount_nullId(){ + public void testDeleteAccount_IdEqualsZero() { final Account account = createAccount(); account.setId(0); - final boolean persistenceApiResponse = userService.deleteAccount(account); + final boolean isDeleteSuccessful = userService.deleteAccount(account); - assertNotNull(persistenceApiResponse); - assertFalse(persistenceApiResponse); - verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + assertFalse(isDeleteSuccessful); + verifyZeroInteractions(restService); } @Test - public void testDeleteAccount_persistenceApiResponseIsNotPresent(){ - final boolean response= false; + public void testDeleteAccount_PersistenceApiResponseIsEmpty() { final Account account = createAccount(); account.setId(1); - when(restService.delete(anyString(), anyInt(), anyInt(), any())) - .thenReturn(response); - final boolean persistenceApiResponse = userService.deleteAccount(account); + when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(false); - assertNotNull(persistenceApiResponse); - assertFalse(persistenceApiResponse); - assertEquals(response, persistenceApiResponse); + final boolean isDeleteSuccessful = userService.deleteAccount(account); + + assertFalse(isDeleteSuccessful); verify(restService).delete(anyString(), anyInt(), anyInt(), any()); } @Test - public void testDeleteAccount_catchException(){ + public void testDeleteAccount_RestServiceThrowsException() { final Account account = createAccount(); account.setId(1); when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class); + final boolean persistenceApiResponse = userService.deleteAccount(account); - assertNotNull(persistenceApiResponse); assertFalse(persistenceApiResponse); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); }