UCSBDiningMenuService.java

1
package edu.ucsb.cs156.dining.services;
2
3
import java.util.Arrays;
4
import lombok.extern.slf4j.Slf4j;
5
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.boot.web.client.RestTemplateBuilder;
7
import org.springframework.cache.annotation.Cacheable;
8
import org.springframework.http.HttpEntity;
9
import org.springframework.http.HttpHeaders;
10
import org.springframework.http.HttpMethod;
11
import org.springframework.http.HttpStatus;
12
import org.springframework.http.MediaType;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.stereotype.Service;
15
import org.springframework.web.client.HttpClientErrorException;
16
import org.springframework.web.client.HttpServerErrorException;
17
import org.springframework.web.client.RestTemplate;
18
19
/** Service object that wraps the UCSB Dining Menu API */
20
@Service
21
@Slf4j
22
public class UCSBDiningMenuService {
23
24
  @Value("${app.ucsb.api.consumer_key}")
25
  private String apiKey;
26
27
  private RestTemplate restTemplate = new RestTemplate();
28
29
  public UCSBDiningMenuService(RestTemplateBuilder restTemplateBuilder) throws Exception {
30
    restTemplate = restTemplateBuilder.build();
31
  }
32
33
  public static final String ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT =
34
      "https://api.ucsb.edu/dining/menu/v1/{date-time}/{dining-common-code}";
35
36
  @Cacheable("menu")
37
  public String getJSON(String dateTime, String diningCommonCode) throws Exception {
38
39
    HttpHeaders headers = new HttpHeaders();
40 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
41 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
42 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-version", "1.0");
43 1 1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-key", this.apiKey);
44
45
    HttpEntity<String> entity = new HttpEntity<>("body", headers);
46
47
    String url = ALL_MEAL_TIMES_AT_A_DINING_COMMON_ENDPOINT;
48
    url.replace("{date-time}", dateTime);
49
    url.replace("{dining-common-code}", diningCommonCode);
50
51
    log.info("url=" + url);
52
53
    String retVal = "";
54
    MediaType contentType = null;
55
    HttpStatus statusCode = null;
56
57
    try {
58
      ResponseEntity<String> re =
59
          restTemplate.exchange(
60
              url, HttpMethod.GET, entity, String.class, dateTime, diningCommonCode);
61
      contentType = re.getHeaders().getContentType();
62
      statusCode = (HttpStatus) re.getStatusCode();
63
      retVal = re.getBody();
64
65
      log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode);
66
    } catch (HttpClientErrorException | HttpServerErrorException e) {
67
      log.info("Dining commons {} closed on {}: {}", diningCommonCode, dateTime, e.getMessage());
68 1 1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED
      return null;
69
    }
70
71 1 1. getJSON : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuService::getJSON → KILLED
    return retVal;
72
  }
73
}

Mutations

40

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

41

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

42

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

43

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

68

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

71

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.17.0