ReportsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.Report;
4
import edu.ucsb.cs156.happiercows.entities.ReportLine;
5
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.happiercows.helpers.ReportCSVHelper;
7
import edu.ucsb.cs156.happiercows.repositories.GameRepository;
8
import edu.ucsb.cs156.happiercows.repositories.ReportLineRepository;
9
import edu.ucsb.cs156.happiercows.repositories.ReportRepository;
10
import edu.ucsb.cs156.happiercows.repositories.FarmerRepository;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import io.swagger.v3.oas.annotations.Operation;
13
import io.swagger.v3.oas.annotations.Parameter;
14
15
import org.springframework.data.domain.Sort.Order;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.MediaType;
18
import org.springframework.http.ResponseEntity;
19
20
import java.io.ByteArrayInputStream;
21
import java.io.IOException;
22
import java.util.List;
23
import java.util.Optional;
24
25
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.core.io.InputStreamResource;
27
import org.springframework.core.io.Resource;
28
import org.springframework.data.domain.Sort;
29
import org.springframework.security.access.prepost.PreAuthorize;
30
import org.springframework.transaction.annotation.Transactional;
31
import org.springframework.web.bind.annotation.DeleteMapping;
32
import org.springframework.web.bind.annotation.GetMapping;
33
import org.springframework.web.bind.annotation.RequestMapping;
34
import org.springframework.web.bind.annotation.RequestParam;
35
import org.springframework.web.bind.annotation.RestController;
36
37
@Tag(name = "Reports")
38
@RequestMapping("/api/reports")
39
@RestController
40
public class ReportsController extends ApiController {
41
42
    @Autowired
43
    GameRepository gameRepository;
44
45
    @Autowired
46
    FarmerRepository farmerRepository;
47
48
    @Autowired
49
    ReportRepository reportRepository;
50
51
    @Autowired
52
    ReportLineRepository reportLineRepository;
53
54
    @Operation(summary = "Get all report headers")
55
    @PreAuthorize("hasRole('ROLE_ADMIN')")
56
    @GetMapping("")
57
    public Iterable<Report> allReports() {
58
        Iterable<Report> reports = reportRepository.findAll(
59
                Sort.by(List.of(
60
                        new Order(Sort.Direction.ASC, "gameId"),
61
                        new Order(Sort.Direction.DESC, "id"))));
62 1 1. allReports : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReports → KILLED
        return reports;
63
    }
64
65
    @Operation(summary = "Get report headers for a given report")
66
    @PreAuthorize("hasRole('ROLE_ADMIN')")
67
    @GetMapping("/byReportId")
68
    public Optional<Report> findByReportId(
69
            @Parameter(name = "reportId") @RequestParam Long reportId) {
70
        Optional<Report> reports = reportRepository.findById(reportId);
71 1 1. findByReportId : replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/controllers/ReportsController::findByReportId → KILLED
        return reports;
72
    }
73
74
    @Operation(summary = "Get report headers for a given user game")
75
    @PreAuthorize("hasRole('ROLE_ADMIN')")
76
    @GetMapping("/headers")
77
    public Iterable<Report> allReportsByGameId(
78
            @Parameter(name = "gameId") @RequestParam Long gameId) {
79
        Iterable<Report> reports = reportRepository.findAllByGameId(gameId);
80 1 1. allReportsByGameId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReportsByGameId → KILLED
        return reports;
81
    }
82
83
    @Operation(summary = "Get report lines for a report id")
84
    @PreAuthorize("hasRole('ROLE_ADMIN')")
85
    @GetMapping("/lines")
86
    public Iterable<ReportLine> allLinesByReportId(
87
            @Parameter(name = "reportId") @RequestParam Long reportId) {
88
        Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId);
89 1 1. allLinesByReportId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ReportsController::allLinesByReportId → KILLED
        return reportLines;
90
    }
91
92
    @Operation(summary = "Get report lines for a report id and user game id")
93
    @PreAuthorize("hasRole('ROLE_ADMIN')")
94
    @GetMapping("/download")
95
    public ResponseEntity<Resource> getLinesCSV(
96
            @Parameter(name = "reportId") @RequestParam Long reportId) throws IOException {
97
98
        Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId);
99
100
        String filename = String.format("report%05d.csv",reportId);
101
102
        ByteArrayInputStream bais = ReportCSVHelper.toCSV(reportLines);
103
        InputStreamResource isr = new InputStreamResource(bais);
104
105 1 1. getLinesCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::getLinesCSV → KILLED
        return ResponseEntity.ok()
106
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
107
                .contentType(MediaType.parseMediaType("application/csv"))
108
                .body(isr);
109
    }
110
111
    // Spring Data's derived deleteAllByReportId query (unlike save()/delete())
112
    // requires an active transaction to run; without @Transactional here this
