| 1 | package edu.ucsb.cs156.happiercows.helpers; | |
| 2 | ||
| 3 | import java.io.ByteArrayInputStream; | |
| 4 | import java.io.ByteArrayOutputStream; | |
| 5 | import java.io.IOException; | |
| 6 | import java.io.PrintWriter; | |
| 7 | import java.util.Arrays; | |
| 8 | import java.util.List; | |
| 9 | import org.apache.commons.csv.CSVFormat; | |
| 10 | import org.apache.commons.csv.CSVPrinter; | |
| 11 | import edu.ucsb.cs156.happiercows.entities.ReportLine; | |
| 12 | ||
| 13 | /* | |
| 14 | * This code is based on | |
| 15 | * <a href="https://bezkoder.com/spring-boot-download-csv-file/">https://bezkoder.com/spring-boot-download-csv-file/</a> | |
| 16 | * and provides a way to serve up a CSV file containing information associated | |
| 17 | * with an instructor report. | |
| 18 | */ | |
| 19 | ||
| 20 | public class ReportCSVHelper { | |
| 21 | ||
| 22 | private ReportCSVHelper() {} | |
| 23 | ||
| 24 | /** | |
| 25 | * This method is a hack to avoid a jacoco issue; it isn't possible to | |
| 26 | * exclude an individual method call from jacoco coverage, but we can | |
| 27 | * exclude the entire method. | |
| 28 | * @param out | |
| 29 | */ | |
| 30 | ||
| 31 | public static void flush_and_close_noPitest(ByteArrayOutputStream out, CSVPrinter csvPrinter) throws IOException { | |
| 32 | csvPrinter.flush(); | |
| 33 | csvPrinter.close(); | |
| 34 | out.flush(); | |
| 35 | out.close(); | |
| 36 | } | |
| 37 | | |
| 38 | public static ByteArrayInputStream toCSV(Iterable<ReportLine> lines) throws IOException { | |
| 39 | final CSVFormat format = CSVFormat.DEFAULT; | |
| 40 | ||
| 41 | List<String> headers = Arrays.asList( | |
| 42 | "id", | |
| 43 | "reportId", | |
| 44 | "userId", | |
| 45 | "username", | |
| 46 | "totalWealth", | |
| 47 | "numOfCows", | |
| 48 | "avgCowHealth", | |
| 49 | "cowsBought", | |
| 50 | "cowsSold", | |
| 51 | "cowDeaths", | |
| 52 | "reportDate"); | |
| 53 | ||
| 54 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
| 55 | CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); | |
| 56 | ||
| 57 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(headers); |
| 58 | ||
| 59 | for (ReportLine line : lines) { | |
| 60 | List<String> data = Arrays.asList( | |
| 61 | String.valueOf(line.getId()), | |
| 62 | String.valueOf(line.getReportId()), | |
| 63 | String.valueOf(line.getUserId()), | |
| 64 | line.getUsername(), | |
| 65 | String.valueOf(line.getTotalWealth()), | |
| 66 | String.valueOf(line.getNumOfCows()), | |
| 67 | String.valueOf(line.getAvgCowHealth()), | |
| 68 | String.valueOf(line.getCowsBought()), | |
| 69 | String.valueOf(line.getCowsSold()), | |
| 70 | String.valueOf(line.getCowDeaths()), | |
| 71 | String.valueOf(line.getCreateDate())); | |
| 72 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(data); |
| 73 | } | |
| 74 | ||
| 75 |
1
1. toCSV : removed call to edu/ucsb/cs156/happiercows/helpers/ReportCSVHelper::flush_and_close_noPitest → KILLED |
flush_and_close_noPitest(out, csvPrinter); |
| 76 |
1
1. toCSV : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/ReportCSVHelper::toCSV → KILLED |
return new ByteArrayInputStream(out.toByteArray()); |
| 77 | } | |
| 78 | } | |
Mutations | ||
| 57 |
1.1 |
|
| 72 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |