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