Skip to content
Snippets Groups Projects
Commit 15502ab1 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#691 - explicitly delete old account icon to avoid remaining references

parent 92938a44
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import de.deadlocker8.budgetmaster.accounts.AccountRepository;
import de.deadlocker8.budgetmaster.database.accountmatches.AccountMatch;
import de.deadlocker8.budgetmaster.database.accountmatches.AccountMatchList;
import de.deadlocker8.budgetmaster.icon.Icon;
import de.deadlocker8.budgetmaster.icon.IconRepository;
import de.deadlocker8.budgetmaster.services.EntityType;
import de.deadlocker8.budgetmaster.services.ImportResultItem;
import org.slf4j.Logger;
......@@ -18,9 +19,12 @@ public class AccountImporter extends ItemImporter<Account>
{
private static final Logger LOGGER = LoggerFactory.getLogger(AccountImporter.class);
public AccountImporter(AccountRepository accountRepository)
private final IconRepository iconRepository;
public AccountImporter(AccountRepository accountRepository, IconRepository iconRepository)
{
super(accountRepository, EntityType.ACCOUNT);
this.iconRepository = iconRepository;
}
public ImportResultItem importItems(List<Account> accounts, AccountMatchList accountMatchList)
......@@ -46,6 +50,14 @@ public class AccountImporter extends ItemImporter<Account>
if(sourceIcon != null)
{
LOGGER.debug("Overwriting destination account icon");
if(destinationAccount.getIconReference() == null)
{
System.out.println("eimer");
}
// explicitly delete old icon to avoid remaining references
iconRepository.delete(destinationAccount.getIconReference());
destinationAccount.setIconReference(sourceIcon);
repository.save(destinationAccount);
}
......
......@@ -71,7 +71,7 @@ public class ImportService
importResultItems.add(new ImageImporter(imageRepository).importItems(database.getImages()));
new IconImporter(iconRepository).importItems(database.getIcons());
importResultItems.add(new CategoryImporter(categoryRepository).importItems(database.getCategories()));
importResultItems.add(new AccountImporter(accountRepository).importItems(database.getAccounts(), accountMatchList));
importResultItems.add(new AccountImporter(accountRepository, iconRepository).importItems(database.getAccounts(), accountMatchList));
final TagImporter tagImporter = new TagImporter(tagRepository);
importResultItems.add(new TransactionImporter(transactionRepository, tagImporter).importItems(database.getTransactions()));
......
......@@ -108,13 +108,21 @@ class ImportServiceTest
final Account sourceAccount3 = new Account("Second Account", AccountType.CUSTOM);
sourceAccount3.setID(8);
Icon iconDestAccount2 = new Icon("fas fa-ambulance");
iconDestAccount2 = iconService.getRepository().save(iconDestAccount2);
Icon iconDestAccount3 = new Icon("fas fa-apple");
iconDestAccount3 = iconService.getRepository().save(iconDestAccount3);
// destination accounts
final Account destAccount1 = accountRepository.findByIsDefault(true);
Account destAccount2 = new Account("Destination_Account_1", AccountType.CUSTOM);
destAccount2.setAccountState(AccountState.FULL_ACCESS);
destAccount2.setIconReference(iconDestAccount2);
destAccount2 = accountRepository.save(destAccount2);
Account destAccount3 = new Account("Destination_Account_2", AccountType.CUSTOM);
destAccount3.setAccountState(AccountState.FULL_ACCESS);
destAccount3.setIconReference(iconDestAccount3);
destAccount3 = accountRepository.save(destAccount3);
// account matches
......@@ -145,25 +153,24 @@ class ImportServiceTest
final Icon iconAllAccounts = createIcon(1, "fas fa-landmark", null, null);
final Icon iconCategoryNone = createIcon(3, null, null, null);
final Icon iconCategoryRest = createIcon(4, null, null, null);
final Icon iconAccountReadOnly = createIcon(8, "fas fa-ban", "#2eb952ff", null);
final Icon iconAccountDefault = createIcon(9, null, null, image);
final Icon iconAccountSecond = createIcon(10, null, "#2e79b9ff", null);
final Icon iconCategoryCar = createIcon(11, "fas fa-ambulance", null, null);
final Icon iconCategoryRent = createIcon(12, null, null, image);
final Icon iconTemplateFull = createIcon(13, "fas fa-battery-three-quarters", "#e34f4fff", null);
final Icon iconTemplateUngrouped = createIcon(14, null, "#212121ff", null);
final Icon iconTemplateRandom = createIcon(15, "fas fa-award", "#212121ff", null);
final Icon iconTemplateWithTags = createIcon(16, null, null, image);
final Icon iconAccountReadOnly = createIcon(10, "fas fa-ban", "#2eb952ff", null);
final Icon iconAccountDefault = createIcon(11, null, null, image);
final Icon iconAccountSecond = createIcon(12, null, "#2e79b9ff", null);
final Icon iconCategoryCar = createIcon(13, "fas fa-ambulance", null, null);
final Icon iconCategoryRent = createIcon(14, null, null, image);
final Icon iconTemplateFull = createIcon(15, "fas fa-battery-three-quarters", "#e34f4fff", null);
final Icon iconTemplateUngrouped = createIcon(16, null, "#212121ff", null);
final Icon iconTemplateRandom = createIcon(17, "fas fa-award", "#212121ff", null);
final Icon iconTemplateWithTags = createIcon(18, null, null, image);
assertThat(iconService.getRepository().findAll())
.hasSize(16)
.hasSize(15)
.containsExactlyInAnyOrder(
iconAllAccounts,
createIcon(2, null, null, null),
iconCategoryNone,
iconCategoryRest,
createIcon(5, "fas fa-landmark", null, null),
createIcon(6, null, null, null),
createIcon(7, null, null, null),
createIcon(7, "fas fa-landmark", null, null),
createIcon(8, null, null, null),
createIcon(9, null, null, null),
iconAccountReadOnly,
iconAccountDefault,
iconAccountSecond,
......
......@@ -6,7 +6,6 @@ import de.deadlocker8.budgetmaster.accounts.AccountType;
import de.deadlocker8.budgetmaster.database.accountmatches.AccountMatch;
import de.deadlocker8.budgetmaster.database.accountmatches.AccountMatchList;
import de.deadlocker8.budgetmaster.database.importer.AccountImporter;
import de.deadlocker8.budgetmaster.database.importer.IconImporter;
import de.deadlocker8.budgetmaster.icon.Icon;
import de.deadlocker8.budgetmaster.icon.IconRepository;
import de.deadlocker8.budgetmaster.services.EntityType;
......@@ -57,7 +56,7 @@ class AccountImporterTest
accountMatch2.setAccountDestination(destinationAccount2);
final AccountMatchList accountMatchList = new AccountMatchList(List.of(accountMatch, accountMatch2));
final AccountImporter importer = new AccountImporter(accountRepository);
final AccountImporter importer = new AccountImporter(accountRepository, iconRepository);
final ImportResultItem resultItem = importer.importItems(List.of(sourceAccount, sourceAccount2), accountMatchList);
final ImportResultItem expected = new ImportResultItem(EntityType.ACCOUNT, 2, 2, List.of());
......@@ -76,8 +75,12 @@ class AccountImporterTest
sourceAccount.setID(2);
sourceAccount.setIconReference(icon);
Icon existingIcon = new Icon("abc");
existingIcon = iconRepository.save(existingIcon);
Account destinationAccount = new Account("DestinationAccount", AccountType.CUSTOM);
destinationAccount.setID(1);
destinationAccount.setIconReference(existingIcon);
destinationAccount = accountRepository.save(destinationAccount);
final AccountMatch accountMatch = new AccountMatch(sourceAccount);
......@@ -85,12 +88,14 @@ class AccountImporterTest
final AccountMatchList accountMatchList = new AccountMatchList(List.of(accountMatch));
final AccountImporter importer = new AccountImporter(accountRepository);
final AccountImporter importer = new AccountImporter(accountRepository, iconRepository);
final ImportResultItem resultItem = importer.importItems(List.of(sourceAccount), accountMatchList);
final ImportResultItem expected = new ImportResultItem(EntityType.ACCOUNT, 1, 1, List.of());
assertThat(resultItem).isEqualTo(expected);
assertThat(sourceAccount).hasFieldOrPropertyWithValue("ID", destinationAccount.getID());
assertThat(accountRepository.getById(destinationAccount.getID())).hasFieldOrPropertyWithValue("iconReference", icon);
assertThat(iconRepository.findAll()).hasSize(1);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment