BalanceFileReaderImplV2 implements BalanceFileReader { @Override public Stream<AccountBalance> read(File file) { if (file == null) { throw new InvalidDatasetException("Null file provided to balance file reader"); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)), balanceParserProperties.getFileBufferSize()); long consensusTimestamp = parseHeaderForConsensusTimestamp(reader); long shard = balanceParserProperties.getMirrorProperties().getShard(); return reader.lines() .map(line -> { try { return parser.parse(line, consensusTimestamp, shard); } catch (InvalidDatasetException ex) { log.error(ex); return null; } }) .filter(Objects::nonNull) .onClose(() -> { try { reader.close(); } catch (Exception ex) { } }); } catch (IOException ex) { throw new InvalidDatasetException("Error reading account balance file", ex); } }  @Override Stream<AccountBalance> read(File file); boolean isFirstLineFromFileVersion(String firstLine);  }
@Test void readValid() throws IOException { Stream<AccountBalance> accountBalanceStream = balanceFileReader.read(balanceFile); verifySuccess(balanceFile, accountBalanceStream, sampleConsensusTimestamp, 2); }
@Test void readInvalidFileWithLeadingEmptyLine() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); List<String> copy = new LinkedList<>(); copy.add(""); copy.addAll(lines); FileUtils.writeLines(testFile, copy); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileHasNoVersionHeader() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); lines.remove(0); FileUtils.writeLines(testFile, lines); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileHasNoTimestampHeader() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); lines.remove(1); FileUtils.writeLines(testFile, lines); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileHasV1TimestampHeader() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); lines.remove(1); List<String> copy = new LinkedList<>(); copy.add(lines.get(0)); lines.remove(0); copy.add("TimeStamp:2020-09-22T04:25:00.083212003Z"); copy.addAll(lines); FileUtils.writeLines(testFile, copy); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileHasNoHeader() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); lines.remove(0); lines.remove(0); lines.remove(0); FileUtils.writeLines(testFile, lines); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileHasNoColumnHeader() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); lines.remove(2); FileUtils.writeLines(testFile, lines); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileIsEmpty() throws IOException { FileUtils.write(testFile, "", "utf-8"); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileDoesNotExist() { assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readInvalidWhenFileHasMalformedTimestamp() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); lines.remove(1); List<String> copy = new LinkedList<>(); copy.add(lines.get(0)); lines.remove(0); copy.add("# Timestamp:AAAA-08-30T18:15:00.016002001Z"); copy.addAll(lines); FileUtils.writeLines(testFile, copy); assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(testFile); }); }
@Test void readValidWhenFileHasTrailingEmptyLines() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); FileUtils.writeLines(testFile, lines); FileUtils.writeStringToFile(testFile, "\n\n\n", "utf-8", true); Stream<AccountBalance> accountBalanceStream = balanceFileReader.read(testFile); verifySuccess(testFile, accountBalanceStream, sampleConsensusTimestamp, 2); }
@Test void readValidWhenFileHasBadTrailingLines() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); FileUtils.writeLines(testFile, lines); FileUtils.writeStringToFile(testFile, "\n0.0.3.20340\nfoobar\n", "utf-8", true); Stream<AccountBalance> accountBalanceStream = balanceFileReader.read(testFile); verifySuccess(testFile, accountBalanceStream, sampleConsensusTimestamp, 2); }
@Test void readValidWhenFileHasLinesWithDifferentShardNum() throws IOException { List<String> lines = FileUtils.readLines(balanceFile, "utf-8"); FileUtils.writeLines(testFile, lines); long otherShard = mirrorProperties.getShard() + 1; FileUtils.writeStringToFile(testFile, String.format("\n%d,0,3,340\n%d,0,4,340\n", otherShard, otherShard), "utf-8", true); Stream<AccountBalance> accountBalanceStream = balanceFileReader.read(testFile); verifySuccess(testFile, accountBalanceStream, sampleConsensusTimestamp, 2); }
@Test void readNullFile() throws IOException { assertThrows(InvalidDatasetException.class, () -> { balanceFileReader.read(null); }); }