UCSBAPIQuarterService.java

1
package edu.ucsb.cs156.courses.services;
2
3
import com.fasterxml.jackson.core.type.TypeReference;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import edu.ucsb.cs156.courses.entities.UCSBAPIQuarter;
6
import edu.ucsb.cs156.courses.models.Quarter;
7
import edu.ucsb.cs156.courses.repositories.UCSBAPIQuarterRepository;
8
import java.util.ArrayList;
9
import java.util.Arrays;
10
import java.util.List;
11
import lombok.extern.slf4j.Slf4j;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.boot.web.client.RestTemplateBuilder;
15
import org.springframework.http.HttpEntity;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.HttpMethod;
18
import org.springframework.http.HttpStatus;
19
import org.springframework.http.MediaType;
20
import org.springframework.http.ResponseEntity;
21
import org.springframework.stereotype.Service;
22
import org.springframework.web.client.RestTemplate;
23
24
/** Service object that wraps the UCSB Academic Curriculum API */
25
@Service
26
@Slf4j
27
public class UCSBAPIQuarterService {
28
29
  @Value("${app.startQtrYYYYQ:20221}")
30
  private String startQtrYYYYQ;
31
32
  @Value("${app.endQtrYYYYQ:20222}")
33
  private String endQtrYYYYQ;
34
35
  @Autowired private ObjectMapper objectMapper;
36
37
  @Autowired UCSBAPIQuarterRepository ucsbApiQuarterRepository;
38
39
  @Value("${app.ucsb.api.consumer_key}")
40
  private String apiKey;
41
42
  private RestTemplate restTemplate = new RestTemplate();
43
44
  public UCSBAPIQuarterService(RestTemplateBuilder restTemplateBuilder) throws Exception {
45
    restTemplate = restTemplateBuilder.build();
46
  }
47
48
  public static final String CURRENT_QUARTER_ENDPOINT =
49
      "https://api.ucsb.edu/academics/quartercalendar/v1/quarters/current";
50
51
  public static final String ALL_QUARTERS_ENDPOINT =
52
      "https://api.ucsb.edu/academics/quartercalendar/v1/quarters";
53
54
  public static final String END_QUARTER_ENDPOINT =
55
      "https://api.ucsb.edu/academics/quartercalendar/v1/quarters/end";
56
57
  public String getStartQtrYYYYQ() {
58 1 1. getStartQtrYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getStartQtrYYYYQ → KILLED
    return startQtrYYYYQ;
59
  }
60
61
  public String getEndQtrYYYYQ() {
62 1 1. getEndQtrYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getEndQtrYYYYQ → KILLED
    return endQtrYYYYQ;
63
  }
64
65
  public String getCurrentQuarterYYYYQ() throws Exception {
66
    UCSBAPIQuarter quarter = getCurrentQuarter();
67 1 1. getCurrentQuarterYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarterYYYYQ → KILLED
    return quarter.getQuarter();
68
  }
69
70
  public UCSBAPIQuarter getCurrentQuarter() throws Exception {
71
    HttpHeaders headers = new HttpHeaders();
72 1 1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
73 1 1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
74 1 1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-version", "1.0");
75 1 1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-key", this.apiKey);
76
77
    HttpEntity<String> entity = new HttpEntity<>("body", headers);
78
79
    String url = CURRENT_QUARTER_ENDPOINT;
80
81
    log.info("url=" + url);
82
83
    String retVal = "";
84
    MediaType contentType = null;
85
    HttpStatus statusCode = null;
86
87
    ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
88
    contentType = re.getHeaders().getContentType();
89
    statusCode = (HttpStatus) re.getStatusCode();
90
    retVal = re.getBody();
91
92
    log.info(
93
        "json: {} contentType: {} statusCode: {} entity: {}",
94
        retVal,
95
        contentType,
96
        statusCode,
97
        entity);
98
    UCSBAPIQuarter quarter = null;
99
    quarter = objectMapper.readValue(retVal, UCSBAPIQuarter.class);
100 1 1. getCurrentQuarter : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED
    return quarter;
101
  }
102
103
  public List<UCSBAPIQuarter> getAllQuarters() throws Exception {
104
    List<UCSBAPIQuarter> quarters = ucsbApiQuarterRepository.findAll();
105 1 1. getAllQuarters : negated conditional → KILLED
    if (quarters.isEmpty()) {
106
      quarters = this.loadAllQuarters();
107
    }
108 1 1. getAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuarters → KILLED
    return quarters;
109
  }
110
111
  public List<UCSBAPIQuarter> getAllQuartersFromAPI() throws Exception {
112
    HttpHeaders headers = new HttpHeaders();
113 1 1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
114 1 1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
115 1 1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-version", "1.0");
116 1 1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED
    headers.set("ucsb-api-key", this.apiKey);
117
118
    HttpEntity<String> entity = new HttpEntity<>("body", headers);
119
120
    String url = ALL_QUARTERS_ENDPOINT;
121
122
    log.info("url=" + url);
123
124
    String retVal = "";
125
    MediaType contentType = null;
126
    HttpStatus statusCode = null;
127
128
    ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
129
    contentType = re.getHeaders().getContentType();
130
    statusCode = (HttpStatus) re.getStatusCode();
131
    retVal = re.getBody();
132
133
    log.info(
134
        "json: {} contentType: {} statusCode: {} entity: {}",
135
        retVal,
136
        contentType,
137
        statusCode,
138
        entity);
139
    List<UCSBAPIQuarter> quarters = null;
140
    quarters = objectMapper.readValue(retVal, new TypeReference<List<UCSBAPIQuarter>>() {});
141 1 1. getAllQuartersFromAPI : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuartersFromAPI → KILLED
    return quarters;
142
  }
143
144
  public boolean quarterYYYYQInRange(String quarterYYYYQ) {
145 2 1. quarterYYYYQInRange : changed conditional boundary → KILLED
2. quarterYYYYQInRange : negated conditional → KILLED
    boolean dateGEStart = quarterYYYYQ.compareTo(startQtrYYYYQ) >= 0;
146 2 1. quarterYYYYQInRange : changed conditional boundary → KILLED
2. quarterYYYYQInRange : negated conditional → KILLED
    boolean dateLEEnd = quarterYYYYQ.compareTo(endQtrYYYYQ) <= 0;
147 3 1. quarterYYYYQInRange : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::quarterYYYYQInRange → KILLED
2. quarterYYYYQInRange : negated conditional → KILLED
3. quarterYYYYQInRange : negated conditional → KILLED
    return (dateGEStart && dateLEEnd);
148
  }
149
150
  public List<UCSBAPIQuarter> loadAllQuarters() throws Exception {
151
    List<UCSBAPIQuarter> quarters = this.getAllQuartersFromAPI();
152
    List<UCSBAPIQuarter> savedQuarters = new ArrayList<UCSBAPIQuarter>();
153 1 1. loadAllQuarters : removed call to java/util/List::forEach → KILLED
    quarters.forEach(
154
        (quarter) -> {
155 1 1. lambda$loadAllQuarters$0 : negated conditional → KILLED
          if (quarterYYYYQInRange(quarter.getQuarter())) {
156
            ucsbApiQuarterRepository.save(quarter);
157
            savedQuarters.add(quarter);
158
          }
159
        });
160
    log.info("savedQuarters.size={}", savedQuarters.size());
161 1 1. loadAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::loadAllQuarters → KILLED
    return savedQuarters;
162
  }
163
164
  public List<String> getActiveQuarterList() throws Exception {
165
    List<String> activeQuarters = new ArrayList<>();
166
    String startQuarter = getCurrentQuarterYYYYQ();
167
    String endQuarter = getEndQtrYYYYQ();
168
169
    List<Quarter> quarterObjects = Quarter.quarterList(startQuarter, endQuarter);
170
171
    for (Quarter quarter : quarterObjects) {
172
      activeQuarters.add(quarter.getYYYYQ());
173
    }
174
175 1 1. getActiveQuarterList : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarterList → KILLED
    return activeQuarters;
176
  }
177
}

