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
|
@Test
|
||||||
public void testUserService(){
|
public void testUserService(){
|
||||||
final Account testAccount = new Account();
|
final Account testAccount = creatAccount();
|
||||||
testAccount.setUsername("test user");
|
|
||||||
testAccount.setPassword("test password");
|
|
||||||
testAccount.setAdministratorStatus(false);
|
|
||||||
testAccount.setLastLogin(new Date());
|
|
||||||
|
|
||||||
final Optional<Account> testCreatedAccount = userService.createNewAccount(testAccount);
|
final Optional<Account> testCreatedAccount = userService.createNewAccount(testAccount);
|
||||||
|
|
||||||
@@ -38,25 +34,29 @@ public class UserServiceIntegrationTest {
|
|||||||
assertEquals("test password", returnedAccount.getPassword());
|
assertEquals("test password", returnedAccount.getPassword());
|
||||||
assertFalse(returnedAccount.isAdministratorStatus());
|
assertFalse(returnedAccount.isAdministratorStatus());
|
||||||
|
|
||||||
final Optional<Account> updatePasswordTestCreatedAccount = userService.updateAccount(returnedAccount);
|
returnedAccount.setUsername("test updatedUser");
|
||||||
|
returnedAccount.setPassword("test updatedPassword");
|
||||||
|
|
||||||
assertTrue(updatePasswordTestCreatedAccount.isPresent());
|
final Optional<Account> updatedTestCreatedAccount = userService.updateAccount(returnedAccount);
|
||||||
final Account returnedUpdatedPasswordAccount = updatePasswordTestCreatedAccount.get();
|
|
||||||
assertEquals("test user", returnedUpdatedPasswordAccount.getUsername());
|
|
||||||
assertEquals("password", returnedUpdatedPasswordAccount.getPassword());
|
|
||||||
assertFalse(returnedAccount.isAdministratorStatus());
|
|
||||||
|
|
||||||
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 boolean result = userService.deleteAccount(returnedUpdatedAccount);
|
||||||
final Account returnedUpdatedUsernameAccount = updateUsernameTestCreatedAccount.get();
|
|
||||||
assertEquals("user", returnedUpdatedUsernameAccount.getUsername());
|
|
||||||
assertEquals("password", returnedUpdatedUsernameAccount.getPassword());
|
|
||||||
assertFalse(returnedAccount.isAdministratorStatus());
|
|
||||||
|
|
||||||
final boolean result = userService.deleteAccount(returnedUpdatedUsernameAccount);
|
|
||||||
|
|
||||||
assertTrue(result);
|
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) {
|
public boolean deleteAccount(final Account account) {
|
||||||
if(account == null){
|
if(account == null){
|
||||||
LOG.error("Username does not exist, returning {}");
|
LOG.error("Username does not exist, returning {}");
|
||||||
return false;
|
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();
|
final Instant start = Instant.now();
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
|||||||
@@ -27,11 +27,7 @@ public class UserServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCreateNewAccount() throws ParseException {
|
public void testCreateNewAccount() throws ParseException {
|
||||||
|
|
||||||
final Account account = new Account();
|
final Account account = createAccount();
|
||||||
account.setUsername("Test username");
|
|
||||||
account.setPassword("test password");
|
|
||||||
account.setAdministratorStatus(false);
|
|
||||||
account.setLastLogin(new Date());
|
|
||||||
|
|
||||||
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));
|
||||||
@@ -41,4 +37,43 @@ public class UserServiceTest {
|
|||||||
assertTrue(response.isPresent());
|
assertTrue(response.isPresent());
|
||||||
assertEquals(account, response.get());
|
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