| 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.time.ZoneId; | |
| 8 | import java.time.format.DateTimeFormatter; | |
| 9 | import java.util.Arrays; | |
| 10 | import java.util.List; | |
| 11 | import org.apache.commons.csv.CSVFormat; | |
| 12 | import org.apache.commons.csv.CSVPrinter; | |
| 13 | import edu.ucsb.cs156.happiercows.entities.CommonStats; | |
| 14 | ||
| 15 | /* | |
| 16 | * This code is based on | |
| 17 | * <a href="https://bezkoder.com/spring-boot-download-csv-file/">https://bezkoder.com/spring-boot-download-csv-file/</a> | |
| 18 | * and provides a way to serve up a CSV file containing information associated | |
| 19 | * with an instructor report. | |
| 20 | */ | |
| 21 | ||
| 22 | public class CommonStatsCSVHelper { | |
| 23 | ||
| 24 | private CommonStatsCSVHelper() {} | |
| 25 | ||
| 26 | /** | |
| 27 | * This method is a hack to avoid a pitest issue; it isn't possible to | |
| 28 | * exclude an individual method call from jacoco coverage, but we can | |
| 29 | * exclude the entire method by name in the pom.xml | |
| 30 | * @param out | |
| 31 | */ | |
| 32 | ||
| 33 | public static void flush_and_close_noPitest(ByteArrayOutputStream out, CSVPrinter csvPrinter) throws IOException { | |
| 34 | csvPrinter.flush(); | |
| 35 | csvPrinter.close(); | |
| 36 | out.flush(); | |
| 37 | out.close(); | |
| 38 | } | |
| 39 | | |
| 40 | public static ByteArrayInputStream toCSV(Iterable<CommonStats> stats) throws IOException { | |
| 41 | final CSVFormat format = CSVFormat.DEFAULT; | |
| 42 | ||
| 43 | List<String> headers = Arrays.asList( | |
| 44 | "id", | |
| 45 | "commonsId", | |
| 46 | "numCows", | |
| 47 | "avgHealth", | |
| 48 | "createDate"); | |
| 49 | ||
| 50 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
| 51 | CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); | |
| 52 | ||
| 53 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(headers); |
| 54 | ZoneId pst = ZoneId.of("America/Los_Angeles"); | |
| 55 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(pst); | |
| 56 | for (CommonStats line : stats) { | |
| 57 | List<String> data = Arrays.asList( | |
| 58 | String.valueOf(line.getId()), | |
| 59 | String.valueOf(line.getCommonsId()), | |
| 60 | String.valueOf(line.getNumCows()), | |
| 61 | String.valueOf(line.getAvgHealth()), | |
| 62 | formatter.format(line.getCreateDate())); | |
| 63 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(data); |
| 64 | } | |
| 65 | ||
| 66 |
1
1. toCSV : removed call to edu/ucsb/cs156/happiercows/helpers/CommonStatsCSVHelper::flush_and_close_noPitest → KILLED |
flush_and_close_noPitest(out, csvPrinter); |
| 67 |
1
1. toCSV : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/CommonStatsCSVHelper::toCSV → KILLED |
return new ByteArrayInputStream(out.toByteArray()); |
| 68 | } | |
| 69 | } | |
Mutations | ||
| 53 |
1.1 |
|
| 63 |
1.1 |
|
| 66 |
1.1 |
|
| 67 |
1.1 |