PAN-15 fix the request changes
This commit is contained in:
+27
-16
@@ -24,36 +24,47 @@ public class UserServiceIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testUserService(){
|
||||
final Account testAccount = creatAccount();
|
||||
|
||||
final Optional<Account> testCreatedAccount = userService.createNewAccount(testAccount);
|
||||
final Account testAccount = createAccount();
|
||||
|
||||
final Optional<Account> 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<Account> 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<Account> updatedTestCreatedAccount = userService.updateAccount(returnedAccount);
|
||||
final Optional<Account> 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<Account> 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());
|
||||
|
||||
|
||||
@@ -26,7 +26,99 @@ public class UserService {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<Account> createNewAccount(final Account account) {
|
||||
public Optional<Account> getListOfAccount(){
|
||||
final String auth = "";
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> 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<Account> 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<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> 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<Account> 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<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> 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<Account> 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<Account> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Account> 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<Account> 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<Account> response = userService.createNewAccount(account);
|
||||
final Optional<Account> 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<Account> 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<Account> 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<Account> 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 );
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user