Mutations

58

1.1
Location : getStartQtrYYYYQ
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getStartQtrYYYYQ()]
replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getStartQtrYYYYQ → KILLED

62

1.1
Location : getEndQtrYYYYQ
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getEndQtrYYYYQ()]
replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getEndQtrYYYYQ → KILLED

67

1.1
Location : getCurrentQuarterYYYYQ
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getCurrentQuarterYYYYQ()]
replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarterYYYYQ → KILLED

72

1.1
Location : getCurrentQuarter
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getCurrentQuarterYYYYQ()]
removed call to org/springframework/http/HttpHeaders::setAccept → KILLED

73

1.1
Location : getCurrentQuarter
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getCurrentQuarterYYYYQ()]
removed call to org/springframework/http/HttpHeaders::setContentType → KILLED

74

1.1
Location : getCurrentQuarter
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getCurrentQuarterYYYYQ()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

75

1.1
Location : getCurrentQuarter
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getCurrentQuarterYYYYQ()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

100

1.1
Location : getCurrentQuarter
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getCurrentQuarterYYYYQ()]
replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED

105

1.1
Location : getAllQuarters
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuarters_preloaded()]
negated conditional → KILLED

108

1.1
Location : getAllQuarters
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuarters_preloaded()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuarters → KILLED

113

1.1
Location : getAllQuartersFromAPI
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuartersFromAPI()]
removed call to org/springframework/http/HttpHeaders::setAccept → KILLED

114

1.1
Location : getAllQuartersFromAPI
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuartersFromAPI()]
removed call to org/springframework/http/HttpHeaders::setContentType → KILLED

115

1.1
Location : getAllQuartersFromAPI
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuartersFromAPI()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

116

1.1
Location : getAllQuartersFromAPI
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuartersFromAPI()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

141

1.1
Location : getAllQuartersFromAPI
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuartersFromAPI()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuartersFromAPI → KILLED

145

1.1
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20211_true()]
changed conditional boundary → KILLED

2.2
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20204_false()]
negated conditional → KILLED

146

1.1
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20223_true()]
changed conditional boundary → KILLED

2.2
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20212_true()]
negated conditional → KILLED

147

1.1
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20204_false()]
replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::quarterYYYYQInRange → KILLED

2.2
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20212_true()]
negated conditional → KILLED

3.3
Location : quarterYYYYQInRange
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_quarterYYYYQInRange_20204_false()]
negated conditional → KILLED

153

1.1
Location : loadAllQuarters
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuarters_empty()]
removed call to java/util/List::forEach → KILLED

155

1.1
Location : lambda$loadAllQuarters$0
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuarters_empty()]
negated conditional → KILLED

161

1.1
Location : loadAllQuarters
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getAllQuarters_empty()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::loadAllQuarters → KILLED

175

1.1
Location : getActiveQuarterList
Killed by : edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBAPIQuarterServiceTests]/[method:test_getActiveQuarterList()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarterList → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0