1 | package edu.ucsb.cs156.courses.services; | |
2 | ||
3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
4 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
5 | import edu.ucsb.cs156.courses.documents.CoursePage; | |
6 | import java.util.Arrays; | |
7 | import java.util.HashMap; | |
8 | import java.util.List; | |
9 | import java.util.Map; | |
10 | import lombok.extern.slf4j.Slf4j; | |
11 | import org.springframework.beans.factory.annotation.Autowired; | |
12 | import org.springframework.beans.factory.annotation.Value; | |
13 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
14 | import org.springframework.http.HttpEntity; | |
15 | import org.springframework.http.HttpHeaders; | |
16 | import org.springframework.http.HttpMethod; | |
17 | import org.springframework.http.HttpStatus; | |
18 | import org.springframework.http.MediaType; | |
19 | import org.springframework.http.ResponseEntity; | |
20 | import org.springframework.stereotype.Service; | |
21 | import org.springframework.web.client.RestTemplate; | |
22 | ||
23 | /** Service object that wraps the UCSB Academic Curriculum API */ | |
24 | @Service | |
25 | @Slf4j | |
26 | public class UCSBCurriculumService { | |
27 | ||
28 | @Autowired private ObjectMapper objectMapper; | |
29 | ||
30 | @Value("${app.ucsb.api.consumer_key}") | |
31 | private String apiKey; | |
32 | ||
33 | private RestTemplate restTemplate = new RestTemplate(); | |
34 | ||
35 | public UCSBCurriculumService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
36 | restTemplate = restTemplateBuilder.build(); | |
37 | } | |
38 | ||
39 | public static final String CURRICULUM_ENDPOINT = | |
40 | "https://api.ucsb.edu/academics/curriculums/v1/classes/search"; | |
41 | ||
42 | public static final String SUBJECTS_ENDPOINT = | |
43 | "https://api.ucsb.edu/students/lookups/v1/subjects"; | |
44 | ||
45 | public static final String SECTION_ENDPOINT = | |
46 | "https://api.ucsb.edu/academics/curriculums/v1/classsection/{quarter}/{enrollcode}"; | |
47 | ||
48 | public static final String ALL_SECTIONS_ENDPOINT = | |
49 | "https://api.ucsb.edu/academics/curriculums/v3/classes/{quarter}/{enrollcode}"; | |
50 | ||
51 | public static final String FINALS_ENDPOINT = | |
52 | "https://api.ucsb.edu/academics/curriculums/v3/finals"; | |
53 | ||
54 | public String getJSON(String subjectArea, String quarter, String courseLevel) throws Exception { | |
55 | ||
56 | HttpHeaders headers = new HttpHeaders(); | |
57 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
58 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
59 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
60 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
61 | ||
62 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
63 | ||
64 | String params = | |
65 | String.format( | |
66 | "?quarter=%s&subjectCode=%s&objLevelCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
67 | quarter, subjectArea, courseLevel, 1, 100, "true"); | |
68 | String url = CURRICULUM_ENDPOINT + params; | |
69 | ||
70 |
1
1. getJSON : negated conditional → KILLED |
if (courseLevel.equals("A")) { |
71 | params = | |
72 | String.format( | |
73 | "?quarter=%s&subjectCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
74 | quarter, subjectArea, 1, 100, "true"); | |
75 | url = CURRICULUM_ENDPOINT + params; | |
76 | } | |
77 | ||
78 | log.info("url=" + url); | |
79 | ||
80 | String retVal = ""; | |
81 | MediaType contentType = null; | |
82 | HttpStatus statusCode = null; | |
83 | ||
84 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
85 | contentType = re.getHeaders().getContentType(); | |
86 | statusCode = (HttpStatus) re.getStatusCode(); | |
87 | retVal = re.getBody(); | |
88 | ||
89 | log.trace("json: {}", retVal); | |
90 | log.info("contentType: {} statusCode: {}", contentType, statusCode); | |
91 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSON → KILLED |
return retVal; |
92 | } | |
93 | ||
94 | public List<ConvertedSection> getConvertedSections( | |
95 | String subjectArea, String quarter, String courseLevel) throws Exception { | |
96 | String json = getJSON(subjectArea, quarter, courseLevel); | |
97 | CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
98 | List<ConvertedSection> result = coursePage.convertedSections(); | |
99 |
1
1. getConvertedSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getConvertedSections → KILLED |
return result; |
100 | } | |
101 | ||
102 | public String getSectionJSON(String subjectArea, String quarter, String courseLevel) | |
103 | throws Exception { | |
104 | List<ConvertedSection> l = getConvertedSections(subjectArea, quarter, courseLevel); | |
105 | ||
106 | String arrayToJson = objectMapper.writeValueAsString(l); | |
107 | ||
108 |
1
1. getSectionJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSectionJSON → KILLED |
return arrayToJson; |
109 | } | |
110 | ||
111 | public String getSubjectsJSON() throws Exception { | |
112 | ||
113 | HttpHeaders headers = new HttpHeaders(); | |
114 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
115 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
116 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
117 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
118 | ||
119 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
120 | ||
121 | log.info("url=" + SUBJECTS_ENDPOINT); | |
122 | ||
123 | String retVal = ""; | |
124 | MediaType contentType = null; | |
125 | HttpStatus statusCode = null; | |
126 | ||
127 | ResponseEntity<String> re = | |
128 | restTemplate.exchange(SUBJECTS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
129 | contentType = re.getHeaders().getContentType(); | |
130 | statusCode = (HttpStatus) re.getStatusCode(); | |
131 | retVal = re.getBody(); | |
132 | ||
133 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
134 |
1
1. getSubjectsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSubjectsJSON → KILLED |
return retVal; |
135 | } | |
136 | ||
137 | /** | |
138 | * This method retrieves exactly one section matching the enrollCode and quarter arguments, if | |
139 | * such a section exists. | |
140 | */ | |
141 | public String getSection(String enrollCode, String quarter) throws Exception { | |
142 | ||
143 | HttpHeaders headers = new HttpHeaders(); | |
144 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
145 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
146 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
147 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
148 | ||
149 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
150 | ||
151 | String url = SECTION_ENDPOINT; | |
152 | ||
153 | log.info("url=" + url); | |
154 | ||
155 | Map<String, String> params = new HashMap<>(); | |
156 | params.put("quarter", quarter); | |
157 | params.put("enrollcode", enrollCode); | |
158 | ||
159 | String retVal = ""; | |
160 | MediaType contentType = null; | |
161 | HttpStatus statusCode = null; | |
162 | ||
163 | ResponseEntity<String> re = | |
164 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
165 | contentType = re.getHeaders().getContentType(); | |
166 | statusCode = (HttpStatus) re.getStatusCode(); | |
167 | retVal = re.getBody(); | |
168 | ||
169 |
1
1. getSection : negated conditional → KILLED |
if (retVal.equals("null")) { |
170 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
171 | } | |
172 | ||
173 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
174 |
1
1. getSection : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSection → KILLED |
return retVal; |
175 | } | |
176 | ||
177 | /** | |
178 | * This method retrieves all of the sections related to a certain enroll code. For example, if the | |
179 | * enrollCode is for a discussion section, the lecture section and all related discussion sections | |
180 | * will also be returned. | |
181 | */ | |
182 | public String getAllSections(String enrollCode, String quarter) throws Exception { | |
183 | ||
184 | HttpHeaders headers = new HttpHeaders(); | |
185 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
186 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
187 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
188 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
189 | ||
190 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
191 | ||
192 | String url = ALL_SECTIONS_ENDPOINT; | |
193 | ||
194 | log.info("url=" + url); | |
195 | ||
196 | Map<String, String> params = new HashMap<>(); | |
197 | params.put("quarter", quarter); | |
198 | params.put("enrollcode", enrollCode); | |
199 | ||
200 | String retVal = ""; | |
201 | MediaType contentType = null; | |
202 | HttpStatus statusCode = null; | |
203 | ||
204 | ResponseEntity<String> re = | |
205 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
206 | contentType = re.getHeaders().getContentType(); | |
207 | statusCode = (HttpStatus) re.getStatusCode(); | |
208 | retVal = re.getBody(); | |
209 | ||
210 |
1
1. getAllSections : negated conditional → KILLED |
if (retVal.equals("null")) { |
211 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
212 | } | |
213 | ||
214 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
215 |
1
1. getAllSections : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getAllSections → KILLED |
return retVal; |
216 | } | |
217 | ||
218 | public String getJSONbyQtrEnrollCd(String quarter, String enrollCd) throws Exception { | |
219 | ||
220 | HttpHeaders headers = new HttpHeaders(); | |
221 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
222 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
223 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
224 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
225 | ||
226 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
227 | ||
228 | String url = | |
229 | "https://api.ucsb.edu/academics/curriculums/v3/classsection/" + quarter + "/" + enrollCd; | |
230 | ||
231 | log.info("url=" + url); | |
232 | ||
233 | String retVal = ""; | |
234 | MediaType contentType = null; | |
235 | HttpStatus statusCode = null; | |
236 | ||
237 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
238 | contentType = re.getHeaders().getContentType(); | |
239 | statusCode = (HttpStatus) re.getStatusCode(); | |
240 | retVal = re.getBody(); | |
241 | ||
242 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
243 |
1
1. getJSONbyQtrEnrollCd : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSONbyQtrEnrollCd → KILLED |
return retVal; |
244 | } | |
245 | ||
246 | public String getFinalsInfo(String quarter, String enrollCd) throws Exception { | |
247 | HttpHeaders headers = new HttpHeaders(); | |
248 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
249 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
250 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
251 | ||
252 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
253 | ||
254 | String params = String.format("?quarter=%s&enrollCode=%s", quarter, enrollCd); | |
255 | String url = FINALS_ENDPOINT + params; | |
256 | ||
257 | log.info("url=" + url); | |
258 | ||
259 | String retVal; | |
260 | MediaType contentType; | |
261 | HttpStatus statusCode; | |
262 | ||
263 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
264 | contentType = re.getHeaders().getContentType(); | |
265 | statusCode = (HttpStatus) re.getStatusCode(); | |
266 | retVal = re.getBody(); | |
267 | ||
268 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
269 |
1
1. getFinalsInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getFinalsInfo → KILLED |
return retVal; |
270 | } | |
271 | } | |
Mutations | ||
57 |
1.1 |
|
58 |
1.1 |
|
59 |
1.1 |
|
60 |
1.1 |
|
70 |
1.1 |
|
91 |
1.1 |
|
99 |
1.1 |
|
108 |
1.1 |
|
114 |
1.1 |
|
115 |
1.1 |
|
116 |
1.1 |
|
117 |
1.1 |
|
134 |
1.1 |
|
144 |
1.1 |
|
145 |
1.1 |
|
146 |
1.1 |
|
147 |
1.1 |
|
169 |
1.1 |
|
174 |
1.1 |
|
185 |
1.1 |
|
186 |
1.1 |
|
187 |
1.1 |
|
188 |
1.1 |
|
210 |
1.1 |
|
215 |
1.1 |
|
221 |
1.1 |
|
222 |
1.1 |
|
223 |
1.1 |
|
224 |
1.1 |
|
243 |
1.1 |
|
248 |
1.1 |
|
249 |
1.1 |
|
250 |
1.1 |
|
269 |
1.1 |