From 15502ab1b364f0a6edf709336c3b7feb74d936bb Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Thu, 14 Apr 2022 23:06:36 +0200 Subject: [PATCH] #691 - explicitly delete old account icon to avoid remaining references --- .../database/importer/AccountImporter.java | 14 +++++++- .../budgetmaster/services/ImportService.java | 2 +- .../unit/database/ImportServiceTest.java | 35 +++++++++++-------- .../importer/AccountImporterTest.java | 11 ++++-- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/deadlocker8/budgetmaster/database/importer/AccountImporter.java b/src/main/java/de/deadlocker8/budgetmaster/database/importer/AccountImporter.java index db03780d6..4c481fab0 100644 --- a/src/main/java/de/deadlocker8/budgetmaster/database/importer/AccountImporter.java +++ b/src/main/java/de/deadlocker8/budgetmaster/database/importer/AccountImporter.java @@ -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); } diff --git a/src/main/java/de/deadlocker8/budgetmaster/services/ImportService.java b/src/main/java/de/deadlocker8/budgetmaster/services/ImportService.java index b5d2603e2..c498c5981 100644 --- a/src/main/java/de/deadlocker8/budgetmaster/services/ImportService.java +++ b/src/main/java/de/deadlocker8/budgetmaster/services/ImportService.java @@ -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())); diff --git a/src/test/java/de/deadlocker8/budgetmaster/unit/database/ImportServiceTest.java b/src/test/java/de/deadlocker8/budgetmaster/unit/database/ImportServiceTest.java index 97ac3b4e5..aaf3b603f 100644 --- a/src/test/java/de/deadlocker8/budgetmaster/unit/database/ImportServiceTest.java +++ b/src/test/java/de/deadlocker8/budgetmaster/unit/database/ImportServiceTest.java @@ -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, diff --git a/src/test/java/de/deadlocker8/budgetmaster/unit/database/importer/AccountImporterTest.java b/src/test/java/de/deadlocker8/budgetmaster/unit/database/importer/AccountImporterTest.java index 2c63d3493..cc9102538 100644 --- a/src/test/java/de/deadlocker8/budgetmaster/unit/database/importer/AccountImporterTest.java +++ b/src/test/java/de/deadlocker8/budgetmaster/unit/database/importer/AccountImporterTest.java @@ -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 -- GitLab