| 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 static final String GE_ENDPOINT = "https://api.ucsb.edu/students/lookups/v1/requirements"; | |
| 55 | ||
| 56 |   public String getJSON(String subjectArea, String quarter, String courseLevel) throws Exception { | |
| 57 | ||
| 58 |     HttpHeaders headers = new HttpHeaders(); | |
| 59 | 
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 60 | 
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 61 | 
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "1.0"); | 
| 62 | 
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 63 | ||
| 64 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 65 | ||
| 66 |     String params = | |
| 67 |         String.format( | |
| 68 |             "?quarter=%s&subjectCode=%s&objLevelCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
| 69 |             quarter, subjectArea, courseLevel, 1, 100, "true"); | |
| 70 |     String url = CURRICULUM_ENDPOINT + params; | |
| 71 | ||
| 72 | 
1
1. getJSON : negated conditional → KILLED | 
    if (courseLevel.equals("A")) { | 
| 73 |       params = | |
| 74 |           String.format( | |
| 75 |               "?quarter=%s&subjectCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
| 76 |               quarter, subjectArea, 1, 100, "true"); | |
| 77 |       url = CURRICULUM_ENDPOINT + params; | |
| 78 |     } | |
| 79 | ||
| 80 |     log.info("url=" + url); | |
| 81 | ||
| 82 |     String retVal = ""; | |
| 83 |     MediaType contentType = null; | |
| 84 |     HttpStatus statusCode = null; | |
| 85 | ||
| 86 |     ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 87 |     contentType = re.getHeaders().getContentType(); | |
| 88 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 89 |     retVal = re.getBody(); | |
| 90 | ||
| 91 |     log.trace("json: {}", retVal); | |
| 92 |     log.info("contentType: {} statusCode: {}", contentType, statusCode); | |
| 93 | 
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSON → KILLED | 
    return retVal; | 
