From 6791f6c61fe64cfa1cc96d65669da3def5c31e61 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 24 Mar 2019 17:31:40 -0600 Subject: [PATCH] Merge branch 'master' of https://github.com/atusa17/ptp into PAN-15 # Conflicts: # src/main/java/edu/msudenver/tsp/website/Application.java --- .../services/UserServiceIntegrationTest.java | 38 +++++++-------- .../msudenver/tsp/services/UserService.java | 41 +++------------- .../tsp/services/UserServiceTest.java | 47 ++++++++++++++++--- 3 files changed, 66 insertions(+), 60 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 e8abd0b..be372fc 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -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 testCreatedAccount = userService.createNewAccount(testAccount); @@ -38,25 +34,29 @@ public class UserServiceIntegrationTest { assertEquals("test password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); - final Optional 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 updatedTestCreatedAccount = userService.updateAccount(returnedAccount); - final Optional 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; + } } 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 d717e2b..2e7a468 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -93,47 +93,18 @@ public class UserService { } } - public Optional 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 typeToken = new TypeToken(){}; - final Optional 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{ 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 ab1f321..1fb9dda 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -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()); } -} \ No newline at end of file + + @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 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; + } +}