JobsController.java

1
package edu.ucsb.cs156.courses.controllers;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import edu.ucsb.cs156.courses.collections.ConvertedSectionCollection;
5
import edu.ucsb.cs156.courses.entities.Job;
6
import edu.ucsb.cs156.courses.jobs.TestJob;
7
import edu.ucsb.cs156.courses.jobs.UpdateCourseDataJobFactory;
8
import edu.ucsb.cs156.courses.jobs.UploadGradeDataJob;
9
import edu.ucsb.cs156.courses.jobs.UploadGradeDataJobFactory;
10
import edu.ucsb.cs156.courses.repositories.JobsRepository;
11
import edu.ucsb.cs156.courses.services.jobs.JobService;
12
import io.swagger.v3.oas.annotations.Operation;
13
import io.swagger.v3.oas.annotations.Parameter;
14
import io.swagger.v3.oas.annotations.tags.Tag;
15
import java.util.Map;
16
import lombok.extern.slf4j.Slf4j;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.web.bind.annotation.DeleteMapping;
20
import org.springframework.web.bind.annotation.GetMapping;
21
import org.springframework.web.bind.annotation.PostMapping;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RestController;
25
26
@Tag(name = "Jobs")
27
@RequestMapping("/api/jobs")
28
@RestController
29
@Slf4j
30
public class JobsController extends ApiController {
31
  @Autowired private JobsRepository jobsRepository;
32
33
  @Autowired private ConvertedSectionCollection convertedSectionCollection;
34
35
  @Autowired private JobService jobService;
36
37
  @Autowired ObjectMapper mapper;
38
39
  @Autowired UpdateCourseDataJobFactory updateCourseDataJobFactory;
40
41
  @Autowired UploadGradeDataJobFactory updateGradeDataJobFactory;
42
43
  @Operation(summary = "List all jobs")
44
  @PreAuthorize("hasRole('ROLE_ADMIN')")
45
  @GetMapping("/all")
46
  public Iterable<Job> allJobs() {
47
    Iterable<Job> jobs = jobsRepository.findAll();
48 1 1. allJobs : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/JobsController::allJobs → KILLED
    return jobs;
49
  }
50
51
  @Operation(summary = "Delete all job records")
52
  @PreAuthorize("hasRole('ROLE_ADMIN')")
53
  @DeleteMapping("/all")
54
  public Map<String, String> deleteAllJobs() {
55 1 1. deleteAllJobs : removed call to edu/ucsb/cs156/courses/repositories/JobsRepository::deleteAll → KILLED
    jobsRepository.deleteAll();
56 1 1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED
    return Map.of("message", "All jobs deleted");
57
  }
58
59
  @Operation(summary = "Delete specific job record")
60
  @PreAuthorize("hasRole('ROLE_ADMIN')")
61
  @DeleteMapping("")
62
  public Map<String, String> deleteAllJobs(@Parameter(name = "id") @RequestParam Long id) {
63 1 1. deleteAllJobs : negated conditional → KILLED
    if (!jobsRepository.existsById(id)) {
64 1 1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED
      return Map.of("message", String.format("Job with id %d not found", id));
65
    }
66 1 1. deleteAllJobs : removed call to edu/ucsb/cs156/courses/repositories/JobsRepository::deleteById → KILLED
    jobsRepository.deleteById(id);
67 1 1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED
    return Map.of("message", String.format("Job with id %d deleted", id));
68
  }
69
70
  @Operation(summary = "Launch Test Job (click fail if you want to test exception handling)")
71
  @PreAuthorize("hasRole('ROLE_ADMIN')")
72
  @PostMapping("/launch/testjob")
73
  public Job launchTestJob(
74
      @Parameter(name = "fail") @RequestParam Boolean fail,
75
      @Parameter(name = "sleepMs") @RequestParam Integer sleepMs) {
76
77
    TestJob testJob = TestJob.builder().fail(fail).sleepMs(sleepMs).build();
78 1 1. launchTestJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchTestJob → KILLED
    return jobService.runAsJob(testJob);
79
  }
80
81
  @Operation(summary = "Launch Job to Update Course Data")
82
  @PreAuthorize("hasRole('ROLE_ADMIN')")
83
  @PostMapping("/launch/updateCourses")
84
  public Job launchUpdateCourseDataJob(
85
      @Parameter(name = "quarterYYYYQ", description = "quarter (YYYYQ format)") @RequestParam
86
          String quarterYYYYQ,
87
      @Parameter(name = "subjectArea") @RequestParam String subjectArea,
88
      @Parameter(
89
              name = "ifStale",
90
              description = "true if job should only update when data is stale")
91
          @RequestParam(defaultValue = "true")
92
          Boolean ifStale) {
93
94
    log.info(
95
        "launchUpdateCourseDataJob: quarterYYYYQ={}, subjectArea={}, ifStale={}",
96
        quarterYYYYQ,
97
        subjectArea,
98
        ifStale);
99
    var job =
100
        updateCourseDataJobFactory.createForSubjectAndQuarterAndIfStale(
101
            subjectArea, quarterYYYYQ, ifStale);
102
103 1 1. launchUpdateCourseDataJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataJob → KILLED
    return jobService.runAsJob(job);
104
  }
105
106
  @Operation(summary = "Launch Job to Update Course Data using Quarter")
107
  @PreAuthorize("hasRole('ROLE_ADMIN')")
108
  @PostMapping("/launch/updateQuarterCourses")
109
  public Job launchUpdateCourseDataWithQuarterJob(
110
      @Parameter(name = "quarterYYYYQ", description = "quarter (YYYYQ format)") @RequestParam
111
          String quarterYYYYQ,
112
      @Parameter(
113
              name = "ifStale",
114
              description = "true if job should only update when data is stale")
115
          @RequestParam(defaultValue = "true")
116
          Boolean ifStale) {
117
118
    var job = updateCourseDataJobFactory.createForQuarter(quarterYYYYQ);
119
120 1 1. launchUpdateCourseDataWithQuarterJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataWithQuarterJob → KILLED
    return jobService.runAsJob(job);
121
  }
122
123
  @Operation(summary = "Launch Job to Update Course Data for range of quarters")
124
  @PreAuthorize("hasRole('ROLE_ADMIN')")
125
  @PostMapping("/launch/updateCoursesRangeOfQuarters")
126
  public Job launchUpdateCourseDataRangeOfQuartersJob(
127
      @Parameter(name = "start_quarterYYYYQ", description = "start quarter (YYYYQ format)")
128
          @RequestParam
129
          String start_quarterYYYYQ,
130
      @Parameter(name = "end_quarterYYYYQ", description = "end quarter (YYYYQ format)")
131
          @RequestParam
132
          String end_quarterYYYYQ,
133
      @Parameter(
134
              name = "ifStale",
135
              description = "true if job should only update when data is stale")
136
          @RequestParam(defaultValue = "true")
137
          Boolean ifStale) {
138
139
    var job =
140
        updateCourseDataJobFactory.createForQuarterRange(start_quarterYYYYQ, end_quarterYYYYQ);
141
142 1 1. launchUpdateCourseDataRangeOfQuartersJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataRangeOfQuartersJob → KILLED
    return jobService.runAsJob(job);
143
  }
144
145
  @Operation(
146
      summary = "Launch Job to Update Course Data for a range of quarters for a single subject")
147
  @PreAuthorize("hasRole('ROLE_ADMIN')")
148
  @PostMapping("/launch/updateCoursesRangeOfQuartersSingleSubject")
149
  public Job launchUpdateCourseDataRangeOfQuartersSingleSubjectJob(
150
      @Parameter(name = "subjectArea", description = "subject area") @RequestParam
151
          String subjectArea,
152
      @Parameter(name = "start_quarterYYYYQ", description = "start quarter (YYYYQ format)")
153
          @RequestParam
154
          String start_quarterYYYYQ,
155
      @Parameter(name = "end_quarterYYYYQ", description = "end quarter (YYYYQ format)")
156
          @RequestParam
157
          String end_quarterYYYYQ,
158
      @Parameter(
159
              name = "ifStale",
160
              description = "true if job should only update when data is stale")
161
          @RequestParam(defaultValue = "true")
162
          Boolean ifStale) {
163
164
    var job =
165
        updateCourseDataJobFactory.createForSubjectAndQuarterRange(
166
            subjectArea, start_quarterYYYYQ, end_quarterYYYYQ);
167
168 1 1. launchUpdateCourseDataRangeOfQuartersSingleSubjectJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataRangeOfQuartersSingleSubjectJob → KILLED
    return jobService.runAsJob(job);
169
  }
170
171
  @Operation(summary = "Launch Job to update grade history")
172
  @PreAuthorize("hasRole('ROLE_ADMIN')")
173
  @PostMapping("/launch/uploadGradeData")
174
  public Job launchUploadGradeData() {
175
    UploadGradeDataJob updateGradeDataJob = updateGradeDataJobFactory.create();
176 1 1. launchUploadGradeData : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUploadGradeData → KILLED
    return jobService.runAsJob(updateGradeDataJob);
177
  }
178
}

