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());
assertFalse(returnedAccount.isAdministratorStatus());
final Optional<Account> getAccountById = userService.getAccountById(returnedAccount.getId());
final Optional<Account> 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<Account> getAccountByUsername = userService.getAccountByUsername(returnedAccount.getUsername());
final Optional<Account> 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");
@@ -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<Account> getListOfAccount(){
final String auth = null;
public Optional<List<Account>> getListOfAccounts() {
final Instant start = Instant.now();
try {
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/",
final TypeToken<List<Account>> typeToken = new TypeToken<List<Account>>() {};
final Optional<List<Account>> 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<Account> getAccountById(final int id) {
public Optional<Account> 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<Account> typeToken = new TypeToken<Account>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id="+id,
final Optional<Account> 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<Account> getAccountByUsername(final String username) {
public Optional<Account> 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<Account> typeToken = new TypeToken<Account>() {};
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username="+username,
final Optional<Account> 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());
}
}
}
@@ -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<Account> 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<Account> response = userService.getListOfAccount();
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(), anyString())).thenReturn(Optional.of(accountList));
final Optional<List<Account>> 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<Account> 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<Account> response = userService.getListOfAccount();
final Optional<List<Account>> 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<Account> 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<Account> response = userService.getListOfAccount();
final Optional<List<Account>> 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<Account> response = userService.getAccountById(1);
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account));
final Optional<Account> 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<Account> response = userService.getAccountById(0);
public void testFindAccountById_IdEqualsZero() {
final Optional<Account> 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<Account> response = userService.getAccountById(1);
final Optional<Account> 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<Account> response = userService.getAccountById(1);
final Optional<Account> 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<Account> response = userService.getAccountByUsername(account.getUsername());
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account));
final Optional<Account> 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<Account> response = userService.getAccountByUsername(null);
public void testFindAccountByUsername_NullUsername() {
final Optional<Account> 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<Account> response = userService.getAccountByUsername(account.getUsername());
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
final Optional<Account> 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<Account> 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<Account> 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<Account> 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<Account> response = userService.createAccount(account);
final Optional<Account> 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<Account> 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<Account> 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<Account> 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<Account> 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<Account> 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<Account> 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());
}