LoadMenuItemsJob.java

1
package edu.ucsb.cs156.dining.jobs;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.JsonNode;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import edu.ucsb.cs156.dining.models.DiningCommons;
7
import edu.ucsb.cs156.dining.models.Entree;
8
import edu.ucsb.cs156.dining.services.DiningCommonsService;
9
import edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsService;
10
import edu.ucsb.cs156.dining.services.UCSBDiningMenuService;
11
import edu.ucsb.cs156.jobs.services.JobContext;
12
import edu.ucsb.cs156.jobs.services.JobContextConsumer;
13
import java.time.LocalDate;
14
import java.util.List;
15
import java.util.concurrent.Callable;
16
import lombok.Builder;
17
import lombok.Getter;
18
import org.springframework.web.client.HttpClientErrorException;
19
import org.springframework.web.client.RestClientException;
20
21
/**
22
 * Downloads the menus for every dining commons and meal on each day in [startDate, endDate] from
23
 * the UCSB Dining API, and saves any MenuItems not already in the database. Backs off exponentially
24
 * when the API responds with 429 Too Many Requests.
25
 */
26
@Builder
27
@Getter
28
public class LoadMenuItemsJob implements JobContextConsumer {
29
30
  /** Abstraction over Thread.sleep so tests don't have to actually wait. */
31
  @FunctionalInterface
32
  public interface Sleeper {
33
    void sleep(long ms) throws InterruptedException;
34
  }
35
36
  private DiningCommonsService diningCommonsService;
37
  private UCSBDiningMenuService menuService;
38
  private UCSBDiningMenuItemsService menuItemsService;
39
  private ObjectMapper objectMapper;
40
  private LocalDate startDate;
41
  private LocalDate endDate;
42
43
  @Builder.Default private Sleeper sleeper = Thread::sleep;
44
  @Builder.Default private int maxRetries = 6;
45
  @Builder.Default private long initialBackoffMs = 1000;
46
47
  @Override
48
  public void accept(JobContext ctx) throws Exception {
49 1 1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
    ctx.log("Loading menu items from " + startDate + " to " + endDate);
50
51 1 1. lambda$accept$0 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$accept$0 → KILLED
    List<DiningCommons> commons = withBackoff(ctx, () -> diningCommonsService.get());
52
53
    int count = 0;
54 1 1. accept : negated conditional → KILLED
    for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
55
      for (DiningCommons dc : commons) {
56 1 1. accept : Replaced integer addition with subtraction → KILLED
        count += loadDay(ctx, date.toString(), dc.getCode());
57
      }
58
    }
59
60 1 1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
    ctx.log("Done loading menu items; " + count + " menu items processed.");
61
  }
62
63
  private int loadDay(JobContext ctx, String date, String commonsCode) throws Exception {
64 1 1. lambda$loadDay$0 : replaced return value with "" for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$loadDay$0 → KILLED
    String mealsJson = withBackoff(ctx, () -> menuService.getJSON(date, commonsCode));
65 1 1. loadDay : negated conditional → KILLED
    if (mealsJson == null) {
66
      // getJSON returns null when the dining commons is closed that day
67
      return 0;
68
    }
69
70
    int count = 0;
71
    for (JsonNode meal : objectMapper.readTree(mealsJson)) {
72
      String mealCode = meal.get("code").asText();
73
      try {
74
        List<Entree> entrees =
75 1 1. lambda$loadDay$1 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$loadDay$1 → KILLED
            withBackoff(ctx, () -> menuItemsService.get(date, commonsCode, mealCode));
76 1 1. loadDay : negated conditional → KILLED
        if (!entrees.isEmpty()) {
77 1 1. loadDay : Replaced integer addition with subtraction → KILLED
          count += menuItemsService.getOrCreateMenuItems(commonsCode, mealCode, entrees).size();
78
        }
79
      } catch (HttpClientErrorException.TooManyRequests e) {
80
        throw e;
81
      } catch (RestClientException | JsonProcessingException e) {
82
        ctx.log("Skipping " + commonsCode + " " + mealCode + " on " + date + ": " + e.getMessage());
83
      }
84
    }
85 1 1. loadDay : replaced int return with 0 for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::loadDay → KILLED
    return count;
86
  }
87
88
  /** Runs call, retrying with exponential backoff if the API responds 429 Too Many Requests. */
89
  <T> T withBackoff(JobContext ctx, Callable<T> call) throws Exception {
90
    long delayMs = initialBackoffMs;
91 1 1. withBackoff : Changed increment from 1 to -1 → TIMED_OUT
    for (int attempt = 0; ; attempt++) {
92
      try {
93 1 1. withBackoff : replaced return value with null for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::withBackoff → KILLED
        return call.call();
94
      } catch (HttpClientErrorException.TooManyRequests e) {
95 2 1. withBackoff : changed conditional boundary → KILLED
2. withBackoff : negated conditional → KILLED
        if (attempt >= maxRetries) {
96 1 1. withBackoff : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
          ctx.log("Still rate limited after " + maxRetries + " retries; giving up.");
97
          throw e;
98
        }
99 1 1. withBackoff : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED
        ctx.log("Rate limited by UCSB API (429); retrying in " + delayMs + " ms");
100 1 1. withBackoff : removed call to edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob$Sleeper::sleep → KILLED
        sleeper.sleep(delayMs);
101 1 1. withBackoff : Replaced long multiplication with division → KILLED
        delayMs *= 2;
102
      }
103
    }
104
  }
105
}

Mutations

49

1.1
Location : accept
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:loads_every_day_commons_and_meal_in_range()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

51

1.1
Location : lambda$accept$0
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$accept$0 → KILLED

54

1.1
Location : accept
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
negated conditional → KILLED

56

1.1
Location : accept
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:retries_with_exponential_backoff_on_429()]
Replaced integer addition with subtraction → KILLED

60

1.1
Location : accept
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:skips_closed_commons_and_empty_meals()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

64

1.1
Location : lambda$loadDay$0
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
replaced return value with "" for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$loadDay$0 → KILLED

65

1.1
Location : loadDay
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
negated conditional → KILLED

75

1.1
Location : lambda$loadDay$1
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:retries_with_exponential_backoff_on_429()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$loadDay$1 → KILLED

76

1.1
Location : loadDay
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:skips_closed_commons_and_empty_meals()]
negated conditional → KILLED

77

1.1
Location : loadDay
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:retries_with_exponential_backoff_on_429()]
Replaced integer addition with subtraction → KILLED

85

1.1
Location : loadDay
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:retries_with_exponential_backoff_on_429()]
replaced int return with 0 for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::loadDay → KILLED

91

1.1
Location : withBackoff
Killed by : none
Changed increment from 1 to -1 → TIMED_OUT

93

1.1
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
replaced return value with null for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::withBackoff → KILLED

95

1.1
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
changed conditional boundary → KILLED

2.2
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
negated conditional → KILLED

96

1.1
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

99

1.1
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:retries_with_exponential_backoff_on_429()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED

100

1.1
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
removed call to edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob$Sleeper::sleep → KILLED

101

1.1
Location : withBackoff
Killed by : edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.jobs.LoadMenuItemsJobTests]/[method:gives_up_after_max_retries_on_429()]
Replaced long multiplication with division → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1