JobService.java

1
package edu.ucsb.cs156.frontiers.services.jobs;
2
3
import edu.ucsb.cs156.frontiers.entities.Course;
4
import edu.ucsb.cs156.frontiers.entities.Job;
5
import edu.ucsb.cs156.frontiers.repositories.JobsRepository;
6
import edu.ucsb.cs156.frontiers.services.CurrentUserService;
7
import lombok.extern.slf4j.Slf4j;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.context.annotation.Lazy;
10
import org.springframework.scheduling.annotation.Async;
11
import org.springframework.stereotype.Service;
12
13
@Service
14
@Slf4j
15
public class JobService {
16
  @Autowired private JobsRepository jobsRepository;
17
18
  @Autowired private CurrentUserService currentUserService;
19
20
  @Autowired private JobContextFactory contextFactory;
21
22
  /*
23
   * This is a self-referential bean to allow for async method calls within the same class.
24
   */
25
  @Lazy @Autowired private JobService self;
26
27
  public Job runAsJob(JobContextConsumer jobFunction) {
28
    String jobName = jobFunction.getClass().getName().replace("edu.ucsb.cs156.frontiers.jobs.", "");
29
30
    Course course = jobFunction.getCourse();
31
32
    Job job =
33
        Job.builder()
34
            .createdBy(currentUserService.getUser())
35
            .status("running")
36
            .jobName(jobName)
37
            .course(course)
38 1 1. runAsJob : negated conditional → KILLED
            .courseName(course != null ? course.getCourseName() : null)
39
            .userEmail(currentUserService.getUser().getEmail())
40
            .build();
41
42
    log.info("Starting job: {}, jobName={}", job.getId(), job.getJobName());
43
44
    jobsRepository.save(job);
45 1 1. runAsJob : removed call to edu/ucsb/cs156/frontiers/services/jobs/JobService::runJobAsync → KILLED
    self.runJobAsync(job, jobFunction);
46
47 1 1. runAsJob : replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::runAsJob → KILLED
    return job;
48
  }
49
50
  @Async
51
  public void runJobAsync(Job job, JobContextConsumer jobFunction) {
52
    JobContext context = contextFactory.createContext(job);
53
54
    try {
55
      jobFunction.accept(context);
56
    } catch (Exception e) {
57 1 1. runJobAsync : removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED
      job.setStatus("error");
58
      context.log(e.getMessage());
59
      return;
60
    }
61
62 1 1. runJobAsync : removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED
    job.setStatus("complete");
63
    jobsRepository.save(job);
64
  }
65
66
  public String getJobLogs(Long jobId) {
67
    Job job =
68
        jobsRepository
69
            .findById(jobId)
70 1 1. lambda$getJobLogs$0 : replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::lambda$getJobLogs$0 → KILLED
            .orElseThrow(() -> new IllegalArgumentException("Job not found"));
71
72
    String log = job.getLog();
73 1 1. getJobLogs : negated conditional → KILLED
    return log != null ? log : "";
74
  }
75
}

Mutations

38

1.1
Location : runAsJob
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsJob_withNullCourse_setsCourseNameToNull()]
negated conditional → KILLED

45

1.1
Location : runAsJob
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsJob_withNullCourse_setsCourseNameToNull()]
removed call to edu/ucsb/cs156/frontiers/services/jobs/JobService::runJobAsync → KILLED

47

1.1
Location : runAsJob
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsJob_withNullCourse_setsCourseNameToNull()]
replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::runAsJob → KILLED

57

1.1
Location : runJobAsync
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsyncJob_handles_error()]
removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED

62

1.1
Location : runJobAsync
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsyncJob_fires_correctly()]
removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED

70

1.1
Location : lambda$getJobLogs$0
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:test_getJobLogs_job_not_found()]
replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::lambda$getJobLogs$0 → KILLED

73

1.1
Location : getJobLogs
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:test_getJobLogs_with_null_log()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0