Merge branch 'master' of https://github.com/atusa17/ptp into PAN-15
# Conflicts: # src/main/java/edu/msudenver/tsp/website/Application.java
This commit is contained in:
+19
-19
@@ -24,11 +24,7 @@ public class UserServiceIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testUserService(){
|
||||
final Account testAccount = new Account();
|
||||
testAccount.setUsername("test user");
|
||||
testAccount.setPassword("test password");
|
||||
testAccount.setAdministratorStatus(false);
|
||||
testAccount.setLastLogin(new Date());
|
||||
final Account testAccount = creatAccount();
|
||||
|
||||
final Optional<Account> testCreatedAccount = userService.createNewAccount(testAccount);
|
||||
|
||||
@@ -38,25 +34,29 @@ public class UserServiceIntegrationTest {
|
||||
assertEquals("test password", returnedAccount.getPassword());
|
||||
assertFalse(returnedAccount.isAdministratorStatus());
|
||||
|
||||
final Optional<Account> updatePasswordTestCreatedAccount = userService.updateAccount(returnedAccount);
|
||||
returnedAccount.setUsername("test updatedUser");
|
||||
returnedAccount.setPassword("test updatedPassword");
|
||||
|
||||
assertTrue(updatePasswordTestCreatedAccount.isPresent());
|
||||
final Account returnedUpdatedPasswordAccount = updatePasswordTestCreatedAccount.get();
|
||||
assertEquals("test user", returnedUpdatedPasswordAccount.getUsername());
|
||||
assertEquals("password", returnedUpdatedPasswordAccount.getPassword());
|
||||
assertFalse(returnedAccount.isAdministratorStatus());
|
||||
final Optional<Account> updatedTestCreatedAccount = userService.updateAccount(returnedAccount);
|
||||
|
||||
final Optional<Account> updateUsernameTestCreatedAccount = userService.updateUsername(returnedUpdatedPasswordAccount, "user");
|
||||
assertTrue(updatedTestCreatedAccount .isPresent());
|
||||
final Account returnedUpdatedAccount = updatedTestCreatedAccount.get();
|
||||
assertEquals("test updatedUser", returnedUpdatedAccount.getUsername());
|
||||
assertEquals("test updatedPassword", returnedUpdatedAccount.getPassword());
|
||||
assertFalse(returnedUpdatedAccount.isAdministratorStatus());
|
||||
|
||||
assertTrue(updateUsernameTestCreatedAccount.isPresent());
|
||||
final Account returnedUpdatedUsernameAccount = updateUsernameTestCreatedAccount.get();
|
||||
assertEquals("user", returnedUpdatedUsernameAccount.getUsername());
|
||||
assertEquals("password", returnedUpdatedUsernameAccount.getPassword());
|
||||
assertFalse(returnedAccount.isAdministratorStatus());
|
||||
|
||||
final boolean result = userService.deleteAccount(returnedUpdatedUsernameAccount);
|
||||
final boolean result = userService.deleteAccount(returnedUpdatedAccount);
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
private Account creatAccount(){
|
||||
final Account testAccount = new Account();
|
||||
testAccount.setUsername("test user");
|
||||
testAccount.setPassword("test password");
|
||||
testAccount.setAdministratorStatus(false);
|
||||
testAccount.setLastLogin(new Date());
|
||||
|
||||
return testAccount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -93,47 +93,18 @@ public class UserService {
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> updateUsername(final Account account , final String username){
|
||||
|
||||
if(account ==null){
|
||||
LOG.error("user not exist, returning{}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Integer id = account.getId();
|
||||
account.setUsername(username);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try{
|
||||
final String auth = "";
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>(){};
|
||||
final Optional<Account> persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id,
|
||||
new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account),
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to update username for account {}",account.toString());
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error updating account {}", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteAccount(final Account account) {
|
||||
if(account == null){
|
||||
LOG.error("Username does not exist, returning {}");
|
||||
return false;
|
||||
}
|
||||
final Integer id = account.getId();
|
||||
|
||||
if (account.getId() == 0) {
|
||||
LOG.error("No user ID specified! Returning {}");
|
||||
return false;
|
||||
}
|
||||
final int id = account.getId();
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try{
|
||||
|
||||
@@ -27,11 +27,7 @@ public class UserServiceTest {
|
||||
@Test
|
||||
public void testCreateNewAccount() throws ParseException {
|
||||
|
||||
final Account account = new Account();
|
||||
account.setUsername("Test username");
|
||||
account.setPassword("test password");
|
||||
account.setAdministratorStatus(false);
|
||||
account.setLastLogin(new Date());
|
||||
final Account account = createAccount();
|
||||
|
||||
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()))
|
||||
.thenReturn(Optional.of(account));
|
||||
@@ -41,4 +37,43 @@ public class UserServiceTest {
|
||||
assertTrue(response.isPresent());
|
||||
assertEquals(account, response.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateAccount() throws ParseException {
|
||||
final Account account = createAccount();
|
||||
account.setId(1);
|
||||
|
||||
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()))
|
||||
.thenReturn(Optional.of(account));
|
||||
|
||||
final Optional<Account> response = userService.updateAccount(account);
|
||||
|
||||
assertTrue(response.isPresent());
|
||||
assertEquals(account, response.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteAccount() throws ParseException {
|
||||
final boolean response= true;
|
||||
final Account account = createAccount();
|
||||
account.setId(1);
|
||||
|
||||
when(restService.delete(anyString(), anyInt(), anyInt(), any()))
|
||||
.thenReturn(response);
|
||||
|
||||
final boolean persistenceApiResponse = userService.deleteAccount(account);
|
||||
|
||||
assertTrue(persistenceApiResponse );
|
||||
assertEquals(response, persistenceApiResponse );
|
||||
}
|
||||
|
||||
|
||||
private Account createAccount() {
|
||||
final Account account = new Account();
|
||||
account.setUsername("Test username");
|
||||
account.setPassword("test password");
|
||||
account.setAdministratorStatus(true);
|
||||
account.setLastLogin(new Date());
|
||||
return account;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user