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