ReportsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.Profit;
4
import edu.ucsb.cs156.happiercows.entities.Report;
5
import edu.ucsb.cs156.happiercows.entities.ReportLine;
6
import edu.ucsb.cs156.happiercows.entities.UserCommons;
7
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
8
import edu.ucsb.cs156.happiercows.helpers.ReportCSVHelper;
9
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
10
import edu.ucsb.cs156.happiercows.repositories.ProfitRepository;
11
import edu.ucsb.cs156.happiercows.repositories.ReportLineRepository;
12
import edu.ucsb.cs156.happiercows.repositories.ReportRepository;
13
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
14
import io.swagger.v3.oas.annotations.tags.Tag;
15
import io.swagger.v3.oas.annotations.Operation;
16
import io.swagger.v3.oas.annotations.Parameter;
17
import lombok.extern.slf4j.Slf4j;
18
19
import org.springframework.data.domain.Sort.Order;
20
import org.springframework.http.HttpHeaders;
21
import org.springframework.http.MediaType;
22
import org.springframework.http.ResponseEntity;
23
24
import java.io.ByteArrayInputStream;
25
import java.io.IOException;
26
import java.util.List;
27
import java.util.Optional;
28
29
import org.apache.catalina.session.FileStore;
30
import org.springframework.beans.factory.annotation.Autowired;
31
import org.springframework.core.io.InputStreamResource;
32
import org.springframework.core.io.Resource;
33
import org.springframework.data.domain.Sort;
34
import org.springframework.security.access.prepost.PreAuthorize;
35
import org.springframework.web.bind.annotation.GetMapping;
36
import org.springframework.web.bind.annotation.RequestMapping;
37
import org.springframework.web.bind.annotation.RequestParam;
38
import org.springframework.web.bind.annotation.RestController;
39
40
@Tag(name = "Reports")
41
@RequestMapping("/api/reports")
42
@RestController
43
public class ReportsController extends ApiController {
44
45
    @Autowired
46
    CommonsRepository commonsRepository;
47
48
    @Autowired
49
    UserCommonsRepository userCommonsRepository;
50
51
    @Autowired
52
    ReportRepository reportRepository;
53
54
    @Autowired
55
    ReportLineRepository reportLineRepository;
56
57
    @Operation(summary = "Get all report headers")
58
    @PreAuthorize("hasRole('ROLE_ADMIN')")
59
    @GetMapping("")
60
    public Iterable<Report> allReports() {
61
        Iterable<Report> reports = reportRepository.findAll(
62
                Sort.by(List.of(
63
                        new Order(Sort.Direction.ASC, "commonsId"),
64
                        new Order(Sort.Direction.DESC, "id"))));
65 1 1. allReports : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReports → KILLED
        return reports;
66
    }
67
68
    @Operation(summary = "Get report headers for a given report")
69
    @PreAuthorize("hasRole('ROLE_ADMIN')")
70
    @GetMapping("/byReportId")
71
    public Optional<Report> findByReportId(
72
            @Parameter(name = "reportId") @RequestParam Long reportId) {
73
        Optional<Report> reports = reportRepository.findById(reportId);
74 1 1. findByReportId : replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/controllers/ReportsController::findByReportId → KILLED
        return reports;
75
    }
76
77
    @Operation(summary = "Get report headers for a given user commons")
78
    @PreAuthorize("hasRole('ROLE_ADMIN')")
79
    @GetMapping("/headers")
80
    public Iterable<Report> allReportsByCommonsId(
81
            @Parameter(name = "commonsId") @RequestParam Long commonsId) {
82
        Iterable<Report> reports = reportRepository.findAllByCommonsId(commonsId);
83 1 1. allReportsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReportsByCommonsId → KILLED
        return reports;
84
    }
85
86
    @Operation(summary = "Get report lines for a report id")
87
    @PreAuthorize("hasRole('ROLE_ADMIN')")
88
    @GetMapping("/lines")
89
    public Iterable<ReportLine> allLinesByReportId(
90
            @Parameter(name = "reportId") @RequestParam Long reportId) {
91
        Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId);
92 1 1. allLinesByReportId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allLinesByReportId → KILLED
        return reportLines;
93
    }
94
95
    @Operation(summary = "Get report lines for a report id and user commons id")
96
    @PreAuthorize("hasRole('ROLE_ADMIN')")
97
    @GetMapping("/download")
98
    public ResponseEntity<Resource> getLinesCSV(
99
            @Parameter(name = "reportId") @RequestParam Long reportId) throws IOException {
100
101
        Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId);
102
103
        String filename = String.format("report%05d.csv",reportId);
104
105
        ByteArrayInputStream bais = ReportCSVHelper.toCSV(reportLines);
106
        InputStreamResource isr = new InputStreamResource(bais);
107
108 1 1. getLinesCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::getLinesCSV → KILLED
        return ResponseEntity.ok()
109
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
110
                .contentType(MediaType.parseMediaType("application/csv"))
111
                .body(isr);
112
    }
113
114
}

Mutations

65

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 null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReports → KILLED

74

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

83

1.1
Location : allReportsByCommonsId
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 null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReportsByCommonsId → KILLED

92

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 null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allLinesByReportId → KILLED

108

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

Active mutators

Tests examined


Report generated by PIT 1.7.3