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 | import lombok.AccessLevel; | |
13 | import lombok.NoArgsConstructor; | |
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 pitest coverage, but we can | |
29 | * exclude the entire method by name in the pitest settings in pom.xml | |
30 | * @param out stream to close | |
31 | * @param csvPrinter printer to close | |
32 | */ | |
33 | ||
34 | public static void flush_and_close_noPitest(ByteArrayOutputStream out, CSVPrinter csvPrinter) throws IOException { | |
35 | csvPrinter.flush(); | |
36 | csvPrinter.close(); | |
37 | out.flush(); | |
38 | out.close(); | |
39 | } | |
40 | ||
41 | public static ByteArrayInputStream toCSV(Iterable<CommonStats> lines) throws IOException { | |
42 | final CSVFormat format = CSVFormat.DEFAULT; | |
43 | ||
44 | List<String> headers = Arrays.asList( | |
45 | "id", | |
46 | "commonsId", | |
47 | "numCows", | |
48 | "avgHealth", | |
49 | "timestamp"); | |
50 | ||
51 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
52 | CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); | |
53 | ||
54 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(headers); |
55 | ||
56 | for (CommonStats stats : lines) { | |
57 | List<String> data = Arrays.asList( | |
58 | String.valueOf(stats.getId()), | |
59 | String.valueOf(stats.getCommonsId()), | |
60 | String.valueOf(stats.getNumCows()), | |
61 | String.valueOf(stats.getAvgHealth()), | |
62 | String.valueOf(stats.getTimestamp())); | |
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 | ||
54 |
1.1 |
|
63 |
1.1 |
|
66 |
1.1 |
|
67 |
1.1 |