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

#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
parent 3055dadb
No related branches found
No related tags found
No related merge requests found
Pipeline #6284 passed
...@@ -33,13 +33,15 @@ public class AccountImporter extends ItemImporter<Account> ...@@ -33,13 +33,15 @@ public class AccountImporter extends ItemImporter<Account>
final List<String> collectedErrorMessages = new ArrayList<>(); final List<String> collectedErrorMessages = new ArrayList<>();
int numberOfImportedItems = 0; int numberOfImportedItems = 0;
final List<Account> accountsToUpdate = new ArrayList<>(accounts);
for(AccountMatch accountMatch : accountMatchList.getAccountMatches()) for(AccountMatch accountMatch : accountMatchList.getAccountMatches())
{ {
LOGGER.debug(MessageFormat.format("Importing account {0} -> {1}", accountMatch.getAccountSource().getName(), accountMatch.getAccountDestination().getName())); LOGGER.debug(MessageFormat.format("Importing account {0} -> {1}", accountMatch.getAccountSource().getName(), accountMatch.getAccountDestination().getName()));
try try
{ {
final Account sourceAccount = accounts.stream() final Account sourceAccount = accountsToUpdate.stream()
.filter(account -> account.getID().equals(accountMatch.getAccountSource().getID())) .filter(account -> account.getID().equals(accountMatch.getAccountSource().getID()))
.findFirst() .findFirst()
.orElseThrow(); .orElseThrow();
...@@ -51,17 +53,16 @@ public class AccountImporter extends ItemImporter<Account> ...@@ -51,17 +53,16 @@ public class AccountImporter extends ItemImporter<Account>
{ {
LOGGER.debug("Overwriting destination account icon"); LOGGER.debug("Overwriting destination account icon");
if(destinationAccount.getIconReference() == null)
{
System.out.println("eimer");
}
// explicitly delete old icon to avoid remaining references // 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); destinationAccount.setIconReference(sourceIcon);
repository.save(destinationAccount); repository.save(destinationAccount);
} }
accountsToUpdate.remove(sourceAccount);
sourceAccount.updateFromOtherAccount(destinationAccount); sourceAccount.updateFromOtherAccount(destinationAccount);
numberOfImportedItems++; numberOfImportedItems++;
} }
......
...@@ -46,7 +46,7 @@ class AccountImporterTest ...@@ -46,7 +46,7 @@ class AccountImporterTest
accountRepository.save(destinationAccount); accountRepository.save(destinationAccount);
final Account destinationAccount2 = new Account("DestinationAccount 2", AccountType.CUSTOM); final Account destinationAccount2 = new Account("DestinationAccount 2", AccountType.CUSTOM);
destinationAccount.setID(2); destinationAccount2.setID(2);
accountRepository.save(destinationAccount2); accountRepository.save(destinationAccount2);
final AccountMatch accountMatch = new AccountMatch(sourceAccount); final AccountMatch accountMatch = new AccountMatch(sourceAccount);
...@@ -98,4 +98,41 @@ class AccountImporterTest ...@@ -98,4 +98,41 @@ class AccountImporterTest
assertThat(iconRepository.findAll()).hasSize(1); 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment