| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.example.entities.Job; | |
| 6 | import edu.ucsb.cs156.example.errors.EntityNotFoundException; | |
| 7 | import edu.ucsb.cs156.example.jobs.TestJob; | |
| 8 | import edu.ucsb.cs156.example.repositories.JobsRepository; | |
| 9 | import edu.ucsb.cs156.example.services.jobs.JobService; | |
| 10 | import io.swagger.v3.oas.annotations.Operation; | |
| 11 | import io.swagger.v3.oas.annotations.Parameter; | |
| 12 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 13 | import java.util.Map; | |
| 14 | import lombok.extern.slf4j.Slf4j; | |
| 15 | import org.springframework.beans.factory.annotation.Autowired; | |
| 16 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 17 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 18 | import org.springframework.web.bind.annotation.GetMapping; | |
| 19 | import org.springframework.web.bind.annotation.PathVariable; | |
| 20 | import org.springframework.web.bind.annotation.PostMapping; | |
| 21 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 22 | import org.springframework.web.bind.annotation.RequestParam; | |
| 23 | import org.springframework.web.bind.annotation.RestController; | |
| 24 | ||
| 25 | @Tag(name = "Jobs") | |
| 26 | @RequestMapping("/api/jobs") | |
| 27 | @RestController | |
| 28 | @Slf4j | |
| 29 | public class JobsController extends ApiController { | |
| 30 | @Autowired private JobsRepository jobsRepository; | |
| 31 | ||
| 32 | @Autowired private JobService jobService; | |
| 33 | ||
| 34 | @Autowired ObjectMapper mapper; | |
| 35 | ||
| 36 | @Operation(summary = "List all jobs") | |
| 37 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 38 | @GetMapping("/all") | |
| 39 | public Iterable<Job> allJobs() { | |
| 40 | Iterable<Job> jobs = jobsRepository.findAll(); | |
| 41 |
1
1. allJobs : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/JobsController::allJobs → KILLED |
return jobs; |
| 42 | } | |
| 43 | ||
| 44 | @Operation(summary = "Delete all job records") | |
| 45 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 46 | @DeleteMapping("/all") | |
| 47 | public Map<String, String> deleteAllJobs() { | |
| 48 |
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/example/repositories/JobsRepository::deleteAll → KILLED |
jobsRepository.deleteAll(); |
| 49 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/example/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", "All jobs deleted"); |
| 50 | } | |
| 51 | ||
| 52 | @Operation(summary = "Get a specific Job Log by ID if it is in the database") | |
| 53 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 54 | @GetMapping("") | |
| 55 | public Job getJobLogById( | |
| 56 | @Parameter(name = "id", description = "ID of the job") @RequestParam Long id) | |
| 57 | throws JsonProcessingException { | |
| 58 | ||
| 59 | Job job = | |
| 60 |
1
1. lambda$getJobLogById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/JobsController::lambda$getJobLogById$0 → KILLED |
jobsRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(Job.class, id)); |
| 61 | ||
| 62 |
1
1. getJobLogById : replaced return value with null for edu/ucsb/cs156/example/controllers/JobsController::getJobLogById → KILLED |
return job; |
| 63 | } | |
| 64 | ||
| 65 | @Operation(summary = "Delete specific job record") | |
| 66 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 67 | @DeleteMapping("") | |
| 68 | public Map<String, String> deleteAllJobs(@Parameter(name = "id") @RequestParam Long id) { | |
| 69 |
1
1. deleteAllJobs : negated conditional → KILLED |
if (!jobsRepository.existsById(id)) { |
| 70 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/example/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", String.format("Job with id %d not found", id)); |
| 71 | } | |
| 72 |
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/example/repositories/JobsRepository::deleteById → KILLED |
jobsRepository.deleteById(id); |
| 73 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/example/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", String.format("Job with id %d deleted", id)); |
| 74 | } | |
| 75 | ||
| 76 | @Operation(summary = "Launch Test Job (click fail if you want to test exception handling)") | |
| 77 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 78 | @PostMapping("/launch/testjob") | |
| 79 | public Job launchTestJob( | |
| 80 | @Parameter(name = "fail") @RequestParam Boolean fail, | |
| 81 | @Parameter(name = "sleepMs") @RequestParam Integer sleepMs) { | |
| 82 | ||
| 83 | TestJob testJob = TestJob.builder().fail(fail).sleepMs(sleepMs).build(); | |
| 84 |
1
1. launchTestJob : replaced return value with null for edu/ucsb/cs156/example/controllers/JobsController::launchTestJob → KILLED |
return jobService.runAsJob(testJob); |
| 85 | } | |
| 86 | ||
| 87 | @Operation(summary = "Get long job logs") | |
| 88 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 89 | @GetMapping("/logs/{id}") | |
| 90 | public String getJobLogs(@Parameter(name = "id", description = "Job ID") @PathVariable Long id) { | |
| 91 | ||
| 92 |
1
1. getJobLogs : replaced return value with "" for edu/ucsb/cs156/example/controllers/JobsController::getJobLogs → KILLED |
return jobService.getJobLogs(id); |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 48 |
1.1 |
|
| 49 |
1.1 |
|
| 60 |
1.1 |
|
| 62 |
1.1 |
|
| 69 |
1.1 |
|
| 70 |
1.1 |
|
| 72 |
1.1 |
|
| 73 |
1.1 |
|
| 84 |
1.1 |
|
| 92 |
1.1 |