UCSBDiningMenuItemsService.java

1
package edu.ucsb.cs156.dining.services;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.core.type.TypeReference;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import edu.ucsb.cs156.dining.models.Entree;
7
import java.util.List;
8
import lombok.extern.slf4j.Slf4j;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Value;
11
import org.springframework.boot.web.client.RestTemplateBuilder;
12
import org.springframework.cache.annotation.Cacheable;
13
import org.springframework.http.HttpEntity;
14
import org.springframework.http.HttpHeaders;
15
import org.springframework.http.HttpMethod;
16
import org.springframework.http.MediaType;
17
import org.springframework.http.ResponseEntity;
18
import org.springframework.stereotype.Service;
19
import org.springframework.web.client.RestTemplate;
20
21
@Service
22
@Slf4j
23
public class UCSBDiningMenuItemsService {
24
25
  @Autowired private ObjectMapper objectMapper;
26
27
  @Value("${app.ucsb.api.consumer_key}")
28
  private String apiKey;
29
30
  private RestTemplate restTemplate = new RestTemplate();
31
32
  public UCSBDiningMenuItemsService(RestTemplateBuilder restTemplateBuilder) throws Exception {
33
    restTemplate = restTemplateBuilder.build();
34
  }
35
36
  public static final String ALL_MEAL_ITEMS_AT_A_DINING_COMMON_ENDPOINT =
37
      "https://api.ucsb.edu/dining/menu/v1/{date-time}/{dining-common-code}/{meal-code}";
38
39
  /**
40
   * Create a List of Entree from json representation
41
   *
42
   * @param dateTime String of date in iso format
43
   * @param diningCommonCode String of dining common
44
   * @param mealCode String of meal code
45
   * @return a list of menu items
46
   */
47
  @Cacheable("menuItem")
48
  public List<Entree> get(String dateTime, String diningCommonCode, String mealCode)
49
      throws JsonProcessingException {
50
51
    HttpHeaders headers = new HttpHeaders();
52 1 1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
53 1 1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
54 1 1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-key", this.apiKey);
55
56
    HttpEntity<String> entity = new HttpEntity<>(headers);
57
    String url = ALL_MEAL_ITEMS_AT_A_DINING_COMMON_ENDPOINT;
58
    url = url.replace("{date-time}", dateTime);
59
    url = url.replace("{dining-common-code}", diningCommonCode);
60
    url = url.replace("{meal-code}", mealCode);
61
62
    log.info(
63
        "Fetching menu items for date: {}, dining common: {}, meal: {}",
64
        dateTime,
65
        diningCommonCode,
66
        mealCode);
67
68
    ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
69
    String retBody = re.getBody();
70
71
    List<Entree> menuItems = objectMapper.readValue(retBody, new TypeReference<List<Entree>>() {});
72
73 1 1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::get → KILLED
    return menuItems;
74
  }
75
}

Mutations

52

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

53

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

54

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

73

1.1
Location : get
Killed by : edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsServiceTests]/[method:test_get_success()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::get → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0