JobService.java

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

Mutations

40

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

42

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

52

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

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_fires_correctly()]
removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED

65

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

68

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