PAN-15 UserService.java and completed UserSservice integration test
This commit is contained in:
BIN
Binary file not shown.
@@ -16,6 +16,7 @@ values ('admin', 'secret', true),
|
||||
('BrittanyBi', 'secret', true),
|
||||
('lanlanzeliu', 'secret', true),
|
||||
('tramanh305', 'secret', true);
|
||||
|
||||
create table definitions (
|
||||
id int not null auto_increment primary key unique,
|
||||
name varchar(200) not null,
|
||||
|
||||
+3
@@ -6,4 +6,7 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AccountsRepository extends CrudRepository<AccountDto, Integer> {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
+1
@@ -33,6 +33,7 @@ public class AccountControllerTest {
|
||||
|
||||
@Test
|
||||
public void testGetAllAccounts() {
|
||||
|
||||
final AccountDto accountDto = createAccount();
|
||||
final List<AccountDto> accountDtoList = new ArrayList<>();
|
||||
accountDtoList.add(accountDto);
|
||||
|
||||
+22
-2
@@ -9,7 +9,6 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -24,7 +23,7 @@ public class UserServiceIntegrationTest {
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
public void testCreateNewUser() throws ParseException {
|
||||
public void testUserService(){
|
||||
final Account testAccount = new Account();
|
||||
testAccount.setUsername("test user");
|
||||
testAccount.setPassword("test password");
|
||||
@@ -38,5 +37,26 @@ public class UserServiceIntegrationTest {
|
||||
assertEquals("test user", returnedAccount.getUsername());
|
||||
assertEquals("test password", returnedAccount.getPassword());
|
||||
assertFalse(returnedAccount.isAdministratorStatus());
|
||||
|
||||
final Optional<Account> updatePasswordTestCreatedAccount = userService.updatePassword(returnedAccount, "password");
|
||||
|
||||
assertTrue(updatePasswordTestCreatedAccount.isPresent());
|
||||
final Account returnedUpdatedPasswordAccount = updatePasswordTestCreatedAccount.get();
|
||||
assertEquals("test user", returnedUpdatedPasswordAccount.getUsername());
|
||||
assertEquals("password", returnedUpdatedPasswordAccount.getPassword());
|
||||
assertFalse(returnedAccount.isAdministratorStatus());
|
||||
|
||||
final Optional<Account> updateUsernameTestCreatedAccount = userService.updateUsername(returnedUpdatedPasswordAccount, "user");
|
||||
|
||||
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);
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,16 +43,21 @@ public class RestService {
|
||||
return send(requestFactory.post(uri, requestJson), null, connectionTimeout, socketTimeout, type);
|
||||
}
|
||||
|
||||
<T> Optional<T> patch(final String uri, final String requestJson, final TypeToken<T> type, final Integer connectionTimeout, final Integer socketTimeout) {
|
||||
LOG.info("Sending Patch {} with body: {}", uri, requestJson);
|
||||
return send(requestFactory.patch(uri, requestJson), null, connectionTimeout, socketTimeout, type);
|
||||
}
|
||||
Optional<HttpResponse> post(final String uri, final String requestJson, final Integer connectionTimeout, final Integer socketTimeout) {
|
||||
LOG.info("Sending POST {} with body: {}", uri, requestJson);
|
||||
return send(requestFactory.post(uri, requestJson), null, connectionTimeout, socketTimeout);
|
||||
}
|
||||
|
||||
<T> Optional<T> put(final String uri, final String requestJson, final TypeToken<T> type, final Integer connectionTimeout, final Integer socketTimeout, final String auth) {
|
||||
<T> Optional<T> put(final String uri, final String requestJson, final TypeToken<T> type, final Integer connectionTimeout, final Integer socketTimeout) {
|
||||
LOG.info("Sending PUT {} with body: {}", uri, requestJson);
|
||||
return send(requestFactory.put(uri, requestJson), auth, connectionTimeout, socketTimeout, type);
|
||||
return send(requestFactory.put(uri, requestJson), null, connectionTimeout, socketTimeout, type);
|
||||
}
|
||||
|
||||
|
||||
private <T> Optional<T> send(final Request request, final String auth, final Integer connectionTimeout, final Integer socketTimeout, final TypeToken<T> type) {
|
||||
try {
|
||||
final Optional<HttpResponse> optionalHttpResponse = send(request, auth, connectionTimeout, socketTimeout);
|
||||
|
||||
@@ -6,6 +6,7 @@ import edu.msudenver.tsp.services.dto.Account;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -16,16 +17,19 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class UserService {
|
||||
private final RestService restService;
|
||||
@Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds;
|
||||
@Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds;
|
||||
@Value("${persistence.api.base.url}") private String persistenceApiBaseUrl;
|
||||
@Value("${persistence.api.connection.timeout.milliseconds}")
|
||||
private int connectionTimeoutMilliseconds;
|
||||
@Value("${persistence.api.socket.timeout.milliseconds}")
|
||||
private int socketTimeoutMilliseconds;
|
||||
@Value("${persistence.api.base.url}")
|
||||
private String persistenceApiBaseUrl;
|
||||
|
||||
@Autowired
|
||||
public UserService(final RestService restService) {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<Account> createNewAccount(final Account account) {
|
||||
public Optional<Account> createNewAccount(final Account account) {
|
||||
if (account == null) {
|
||||
LOG.error("Given null account, returning {}");
|
||||
return Optional.empty();
|
||||
@@ -33,7 +37,8 @@ public class UserService {
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {
|
||||
};
|
||||
final Optional<Account> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "accounts/",
|
||||
new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account),
|
||||
typeToken,
|
||||
@@ -41,7 +46,7 @@ public class UserService {
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
LOG.info("Returning {}", persistenceApiResponse);
|
||||
} else {
|
||||
LOG.info("Unable to create new account {}", account.toString());
|
||||
}
|
||||
@@ -54,4 +59,102 @@ public class UserService {
|
||||
LOG.info("Create new account request took {} ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> updatePassword(final Account account , final String password){
|
||||
|
||||
if(account ==null){
|
||||
LOG.error("user not exist, returning{}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Integer id = account.getId();
|
||||
account.setPassword(password);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try{
|
||||
final String auth = "";
|
||||
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.info("Unable to update password 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 Optional<Account> 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<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.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 not exist, returning{}");
|
||||
return false;
|
||||
}
|
||||
final Integer 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("return {}", persistenceApiResponse);
|
||||
}
|
||||
else {
|
||||
LOG.info("Unable to delete user {}",account.toString());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,5 @@ public class Account extends BaseDto implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7095627971593953734L;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,4 +22,8 @@ public class RequestFactory {
|
||||
public Request put(final String uri, final String requestJson) {
|
||||
return StringUtils.isNotBlank(requestJson) ? Request.Put(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Put(uri);
|
||||
}
|
||||
|
||||
public Request patch(final String uri, final String requestJson) {
|
||||
return StringUtils.isNotBlank(requestJson) ? Request.Patch(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Patch(uri);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user