| 94 |   } | |
| 95 | ||
| 96 |   public List<ConvertedSection> getConvertedSections( | |
| 97 |       String subjectArea, String quarter, String courseLevel) throws Exception { | |
| 98 |     String json = getJSON(subjectArea, quarter, courseLevel); | |
| 99 |     CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
| 100 |     List<ConvertedSection> result = coursePage.convertedSections(); | |
| 101 | 
1
1. getConvertedSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getConvertedSections → KILLED | 
    return result; | 
| 102 |   } | |
| 103 | ||
| 104 |   public String getSectionJSON(String subjectArea, String quarter, String courseLevel) | |
| 105 |       throws Exception { | |
| 106 |     List<ConvertedSection> l = getConvertedSections(subjectArea, quarter, courseLevel); | |
| 107 | ||
| 108 |     String arrayToJson = objectMapper.writeValueAsString(l); | |
| 109 | ||
| 110 | 
1
1. getSectionJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSectionJSON → KILLED | 
    return arrayToJson; | 
| 111 |   } | |
| 112 | ||
| 113 |   public String getSubjectsJSON() throws Exception { | |
| 114 | ||
| 115 |     HttpHeaders headers = new HttpHeaders(); | |
| 116 | 
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 117 | 
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 118 | 
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "1.0"); | 
| 119 | 
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 120 | ||
| 121 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 122 | ||
| 123 |     log.info("url=" + SUBJECTS_ENDPOINT); | |
| 124 | ||
| 125 |     String retVal = ""; | |
| 126 |     MediaType contentType = null; | |
| 127 |     HttpStatus statusCode = null; | |
| 128 | ||
| 129 |     ResponseEntity<String> re = | |
| 130 |         restTemplate.exchange(SUBJECTS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 131 |     contentType = re.getHeaders().getContentType(); | |
| 132 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 133 |     retVal = re.getBody(); | |
| 134 | ||
| 135 |     log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 136 | 
1
1. getSubjectsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSubjectsJSON → KILLED | 
    return retVal; | 
| 137 |   } | |
| 138 | ||
| 139 |   public String getGeInfo() throws Exception { | |
| 140 |     HttpHeaders headers = new HttpHeaders(); | |
| 141 | 
1
1. getGeInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 142 | 
1
1. getGeInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "3.0"); | 
| 143 | 
1
1. getGeInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 144 | ||
| 145 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 146 |     ResponseEntity<String> re = | |
| 147 |         restTemplate.exchange(GE_ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 148 | ||
| 149 |     log.info( | |
| 150 |         "url={}  status={}  contentType={}", | |
| 151 |         GE_ENDPOINT, | |
| 152 |         re.getStatusCode(), | |
| 153 |         re.getHeaders().getContentType()); | |
| 154 | 
1
1. getGeInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getGeInfo → KILLED | 
    return re.getBody(); | 
| 155 |   } | |
| 156 | ||
| 157 |   /** | |
| 158 |    * This method retrieves exactly one section matching the enrollCode and quarter arguments, if | |
| 159 |    * such a section exists. | |
| 160 |    */ | |
| 161 |   public String getSection(String enrollCode, String quarter) throws Exception { | |
| 162 | ||
| 163 |     HttpHeaders headers = new HttpHeaders(); | |
| 164 | 
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 165 | 
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 166 | 
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "1.0"); | 
| 167 | 
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 168 | ||
| 169 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 170 | ||
| 171 |     String url = SECTION_ENDPOINT; | |
| 172 | ||
| 173 |     log.info("url=" + url); | |
| 174 | ||
| 175 |     Map<String, String> params = new HashMap<>(); | |
| 176 |     params.put("quarter", quarter); | |
| 177 |     params.put("enrollcode", enrollCode); | |
| 178 | ||
| 179 |     String retVal = ""; | |
| 180 |     MediaType contentType = null; | |
| 181 |     HttpStatus statusCode = null; | |
| 182 | ||
| 183 |     ResponseEntity<String> re = | |
| 184 |         restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
| 185 |     contentType = re.getHeaders().getContentType(); | |
| 186 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 187 |     retVal = re.getBody(); | |
| 188 | ||
| 189 | 
1
1. getSection : negated conditional → KILLED | 
    if (retVal.equals("null")) { | 
| 190 |       retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
| 191 |     } | |
| 192 | ||
| 193 |     log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 194 | 
1
1. getSection : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSection → KILLED | 
    return retVal; | 
| 195 |   } | |
| 196 | ||
| 197 |   /** | |
| 198 |    * This method retrieves all of the sections related to a certain enroll code. For example, if the | |
| 199 |    * enrollCode is for a discussion section, the lecture section and all related discussion sections | |
| 200 |    * will also be returned. | |
| 201 |    */ | |
| 202 |   public String getAllSections(String enrollCode, String quarter) throws Exception { | |
| 203 | ||
| 204 |     HttpHeaders headers = new HttpHeaders(); | |
| 205 | 
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 206 | 
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 207 | 
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "3.0"); | 
| 208 | 
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 209 | ||
| 210 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 211 | ||
| 212 |     String url = ALL_SECTIONS_ENDPOINT; | |
| 213 | ||
| 214 |     log.info("url=" + url); | |
| 215 | ||
| 216 |     Map<String, String> params = new HashMap<>(); | |
| 217 |     params.put("quarter", quarter); | |
| 218 |     params.put("enrollcode", enrollCode); | |
| 219 | ||
| 220 |     String retVal = ""; | |
| 221 |     MediaType contentType = null; | |
| 222 |     HttpStatus statusCode = null; | |
| 223 | ||
| 224 |     ResponseEntity<String> re = | |
| 225 |         restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
| 226 |     contentType = re.getHeaders().getContentType(); | |
| 227 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 228 |     retVal = re.getBody(); | |
| 229 | ||
| 230 | 
1
1. getAllSections : negated conditional → KILLED | 
    if (retVal.equals("null")) { | 
| 231 |       retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
| 232 |     } | |
| 233 | ||
| 234 |     log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 235 | 
1
1. getAllSections : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getAllSections → KILLED | 
    return retVal; | 
