DiningCommonsService.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.DiningCommons;
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.http.HttpEntity;
13
import org.springframework.http.HttpHeaders;
14
import org.springframework.http.HttpMethod;
15
import org.springframework.http.MediaType;
16
import org.springframework.http.ResponseEntity;
17
import org.springframework.stereotype.Service;
18
import org.springframework.web.client.RestTemplate;
19
20
// Reworked from proj-course's UCSBSubjectsService.java :
21
// https://github.com/ucsb-cs156/proj-courses/blob/main/src/main/java/edu/ucsb/cs156/courses/services/UCSBSubjectsService.java
22
23
// Uses UCSB's developer api for dining commons.
24
// https://developer.ucsb.edu/apis/dining/dining-commons
25
26
// Service for the dining commons page.
27
// This service particularly utilizes an endpoint for getting all dining commons from the above api.
28
29
@Slf4j
30
@Service("DiningCommons")
31
public class DiningCommonsService {
32
33
  @Autowired private ObjectMapper mapper;
34
35
  @Value("${app.ucsb.api.consumer_key}")
36
  private String apiKey;
37
38
  public static final String ENDPOINT = "https://api.ucsb.edu/dining/commons/v1/";
39
40
  private final RestTemplate restTemplate;
41
42
  public DiningCommonsService(RestTemplateBuilder restTemplateBuilder) {
43
    restTemplate = restTemplateBuilder.build();
44
  }
45
46
  public List<DiningCommons> get() throws JsonProcessingException {
47
48
    HttpHeaders headers = new HttpHeaders();
49 1 1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
50 1 1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
51 1 1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-key", this.apiKey);
52
53
    HttpEntity<String> entity = new HttpEntity<>(headers);
54
    ResponseEntity<String> re =
55
        restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class);
56
57
    String retBody = re.getBody();
58
    List<DiningCommons> commons =
59
        mapper.readValue(retBody, new TypeReference<List<DiningCommons>>() {});
60
61 1 1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/DiningCommonsService::get → KILLED
    return commons;
62
  }
63
}

Mutations

49

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

50

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

51

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

61

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

Active mutators

Tests examined


Report generated by PIT 1.17.0