| 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.format.DateTimeFormatter; | |
| 8 | import java.util.Arrays; | |
| 9 | import java.util.List; | |
| 10 | import org.apache.commons.csv.CSVFormat; | |
| 11 | import org.apache.commons.csv.CSVPrinter; | |
| 12 | import edu.ucsb.cs156.happiercows.entities.GameStats; | |
| 13 | import edu.ucsb.cs156.happiercows.utilities.PacificTimeUtils; | |
| 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 GameStatsCSVHelper { | |
| 23 | ||
| 24 | private GameStatsCSVHelper() {} | |
| 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<GameStats> stats) throws IOException { | |
| 41 | final CSVFormat format = CSVFormat.DEFAULT; | |
| 42 | ||
| 43 | List<String> headers = Arrays.asList( | |
| 44 | "id", | |
| 45 | "gameId", | |
| 46 | "numCows", | |
| 47 | "avgHealth", | |
| 48 | "effectiveCapacity", | |
| 49 | "numFarmers", | |
| 50 | "capacityPerUser", | |
| 51 | "carryingCapacity", | |
| 52 | "createDate"); | |
| 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 | DateTimeFormatter formatter = | |
| 59 | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(PacificTimeUtils.ZONE); | |
| 60 | for (GameStats line : stats) { | |
| 61 | List<String> data = Arrays.asList( | |
| 62 | String.valueOf(line.getId()), | |
| 63 | String.valueOf(line.getGameId()), | |
| 64 | String.valueOf(line.getNumCows()), | |
| 65 | String.valueOf(line.getAvgHealth()), | |
| 66 | String.valueOf(line.getEffectiveCapacity()), | |
| 67 | String.valueOf(line.getNumFarmers()), | |
| 68 | String.valueOf(line.getCapacityPerUser()), | |
| 69 | String.valueOf(line.getCarryingCapacity()), | |
| 70 | formatter.format(line.getCreateDate())); | |
| 71 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(data); |
| 72 | } | |
| 73 | ||
| 74 |
1
1. toCSV : removed call to edu/ucsb/cs156/happiercows/helpers/GameStatsCSVHelper::flush_and_close_noPitest → KILLED |
flush_and_close_noPitest(out, csvPrinter); |
| 75 |
1
1. toCSV : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/GameStatsCSVHelper::toCSV → KILLED |
return new ByteArrayInputStream(out.toByteArray()); |
| 76 | } | |
| 77 | } | |
Mutations | ||
| 57 |
1.1 |
|
| 71 |
1.1 |
|
| 74 |
1.1 |
|
| 75 |
1.1 |