CSVArtifactMapper { public Path writeArtifactsToCsvFile(Collection<Artifact> artifacts) { try (BufferedWriter writer = Files.newBufferedWriter(csvFile); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(COLUMN_NAMES)) ) { for (Artifact artifact : artifacts) { csvPrinter.printRecords(makeCsvRecordsFromArtifact(artifact)); } csvPrinter.flush(); return csvFile.toAbsolutePath(); } catch (IOException e) { LOGGER.error("Error when writing the csv file", e); return null; } } CSVArtifactMapper(Path csvFile, Charset encoding, char delimiter, Path baseDir); Path writeArtifactsToCsvFile(Collection<Artifact> artifacts); Collection<Artifact> createArtifactsList();  }
@Test public void writeReleaseListToCSVFileTest() throws IOException { List<Artifact> artifacts = new ArrayList<>(); artifacts.add(mkArtifact("test", false)); artifacts.add(mkArtifact("test1", false) .addCoordinate(new Coordinate("pkg:maven/test/test1@1.2.3")) .addCoordinate(new Coordinate("pkg:p2/test/test1@1.2.3"))); CSVArtifactMapper csvArtifactMapper = new CSVArtifactMapper(csvFile.toPath(), StandardCharsets.UTF_8, DELIMITER, csvFile.getParentFile().toPath()); csvArtifactMapper.writeArtifactsToCsvFile(artifacts); assertThat(csvFile.exists()).isTrue(); List<CSVRecord> records = parseCsvFile(); assertThat(records.size()).isEqualTo(3); }
@Test public void writeSingleReleaseListToCSVFileTest() throws IOException { List<Artifact> oneArtifact = Collections.singletonList(mkArtifact("test:test", true) .addFact(new ArtifactCPE("cpeId"))); CSVArtifactMapper csvArtifactMapper = new CSVArtifactMapper(csvFile.toPath(), StandardCharsets.UTF_8, DELIMITER, csvFile.getParentFile().toPath()); csvArtifactMapper.writeArtifactsToCsvFile(oneArtifact); assertThat(csvFile.exists()).isTrue(); List<CSVRecord> records = parseCsvFile(); assertThat(records.size()).isEqualTo(1); CSVRecord csvRecord = records.get(0); for (String csvColumn : CSV_COLUMNS) { if (!csvColumn.equals("File Name")) { assertThat(csvRecord.get(csvColumn).isEmpty()).isFalse(); } } }
@Test public void writeEmptyReleaseListToCSVFileTest() throws IOException { List<Artifact> emptyArtifacts = new ArrayList<>(); CSVArtifactMapper csvArtifactMapper = new CSVArtifactMapper(csvFile.toPath(), StandardCharsets.UTF_8, DELIMITER, csvFile.getParentFile().toPath()); csvArtifactMapper.writeArtifactsToCsvFile(emptyArtifacts); assertThat(csvFile.exists()).isTrue(); withParser(csvParser -> { assertThat(csvParser.getHeaderMap().size()).isEqualTo(CSV_COLUMNS.length); assertThat(csvParser.getRecordNumber()).isEqualTo(0); return null; }); }