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