PAN-15 fix the request changes

This commit is contained in:
dantanxiaotian
2019-03-27 12:18:12 -06:00
parent eedc7a3eee
commit b1d4ed9719
4 changed files with 212 additions and 36 deletions
@@ -24,36 +24,47 @@ public class UserServiceIntegrationTest {
@Test @Test
public void testUserService(){ public void testUserService(){
final Account testAccount = creatAccount(); final Account testAccount = createAccount();
final Optional<Account> testCreatedAccount = userService.createNewAccount(testAccount);
final Optional<Account> testCreatedAccount = userService.createAccount(testAccount);
assertTrue(testCreatedAccount.isPresent()); assertTrue(testCreatedAccount.isPresent());
final Account returnedAccount = testCreatedAccount.get(); final Account returnedAccount = testCreatedAccount.get();
assertEquals("test user", returnedAccount.getUsername()); assertEquals("test_user", returnedAccount.getUsername());
assertEquals("test password", returnedAccount.getPassword()); assertEquals("test_password", returnedAccount.getPassword());
assertFalse(returnedAccount.isAdministratorStatus()); assertFalse(returnedAccount.isAdministratorStatus());
returnedAccount.setUsername("test updatedUser"); final Optional<Account> getAccountById = userService.getAccountById(returnedAccount.getId());
returnedAccount.setPassword("test updatedPassword"); 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()); returnedAccount.setUsername("test_updatedUser");
final Account returnedUpdatedAccount = updatedTestCreatedAccount.get(); returnedAccount.setPassword("test_updatedPassword");
assertEquals("test updatedUser", returnedUpdatedAccount.getUsername());
assertEquals("test updatedPassword", returnedUpdatedAccount.getPassword()); 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()); assertFalse(returnedUpdatedAccount.isAdministratorStatus());
final boolean result = userService.deleteAccount(returnedUpdatedAccount); final boolean result = userService.deleteAccount(returnedUpdatedAccount);
assertTrue(result); assertTrue(result);
} }
private Account creatAccount(){ private Account createAccount(){
final Account testAccount = new Account(); final Account testAccount = new Account();
testAccount.setUsername("test user"); testAccount.setUsername("test_user");
testAccount.setPassword("test password"); testAccount.setPassword("test_password");
testAccount.setAdministratorStatus(false); testAccount.setAdministratorStatus(false);
testAccount.setLastLogin(new Date()); testAccount.setLastLogin(new Date());
@@ -26,7 +26,99 @@ public class UserService {
this.restService = restService; 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) { if (account == null) {
LOG.error("Given null account, returning {}"); LOG.error("Given null account, returning {}");
return Optional.empty(); return Optional.empty();
@@ -58,7 +150,7 @@ public class UserService {
public Optional<Account> updateAccount(final Account account) { public Optional<Account> updateAccount(final Account account) {
if (account == null) { if (account == null) {
LOG.error("User does not exist, returning {}"); LOG.error("Specified account is null; returning {}");
return Optional.empty(); return Optional.empty();
} }
@@ -81,7 +173,7 @@ public class UserService {
if (persistenceApiResponse.isPresent()) { if (persistenceApiResponse.isPresent()) {
LOG.info("Returning {}", persistenceApiResponse.get()); LOG.info("Returning {}", persistenceApiResponse.get());
} else { } else {
LOG.info("Unable to update user {} with id", account.getId()); LOG.info("Unable to update user with id {}", account.getId());
} }
return persistenceApiResponse; return persistenceApiResponse;
@@ -95,8 +187,8 @@ public class UserService {
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("Specified account is null; returning {}");
return false; return false;
} }
@@ -107,13 +199,13 @@ public class UserService {
final int id = account.getId(); final int id = account.getId();
final Instant start = Instant.now(); final Instant start = Instant.now();
try{ try {
final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/" + id, final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id,
connectionTimeoutMilliseconds, connectionTimeoutMilliseconds,
socketTimeoutMilliseconds, HttpStatus.NO_CONTENT ); socketTimeoutMilliseconds, HttpStatus.NO_CONTENT);
if(persistenceApiResponse){ if (persistenceApiResponse){
LOG.info("return {}", persistenceApiResponse); LOG.info("Returning {}", persistenceApiResponse);
} }
else { else {
LOG.info("Unable to delete user {}", account.toString()); LOG.info("Unable to delete user {}", account.toString());
@@ -124,7 +216,7 @@ public class UserService {
LOG.error("Error deleting user {}", e); LOG.error("Error deleting user {}", e);
return false; return false;
} finally { } 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.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.text.ParseException; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -23,27 +24,65 @@ public class UserServiceTest {
@InjectMocks private UserService userService; @InjectMocks private UserService userService;
@Test @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(); 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)); .thenReturn(Optional.of(account));
final Optional<Account> response = userService.getAccountById(1);
final Optional<Account> response = userService.createNewAccount(account);
assertTrue(response.isPresent()); assertTrue(response.isPresent());
assertEquals(account, response.get()); assertEquals(account, response.get());
} }
@Test @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(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()))
.thenReturn(Optional.of(account)); .thenReturn(Optional.of(account));
final Optional<Account> response = userService.updateAccount(account); final Optional<Account> response = userService.updateAccount(account);
assertTrue(response.isPresent()); assertTrue(response.isPresent());
@@ -51,14 +90,13 @@ public class UserServiceTest {
} }
@Test @Test
public void testDeleteAccount() throws ParseException { public void testDeleteAccount(){
final boolean response= true; final boolean response= true;
final Account account = createAccount(); final Account account = createAccount();
account.setId(1); account.setId(1);
when(restService.delete(anyString(), anyInt(), anyInt(), any())) when(restService.delete(anyString(), anyInt(), anyInt(), any()))
.thenReturn(response); .thenReturn(response);
final boolean persistenceApiResponse = userService.deleteAccount(account); final boolean persistenceApiResponse = userService.deleteAccount(account);
assertTrue(persistenceApiResponse ); 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";
}
}
}