BalanceFileParser extends FileWatcher { @Override public void parse() { Stopwatch stopwatch = Stopwatch.createStarted(); DateRangeFilter dateRangeFilter = mirrorDateRangePropertiesProcessor.getDateRangeFilter(parserProperties.getStreamType()); try { File balanceFilePath = parserProperties.getValidPath().toFile(); File[] balanceFiles = Objects.requireNonNullElseGet(balanceFilePath.listFiles(), () -> new File[] {}); Arrays.sort(balanceFiles, Collections.reverseOrder()); for (File balanceFile : balanceFiles) { if (ShutdownHelper.isStopping()) { throw new RuntimeException("Process is shutting down"); } parseBalanceFile(balanceFile, dateRangeFilter); } log.info("Completed processing {} balance files in {}", balanceFiles.length, stopwatch); } catch (Exception e) { log.error("Error processing balances files after {}", stopwatch, e); } } BalanceFileParser(BalanceParserProperties parserProperties,
            MirrorDateRangePropertiesProcessor mirrorDateRangePropertiesProcessor,
            AccountBalancesFileLoader accountBalancesFileLoader); @Override void parse();  }
@Test void noFiles() throws Exception { balanceFileParser.parse(); assertParsedFiles(); assertAccountBalances(); }
@Test void discardFiles() throws Exception { insertAccountBalanceFiles(balanceFile1, balanceFile2); fileCopier.copy(); balanceFileParser.parse(); assertValidFiles(); assertParsedFiles(); assertAccountBalances(balanceFile1, balanceFile2); }
@Test void keepFiles() throws Exception { insertAccountBalanceFiles(balanceFile1, balanceFile2); parserProperties.setKeepFiles(true); fileCopier.copy(); balanceFileParser.parse(); assertValidFiles(); assertParsedFiles(balanceFile1, balanceFile2); assertAccountBalances(balanceFile1, balanceFile2); }
@Test void invalidFile() throws Exception { insertAccountBalanceFiles(balanceFile2); fileCopier.copy(); File file1 = fileCopier.getTo().resolve(balanceFile1.getFilename()).toFile(); FileUtils.writeStringToFile(file1, "corrupt", "UTF-8"); balanceFileParser.parse(); assertValidFiles(balanceFile1); assertParsedFiles(); assertAccountBalances(balanceFile2); }
@Test void missingFileInDb() throws Exception { fileCopier.copy(); balanceFileParser.parse(); assertParsedFiles(); assertAccountBalances(); }