UCSBDiningMenuService.java

1
package edu.ucsb.cs156.dining.services;
2
3
import java.time.Duration;
4
import java.util.Arrays;
5
import lombok.extern.slf4j.Slf4j;
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.boot.web.client.RestTemplateBuilder;
8
import org.springframework.cache.annotation.Cacheable;
9
import org.springframework.http.HttpEntity;
10
import org.springframework.http.HttpHeaders;
11
import org.springframework.http.HttpMethod;
12
import org.springframework.http.HttpStatus;
13
import org.springframework.http.MediaType;
14
import org.springframework.http.ResponseEntity;
15
import org.springframework.stereotype.Service;
16
import org.springframework.web.client.HttpClientErrorException;
17
import org.springframework.web.client.HttpServerErrorException;
18
import org.springframework.web.client.RestTemplate;
19
20
/** Service object that wraps the UCSB Dining Menu API */
21
@Service
22
@Slf4j
23
public class UCSBDiningMenuService {
24
25
  @Value("${app.ucsb.api.consumer_key}")
26
  private String apiKey;
27
28
  @Value("${app.ucsb.api.host}")
29
  private String apiHost;
30
31
  @Value("${app.name}")
32
  private String appName;
33
34
  @Value("${app.hostname}")
35
  private String appHostname;
36
37
  /*
38
   * A RestTemplate built with no timeout blocks its calling thread forever on a hung external
39
   * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app
40
   * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover
41
   * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only
42
   * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous
43
   * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job
44
   * can't hang forever.
45
   */
46
  private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10);
47
  private static final Duration READ_TIMEOUT = Duration.ofSeconds(60);
48
49
  private RestTemplate restTemplate = new RestTemplate();
50
51
  public UCSBDiningMenuService(RestTemplateBuilder restTemplateBuilder) throws Exception {
52
    restTemplate =
53
        restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build();
54
  }
55
56
  public static final String ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT =
57
      "{apiHost}/dining/menu/v1/{date-time}/{dining-common-code}";
58
59
  @Cacheable("menu")
60
  public String getJSON(String dateTime, String diningCommonCode) throws Exception {
61
62
    HttpHeaders headers = new HttpHeaders();
63 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
64 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
65 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-version", "1.0");
66 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-key", this.apiKey);
67 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("X-Requesting-App", this.appName + "." + this.appHostname);
68
69
    HttpEntity<String> entity = new HttpEntity<>("body", headers);
70
71
    String url = ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT;
72
    url = url.replace("{apiHost}", apiHost);
73
    url = url.replace("{date-time}", dateTime);
74
    url = url.replace("{dining-common-code}", diningCommonCode);
75
76
    log.info("url=" + url);
77
78
    String retVal = "";
79
    MediaType contentType = null;
80
    HttpStatus statusCode = null;
81
82
    try {
83
      ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
84
      contentType = re.getHeaders().getContentType();
85
      statusCode = (HttpStatus) re.getStatusCode();
86
      retVal = re.getBody();
87
88
      log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode);
89
    } catch (HttpClientErrorException.TooManyRequests e) {
90
      throw e;
91
    } catch (HttpClientErrorException | HttpServerErrorException e) {
92
      log.info("Dining commons {} closed on {}: {}", diningCommonCode, dateTime, e.getMessage());
93 1 1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED
      return null;
94
    }
95
96 1 1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED
    return retVal;
97
  }
98
}

Mutations

63

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_returns_null_on_404()]
removed call to org/springframework/http/HttpHeaders::setAccept → KILLED

64

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_returns_null_on_404()]
removed call to org/springframework/http/HttpHeaders::setContentType → KILLED

65

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_returns_null_on_404()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

66

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_returns_null_on_404()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

67

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_returns_null_on_404()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

93

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_returns_null_on_404()]
replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED

96

1.1
Location : getJSON
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuServiceTests]/[method:test_getJSON_success()]
replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1