From c47e07ecc8fe1865a6a24bae886a421dded82d61 Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Fri, 15 Apr 2022 00:16:25 +0200 Subject: [PATCH] #691 - skip already imported accounts: Source accounts are updated from destination accounts. To avoid filtering a already imort account via stream all already imported accounts are removed from the list of accounts to import --- .../database/importer/AccountImporter.java | 13 ++++--- .../importer/AccountImporterTest.java | 39 ++++++++++++++++++- 2 files changed, 45 insertions(+), 7 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 4c481fab0..6ce884c99 100644 --- a/src/main/java/de/deadlocker8/budgetmaster/database/importer/AccountImporter.java +++ b/src/main/java/de/deadlocker8/budgetmaster/database/importer/AccountImporter.java @@ -33,13 +33,15 @@ public class AccountImporter extends ItemImporter<Account> final List<String> collectedErrorMessages = new ArrayList<>(); int numberOfImportedItems = 0; + final List<Account> accountsToUpdate = new ArrayList<>(accounts); + for(AccountMatch accountMatch : accountMatchList.getAccountMatches()) { LOGGER.debug(MessageFormat.format("Importing account {0} -> {1}", accountMatch.getAccountSource().getName(), accountMatch.getAccountDestination().getName())); try { - final Account sourceAccount = accounts.stream() + final Account sourceAccount = accountsToUpdate.stream() .filter(account -> account.getID().equals(accountMatch.getAccountSource().getID())) .findFirst() .orElseThrow(); @@ -51,17 +53,16 @@ public class AccountImporter extends ItemImporter<Account> { 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()); + final Icon existingIcon = destinationAccount.getIconReference(); + destinationAccount.setIconReference(null); + iconRepository.delete(existingIcon); destinationAccount.setIconReference(sourceIcon); repository.save(destinationAccount); } + accountsToUpdate.remove(sourceAccount); sourceAccount.updateFromOtherAccount(destinationAccount); numberOfImportedItems++; } 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 cc9102538..10c6ec4a4 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 @@ -46,7 +46,7 @@ class AccountImporterTest accountRepository.save(destinationAccount); final Account destinationAccount2 = new Account("DestinationAccount 2", AccountType.CUSTOM); - destinationAccount.setID(2); + destinationAccount2.setID(2); accountRepository.save(destinationAccount2); final AccountMatch accountMatch = new AccountMatch(sourceAccount); @@ -98,4 +98,41 @@ class AccountImporterTest assertThat(iconRepository.findAll()).hasSize(1); } + + @Test + void test_skipAlreadyImportedAccounts() + { + final Account sourceAccount = new Account("SourceAccount", AccountType.CUSTOM); + sourceAccount.setID(1); + + final Account sourceAccount2 = new Account("SourceAccount 2", AccountType.CUSTOM); + sourceAccount2.setID(2); + + final Account destinationAccount = new Account("DestinationAccount", AccountType.CUSTOM); + destinationAccount.setID(1); + accountRepository.save(destinationAccount); + + final Account destinationAccount2 = new Account("DestinationAccount 2", AccountType.CUSTOM); + destinationAccount2.setID(2); + accountRepository.save(destinationAccount2); + + final AccountMatch accountMatch = new AccountMatch(sourceAccount); + accountMatch.setAccountDestination(destinationAccount2); + + final AccountMatch accountMatch2 = new AccountMatch(sourceAccount2); + accountMatch2.setAccountDestination(destinationAccount); + final AccountMatchList accountMatchList = new AccountMatchList(List.of(accountMatch, accountMatch2)); + + 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()); + assertThat(resultItem).isEqualTo(expected); + assertThat(sourceAccount) + .hasFieldOrPropertyWithValue("ID", destinationAccount2.getID()) + .hasFieldOrPropertyWithValue("name", destinationAccount2.getName()); + assertThat(sourceAccount2) + .hasFieldOrPropertyWithValue("ID", destinationAccount.getID()) + .hasFieldOrPropertyWithValue("name", destinationAccount.getName()); + } } \ No newline at end of file -- GitLab