ImportFilesService { public void importFiles(List<String> files, boolean deleteAfterImport) { checkNotNull(files, "Must provide files"); Transactions transactions = transactionsService.retrieve(); for (String path : files) { List<Transaction> fileTransactions = importFile(path); transactions = addTransactions(transactions, fileTransactions); } transactionsService.update(transactions); if (deleteAfterImport) { deleteFiles(files); } } @Inject  ImportFilesService(Window window, TransactionsService transactionsService); List<String> selectedFiles(); void importFiles(List<String> files, boolean deleteAfterImport);  }
@Test public void importFiles() { assertImportFiles(true); }
@Test public void importAvoidsDuplicatesWhenImportingTwoFilesWithTheSameTransactions() { Transaction txn1 = MockTransaction.create("2015-06-12", "description", -1263, "1234123412341234"); Transaction txn2 = MockTransaction.create("2015-06-13", "description 2", -1234, "1234123412341234"); Transactions originalTransactions = new Transactions(ImmutableList.of(txn1)); doReturn(originalTransactions).when(transactionsService).retrieve(); ImportFilesService service = new ImportFilesService(mock(Window.class), transactionsService) { @Override List<Transaction> importFile(String path) { return ImmutableList.of(txn2); } }; service.importFiles(ImmutableList.of("a", "b"), false); ArgumentCaptor<Transactions> transactionsCaptor = ArgumentCaptor.forClass(Transactions.class); verify(transactionsService).update(transactionsCaptor.capture()); Transactions transactions = transactionsCaptor.getValue(); assertTransactions(ImmutableList.of(txn1, txn2), transactions.getTransactions()); }
@Test public void importAvoidsDuplicatesWhenImportingIgnoringDescriptionCase() { Transaction txn1 = MockTransaction.create("2015-06-12", "description", -1263, "1234123412341234"); Transaction txn2 = MockTransaction.create("2015-06-13", "description 2", -1234, "1234123412341234"); Transaction txn3 = MockTransaction.create("2015-06-12", "DESCRiption", -1263, "1234123412341234"); Transactions originalTransactions = new Transactions(ImmutableList.of(txn1, txn2)); doReturn(originalTransactions).when(transactionsService).retrieve(); ImportFilesService service = new ImportFilesService(mock(Window.class), transactionsService) { @Override List<Transaction> importFile(String path) { return ImmutableList.of(txn3); } }; service.importFiles(ImmutableList.of("a"), false); ArgumentCaptor<Transactions> transactionsCaptor = ArgumentCaptor.forClass(Transactions.class); verify(transactionsService).update(transactionsCaptor.capture()); Transactions transactions = transactionsCaptor.getValue(); assertTransactions(ImmutableList.of(txn1, txn2), transactions.getTransactions()); }