113
    // throws TransactionRequiredException, since open-in-view is disabled and
114
    // each repository call would otherwise get its own short-lived context.
115
    @Operation(summary = "Delete a report, along with all of its detail rows")
116
    @PreAuthorize("hasRole('ROLE_ADMIN')")
117
    @DeleteMapping("")
118
    @Transactional
119
    public Object deleteReport(
120
            @Parameter(name = "reportId") @RequestParam Long reportId) {
121
122
        Report report = reportRepository.findById(reportId)
123 1 1. lambda$deleteReport$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::lambda$deleteReport$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Report.class, reportId));
124
125 1 1. deleteReport : removed call to edu/ucsb/cs156/happiercows/repositories/ReportLineRepository::deleteAllByReportId → KILLED
        reportLineRepository.deleteAllByReportId(reportId);
126 1 1. deleteReport : removed call to edu/ucsb/cs156/happiercows/repositories/ReportRepository::delete → KILLED
        reportRepository.delete(report);
127
128 1 1. deleteReport : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::deleteReport → KILLED
        return genericMessage("Report with id %s deleted".formatted(reportId));
129
    }
130
131
    // "Older" means an earlier createDate than the given report - not a lower
132
    // id, and not scoped to the given report's game. See the comment on
133
    // deleteReport above re: why @Transactional is required.
134
    @Operation(summary = "Purge all reports (and their detail rows) with an earlier creation date than a given report")
135
    @PreAuthorize("hasRole('ROLE_ADMIN')")
136
    @DeleteMapping("/purge")
137
    @Transactional
138
    public Object purgeOlderReports(
139
            @Parameter(name = "reportId") @RequestParam Long reportId) {
140
141
        Report report = reportRepository.findById(reportId)
142 1 1. lambda$purgeOlderReports$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::lambda$purgeOlderReports$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Report.class, reportId));
143
144
        Iterable<Report> olderReports = reportRepository.findAllByCreateDateLessThan(report.getCreateDate());
145
146
        int count = 0;
147
        for (Report olderReport : olderReports) {
148 1 1. purgeOlderReports : removed call to edu/ucsb/cs156/happiercows/repositories/ReportLineRepository::deleteAllByReportId → KILLED
            reportLineRepository.deleteAllByReportId(olderReport.getId());
149 1 1. purgeOlderReports : removed call to edu/ucsb/cs156/happiercows/repositories/ReportRepository::delete → KILLED
            reportRepository.delete(olderReport);
150 1 1. purgeOlderReports : Changed increment from 1 to -1 → KILLED
            count++;
151
        }
152
153 1 1. purgeOlderReports : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::purgeOlderReports → KILLED
        return genericMessage("Purged %d report(s) older than report with id %s".formatted(count, reportId));
154
    }
155
156
}

Mutations

62

1.1
Location : allReports
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:get_all_report_headers()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReports → KILLED

71

1.1
Location : findByReportId
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:get_specific_report_header()]
replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/controllers/ReportsController::findByReportId → KILLED

80

1.1
Location : allReportsByGameId
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:get_reports_headers_commonId()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReportsByGameId → KILLED

89

1.1
Location : allLinesByReportId
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:get_reports_lines_commonId()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ReportsController::allLinesByReportId → KILLED

105

1.1
Location : getLinesCSV
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:test_get_csv()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::getLinesCSV → KILLED

123

1.1
Location : lambda$deleteReport$0
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_tries_to_delete_non_existant_report_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::lambda$deleteReport$0 → KILLED

125

1.1
Location : deleteReport
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_delete_a_report()]
removed call to edu/ucsb/cs156/happiercows/repositories/ReportLineRepository::deleteAllByReportId → KILLED

126

1.1
Location : deleteReport
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_delete_a_report()]
removed call to edu/ucsb/cs156/happiercows/repositories/ReportRepository::delete → KILLED

128

1.1
Location : deleteReport
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_delete_a_report()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::deleteReport → KILLED

142

1.1
Location : lambda$purgeOlderReports$0
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_tries_to_purge_non_existant_report_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::lambda$purgeOlderReports$0 → KILLED

148

1.1
Location : purgeOlderReports
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_purge_older_reports()]
removed call to edu/ucsb/cs156/happiercows/repositories/ReportLineRepository::deleteAllByReportId → KILLED

149

1.1
Location : purgeOlderReports
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_purge_older_reports()]
removed call to edu/ucsb/cs156/happiercows/repositories/ReportRepository::delete → KILLED

150

1.1
Location : purgeOlderReports
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_purge_older_reports()]
Changed increment from 1 to -1 → KILLED

153

1.1
Location : purgeOlderReports
Killed by : edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ReportsControllerTests]/[method:admin_can_purge_older_reports()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::purgeOlderReports → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1