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