CurrentMonthMenuItemsLoader.java

1
package edu.ucsb.cs156.dining.services;
2
3
import com.fasterxml.jackson.databind.JsonNode;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import edu.ucsb.cs156.dining.models.DiningCommons;
6
import edu.ucsb.cs156.dining.models.Entree;
7
import java.time.Duration;
8
import java.time.Instant;
9
import java.time.YearMonth;
10
import java.time.ZonedDateTime;
11
import java.time.format.DateTimeFormatter;
12
import java.time.format.DateTimeParseException;
13
import java.util.List;
14
import lombok.extern.slf4j.Slf4j;
15
import org.springframework.beans.factory.annotation.Value;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.stereotype.Service;
18
import org.springframework.web.client.HttpClientErrorException;
19
20
@Service
21
@Slf4j
22
public class CurrentMonthMenuItemsLoader {
23
24
  private static final int MAX_RATE_LIMIT_RETRIES = 3;
25
  private static final String SAMPLE_API_KEY = "see-instructions-in-readme";
26
27
  private final ObjectMapper objectMapper;
28
  private final DiningCommonsService diningCommonsService;
29
  private final UCSBDiningMenuService diningMenuService;
30
  private final UCSBDiningMenuItemsService diningMenuItemsService;
31
  private final String apiKey;
32
33
  public CurrentMonthMenuItemsLoader(
34
      ObjectMapper objectMapper,
35
      DiningCommonsService diningCommonsService,
36
      UCSBDiningMenuService diningMenuService,
37
      UCSBDiningMenuItemsService diningMenuItemsService,
38
      @Value("${app.ucsb.api.consumer_key}") String apiKey) {
39
    this.objectMapper = objectMapper;
40
    this.diningCommonsService = diningCommonsService;
41
    this.diningMenuService = diningMenuService;
42
    this.diningMenuItemsService = diningMenuItemsService;
43
    this.apiKey = apiKey;
44
  }
45
46
  public void loadCurrentMonth() throws Exception {
47 3 1. loadCurrentMonth : negated conditional → KILLED
2. loadCurrentMonth : negated conditional → KILLED
3. loadCurrentMonth : negated conditional → KILLED
    if (apiKey == null || apiKey.isBlank() || SAMPLE_API_KEY.equalsIgnoreCase(apiKey.trim())) {
48
      log.warn("Skipping current-month menu load because the UCSB API key is not configured");
49
      return;
50
    }
51
52
    List<DiningCommons> diningCommons = callWithRateLimitRetry(diningCommonsService::get);
53
    YearMonth currentMonth = YearMonth.now();
54
55 2 1. loadCurrentMonth : negated conditional → KILLED
2. loadCurrentMonth : changed conditional boundary → KILLED
    for (int day = 1; day <= currentMonth.lengthOfMonth(); day++) {
56
      String date = currentMonth.atDay(day).toString();
57
      for (DiningCommons diningCommon : diningCommons) {
58 1 1. loadCurrentMonth : removed call to edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::loadMenuItems → KILLED
        loadMenuItems(date, diningCommon.getCode());
59
      }
60
    }
61
  }
62
63
  private void loadMenuItems(String date, String diningCommonCode) throws Exception {
64
    String menuJson =
65 1 1. lambda$loadMenuItems$0 : replaced return value with "" for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::lambda$loadMenuItems$0 → KILLED
        callWithRateLimitRetry(() -> diningMenuService.getJSON(date, diningCommonCode));
66 1 1. loadMenuItems : negated conditional → KILLED
    if (menuJson == null) {
67
      return;
68
    }
69
70
    JsonNode meals = objectMapper.readTree(menuJson);
71 1 1. loadMenuItems : negated conditional → KILLED
    if (!meals.isArray()) {
72
      throw new IllegalStateException(
73
          "Expected a JSON array of meals for dining common " + diningCommonCode + " on " + date);
74
    }
75
76
    for (JsonNode meal : meals) {
77
      String mealCode = meal.path("code").asText();
78 1 1. loadMenuItems : negated conditional → KILLED
      if (mealCode.isBlank()) {
79
        log.warn("Skipping meal without a code for dining common {} on {}", diningCommonCode, date);
80
        continue;
81
      }
82
83
      List<Entree> entrees =
84
          callWithRateLimitRetry(
85 1 1. lambda$loadMenuItems$1 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::lambda$loadMenuItems$1 → KILLED
              () -> diningMenuItemsService.get(date, diningCommonCode, mealCode));
86
      diningMenuItemsService.getOrCreateMenuItems(diningCommonCode, mealCode, entrees);
87
    }
88
  }
89
90
  private <T> T callWithRateLimitRetry(ApiCall<T> apiCall) throws Exception {
91 1 1. callWithRateLimitRetry : Changed increment from 1 to -1 → TIMED_OUT
    for (int retry = 0; ; retry++) {
92
      try {
93 1 1. callWithRateLimitRetry : replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::callWithRateLimitRetry → KILLED
        return apiCall.call();
94
      } catch (HttpClientErrorException.TooManyRequests e) {
95 2 1. callWithRateLimitRetry : negated conditional → KILLED
2. callWithRateLimitRetry : changed conditional boundary → KILLED
        if (retry >= MAX_RATE_LIMIT_RETRIES) {
96
          throw e;
97
        }
98
99
        Duration delay = retryDelay(e, retry);
100
        log.warn("UCSB API rate limited the request; retrying in {} seconds", delay.toSeconds());
101
        try {
102 1 1. callWithRateLimitRetry : removed call to java/lang/Thread::sleep → KILLED
          Thread.sleep(delay.toMillis());
103
        } catch (InterruptedException interruptedException) {
104 1 1. callWithRateLimitRetry : removed call to java/lang/Thread::interrupt → KILLED
          Thread.currentThread().interrupt();
105
          throw new IllegalStateException(
106
              "Interrupted while backing off from UCSB API rate limit", interruptedException);
107
        }
108
      }
109
    }
110
  }
111
112
  Duration retryDelay(HttpClientErrorException.TooManyRequests exception, int retry) {
113
    HttpHeaders headers = exception.getResponseHeaders();
114 1 1. retryDelay : negated conditional → KILLED
    String retryAfter = headers == null ? null : headers.getFirst(HttpHeaders.RETRY_AFTER);
115 1 1. retryDelay : negated conditional → KILLED
    if (retryAfter != null) {
116
      try {
117 1 1. retryDelay : replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::retryDelay → KILLED
        return Duration.ofSeconds(Math.max(0, Long.parseLong(retryAfter)));
118
      } catch (NumberFormatException ignored) {
119
        try {
120
          Instant retryAt =
121
              ZonedDateTime.parse(retryAfter, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant();
122
          Duration delay = Duration.between(Instant.now(), retryAt);
123 2 1. retryDelay : replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::retryDelay → KILLED
2. retryDelay : negated conditional → KILLED
          return delay.isNegative() ? Duration.ZERO : delay;
124
        } catch (DateTimeParseException ignoredDate) {
125
          log.warn("Ignoring unrecognized UCSB API Retry-After value");
126
        }
127
      }
128
    }
129
130 2 1. retryDelay : Replaced Shift Left with Shift Right → KILLED
2. retryDelay : replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::retryDelay → KILLED
    return Duration.ofSeconds(1L << retry);
131
  }
132
133
  @FunctionalInterface
134
  private interface ApiCall<T> {
135
    T call() throws Exception;
136
  }
137
}

Mutations

47

1.1
Location : loadCurrentMonth
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doesNotCallUcsbApiWhenApiKeyIsNotConfigured()]
negated conditional → KILLED

