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.CommonStats; | |
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 CommonStatsCSVHelper { | |
21 | ||
22 | public CommonStatsCSVHelper() {} | |
23 | ||
24 | /** | |
25 | * This method is a hack to avoid a pitest issue; it isn't possible to | |
26 | * exclude an individual method call from jacoco coverage, but we can | |
27 | * exclude the entire method by name in the pom.xml | |
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<CommonStats> stats) throws IOException { | |
39 | final CSVFormat format = CSVFormat.DEFAULT; | |
40 | ||
41 | List<String> headers = Arrays.asList( | |
42 | "id", | |
43 | "commonsId", | |
44 | "numCows", | |
45 | "avgHealth", | |
46 | "createDate"); | |
47 | ||
48 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
49 | CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); | |
50 | ||
51 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(headers); |
52 | ||
53 | for (CommonStats line : stats) { | |
54 | List<String> data = Arrays.asList( | |
55 | String.valueOf(line.getId()), | |
56 | String.valueOf(line.getCommonsId()), | |
57 | String.valueOf(line.getNumCows()), | |
58 | String.valueOf(line.getAvgHealth()), | |
59 | String.valueOf(line.getCreateDate())); | |
60 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(data); |
61 | } | |
62 | ||
63 |
1
1. toCSV : removed call to edu/ucsb/cs156/happiercows/helpers/CommonStatsCSVHelper::flush_and_close_noPitest → KILLED |
flush_and_close_noPitest(out, csvPrinter); |
64 |
1
1. toCSV : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/CommonStatsCSVHelper::toCSV → KILLED |
return new ByteArrayInputStream(out.toByteArray()); |
65 | } | |
66 | } | |
Mutations | ||
51 |
1.1 |
|
60 |
1.1 |
|
63 |
1.1 |
|
64 |
1.1 |