JobsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
5
import io.swagger.v3.oas.annotations.tags.Tag;
6
import io.swagger.v3.oas.annotations.Operation;
7
import io.swagger.v3.oas.annotations.Parameter;
8
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.data.domain.Page;
11
import org.springframework.data.domain.PageRequest;
12
import org.springframework.data.domain.Sort;
13
import org.springframework.security.access.prepost.PreAuthorize;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RestController;
19
20
21
import edu.ucsb.cs156.happiercows.entities.jobs.Job;
22
import edu.ucsb.cs156.happiercows.jobs.InstructorReportJob;
23
import edu.ucsb.cs156.happiercows.jobs.InstructorReportJobFactory;
24
import edu.ucsb.cs156.happiercows.jobs.InstructorReportJobSingleCommons;
25
import edu.ucsb.cs156.happiercows.jobs.InstructorReportJobSingleCommonsFactory;
26
import edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobFactory;
27
import edu.ucsb.cs156.happiercows.jobs.SetCowHealthJobFactory;
28
import edu.ucsb.cs156.happiercows.jobs.TestJob;
29
import edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobFactory;
30
import edu.ucsb.cs156.happiercows.repositories.jobs.JobsRepository;
31
import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer;
32
import edu.ucsb.cs156.happiercows.services.jobs.JobService;
33
import edu.ucsb.cs156.happiercows.jobs.CommonStatsJob;
34
import edu.ucsb.cs156.happiercows.jobs.CommonStatsJobFactory;
35
36
37
@Tag(name = "Jobs")
38
@RequestMapping("/api/jobs")
39
@RestController
40
public class JobsController extends ApiController {
41
    @Autowired
42
    private JobsRepository jobsRepository;
43
44
    @Autowired
45
    private JobService jobService;
46
47
    @Autowired
48
    ObjectMapper mapper;
49
50
    @Autowired
51
    UpdateCowHealthJobFactory updateCowHealthJobFactory;
52
53
    @Autowired
54
    MilkTheCowsJobFactory milkTheCowsJobFactory;
55
56
    @Autowired
57
    SetCowHealthJobFactory setCowHealthJobFactory;
58
59
    @Autowired
60
    InstructorReportJobFactory instructorReportJobFactory;
61
62
    @Autowired
63
    InstructorReportJobSingleCommonsFactory instructorReportJobSingleCommonsFactory;
64
65
    @Autowired
66
    CommonStatsJobFactory commonStatsJobFactory;
67
68
    @Operation(summary = "List all jobs")
69
    @PreAuthorize("hasRole('ROLE_ADMIN')")
70
    @GetMapping("/all")
71
    public Iterable<Job> allJobs() {
72
        Iterable<Job> jobs = jobsRepository.findAll();
73 1 1. allJobs : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::allJobs → KILLED
        return jobs;
74
    }
75
76
    @Operation(summary = "List all jobs")
77
    @PreAuthorize("hasRole('ROLE_ADMIN')")
78
    @GetMapping("/all/pageable")
79
    public Page<Job> allJobsPaged(
80
         @Parameter(name="page") @RequestParam int page,
81
         @Parameter(name="size") @RequestParam int size
82
    ) {
83
        Page<Job> jobs = jobsRepository.findAll(PageRequest.of(page, size, Sort.by("id").descending()));
84 1 1. allJobsPaged : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::allJobsPaged → KILLED
        return jobs;
85
    }
86
87
    @Operation(summary = "Launch Test Job (click fail if you want to test exception handling)")
88
    @PreAuthorize("hasRole('ROLE_ADMIN')")
89
    @PostMapping("/launch/testjob")
90
    public Job launchTestJob(
91
        @Parameter(name="fail") @RequestParam Boolean fail, 
92
        @Parameter(name="sleepMs") @RequestParam Integer sleepMs
93
    ) {
94
        TestJob testJob = TestJob.builder()
95
        .fail(fail)
96
        .sleepMs(sleepMs)
97
        .build();
98
99 1 1. launchTestJob : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::launchTestJob → KILLED
        return jobService.runAsJob(testJob);
100
    }
101
102
    @Operation(summary = "Launch Job to Milk the Cows (click fail if you want to test exception handling)")
103
    @PreAuthorize("hasRole('ROLE_ADMIN')")
104
    @PostMapping("/launch/milkthecowjob")
105
    public Job launchTestJob(
106
    ) {
107
        JobContextConsumer milkTheCowsJob = milkTheCowsJobFactory.create();
108 1 1. launchTestJob : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::launchTestJob → KILLED
        return jobService.runAsJob(milkTheCowsJob);
109
    }
110
111
    @Operation(summary = "Launch Job to Update Cow Health")
112
    @PreAuthorize("hasRole('ROLE_ADMIN')")
113
    @PostMapping("/launch/updatecowhealth")
114
    public Job updateCowHealth(
115
    ) { 
116
        JobContextConsumer updateCowHealthJob = updateCowHealthJobFactory.create();
117 1 1. updateCowHealth : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::updateCowHealth → KILLED
        return jobService.runAsJob(updateCowHealthJob);
118
    }
119
120
    @Operation(summary = "Launch Job to Set Cow Health")
121
    @PreAuthorize("hasRole('ROLE_ADMIN')")
122
    @PostMapping("/launch/setcowhealth")
123
    public Job setCowHealth( 
124
        @Parameter(name="commonsID") @RequestParam Long commonsID, 
125
        @Parameter(name="health") @RequestParam double health
126
    ) { 
127
        JobContextConsumer setCowHealthJob = setCowHealthJobFactory.create(commonsID, health);
128 1 1. setCowHealth : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::setCowHealth → KILLED
        return jobService.runAsJob(setCowHealthJob);
129
    }
130
131
    @Operation(summary = "Launch Job to Produce Instructor Report")
132
    @PreAuthorize("hasRole('ROLE_ADMIN')")
133
    @PostMapping("/launch/instructorreport")
134
    public Job instructorReport(
135
    ) { 
136
        InstructorReportJob instructorReportJob = (InstructorReportJob) instructorReportJobFactory.create();
137 1 1. instructorReport : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::instructorReport → KILLED
        return jobService.runAsJob(instructorReportJob);
138
    }
139
140
    @Operation(summary = "Launch Job to Produce Instructor Report for a single commons")
141
    @PreAuthorize("hasRole('ROLE_ADMIN')")
142
    @PostMapping("/launch/instructorreportsinglecommons")
143
    public Job instructorReportSingleCommons(
144
         @Parameter(name="commonsId") @RequestParam Long commonsId
145
    ) { 
146
147
        InstructorReportJobSingleCommons instructorReportJobSingleCommons = (InstructorReportJobSingleCommons) instructorReportJobSingleCommonsFactory.create(commonsId);
148 1 1. instructorReportSingleCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::instructorReportSingleCommons → KILLED
        return jobService.runAsJob(instructorReportJobSingleCommons);
149
    }
150
151
    @Operation(summary = "Launch Job to Save Common Stats")
152
    @PreAuthorize("hasRole('ROLE_ADMIN')")
153
    @PostMapping("/launch/commonstats")
154
    public Job commonStats(
155
    ) { 
156
        CommonStatsJob commonStatsJob = (CommonStatsJob) commonStatsJobFactory.create();
157 1 1. commonStats : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::commonStats → KILLED
        return jobService.runAsJob(commonStatsJob);
158
    }
159
}

Mutations

73

1.1
Location : allJobs
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_get_all_jobs()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::allJobs → KILLED

84

1.1
Location : allJobsPaged
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_get_all_jobs_paged()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::allJobsPaged → KILLED

99

1.1
Location : launchTestJob
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_test_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::launchTestJob → KILLED

108

1.1
Location : launchTestJob
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_milk_the_cows_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::launchTestJob → KILLED

117

1.1
Location : updateCowHealth
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_update_cow_health_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::updateCowHealth → KILLED

128

1.1
Location : setCowHealth
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_set_cow_health_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::setCowHealth → KILLED

137

1.1
Location : instructorReport
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_instructor_report_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::instructorReport → KILLED

148

1.1
Location : instructorReportSingleCommons
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_instructor_report_single_commons_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::instructorReportSingleCommons → KILLED

157

1.1
Location : commonStats
Killed by : edu.ucsb.cs156.happiercows.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.JobsControllerTests]/[method:admin_can_launch_common_stats_job()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::commonStats → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3