2.2
Location : loadCurrentMonth
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doesNotCallUcsbApiWhenApiKeyIsNotConfigured()]
negated conditional → KILLED

3.3
Location : loadCurrentMonth
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doesNotCallUcsbApiWhenApiKeyIsNotConfigured()]
negated conditional → KILLED

55

1.1
Location : loadCurrentMonth
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:rejectsUnexpectedMenuResponseShape()]
negated conditional → KILLED

2.2
Location : loadCurrentMonth
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:loadsAndPersistsAvailableMenuItemsForEveryDayInTheCurrentMonth()]
changed conditional boundary → KILLED

58

1.1
Location : loadCurrentMonth
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:rejectsUnexpectedMenuResponseShape()]
removed call to edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::loadMenuItems → KILLED

65

1.1
Location : lambda$loadMenuItems$0
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:skipsMenuItemsWhenTheDiningCommonIsClosed()]
replaced return value with "" for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::lambda$loadMenuItems$0 → KILLED

66

1.1
Location : loadMenuItems
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:skipsMenuItemsWhenTheDiningCommonIsClosed()]
negated conditional → KILLED

71

1.1
Location : loadMenuItems
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:rejectsUnexpectedMenuResponseShape()]
negated conditional → KILLED

78

1.1
Location : loadMenuItems
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:skipsMealsWithoutACode()]
negated conditional → KILLED

85

1.1
Location : lambda$loadMenuItems$1
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:loadsAndPersistsAvailableMenuItemsForEveryDayInTheCurrentMonth()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::lambda$loadMenuItems$1 → KILLED

91

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

93

1.1
Location : callWithRateLimitRetry
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:acceptsAnExpiredHttpDateInRetryAfter()]
replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::callWithRateLimitRetry → KILLED

95

1.1
Location : callWithRateLimitRetry
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:throwsAfterTheMaximumNumberOfRateLimitRetries()]
negated conditional → KILLED

2.2
Location : callWithRateLimitRetry
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:throwsAfterTheMaximumNumberOfRateLimitRetries()]
changed conditional boundary → KILLED

102

1.1
Location : callWithRateLimitRetry
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:restoresInterruptStatusWhenInterruptedDuringBackoff()]
removed call to java/lang/Thread::sleep → KILLED

104

1.1
Location : callWithRateLimitRetry
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:restoresInterruptStatusWhenInterruptedDuringBackoff()]
removed call to java/lang/Thread::interrupt → KILLED

114

1.1
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doublesExponentialBackoffOnLaterRetries()]
negated conditional → KILLED

115

1.1
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doublesExponentialBackoffOnLaterRetries()]
negated conditional → KILLED

117

1.1
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:throwsAfterTheMaximumNumberOfRateLimitRetries()]
replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::retryDelay → KILLED

123

1.1
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:acceptsAnExpiredHttpDateInRetryAfter()]
replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::retryDelay → KILLED

2.2
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:acceptsAnExpiredHttpDateInRetryAfter()]
negated conditional → KILLED

130

1.1
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doublesExponentialBackoffOnLaterRetries()]
Replaced Shift Left with Shift Right → KILLED

2.2
Location : retryDelay
Killed by : edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.CurrentMonthMenuItemsLoaderTests]/[method:doublesExponentialBackoffOnLaterRetries()]
replaced return value with null for edu/ucsb/cs156/dining/services/CurrentMonthMenuItemsLoader::retryDelay → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1