| 236 |   } | |
| 237 | ||
| 238 |   public String getJSONbyQtrEnrollCd(String quarter, String enrollCd) throws Exception { | |
| 239 | ||
| 240 |     HttpHeaders headers = new HttpHeaders(); | |
| 241 | 
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 242 | 
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 243 | 
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "1.0"); | 
| 244 | 
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 245 | ||
| 246 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 247 | ||
| 248 |     String url = | |
| 249 |         "https://api.ucsb.edu/academics/curriculums/v3/classsection/" + quarter + "/" + enrollCd; | |
| 250 | ||
| 251 |     log.info("url=" + url); | |
| 252 | ||
| 253 |     String retVal = ""; | |
| 254 |     MediaType contentType = null; | |
| 255 |     HttpStatus statusCode = null; | |
| 256 | ||
| 257 |     ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 258 |     contentType = re.getHeaders().getContentType(); | |
| 259 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 260 |     retVal = re.getBody(); | |
| 261 | ||
| 262 |     log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 263 | 
1
1. getJSONbyQtrEnrollCd : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSONbyQtrEnrollCd → KILLED | 
    return retVal; | 
| 264 |   } | |
| 265 | ||
| 266 |   public String getFinalsInfo(String quarter, String enrollCd) throws Exception { | |
| 267 |     HttpHeaders headers = new HttpHeaders(); | |
| 268 | 
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 269 | 
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "3.0"); | 
| 270 | 
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 271 | ||
| 272 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 273 | ||
| 274 |     String params = String.format("?quarter=%s&enrollCode=%s", quarter, enrollCd); | |
| 275 |     String url = FINALS_ENDPOINT + params; | |
| 276 | ||
| 277 |     log.info("url=" + url); | |
| 278 | ||
| 279 |     String retVal; | |
| 280 |     MediaType contentType; | |
| 281 |     HttpStatus statusCode; | |
| 282 | ||
| 283 |     ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 284 |     contentType = re.getHeaders().getContentType(); | |
| 285 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 286 |     retVal = re.getBody(); | |
| 287 | ||
| 288 |     log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 289 | 
1
1. getFinalsInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getFinalsInfo → KILLED | 
    return retVal; | 
| 290 |   } | |
| 291 | } | |
Mutations | ||
| 59 | 
 
 1.1  | 
|
| 60 | 
 
 1.1  | 
|
| 61 | 
 
 1.1  | 
|
| 62 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 93 | 
 
 1.1  | 
|
| 101 | 
 
 1.1  | 
|
| 110 | 
 
 1.1  | 
|
| 116 | 
 
 1.1  | 
|
| 117 | 
 
 1.1  | 
|
| 118 | 
 
 1.1  | 
|
| 119 | 
 
 1.1  | 
|
| 136 | 
 
 1.1  | 
|
| 141 | 
 
 1.1  | 
|
| 142 | 
 
 1.1  | 
|
| 143 | 
 
 1.1  | 
|
| 154 | 
 
 1.1  | 
|
| 164 | 
 
 1.1  | 
|
| 165 | 
 
 1.1  | 
|
| 166 | 
 
 1.1  | 
|
| 167 | 
 
 1.1  | 
|
| 189 | 
 
 1.1  | 
|
| 194 | 
 
 1.1  | 
|
| 205 | 
 
 1.1  | 
|
| 206 | 
 
 1.1  | 
|
| 207 | 
 
 1.1  | 
|
| 208 | 
 
 1.1  | 
|
| 230 | 
 
 1.1  | 
|
| 235 | 
 
 1.1  | 
|
| 241 | 
 
 1.1  | 
|
| 242 | 
 
 1.1  | 
|
| 243 | 
 
 1.1  | 
|
| 244 | 
 
 1.1  | 
|
| 263 | 
 
 1.1  | 
|
| 268 | 
 
 1.1  | 
|
| 269 | 
 
 1.1  | 
|
| 270 | 
 
 1.1  | 
|
| 289 | 
 
 1.1  |