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
@@ -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());
}
}
}