GameStatsCSVHelper.java

package edu.ucsb.cs156.happiercows.helpers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import edu.ucsb.cs156.happiercows.entities.GameStats;
import edu.ucsb.cs156.happiercows.utilities.PacificTimeUtils;

/*
 * This code is based on 
 * <a href="https://bezkoder.com/spring-boot-download-csv-file/">https://bezkoder.com/spring-boot-download-csv-file/</a>
 * and provides a way to serve up a CSV file containing information associated
 * with an instructor report.
 */

public class GameStatsCSVHelper {

  private GameStatsCSVHelper() {}

  /**
   * This method is a hack to avoid a pitest issue; it isn't possible to 
   * exclude an individual method call from jacoco coverage, but we can
   * exclude the entire method by name in the pom.xml
   * @param out
   */

  public static void flush_and_close_noPitest(ByteArrayOutputStream out, CSVPrinter csvPrinter) throws IOException {
    csvPrinter.flush();
    csvPrinter.close();
    out.flush();
    out.close();
  }
  
  public static ByteArrayInputStream toCSV(Iterable<GameStats> stats) throws IOException {
    final CSVFormat format = CSVFormat.DEFAULT;

    List<String> headers = Arrays.asList(
        "id",
        "gameId",
        "numCows",
        "avgHealth",
        "effectiveCapacity",
        "numFarmers",
        "capacityPerUser",
        "carryingCapacity",
        "createDate");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format);

    csvPrinter.printRecord(headers);
    DateTimeFormatter formatter =
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(PacificTimeUtils.ZONE);
    for (GameStats line : stats) {
      List<String> data = Arrays.asList(
          String.valueOf(line.getId()),
          String.valueOf(line.getGameId()),
          String.valueOf(line.getNumCows()),
          String.valueOf(line.getAvgHealth()),
          String.valueOf(line.getEffectiveCapacity()),
          String.valueOf(line.getNumFarmers()),
          String.valueOf(line.getCapacityPerUser()),
          String.valueOf(line.getCarryingCapacity()),
          formatter.format(line.getCreateDate()));
      csvPrinter.printRecord(data);
    }

    flush_and_close_noPitest(out, csvPrinter);
    return new ByteArrayInputStream(out.toByteArray());
  }
}