#25 Account edit mask
This commit is contained in:
@@ -54,6 +54,24 @@ public class AccountController {
|
||||
return responseReason.toResponseEntity();
|
||||
}
|
||||
|
||||
@RequestMapping("editAccount")
|
||||
public ResponseEntity editAccount(Long id, String key, String accountGroupName) {
|
||||
final String decoded = ControllerUtil.urlDecode(key);
|
||||
final String decodedGroup = ControllerUtil.urlDecode(accountGroupName);
|
||||
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("/accounts/editAccount got parameters: %s, %s, %s", id, decoded, decodedGroup));
|
||||
}
|
||||
|
||||
final ResponseReason responseReason = this.accountService.editAccount(id, decoded, decodedGroup);
|
||||
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("/accounts/editAccount returns with %s", responseReason.name()));
|
||||
}
|
||||
|
||||
return responseReason.toResponseEntity();
|
||||
}
|
||||
|
||||
@RequestMapping("closeAccount")
|
||||
public ResponseEntity closeAccount(String key) {
|
||||
final String decoded = ControllerUtil.urlDecode(key);
|
||||
|
||||
@@ -72,12 +72,12 @@ public class AccountService {
|
||||
*
|
||||
* @param key the key of the new account
|
||||
* @param type the type of the new account. Must be one of {@link AccountType}.
|
||||
* @param accountGroupName the name of the account group to use, can be <code>null</code>
|
||||
* @param accountGroupName the name of the account group to use
|
||||
*
|
||||
* @return {@link ResponseReason#INVALID_ACCOUNT_TYPE} if the given type is not a valid {@link AccountType}, {@link
|
||||
* ResponseReason#UNKNOWN_ERROR} if an unexpected error occurs, {@link ResponseReason#OK} if the operation completed
|
||||
* successfully, {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key already exists and
|
||||
* {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the optional parameter
|
||||
* {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the parameter
|
||||
* <code>accountGroupName</code> does not identify a valid account group. Never returns <code>null</code>.
|
||||
*/
|
||||
@Transactional(propagation = Propagation.SUPPORTS)
|
||||
@@ -87,17 +87,14 @@ public class AccountService {
|
||||
}
|
||||
|
||||
final Account account = new Account();
|
||||
final AccountGroup accountGroup = this.accountGroupService.getAccountGroupByName(accountGroupName);
|
||||
|
||||
if (StringUtils.isNotEmpty(accountGroupName)) {
|
||||
final AccountGroup accountGroup = this.accountGroupService.getAccountGroupByName(accountGroupName);
|
||||
|
||||
if (accountGroup == null) {
|
||||
return ResponseReason.ACCOUNT_GROUP_NOT_FOUND; // early return
|
||||
}
|
||||
|
||||
account.setAccountGroup(accountGroup);
|
||||
if (accountGroup == null) {
|
||||
return ResponseReason.ACCOUNT_GROUP_NOT_FOUND; // early return
|
||||
}
|
||||
|
||||
account.setAccountGroup(accountGroup);
|
||||
|
||||
account.setKey(key);
|
||||
account.setType(AccountType.valueOf(type));
|
||||
// If we create an account it's implicitly open
|
||||
@@ -120,6 +117,51 @@ public class AccountService {
|
||||
return ResponseReason.OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method edits the account with the given id.
|
||||
*
|
||||
* @param id the id of the account to edit
|
||||
* @param key the new key of the account
|
||||
* @param accountGroupName the new name of the account group to use
|
||||
*
|
||||
* @return {@link ResponseReason#OK} if the operation completed successfully, {@link ResponseReason#UNKNOWN_ERROR}
|
||||
* if an unexpected error occurs, {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key
|
||||
* already exists and {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the parameter
|
||||
* <code>accountGroupName</code> does not identify a valid account group or {@link ResponseReason#ACCOUNT_NOT_FOUND}
|
||||
* if the given id does not identify a valid account. Never returns <code>null</code>.
|
||||
*/
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public ResponseReason editAccount(Long id, String key, String accountGroupName) {
|
||||
final Account account = this.accountRepository.findById(id).orElse(null);
|
||||
|
||||
if(account == null) {
|
||||
return ResponseReason.ACCOUNT_NOT_FOUND;
|
||||
}
|
||||
|
||||
final AccountGroup accountGroup = this.accountGroupService.getAccountGroupByName(accountGroupName);
|
||||
|
||||
if (accountGroup == null) {
|
||||
return ResponseReason.ACCOUNT_GROUP_NOT_FOUND;
|
||||
}
|
||||
|
||||
account.setKey(key);
|
||||
account.setAccountGroup(accountGroup);
|
||||
|
||||
try {
|
||||
this.accountRepository.save(account);
|
||||
} catch (DataIntegrityViolationException dive) {
|
||||
LOGGER.error(String.format("Duplicate key! %s|%s", key, accountGroupName), dive);
|
||||
|
||||
return ResponseReason.DUPLICATE_ACCOUNT_KEY;
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(String.format("Could not save account %s|%s", key, accountGroupName), e);
|
||||
|
||||
return ResponseReason.UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
return ResponseReason.OK;
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public ResponseReason closeAccount(String key) {
|
||||
return setAccountStatus(key, AccountStatus.CLOSED);
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.financer.service;
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.dba.AccountRepository;
|
||||
import de.financer.model.Account;
|
||||
import de.financer.model.AccountGroup;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -40,9 +41,11 @@ public class AccountService_createAccountTest {
|
||||
public void test_createAccount_UNKNOWN_ERROR() {
|
||||
// Arrange
|
||||
Mockito.doThrow(new NullPointerException()).when(this.accountRepository).save(Mockito.any(Account.class));
|
||||
Mockito.when(this.accountGroupService.getAccountGroupByName(Mockito.anyString()))
|
||||
.thenReturn(Mockito.mock(AccountGroup.class));
|
||||
|
||||
// Act
|
||||
ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", null);
|
||||
ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", "Group1");
|
||||
|
||||
// Assert
|
||||
Assert.assertEquals(ResponseReason.UNKNOWN_ERROR, response);
|
||||
@@ -51,10 +54,11 @@ public class AccountService_createAccountTest {
|
||||
@Test
|
||||
public void test_createAccount_OK() {
|
||||
// Arrange
|
||||
// Nothing to do
|
||||
Mockito.when(this.accountGroupService.getAccountGroupByName(Mockito.anyString()))
|
||||
.thenReturn(Mockito.mock(AccountGroup.class));
|
||||
|
||||
// Act
|
||||
ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", null);
|
||||
ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", "Group1");
|
||||
|
||||
// Assert
|
||||
Assert.assertEquals(ResponseReason.OK, response);
|
||||
@@ -79,9 +83,11 @@ public class AccountService_createAccountTest {
|
||||
public void test_createAccount_DUPLICATE_ACCOUNT_KEY() {
|
||||
// Arrange
|
||||
Mockito.doThrow(new DataIntegrityViolationException("DIVE")).when(this.accountRepository).save(Mockito.any(Account.class));
|
||||
Mockito.when(this.accountGroupService.getAccountGroupByName(Mockito.anyString()))
|
||||
.thenReturn(Mockito.mock(AccountGroup.class));
|
||||
|
||||
// Act
|
||||
ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", null);
|
||||
ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", "Group1");
|
||||
|
||||
// Assert
|
||||
Assert.assertEquals(ResponseReason.DUPLICATE_ACCOUNT_KEY, response);
|
||||
|
||||
Reference in New Issue
Block a user