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 List<String> getActiveQuarterList() throws Exception { | |
71 | String start = getCurrentQuarterYYYYQ(); | |
72 | String end = getEndQtrYYYYQ(); | |
73 | ||
74 | List<Quarter> quartersInOrder = Quarter.quarterList(start, end); | |
75 | List<String> result = new ArrayList<String>(); | |
76 | ||
77 | for (Quarter quarter : quartersInOrder) { | |
78 | result.add(quarter.getYYYYQ()); | |
79 | } | |
80 | ||
81 |
1
1. getActiveQuarterList : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarterList → KILLED |
return result; |
82 | } | |
83 | ||
84 | public UCSBAPIQuarter getCurrentQuarter() throws Exception { | |
85 | HttpHeaders headers = new HttpHeaders(); | |
86 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
87 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
88 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
89 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
90 | ||
91 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
92 | ||
93 | String url = CURRENT_QUARTER_ENDPOINT; | |
94 | ||
95 | log.info("url=" + url); | |
96 | ||
97 | String retVal = ""; | |
98 | MediaType contentType = null; | |
99 | HttpStatus statusCode = null; | |
100 | ||
101 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
102 | contentType = re.getHeaders().getContentType(); | |
103 | statusCode = (HttpStatus) re.getStatusCode(); | |
104 | retVal = re.getBody(); | |
105 | ||
106 | log.info( | |
107 | "json: {} contentType: {} statusCode: {} entity: {}", | |
108 | retVal, | |
109 | contentType, | |
110 | statusCode, | |
111 | entity); | |
112 | UCSBAPIQuarter quarter = null; | |
113 | quarter = objectMapper.readValue(retVal, UCSBAPIQuarter.class); | |
114 |
1
1. getCurrentQuarter : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED |
return quarter; |
115 | } | |
116 | ||
117 | public List<UCSBAPIQuarter> getAllQuarters() throws Exception { | |
118 | List<UCSBAPIQuarter> quarters = ucsbApiQuarterRepository.findAll(); | |
119 |
1
1. getAllQuarters : negated conditional → KILLED |
if (quarters.isEmpty()) { |
120 | quarters = this.loadAllQuarters(); | |
121 | } | |
122 |
1
1. getAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuarters → KILLED |
return quarters; |
123 | } | |
124 | ||
125 | public List<UCSBAPIQuarter> getAllQuartersFromAPI() throws Exception { | |
126 | HttpHeaders headers = new HttpHeaders(); | |
127 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
128 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
129 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
130 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
131 | ||
132 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
133 | ||
134 | String url = ALL_QUARTERS_ENDPOINT; | |
135 | ||
136 | log.info("url=" + url); | |
137 | ||
138 | String retVal = ""; | |
139 | MediaType contentType = null; | |
140 | HttpStatus statusCode = null; | |
141 | ||
142 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
143 | contentType = re.getHeaders().getContentType(); | |
144 | statusCode = (HttpStatus) re.getStatusCode(); | |
145 | retVal = re.getBody(); | |
146 | ||
147 | log.info( | |
148 | "json: {} contentType: {} statusCode: {} entity: {}", | |
149 | retVal, | |
150 | contentType, | |
151 | statusCode, | |
152 | entity); | |
153 | List<UCSBAPIQuarter> quarters = null; | |
154 | quarters = objectMapper.readValue(retVal, new TypeReference<List<UCSBAPIQuarter>>() {}); | |
155 |
1
1. getAllQuartersFromAPI : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuartersFromAPI → KILLED |
return quarters; |
156 | } | |
157 | ||
158 | public boolean quarterYYYYQInRange(String quarterYYYYQ) { | |
159 |
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED |
boolean dateGEStart = quarterYYYYQ.compareTo(startQtrYYYYQ) >= 0; |
160 |
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED |
boolean dateLEEnd = quarterYYYYQ.compareTo(endQtrYYYYQ) <= 0; |
161 |
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); |
162 | } | |
163 | ||
164 | public List<UCSBAPIQuarter> loadAllQuarters() throws Exception { | |
165 | List<UCSBAPIQuarter> quarters = this.getAllQuartersFromAPI(); | |
166 | List<UCSBAPIQuarter> savedQuarters = new ArrayList<UCSBAPIQuarter>(); | |
167 |
1
1. loadAllQuarters : removed call to java/util/List::forEach → KILLED |
quarters.forEach( |
168 | (quarter) -> { | |
169 |
1
1. lambda$loadAllQuarters$0 : negated conditional → KILLED |
if (quarterYYYYQInRange(quarter.getQuarter())) { |
170 | ucsbApiQuarterRepository.save(quarter); | |
171 | savedQuarters.add(quarter); | |
172 | } | |
173 | }); | |
174 | log.info("savedQuarters.size={}", savedQuarters.size()); | |
175 |
1
1. loadAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::loadAllQuarters → KILLED |
return savedQuarters; |
176 | } | |
177 | } | |
Mutations | ||
58 |
1.1 |
|
62 |
1.1 |
|
67 |
1.1 |
|
81 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
88 |
1.1 |
|
89 |
1.1 |
|
114 |
1.1 |
|
119 |
1.1 |
|
122 |
1.1 |
|
127 |
1.1 |
|
128 |
1.1 |
|
129 |
1.1 |
|
130 |
1.1 |
|
155 |
1.1 |
|
159 |
1.1 2.2 |
|
160 |
1.1 2.2 |
|
161 |
1.1 2.2 3.3 |
|
167 |
1.1 |
|
169 |
1.1 |
|
175 |
1.1 |