Skip to content

Commit

Permalink
#764 - added new property "endDate" for class account
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlocker8 committed Jun 23, 2024
1 parent 27aad14 commit f036f2c
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -48,7 +50,11 @@ public class Account implements ProvidesID, Iconizable
@Expose
private String description;

public Account(String name, String description, AccountType type, Icon iconReference)
@DateTimeFormat(pattern = "dd.MM.yyyy")
@Expose
private LocalDate endDate;

public Account(String name, String description, AccountType type, Icon iconReference, LocalDate endDate)
{
this.name = name;
this.description = description;
Expand All @@ -57,11 +63,12 @@ public Account(String name, String description, AccountType type, Icon iconRefer
this.isDefault = false;
this.accountState = AccountState.FULL_ACCESS;
this.iconReference = iconReference;
this.endDate = endDate;
}

public Account(String name, String description, AccountType type)
public Account(String name, String description, AccountType type, LocalDate endDate)
{
this(name, description, type, null);
this(name, description, type, null, endDate);
}

public Account()
Expand Down Expand Up @@ -148,6 +155,16 @@ public void setDescription(String description)
this.description = description;
}

public LocalDate getEndDate()
{
return endDate;
}

public void setEndDate(LocalDate endDate)
{
this.endDate = endDate;
}

@Override
public Icon getIconReference()
{
Expand Down Expand Up @@ -202,6 +219,7 @@ public String toString()
", type=" + type +
", iconReference=" + iconReference +
", description='" + description + '\'' +
", endDate=" + endDate +
'}';
}

Expand All @@ -218,12 +236,13 @@ public boolean equals(Object o)
accountState == account.accountState &&
Objects.equals(iconReference, account.iconReference) &&
type == account.type &&
Objects.equals(description, account.description);
Objects.equals(description, account.description) &&
Objects.equals(endDate, account.endDate);
}

