### Question:
MonitoringStatsWriter { public String convertStatsToCsvString(final List<MonitoringStats> stats) { if (CollectionUtils.isEmpty(stats)) { return StringUtils.EMPTY; } final StringWriter stringWriter = new StringWriter(); final CSVWriter csvWriter = new CSVWriter(stringWriter); final List<String[]> allLines = extractTable(stats); csvWriter.writeAll(allLines); return stringWriter.toString(); }  String convertStatsToCsvString(final List<MonitoringStats> stats);  }### Answer:
@Test public void testMonitoringStatsToCsvConversion() { final List<MonitoringStats> stats = createStatsList(); final MonitoringStatsWriter monitoringStatsWriter = new MonitoringStatsWriter(); final String csvInfo = monitoringStatsWriter.convertStatsToCsvString(stats); final String[] linesOfTables = csvInfo.split("\\n"); Assert.assertEquals(3, linesOfTables.length); final List<String[]> table = Arrays.stream(linesOfTables) .map(line -> line.replaceAll("\"", "")) .map(line -> line.split(",", -1)) .peek(cells -> Assert.assertEquals(COMMON_INFO_SIZE + 2 * DISK_INFOS.size() + 2 * NETWORK_INTERFACES.size(), cells.length) ) .collect(Collectors.toList()); final String[] firstStatEntry = table.get(1); final long totalSpace1 = DISK_INFOS.get(0).totalSpace; final long usedSpace1 = DISK_INFOS.get(0).usedSpace; Assert.assertEquals(totalSpace1, Long.parseLong(firstStatEntry[COMMON_INFO_SIZE])); Assert.assertEquals(0, Double.compare(HUNDRED_PERCENTS * usedSpace1 / totalSpace1, Double.parseDouble(firstStatEntry[COMMON_INFO_SIZE + 1]))); Assert.assertEquals(StringUtils.EMPTY, firstStatEntry[COMMON_INFO_SIZE + 2]); Assert.assertEquals(StringUtils.EMPTY, firstStatEntry[COMMON_INFO_SIZE + 3]); final long inBytes1 = NETWORK_INTERFACES_RX.get(0); final long outBytes1 = NETWORK_INTERFACES_TX.get(0); Assert.assertEquals(inBytes1, Long.parseLong(firstStatEntry[COMMON_INFO_SIZE + 4])); Assert.assertEquals(outBytes1, Long.parseLong(firstStatEntry[COMMON_INFO_SIZE + 5])); Assert.assertEquals(StringUtils.EMPTY, firstStatEntry[COMMON_INFO_SIZE + 6]); Assert.assertEquals(StringUtils.EMPTY, firstStatEntry[COMMON_INFO_SIZE + 7]); final String[] secondStatEntry = table.get(2); Assert.assertEquals(StringUtils.EMPTY, secondStatEntry[COMMON_INFO_SIZE]); Assert.assertEquals(StringUtils.EMPTY, secondStatEntry[COMMON_INFO_SIZE + 1]); final long totalSpace2 = DISK_INFOS.get(1).totalSpace; final long usedSpace2 = DISK_INFOS.get(1).usedSpace; Assert.assertEquals(totalSpace2, Long.parseLong(secondStatEntry[COMMON_INFO_SIZE + 2])); Assert.assertEquals(0, Double.compare(HUNDRED_PERCENTS * usedSpace2 / totalSpace2, Double.parseDouble(secondStatEntry[COMMON_INFO_SIZE + 3]))); Assert.assertEquals(StringUtils.EMPTY, secondStatEntry[COMMON_INFO_SIZE + 4]); Assert.assertEquals(StringUtils.EMPTY, secondStatEntry[COMMON_INFO_SIZE + 5]); final long inBytes2 = NETWORK_INTERFACES_RX.get(1); final long outBytes2 = NETWORK_INTERFACES_TX.get(1); Assert.assertEquals(inBytes2, Long.parseLong(secondStatEntry[COMMON_INFO_SIZE + 6])); Assert.assertEquals(outBytes2, Long.parseLong(secondStatEntry[COMMON_INFO_SIZE + 7])); }
@Test public void testEmptyStatsConversion() { final List<MonitoringStats> stats = Collections.emptyList(); final MonitoringStatsWriter monitoringStatsWriter = new MonitoringStatsWriter(); Assert.assertEquals(StringUtils.EMPTY, monitoringStatsWriter.convertStatsToCsvString(stats)); }