Merge branches 'PAN-11' and 'master' of https://github.com/atusa17/ptp into PAN-11
# Conflicts: # services/src/integrationTest/resources/test.properties # services/src/main/java/edu/msudenver/tsp/services/dto/Account.java
This commit is contained in:
@@ -5,11 +5,11 @@ create table accounts (
|
|||||||
id int not null auto_increment primary key unique,
|
id int not null auto_increment primary key unique,
|
||||||
username varchar(50) not null unique,
|
username varchar(50) not null unique,
|
||||||
password varchar(256) not null,
|
password varchar(256) not null,
|
||||||
administrator_status boolean default false,
|
administrator boolean default false,
|
||||||
last_login date,
|
last_login date,
|
||||||
version int default 1
|
version int default 1
|
||||||
);
|
);
|
||||||
insert into accounts (username, password, administrator_status)
|
insert into accounts (username, password, administrator)
|
||||||
values ('admin', 'secret', true),
|
values ('admin', 'secret', true),
|
||||||
('atusa', 'secret', true),
|
('atusa', 'secret', true),
|
||||||
('dantanxiaotian', 'secret', true),
|
('dantanxiaotian', 'secret', true),
|
||||||
|
|||||||
+3
-3
@@ -29,7 +29,7 @@ public class AccountsIntegrationTest {
|
|||||||
|
|
||||||
assertEquals("Test username", savedAccount.getUsername());
|
assertEquals("Test username", savedAccount.getUsername());
|
||||||
assertEquals("test password", savedAccount.getPassword());
|
assertEquals("test password", savedAccount.getPassword());
|
||||||
assertTrue(savedAccount.getAdministratorStatus());
|
assertTrue(savedAccount.getAdministrator());
|
||||||
|
|
||||||
savedAccount.setPassword("Test Update");
|
savedAccount.setPassword("Test Update");
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ public class AccountsIntegrationTest {
|
|||||||
|
|
||||||
assertEquals("Test username", savedAccount.getUsername());
|
assertEquals("Test username", savedAccount.getUsername());
|
||||||
assertEquals("Test Update", savedAccount.getPassword());
|
assertEquals("Test Update", savedAccount.getPassword());
|
||||||
assertTrue(savedAccount.getAdministratorStatus());
|
assertTrue(savedAccount.getAdministrator());
|
||||||
assertEquals(updatedAccount.getId(), id);
|
assertEquals(updatedAccount.getId(), id);
|
||||||
|
|
||||||
accountsRepository.delete(account);
|
accountsRepository.delete(account);
|
||||||
@@ -49,7 +49,7 @@ public class AccountsIntegrationTest {
|
|||||||
final Account account = new Account();
|
final Account account = new Account();
|
||||||
account.setUsername("Test username");
|
account.setUsername("Test username");
|
||||||
account.setPassword("test password");
|
account.setPassword("test password");
|
||||||
account.setAdministratorStatus(true);
|
account.setAdministrator(true);
|
||||||
|
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,19 +19,19 @@ import java.util.Date;
|
|||||||
public class Account extends BaseDto implements Serializable {
|
public class Account extends BaseDto implements Serializable {
|
||||||
@NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
@NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
||||||
@NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
@NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
||||||
@NotNull @Column(name = "administrator_status") private boolean administratorStatus;
|
@NotNull @Column(name = "administrator") private boolean administrator;
|
||||||
@Temporal(TemporalType.DATE) @Column(name = "last_login") private Date lastLogin;
|
@Temporal(TemporalType.DATE) @Column(name = "last_login") private Date lastLogin;
|
||||||
|
|
||||||
private static final long serialVersionUID = 7095627971593953734L;
|
private static final long serialVersionUID = 7095627971593953734L;
|
||||||
|
|
||||||
@JsonProperty("administrator_status")
|
@JsonProperty("administrator_status")
|
||||||
public boolean getAdministratorStatus() {
|
public boolean getAdministrator() {
|
||||||
return administratorStatus;
|
return administrator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty("administrator_status")
|
@JsonProperty("administrator_status")
|
||||||
public void setAdministratorStatus(final boolean administratorStatus) {
|
public void setAdministrator(final boolean administrator) {
|
||||||
this.administratorStatus = administratorStatus;
|
this.administrator = administrator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty("last_login")
|
@JsonProperty("last_login")
|
||||||
|
|||||||
+1
-1
@@ -269,7 +269,7 @@ public class AccountControllerTest {
|
|||||||
final Account account = new Account();
|
final Account account = new Account();
|
||||||
account.setUsername("Test username");
|
account.setUsername("Test username");
|
||||||
account.setPassword("test password");
|
account.setPassword("test password");
|
||||||
account.setAdministratorStatus(true);
|
account.setAdministrator(true);
|
||||||
|
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-2
@@ -12,6 +12,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.AdditionalMatchers.not;
|
import static org.mockito.AdditionalMatchers.not;
|
||||||
@@ -23,11 +24,10 @@ public class DefinitionServiceIntegrationTest {
|
|||||||
@Autowired private DefinitionService definitionService;
|
@Autowired private DefinitionService definitionService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateDefinition() {
|
public void testCRUD() {
|
||||||
final Definition testDefinition = createDefinition();
|
final Definition testDefinition = createDefinition();
|
||||||
final Optional<Definition> createdDefinition = definitionService.createDefinition(testDefinition);
|
final Optional<Definition> createdDefinition = definitionService.createDefinition(testDefinition);
|
||||||
|
|
||||||
assertNotNull(createdDefinition);
|
|
||||||
assertTrue(createdDefinition.isPresent());
|
assertTrue(createdDefinition.isPresent());
|
||||||
assertThat(createdDefinition.get().getId(), is(not(0)));
|
assertThat(createdDefinition.get().getId(), is(not(0)));
|
||||||
assertThat(createdDefinition.get().getVersion(), is(0));
|
assertThat(createdDefinition.get().getVersion(), is(0));
|
||||||
@@ -38,6 +38,35 @@ public class DefinitionServiceIntegrationTest {
|
|||||||
assertNotNull(createdDefinition.get().getNotation());
|
assertNotNull(createdDefinition.get().getNotation());
|
||||||
assertThat(createdDefinition.get().getNotation().size(), is(1));
|
assertThat(createdDefinition.get().getNotation().size(), is(1));
|
||||||
assertThat(createdDefinition.get().getNotation().get(0), is("\\testLaTeX"));
|
assertThat(createdDefinition.get().getNotation().get(0), is("\\testLaTeX"));
|
||||||
|
|
||||||
|
final Optional<Definition> definitionFoundById = definitionService.findById(createdDefinition.get().getId());
|
||||||
|
|
||||||
|
assertThat(definitionFoundById.get(), is(equalTo(createdDefinition.get())));
|
||||||
|
|
||||||
|
final Definition definitionUpdate = new Definition();
|
||||||
|
definitionUpdate.setId(createdDefinition.get().getId());
|
||||||
|
definitionUpdate.setName("Test Update");
|
||||||
|
|
||||||
|
final Optional<Definition> updatedDefinition = definitionService.updateDefinition(definitionUpdate);
|
||||||
|
|
||||||
|
assertTrue(updatedDefinition.isPresent());
|
||||||
|
assertThat(updatedDefinition.get().getId(), is(not(0)));
|
||||||
|
assertThat(updatedDefinition.get().getVersion(), is(1));
|
||||||
|
assertThat(updatedDefinition.get().getName(), is("Test Update"));
|
||||||
|
assertNotNull(updatedDefinition.get().getDefinition());
|
||||||
|
assertThat(updatedDefinition.get().getDefinition().size(), is(1));
|
||||||
|
assertThat(updatedDefinition.get().getDefinition().get(0), is("Test definition 1"));
|
||||||
|
assertNotNull(updatedDefinition.get().getNotation());
|
||||||
|
assertThat(updatedDefinition.get().getNotation().size(), is(1));
|
||||||
|
assertThat(updatedDefinition.get().getNotation().get(0), is("\\testLaTeX"));
|
||||||
|
|
||||||
|
final boolean deletionWasSuccessful = definitionService.deleteDefinition(updatedDefinition.get());
|
||||||
|
|
||||||
|
assertThat(deletionWasSuccessful, is(true));
|
||||||
|
|
||||||
|
final Optional<Definition> deletedDefinitionFoundById = definitionService.findById(createdDefinition.get().getId());
|
||||||
|
|
||||||
|
assertFalse(deletedDefinitionFoundById.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Definition createDefinition() {
|
private Definition createDefinition() {
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package edu.msudenver.tsp.services;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan
|
||||||
|
public class ServicesTestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Autowired
|
||||||
|
public UserService userService(final RestService restService) {
|
||||||
|
return new UserService(restService);
|
||||||
|
}
|
||||||
|
}
|
||||||
+74
@@ -0,0 +1,74 @@
|
|||||||
|
package edu.msudenver.tsp.services;
|
||||||
|
|
||||||
|
import edu.msudenver.tsp.services.dto.Account;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = ServicesTestConfig.class)
|
||||||
|
@TestPropertySource("classpath:test.properties")
|
||||||
|
public class UserServiceIntegrationTest {
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("userService")
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserService(){
|
||||||
|
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());
|
||||||
|
assertFalse(returnedAccount.isAdministrator());
|
||||||
|
|
||||||
|
final Optional<Account> getAccountById = userService.findAccountById(returnedAccount.getId());
|
||||||
|
assertTrue(getAccountById.isPresent());
|
||||||
|
final Account returnedAccountById = getAccountById.get();
|
||||||
|
assertEquals("test_user", returnedAccountById.getUsername());
|
||||||
|
assertEquals("test_password", returnedAccountById.getPassword());
|
||||||
|
assertFalse(returnedAccountById.isAdministrator());
|
||||||
|
|
||||||
|
final Optional<Account> getAccountByUsername = userService.findAccountByUsername(returnedAccount.getUsername());
|
||||||
|
assertTrue(getAccountByUsername.isPresent());
|
||||||
|
final Account returnedAccountByUsername = getAccountByUsername.get();
|
||||||
|
assertEquals("test_user", returnedAccountByUsername.getUsername());
|
||||||
|
assertEquals("test_password", returnedAccountByUsername.getPassword());
|
||||||
|
assertFalse(returnedAccountById.isAdministrator());
|
||||||
|
|
||||||
|
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.isAdministrator());
|
||||||
|
|
||||||
|
final boolean result = userService.deleteAccount(returnedUpdatedAccount);
|
||||||
|
assertTrue(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Account createAccount() {
|
||||||
|
final Account testAccount = new Account();
|
||||||
|
testAccount.setUsername("test_user");
|
||||||
|
testAccount.setPassword("test_password");
|
||||||
|
testAccount.setAdministrator(false);
|
||||||
|
testAccount.setLastLogin(new Date());
|
||||||
|
|
||||||
|
return testAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -44,7 +44,7 @@ public class DefinitionService {
|
|||||||
|
|
||||||
return persistenceApiResponse;
|
return persistenceApiResponse;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Error getting list of definitions! {}", e);
|
LOG.error("Error getting list of definitions!", e);
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
} finally {
|
} finally {
|
||||||
LOG.info("Get all definitions request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
LOG.info("Get all definitions request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
@@ -105,7 +105,7 @@ public class DefinitionService {
|
|||||||
|
|
||||||
return persistenceApiResponse;
|
return persistenceApiResponse;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Error creating new definition {}", e);
|
LOG.error("Error creating new definition", e);
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
} finally {
|
} finally {
|
||||||
LOG.info("Create new definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
LOG.info("Create new definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
@@ -142,7 +142,7 @@ public class DefinitionService {
|
|||||||
|
|
||||||
return persistenceApiResponse;
|
return persistenceApiResponse;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Error updating definition {}", e);
|
LOG.error("Error updating definition", e);
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
} finally {
|
} finally {
|
||||||
LOG.info("Update definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
LOG.info("Update definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
@@ -177,7 +177,7 @@ public class DefinitionService {
|
|||||||
|
|
||||||
return deleteIsSuccessful;
|
return deleteIsSuccessful;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Error when deleting definition {}", e);
|
LOG.error("Error when deleting definition", e);
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
LOG.info("Delete definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
LOG.info("Delete definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import edu.msudenver.tsp.services.dto.Account;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -25,7 +27,96 @@ public class UserService {
|
|||||||
this.restService = restService;
|
this.restService = restService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Account> createNewAccount(final Account account) {
|
public Optional<List<Account>> getListOfAccounts() {
|
||||||
|
final Instant start = Instant.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final TypeToken<List<Account>> typeToken = new TypeToken<List<Account>>() {};
|
||||||
|
final Optional<List<Account>> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/",
|
||||||
|
typeToken,
|
||||||
|
connectionTimeoutMilliseconds,
|
||||||
|
socketTimeoutMilliseconds,
|
||||||
|
null);
|
||||||
|
|
||||||
|
if (persistenceApiResponse.isPresent()) {
|
||||||
|
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||||
|
} else {
|
||||||
|
LOG.warn("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("Get the list of accounts request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Account> findAccountById(final int id) {
|
||||||
|
|
||||||
|
if (id == 0) {
|
||||||
|
LOG.error("No user ID specified! Returning {}");
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
null);
|
||||||
|
|
||||||
|
if (persistenceApiResponse.isPresent()) {
|
||||||
|
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||||
|
} else {
|
||||||
|
LOG.warn("Unable to find account with id {}", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return persistenceApiResponse;
|
||||||
|
} catch (final Exception e) {
|
||||||
|
LOG.error("Error finding account by id", e);
|
||||||
|
return Optional.empty();
|
||||||
|
} finally {
|
||||||
|
LOG.info("Find account by ID request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Account> findAccountByUsername(final String username) {
|
||||||
|
if (username == null) {
|
||||||
|
LOG.error("No username specified! Returning {}");
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
null);
|
||||||
|
|
||||||
|
if (persistenceApiResponse.isPresent()) {
|
||||||
|
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||||
|
} else {
|
||||||
|
LOG.warn("Unable to GET account with username {}", username);
|
||||||
|
}
|
||||||
|
|
||||||
|
return persistenceApiResponse;
|
||||||
|
} catch (final Exception e) {
|
||||||
|
LOG.error("Error finding account by username", e);
|
||||||
|
return Optional.empty();
|
||||||
|
} finally {
|
||||||
|
LOG.info("Find 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();
|
||||||
@@ -34,8 +125,8 @@ public class UserService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||||
final Optional<Account> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/accounts/",
|
final Optional<Account> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "accounts/",
|
||||||
new GsonBuilder().create().toJson(account),
|
new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account),
|
||||||
typeToken,
|
typeToken,
|
||||||
connectionTimeoutMilliseconds,
|
connectionTimeoutMilliseconds,
|
||||||
socketTimeoutMilliseconds);
|
socketTimeoutMilliseconds);
|
||||||
@@ -43,15 +134,88 @@ 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 create new account {}", account.toString());
|
LOG.warn("Unable to create new account {}", account.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return persistenceApiResponse;
|
return persistenceApiResponse;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
LOG.error("Error creating new account {}", e);
|
LOG.error("Error creating new account", e);
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
} finally {
|
} finally {
|
||||||
LOG.info("Create new account request took {} ms", Duration.between(start, Instant.now()).toMillis());
|
LOG.info("Create new account request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Account> updateAccount(final Account account) {
|
||||||
|
if (account == null) {
|
||||||
|
LOG.error("Specified account is null; returning {}");
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (account.getId() == 0) {
|
||||||
|
LOG.error("No user ID specified! Returning {}");
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
final int id = account.getId();
|
||||||
|
final Instant start = Instant.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
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.warn("Unable to update user with id {}", account.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
return persistenceApiResponse;
|
||||||
|
} catch (final Exception e) {
|
||||||
|
LOG.error("Error updating user", e);
|
||||||
|
return Optional.empty();
|
||||||
|
} finally {
|
||||||
|
LOG.info("Update user request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean deleteAccount(final Account account) {
|
||||||
|
if (account == null){
|
||||||
|
LOG.error("Specified account is null; returning {}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (account.getId() == 0) {
|
||||||
|
LOG.error("No user ID specified! Returning {}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int id = account.getId();
|
||||||
|
final Instant start = Instant.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id,
|
||||||
|
connectionTimeoutMilliseconds,
|
||||||
|
socketTimeoutMilliseconds, HttpStatus.NO_CONTENT);
|
||||||
|
if (persistenceApiResponse) {
|
||||||
|
LOG.info("Returning {}", persistenceApiResponse);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LOG.error("Unable to delete user {}", account);
|
||||||
|
}
|
||||||
|
|
||||||
|
return persistenceApiResponse;
|
||||||
|
}catch (final Exception e) {
|
||||||
|
LOG.error("Error deleting user", e);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
LOG.info("Delete user request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import com.google.gson.annotations.SerializedName;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@@ -13,12 +12,10 @@ import java.util.Date;
|
|||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class Account extends BaseDto implements Serializable {
|
public class Account extends BaseDto implements Serializable {
|
||||||
@NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
@Size(max = 50) private String username;
|
||||||
@NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
@Size(max = 256) private String password;
|
||||||
@NotNull @SerializedName("administrator_status") private boolean administratorStatus;
|
@NotNull private boolean administrator;
|
||||||
@SerializedName("last_login") private Date lastLogin;
|
@SerializedName("last_login") private Date lastLogin;
|
||||||
|
|
||||||
private static final long serialVersionUID = 7095627971593953734L;
|
private static final long serialVersionUID = 7095627971593953734L;
|
||||||
|
|
||||||
public interface Insert {}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,6 @@ public class RequestFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Request patch(final String uri, final String requestJson) {
|
public Request patch(final String uri, final String requestJson) {
|
||||||
return StringUtils.isNotBlank(requestJson) ? Request.Put(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Patch(uri);
|
return StringUtils.isNotBlank(requestJson) ? Request.Patch(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Patch(uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
persistence.api.connection.timeout.milliseconds = 5000
|
persistence.api.connection.timeout.milliseconds=5000
|
||||||
persistence.api.socket.timeout.milliseconds = 10000
|
persistence.api.socket.timeout.milliseconds=10000
|
||||||
persistence.api.base.url = http://localhost:8090/
|
persistence.api.base.url=http://localhost:8090/
|
||||||
@@ -0,0 +1,313 @@
|
|||||||
|
package edu.msudenver.tsp.services;
|
||||||
|
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import edu.msudenver.tsp.services.dto.Account;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class UserServiceTest {
|
||||||
|
@Mock private RestService restService;
|
||||||
|
@InjectMocks private UserService userService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetListOfAccounts() {
|
||||||
|
final List<Account> accountList = new ArrayList<>();
|
||||||
|
accountList.add(createAccount());
|
||||||
|
accountList.add(createAccount());
|
||||||
|
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(), anyString())).thenReturn(Optional.of(accountList));
|
||||||
|
|
||||||
|
final Optional<List<Account>> response = userService.getListOfAccounts();
|
||||||
|
|
||||||
|
assertTrue(response.isPresent());
|
||||||
|
assertEquals(accountList, response.get());
|
||||||
|
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetListOfAccounts_PersistenceApiResponseIsEmpty() {
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
final Optional<List<Account>> response = userService.getListOfAccounts();
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetListOfAccounts_RestServiceThrowsException() {
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class);
|
||||||
|
|
||||||
|
final Optional<List<Account>> response = userService.getListOfAccounts();
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountById() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account));
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.findAccountById(1);
|
||||||
|
|
||||||
|
assertTrue(response.isPresent());
|
||||||
|
assertEquals(account, response.get());
|
||||||
|
verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountById_IdEqualsZero() {
|
||||||
|
final Optional<Account> response = userService.findAccountById(0);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
assertEquals(Optional.empty(), response);
|
||||||
|
verifyZeroInteractions(restService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountById_PersistenceApiResponseIsEmpty() {
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.findAccountById(1);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountById_RestServiceThrowsException() {
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class);
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.findAccountById(1);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountByUsername() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account));
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.findAccountByUsername(account.getUsername());
|
||||||
|
|
||||||
|
assertTrue(response.isPresent());
|
||||||
|
assertEquals(account, response.get());
|
||||||
|
verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountByUsername_NullUsername() {
|
||||||
|
final Optional<Account> response = userService.findAccountByUsername(null);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
assertEquals(Optional.empty(), response);
|
||||||
|
verifyZeroInteractions(restService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountByUsername_PersistenceApiResponseIsEmpty() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
|
||||||
|
when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.findAccountByUsername(account.getUsername());
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAccountByUsername_RestServiceThrowsException() {
|
||||||
|
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())).thenThrow(Exception.class);
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.findAccountByUsername("test");
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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());
|
||||||
|
verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAccount_NullAccount() {
|
||||||
|
final Optional<Account> response = userService.createAccount(null);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
assertEquals(Optional.empty(), response);
|
||||||
|
verify(restService, times(0)).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAccount_AccountCouldNotBeCreated() {
|
||||||
|
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.createAccount(createAccount());
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateAccount_RestServiceThrowsException() {
|
||||||
|
when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class);
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.createAccount(createAccount());
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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());
|
||||||
|
assertEquals(account, response.get());
|
||||||
|
verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateAccount_NullAccount() {
|
||||||
|
final Optional<Account> response = userService.updateAccount(null);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verifyZeroInteractions(restService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateAccount_IdEqualsZero() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(0);
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.updateAccount(account);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verifyZeroInteractions(restService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateAccount_PersistenceApiResponseIsEmpty() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(1);
|
||||||
|
|
||||||
|
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.updateAccount(account);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateAccount_RestServiceThrowsException() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(1);
|
||||||
|
|
||||||
|
when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class);
|
||||||
|
|
||||||
|
final Optional<Account> response = userService.updateAccount(account);
|
||||||
|
|
||||||
|
assertFalse(response.isPresent());
|
||||||
|
verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAccount() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(1);
|
||||||
|
|
||||||
|
when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(true);
|
||||||
|
|
||||||
|
final boolean isDeleteSuccessful = userService.deleteAccount(account);
|
||||||
|
|
||||||
|
assertTrue(isDeleteSuccessful);
|
||||||
|
verify(restService).delete(anyString(), anyInt(), anyInt(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAccount_NullAccount() {
|
||||||
|
final boolean isDeleteSuccessful = userService.deleteAccount(null);
|
||||||
|
|
||||||
|
assertFalse(isDeleteSuccessful);
|
||||||
|
verifyZeroInteractions(restService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAccount_IdEqualsZero() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(0);
|
||||||
|
|
||||||
|
final boolean isDeleteSuccessful = userService.deleteAccount(account);
|
||||||
|
|
||||||
|
assertFalse(isDeleteSuccessful);
|
||||||
|
verifyZeroInteractions(restService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAccount_PersistenceApiResponseIsEmpty() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(1);
|
||||||
|
|
||||||
|
when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(false);
|
||||||
|
|
||||||
|
final boolean isDeleteSuccessful = userService.deleteAccount(account);
|
||||||
|
|
||||||
|
assertFalse(isDeleteSuccessful);
|
||||||
|
verify(restService).delete(anyString(), anyInt(), anyInt(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteAccount_RestServiceThrowsException() {
|
||||||
|
final Account account = createAccount();
|
||||||
|
account.setId(1);
|
||||||
|
|
||||||
|
when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class);
|
||||||
|
|
||||||
|
final boolean persistenceApiResponse = userService.deleteAccount(account);
|
||||||
|
|
||||||
|
assertFalse(persistenceApiResponse);
|
||||||
|
verify(restService).delete(anyString(), anyInt(), anyInt(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Account createAccount() {
|
||||||
|
final Account account = new Account();
|
||||||
|
account.setUsername("Test username");
|
||||||
|
account.setPassword("test password");
|
||||||
|
account.setAdministrator(true);
|
||||||
|
account.setLastLogin(new Date());
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,6 @@ import static org.mockito.Mockito.when;
|
|||||||
public class ParserServiceTest {
|
public class ParserServiceTest {
|
||||||
|
|
||||||
@Mock private ParserService mockParserService;
|
@Mock private ParserService mockParserService;
|
||||||
|
|
||||||
@InjectMocks private ParserService parserService;
|
@InjectMocks private ParserService parserService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user