@Override
public int hashCode()
{
return Objects.hash(ID, name, description, isSelected, isDefault, accountState, iconReference, type);
return Objects.hash(ID, name, description, isSelected, isDefault, accountState, iconReference, type, endDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public void createDefaults()
{
if(accountRepository.findAll().isEmpty())
{
Account placeholder = new Account("Placeholder", "", AccountType.ALL);
Account placeholder = new Account("Placeholder", "", AccountType.ALL, null);
final Icon newIcon = iconService.createIconReference(null, PLACEHOLDER_ICON, null);
iconService.getRepository().save(newIcon);
placeholder.setIconReference(newIcon);
accountRepository.save(placeholder);
LOGGER.debug("Created placeholder account");

Account account = accountRepository.save(new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM));
Account account = accountRepository.save(new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM, null));
final Icon iconDefaultAccount = iconService.createIconReference(null, null, null);
iconService.getRepository().save(iconDefaultAccount);
account.setIconReference(iconDefaultAccount);
Expand Down Expand Up @@ -287,6 +287,7 @@ public void updateExistingAccount(Account newAccount)
existingAccount.setIconReference(newAccount.getIconReference());
existingAccount.setType(AccountType.CUSTOM);
existingAccount.setAccountState(newAccount.getAccountState());
existingAccount.setEndDate(newAccount.getEndDate());
accountRepository.save(existingAccount);

if(existingAccount.isDefault() && existingAccount.getAccountState() != AccountState.FULL_ACCESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ private void createDemoAccounts()
{
final Icon icon = iconService.createIconReference(null, null, null);
iconService.getRepository().save(icon);
final Account oldAccount = new Account("Old account", "", AccountType.CUSTOM, icon);
final Account oldAccount = new Account("Old account", "", AccountType.CUSTOM, icon, null);
oldAccount.setAccountState(AccountState.HIDDEN);
accountService.getRepository().save(oldAccount);

final Icon icon2 = iconService.createIconReference(null, null, null);
iconService.getRepository().save(icon2);
final Account readOnlyAccount = new Account("Read-only account", "", AccountType.CUSTOM, icon2);
final Account readOnlyAccount = new Account("Read-only account", "", AccountType.CUSTOM, icon2, null);
readOnlyAccount.setAccountState(AccountState.READ_ONLY);
accountService.getRepository().save(readOnlyAccount);

final Icon icon3 = iconService.createIconReference(null, "fas fa-piggy-bank", null);
iconService.getRepository().save(icon3);
final Account savingsBankAccount = new Account("Savings Bank", "", AccountType.CUSTOM, icon3);
final Account savingsBankAccount = new Account("Savings Bank", "", AccountType.CUSTOM, icon3, null);
accountService.getRepository().save(savingsBankAccount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void properties(DynamicPropertyRegistry registry)
@BeforeEach
void beforeEach()
{
final Account accountWithoutIconReference = new Account("My Account", "", AccountType.CUSTOM);
final Account accountWithoutIconReference = new Account("My Account", "", AccountType.CUSTOM, null);
accountRepository.save(accountWithoutIconReference);

final Category categoryWithoutIconReference = new Category("Car", "#ffcc00", CategoryType.CUSTOM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ class AccountServiceTest
@BeforeEach
void beforeEach()
{
ACCOUNT_DEFAULT = new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM);
ACCOUNT_DEFAULT = new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM, null);

ACCOUNT_PLACEHOLDER = new Account("Placeholder", "", AccountType.ALL);
ACCOUNT_PLACEHOLDER = new Account("Placeholder", "", AccountType.ALL, null);
ACCOUNT_PLACEHOLDER.setID(1);

ACCOUNT_NORMAL = new Account("Normal account", "awesome description", AccountType.CUSTOM);
ACCOUNT_NORMAL = new Account("Normal account", "awesome description", AccountType.CUSTOM, null);
ACCOUNT_NORMAL.setID(3);

ACCOUNT_READONLY = new Account("Readonly account", "", AccountType.CUSTOM);
ACCOUNT_READONLY = new Account("Readonly account", "", AccountType.CUSTOM, null);
ACCOUNT_READONLY.setAccountState(AccountState.READ_ONLY);

ACCOUNT_HIDDEN = new Account("Hidden account", "", AccountType.CUSTOM);
ACCOUNT_HIDDEN = new Account("Hidden account", "", AccountType.CUSTOM, null);
ACCOUNT_HIDDEN.setAccountState(AccountState.HIDDEN);

ACCOUNT_DEFAULT.setID(15);
ACCOUNT_DEFAULT.setDefault(true);

ICON_PLACEHOLDER = new Icon("fas fa-landmark", null);

Mockito.when(accountRepository.save(new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM))).thenReturn(ACCOUNT_DEFAULT);
Mockito.when(accountRepository.save(new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM, null))).thenReturn(ACCOUNT_DEFAULT);
Mockito.when(accountRepository.findByIsDefault(true)).thenReturn(ACCOUNT_DEFAULT);
Mockito.when(accountRepository.findAllByType(AccountType.ALL)).thenReturn(List.of(ACCOUNT_PLACEHOLDER));
Mockito.when(accountRepository.findById(1)).thenReturn(Optional.of(ACCOUNT_PLACEHOLDER));
Expand All @@ -97,9 +97,9 @@ void test_getAllEntitiesAsc()
accounts.add(ACCOUNT_READONLY);
accounts.add(ACCOUNT_HIDDEN);

final Account accountNormal2 = new Account("normal account", "", AccountType.CUSTOM);
final Account accountNormal2 = new Account("normal account", "", AccountType.CUSTOM, null);
accounts.add(accountNormal2);
final Account accountNormal3 = new Account("123 account", "", AccountType.CUSTOM);
final Account accountNormal3 = new Account("123 account", "", AccountType.CUSTOM, null);
accounts.add(accountNormal3);

Mockito.when(accountRepository.findAllByType(AccountType.ALL)).thenReturn(List.of(ACCOUNT_PLACEHOLDER));
Expand All @@ -115,9 +115,9 @@ void test_getAllActivatedAccountsAsc()
final List<Account> accounts = new ArrayList<>();
accounts.add(ACCOUNT_NORMAL);

final Account accountNormal2 = new Account("normal account", "", AccountType.CUSTOM);
final Account accountNormal2 = new Account("normal account", "", AccountType.CUSTOM, null);
accounts.add(accountNormal2);
final Account accountNormal3 = new Account("123 account", "", AccountType.CUSTOM);
final Account accountNormal3 = new Account("123 account", "", AccountType.CUSTOM, null);
accounts.add(accountNormal3);

Mockito.when(accountRepository.findAllByType(AccountType.ALL)).thenReturn(List.of(ACCOUNT_PLACEHOLDER));
Expand All @@ -133,9 +133,9 @@ void test_getAllReadableAccounts()
final List<Account> accounts = new ArrayList<>();
accounts.add(ACCOUNT_NORMAL);

final Account accountNormal2 = new Account("normal account", "", AccountType.CUSTOM);
final Account accountNormal2 = new Account("normal account", "", AccountType.CUSTOM, null);
accounts.add(accountNormal2);
final Account accountNormal3 = new Account("123 account", "", AccountType.CUSTOM);
final Account accountNormal3 = new Account("123 account", "", AccountType.CUSTOM, null);
accounts.add(accountNormal3);

Mockito.when(accountRepository.findAllByType(AccountType.ALL)).thenReturn(List.of(ACCOUNT_PLACEHOLDER));
Expand All @@ -159,13 +159,13 @@ void test_deleteAccount()
Mockito.verify(templateService, Mockito.atLeast(1)).unsetTemplatesWithAccount(Mockito.any());

// placeholder account is set as selected account
final Account accountSelected = new Account("Placeholder", "", AccountType.ALL);
final Account accountSelected = new Account("Placeholder", "", AccountType.ALL, null);
accountSelected.setSelected(true);
accountSelected.setID(1);
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(accountSelected);

// default account is set another account
final Account newDefault = new Account("Normal account", "awesome description", AccountType.CUSTOM);
final Account newDefault = new Account("Normal account", "awesome description", AccountType.CUSTOM, null);
newDefault.setDefault(true);
newDefault.setID(3);
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(newDefault);
Expand All @@ -177,16 +177,16 @@ void test_createDefaults()
accountService.createDefaults();

// createDefaults() may also be called in constructor so 2 calls are possible
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(new Account("Placeholder", "", AccountType.ALL));
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM));
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(new Account("Placeholder", "", AccountType.ALL, null));
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(new Account(Localization.getString(Strings.ACCOUNT_DEFAULT_NAME), "", AccountType.CUSTOM, null));
}

@Test
void test_selectAccount()
{
accountService.selectAccount(3);

final Account accountSelected = new Account(ACCOUNT_NORMAL.getName(), "awesome description", AccountType.CUSTOM);
final Account accountSelected = new Account(ACCOUNT_NORMAL.getName(), "awesome description", AccountType.CUSTOM, null);
accountSelected.setSelected(true);
accountSelected.setID(ACCOUNT_NORMAL.getID());
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(accountSelected);
Expand All @@ -200,13 +200,13 @@ void test_setAsDefaultAccount()
accountService.setAsDefaultAccount(3);

// old default unset
final Account oldDefault = new Account(ACCOUNT_DEFAULT.getName(), "", AccountType.CUSTOM);
final Account oldDefault = new Account(ACCOUNT_DEFAULT.getName(), "", AccountType.CUSTOM, null);
oldDefault.setDefault(false);
oldDefault.setID(ACCOUNT_DEFAULT.getID());
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(oldDefault);

// new default set
final Account newDefault = new Account(ACCOUNT_NORMAL.getName(), "awesome description", AccountType.CUSTOM);
final Account newDefault = new Account(ACCOUNT_NORMAL.getName(), "awesome description", AccountType.CUSTOM, null);
newDefault.setDefault(true);
newDefault.setID(ACCOUNT_NORMAL.getID());
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(newDefault);
Expand All @@ -215,11 +215,11 @@ void test_setAsDefaultAccount()
@Test
void test_updateExistingAccount()
{
final Account account = new Account("my account", "awesome new description", AccountType.CUSTOM);
final Account account = new Account("my account", "awesome new description", AccountType.CUSTOM, null);
account.setAccountState(AccountState.FULL_ACCESS);
account.setID(22);

final Account existingAccount = new Account("existing account", "awesome description", AccountType.CUSTOM);
final Account existingAccount = new Account("existing account", "awesome description", AccountType.CUSTOM, null);
existingAccount.setAccountState(AccountState.READ_ONLY);
existingAccount.setIconReference(new Icon());
existingAccount.setID(22);
Expand All @@ -228,7 +228,7 @@ void test_updateExistingAccount()

accountService.updateExistingAccount(account);

final Account expected = new Account("my account", "awesome new description", AccountType.CUSTOM);
final Account expected = new Account("my account", "awesome new description", AccountType.CUSTOM, null);
expected.setAccountState(AccountState.FULL_ACCESS);
expected.setID(22);

Expand All @@ -238,12 +238,12 @@ void test_updateExistingAccount()
@Test
void test_updateExistingAccount_isDefaultAccount_noFullAccessAnymore()
{
final Account account = new Account("my account", "", AccountType.CUSTOM);
final Account account = new Account("my account", "", AccountType.CUSTOM, null);
account.setAccountState(AccountState.READ_ONLY);
account.setDefault(true);
account.setID(22);

final Account existingAccount = new Account("existing account", "", AccountType.CUSTOM);
final Account existingAccount = new Account("existing account", "", AccountType.CUSTOM, null);
existingAccount.setAccountState(AccountState.FULL_ACCESS);
existingAccount.setIconReference(new Icon());
existingAccount.setDefault(true);
Expand All @@ -255,14 +255,14 @@ void test_updateExistingAccount_isDefaultAccount_noFullAccessAnymore()
accountService.updateExistingAccount(account);

// account updated
final Account expected = new Account("my account", "", AccountType.CUSTOM);
final Account expected = new Account("my account", "", AccountType.CUSTOM, null);
expected.setAccountState(AccountState.READ_ONLY);
expected.setDefault(true);
expected.setID(22);
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(expected);

// new default set
final Account newDefault = new Account(ACCOUNT_NORMAL.getName(), "awesome description", AccountType.CUSTOM);
final Account newDefault = new Account(ACCOUNT_NORMAL.getName(), "awesome description", AccountType.CUSTOM, null);
newDefault.setDefault(true);
newDefault.setID(ACCOUNT_NORMAL.getID());
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(newDefault);
Expand All @@ -271,12 +271,12 @@ void test_updateExistingAccount_isDefaultAccount_noFullAccessAnymore()
@Test
void test_updateExistingAccount_isSelected_nowHidden()
{
final Account account = new Account("my account", "", AccountType.CUSTOM);
final Account account = new Account("my account", "", AccountType.CUSTOM, null);
account.setAccountState(AccountState.HIDDEN);
account.setSelected(true);
account.setID(22);

final Account existingAccount = new Account("existing account", "", AccountType.CUSTOM);
final Account existingAccount = new Account("existing account", "", AccountType.CUSTOM, null);
existingAccount.setAccountState(AccountState.FULL_ACCESS);
existingAccount.setIconReference(new Icon());
existingAccount.setSelected(true);
Expand All @@ -287,14 +287,14 @@ void test_updateExistingAccount_isSelected_nowHidden()
accountService.updateExistingAccount(account);

// account updated
final Account expected = new Account("my account", "", AccountType.CUSTOM);
final Account expected = new Account("my account", "", AccountType.CUSTOM, null);
expected.setAccountState(AccountState.HIDDEN);
expected.setSelected(true);
expected.setID(22);
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(expected);

// placeholder set as selected account
final Account accountSelected = new Account("Placeholder", "", AccountType.ALL);
final Account accountSelected = new Account("Placeholder", "", AccountType.ALL, null);
accountSelected.setSelected(true);
accountSelected.setID(1);
Mockito.verify(accountRepository, Mockito.atLeast(1)).save(accountSelected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void test_deleteIcon_null()
void test_deleteIcon_withReferringAccount()
{
final Icon icon = Mockito.spy(new Icon("fas fa-icons"));
final Account account = Mockito.spy(new Account("account with icon", "", AccountType.CUSTOM, icon));
final Account account = Mockito.spy(new Account("account with icon", "", AccountType.CUSTOM, icon, null));

Mockito.when(icon.getReferringAccount()).thenReturn(account);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IconizableTest
@Test
void test_updateIcon_noExistingItem_newEmptyIcon()
{
final Account account = Mockito.spy(new Account("account with icon", "", AccountType.CUSTOM));
final Account account = Mockito.spy(new Account("account with icon", "", AccountType.CUSTOM, null));

final Icon icon = new Icon(null, null);

Expand All @@ -52,7 +52,7 @@ void test_updateIcon_noExistingItem_newEmptyIcon()
@Test
void test_updateIcon_noExistingItem_newBuiltinIcon()
{
final Account account = Mockito.spy(new Account("account with icon", "", AccountType.CUSTOM));
final Account account = Mockito.spy(new Account("account with icon", "", AccountType.CUSTOM, null));

final String builtinIdentifier = "fas fa-icons";
final Icon icon = new Icon(builtinIdentifier);
Expand All @@ -75,7 +75,7 @@ void test_updateIcon_existingItem_newBuiltinIcon()
final String builtinIdentifier = "fas fa-icons";
final Icon icon = new Icon(builtinIdentifier);

final Account account = new Account("account with icon", "", AccountType.CUSTOM, icon);
final Account account = new Account("account with icon", "", AccountType.CUSTOM, icon, null);
account.setID(18);
final Account accountSpy = Mockito.spy(account);

Expand All @@ -95,7 +95,7 @@ void test_updateIcon_existingItem_newBuiltinIcon()
void test_updateIcon_existingItem_newEmptyIcon()
{
final Icon icon = new Icon("fas fa-icons");
final Account account = new Account("account with icon", "", AccountType.CUSTOM, icon);
final Account account = new Account("account with icon", "", AccountType.CUSTOM, icon, null);
account.setID(18);
final Account accountSpy = Mockito.spy(account);

Expand Down
Loading

0 comments on commit f036f2c

Please sign in to comment.