Mutations

48

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

55

1.1
Location : deleteAllJobs
Killed by : edu.ucsb.cs156.courses.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.JobsControllerTests]/[method:admin_can_delete_all_jobs()]
removed call to edu/ucsb/cs156/courses/repositories/JobsRepository::deleteAll → KILLED

56

1.1
Location : deleteAllJobs
Killed by : edu.ucsb.cs156.courses.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.JobsControllerTests]/[method:admin_can_delete_all_jobs()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED

63

1.1
Location : deleteAllJobs
Killed by : edu.ucsb.cs156.courses.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.JobsControllerTests]/[method:admin_gets_reasonable_error_when_deleting_non_existing_job()]
negated conditional → KILLED

64

1.1
Location : deleteAllJobs
Killed by : edu.ucsb.cs156.courses.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.JobsControllerTests]/[method:admin_gets_reasonable_error_when_deleting_non_existing_job()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED

66

1.1
Location : deleteAllJobs
Killed by : edu.ucsb.cs156.courses.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.JobsControllerTests]/[method:admin_can_delete_specific_job()]
removed call to edu/ucsb/cs156/courses/repositories/JobsRepository::deleteById → KILLED

67

1.1
Location : deleteAllJobs
Killed by : edu.ucsb.cs156.courses.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.JobsControllerTests]/[method:admin_can_delete_specific_job()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED

78

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

103

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

120

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

142

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

168

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

176

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

Active mutators

Tests examined


Report generated by PIT 1.17.0