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 88da28f..b3a547d 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -24,36 +24,47 @@ public class UserServiceIntegrationTest { @Test public void testUserService(){ - final Account testAccount = creatAccount(); - - final Optional testCreatedAccount = userService.createNewAccount(testAccount); + final Account testAccount = createAccount(); + final Optional testCreatedAccount = userService.createAccount(testAccount); assertTrue(testCreatedAccount.isPresent()); final Account returnedAccount = testCreatedAccount.get(); - assertEquals("test user", returnedAccount.getUsername()); - assertEquals("test password", returnedAccount.getPassword()); + assertEquals("test_user", returnedAccount.getUsername()); + assertEquals("test_password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); - returnedAccount.setUsername("test updatedUser"); - returnedAccount.setPassword("test updatedPassword"); + final Optional getAccountById = userService.getAccountById(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 updatedTestCreatedAccount = userService.updateAccount(returnedAccount); + final Optional getAccountByUsername = userService.getAccountByUsername(returnedAccount.getUsername()); + assertTrue(getAccountByUsername.isPresent()); + final Account returnedAccountByUsername = getAccountByUsername.get(); + assertEquals("test_user", returnedAccountByUsername.getUsername()); + assertEquals("test_password", returnedAccountByUsername.getPassword()); + assertFalse(returnedAccountById.isAdministratorStatus()); - assertTrue(updatedTestCreatedAccount .isPresent()); - final Account returnedUpdatedAccount = updatedTestCreatedAccount.get(); - assertEquals("test updatedUser", returnedUpdatedAccount.getUsername()); - assertEquals("test updatedPassword", returnedUpdatedAccount.getPassword()); + returnedAccount.setUsername("test_updatedUser"); + returnedAccount.setPassword("test_updatedPassword"); + + final Optional updatedAccount = userService.updateAccount(returnedAccount); + assertTrue(updatedAccount .isPresent()); + final Account returnedUpdatedAccount = updatedAccount.get(); + assertEquals("test_updatedUser", returnedUpdatedAccount.getUsername()); + assertEquals("test_updatedPassword", returnedUpdatedAccount.getPassword()); assertFalse(returnedUpdatedAccount.isAdministratorStatus()); final boolean result = userService.deleteAccount(returnedUpdatedAccount); - assertTrue(result); } - private Account creatAccount(){ + private Account createAccount(){ final Account testAccount = new Account(); - testAccount.setUsername("test user"); - testAccount.setPassword("test password"); + testAccount.setUsername("test_user"); + testAccount.setPassword("test_password"); testAccount.setAdministratorStatus(false); testAccount.setLastLogin(new Date()); 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 2e7a468..593b0d9 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -26,7 +26,99 @@ public class UserService { this.restService = restService; } - public Optional createNewAccount(final Account account) { + public Optional getListOfAccount(){ + final String auth = ""; + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + auth); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to get the list of accounts"); + } + + return persistenceApiResponse; + } catch (final Exception 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()); + } + } + + public Optional getAccountById(final int id) { + + if (id == 0) { + LOG.error("No user ID specified! Returning {}"); + return Optional.empty(); + } + + final String auth = ""; + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id="+id, + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + auth); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to find account with id {}", id); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error getting account by id {}", e); + return Optional.empty(); + } finally { + LOG.info("Getting account by ID request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional getAccountByUsername(final String username) { + if (username == null) { + LOG.error("No username specified! Returning {}"); + return Optional.empty(); + } + + final String auth = ""; + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username="+username, + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + auth); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to GET account with username{}", username); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error getting account by username {}", e); + return Optional.empty(); + } finally { + LOG.info("Getting account by username request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional createAccount(final Account account) { if (account == null) { LOG.error("Given null account, returning {}"); return Optional.empty(); @@ -58,7 +150,7 @@ public class UserService { public Optional updateAccount(final Account account) { if (account == null) { - LOG.error("User does not exist, returning {}"); + LOG.error("Specified account is null; returning {}"); return Optional.empty(); } @@ -81,7 +173,7 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to update user {} with id", account.getId()); + LOG.info("Unable to update user with id {}", account.getId()); } return persistenceApiResponse; @@ -95,8 +187,8 @@ public class UserService { public boolean deleteAccount(final Account account) { - if(account == null){ - LOG.error("Username does not exist, returning {}"); + if (account == null){ + LOG.error("Specified account is null; returning {}"); return false; } @@ -107,13 +199,13 @@ public class UserService { final int id = account.getId(); final Instant start = Instant.now(); - try{ + try { - final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/" + id, + final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id, connectionTimeoutMilliseconds, - socketTimeoutMilliseconds, HttpStatus.NO_CONTENT ); - if(persistenceApiResponse){ - LOG.info("return {}", persistenceApiResponse); + socketTimeoutMilliseconds, HttpStatus.NO_CONTENT); + if (persistenceApiResponse){ + LOG.info("Returning {}", persistenceApiResponse); } else { LOG.info("Unable to delete user {}", account.toString()); @@ -124,7 +216,7 @@ public class UserService { 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()); } } } 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 7f19401..4a478d9 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -8,8 +8,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import java.text.ParseException; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Optional; import static org.junit.Assert.assertEquals; @@ -23,27 +24,65 @@ public class UserServiceTest { @InjectMocks private UserService userService; @Test - public void testCreateNewAccount() throws ParseException { + public void testGetListOfAccounts(){ + final Account account1 = createAccount(); + final Account account2 = createAccount(); + final List accountList = new ArrayList<>(); + accountList.add(account1); + accountList.add(account2); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.of(accountList)); + final Optional response = userService.getListOfAccount(); + + assertTrue(response.isPresent()); + assertEquals(accountList, response.get()); + } + + @Test + public void testGetAccountById(){ final Account account = createAccount(); + account.setId(1); - when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) .thenReturn(Optional.of(account)); - - final Optional response = userService.createNewAccount(account); + final Optional response = userService.getAccountById(1); assertTrue(response.isPresent()); assertEquals(account, response.get()); } @Test - public void testUpdateAccount() throws ParseException { + public void testGetAccountByUsername(){ + final Account account = createAccount(); + final String username = account.getUsername(); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.of(account)); + final Optional response = userService.getAccountByUsername(username); + + assertTrue(response.isPresent()); + assertEquals(account, response.get()); + } + @Test + public void testCreateAccount(){ + final Account account = createAccount(); + + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenReturn(Optional.of(account)); + final Optional response = userService.createAccount(account); + + assertTrue(response.isPresent()); + assertEquals(account, response.get()); + } + + @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)); - final Optional response = userService.updateAccount(account); assertTrue(response.isPresent()); @@ -51,14 +90,13 @@ public class UserServiceTest { } @Test - public void testDeleteAccount() throws ParseException { + public void testDeleteAccount(){ 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 ); diff --git a/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java new file mode 100644 index 0000000..70247ff --- /dev/null +++ b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java @@ -0,0 +1,35 @@ +package edu.msudenver.tsp.website.controller; + +import edu.msudenver.tsp.website.forms.TheoremForm; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + + + @Slf4j + @Controller + @AllArgsConstructor + @RequestMapping("/login") + public class LogInController { + @GetMapping({"/",""}) + public ModelAndView enterTheoremPage() { + LOG.info("Received request to display the theorem entry page: returning model with name 'Theorem'"); + return new ModelAndView("Theorem"); + } + + @PostMapping({"/",""}) + public String saveTheorem(@Validated final TheoremForm theoremForm, final Model model) { + model.addAttribute("theoremName", theoremForm.getTheoremName()); + model.addAttribute("theorem", theoremForm.getTheorem()); + LOG.info("Saving theorem {}...", theoremForm); + + return "success"; + } + } +}