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