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 | 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 ReportCSVHelper { | |
23 | ||
24 | private ReportCSVHelper() {} | |
25 | ||
26 | /** | |
27 | * This method is a hack to avoid a jacoco issue; it isn't possible to | |
28 | * exclude an individual method call from jacoco coverage, but we can | |
29 | * exclude the entire method. | |
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<ReportLine> lines) throws IOException { | |
41 | final CSVFormat format = CSVFormat.DEFAULT; | |
42 | ||
43 | List<String> headers = Arrays.asList( | |
44 | "id", | |
45 | "reportId", | |
46 | "userId", | |
47 | "username", | |
48 | "totalWealth", | |
49 | "numOfCows", | |
50 | "avgCowHealth", | |
51 | "cowsBought", | |
52 | "cowsSold", | |
53 | "cowDeaths", | |
54 | "reportDate"); | |
55 | ||
56 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
57 | CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); | |
58 | ||
59 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(headers); |
60 | ||
61 | for (ReportLine line : lines) { | |
62 | List<String> data = Arrays.asList( | |
63 | String.valueOf(line.getId()), | |
64 | String.valueOf(line.getReportId()), | |
65 | String.valueOf(line.getUserId()), | |
66 | line.getUsername(), | |
67 | String.valueOf(line.getTotalWealth()), | |
68 | String.valueOf(line.getNumOfCows()), | |
69 | String.valueOf(line.getAvgCowHealth()), | |
70 | String.valueOf(line.getCowsBought()), | |
71 | String.valueOf(line.getCowsSold()), | |
72 | String.valueOf(line.getCowDeaths()), | |
73 | String.valueOf(line.getCreateDate())); | |
74 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(data); |
75 | } | |
76 | ||
77 |
1
1. toCSV : removed call to edu/ucsb/cs156/happiercows/helpers/ReportCSVHelper::flush_and_close_noPitest → KILLED |
flush_and_close_noPitest(out, csvPrinter); |
78 |
1
1. toCSV : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/ReportCSVHelper::toCSV → KILLED |
return new ByteArrayInputStream(out.toByteArray()); |
79 | } | |
80 | } | |
Mutations | ||
59 |
1.1 |
|
74 |
1.1 |
|
77 |
1.1 |
|
78 |
1.1 |