| 1 | package edu.ucsb.cs156.courses.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.courses.entities.UCSBAPIQuarter; | |
| 6 | import edu.ucsb.cs156.courses.models.Quarter; | |
| 7 | import edu.ucsb.cs156.courses.repositories.UCSBAPIQuarterRepository; | |
| 8 | import java.time.Instant; | |
| 9 | import java.time.LocalDateTime; | |
| 10 | import java.time.temporal.ChronoUnit; | |
| 11 | import java.util.ArrayList; | |
| 12 | import java.util.Arrays; | |
| 13 | import java.util.List; | |
| 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.cache.annotation.CacheEvict; | |
| 19 | import org.springframework.cache.annotation.Cacheable; | |
| 20 | import org.springframework.http.HttpEntity; | |
| 21 | import org.springframework.http.HttpHeaders; | |
| 22 | import org.springframework.http.HttpMethod; | |
| 23 | import org.springframework.http.HttpStatus; | |
| 24 | import org.springframework.http.MediaType; | |
| 25 | import org.springframework.http.ResponseEntity; | |
| 26 | import org.springframework.scheduling.annotation.Scheduled; | |
| 27 | import org.springframework.stereotype.Service; | |
| 28 | import org.springframework.web.client.RestTemplate; | |
| 29 | ||
| 30 | /** Service object that wraps the UCSB Academic Curriculum API */ | |
| 31 | @Service | |
| 32 | @Slf4j | |
| 33 | public class UCSBAPIQuarterService { | |
| 34 | ||
| 35 | @Value("${app.startQtrYYYYQ:20221}") | |
| 36 | private String startQtrYYYYQ; | |
| 37 | ||
| 38 | private String endQtrYYYYQ; | |
| 39 | ||
| 40 | @Autowired private ObjectMapper objectMapper; | |
| 41 | ||
| 42 | @Autowired UCSBAPIQuarterRepository ucsbApiQuarterRepository; | |
| 43 | ||
| 44 | @Value("${app.ucsb.api.consumer_key}") | |
| 45 | private String apiKey; | |
| 46 | ||
| 47 | private RestTemplate restTemplate = new RestTemplate(); | |
| 48 | ||
| 49 | public UCSBAPIQuarterService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 50 | restTemplate = restTemplateBuilder.build(); | |
| 51 | } | |
| 52 | ||
| 53 | public static final String CURRENT_QUARTER_ENDPOINT = | |
| 54 | "https://api.ucsb.edu/academics/quartercalendar/v1/quarters/current"; | |
| 55 | ||
| 56 | public static final int CACHE_DURATION_HOURS = 24; | |
| 57 | ||
| 58 | private UCSBAPIQuarter cachedCurrentQuarter = null; | |
| 59 | private Instant cacheTime = null; | |
| 60 | ||
| 61 | void clearCurrentQuarterCache() { | |
| 62 | cachedCurrentQuarter = null; | |
| 63 | cacheTime = null; | |
| 64 | } | |
| 65 | ||
| 66 | void expireCurrentQuarterCache() { | |
| 67 | cacheTime = Instant.EPOCH; | |
| 68 | } | |
| 69 | ||
| 70 | public static final String ALL_QUARTERS_ENDPOINT = | |
| 71 | "https://api.ucsb.edu/academics/quartercalendar/v1/quarters"; | |
| 72 | ||
| 73 | public String getStartQtrYYYYQ() { | |
| 74 |
1
1. getStartQtrYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getStartQtrYYYYQ → KILLED |
return startQtrYYYYQ; |
| 75 | } | |
| 76 | ||
| 77 | public String getEndQtrYYYYQ() { | |
| 78 |
1
1. getEndQtrYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getEndQtrYYYYQ → KILLED |
return endQtrYYYYQ; |
| 79 | } | |
| 80 | ||
| 81 | public void setEndQtrYYYYQ(String YYYYQ) { | |
| 82 | this.endQtrYYYYQ = YYYYQ; | |
| 83 | } | |
| 84 | ||
| 85 | public String getCurrentQuarterYYYYQ() throws Exception { | |
| 86 | UCSBAPIQuarter quarter = getCurrentQuarter(); | |
| 87 |
1
1. getCurrentQuarterYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarterYYYYQ → KILLED |
return quarter.getQuarter(); |
| 88 | } | |
| 89 | ||
| 90 | public String getCurrentEndQuarterYYYYQ() throws Exception { | |
| 91 | String quarter = getCurrentQuarter().getQuarter(); | |
| 92 |
1
1. getCurrentEndQuarterYYYYQ : negated conditional → KILLED |
if (Quarter.getQ(Quarter.yyyyqToInt(quarter)) == "S") { |
| 93 | quarter = Quarter.increment(quarter); | |
| 94 | } | |
| 95 | quarter = Quarter.increment(quarter); | |
| 96 |
1
1. getCurrentEndQuarterYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentEndQuarterYYYYQ → KILLED |
return quarter; |
| 97 | } | |
| 98 | ||
| 99 | public List<String> getActiveQuarterList() throws Exception { | |
| 100 | String start = getCurrentQuarterYYYYQ(); | |
| 101 | String end = getEndQtrYYYYQ(); | |
| 102 | ||
| 103 | List<Quarter> quartersInOrder = Quarter.quarterList(start, end); | |
| 104 | List<String> result = new ArrayList<String>(); | |
| 105 | ||
| 106 | for (Quarter quarter : quartersInOrder) { | |
| 107 | result.add(quarter.getYYYYQ()); | |
| 108 | } | |
| 109 | ||
| 110 |
1
1. getActiveQuarterList : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarterList → KILLED |
return result; |
| 111 | } | |
| 112 | ||
| 113 | // This empties the cache called currentQuarter | |
| 114 | // every night at midnight | |
| 115 | @Scheduled(cron = "0 0 0 * * *") | |
| 116 | @CacheEvict(value = "currentQuarter", allEntries = true) | |
| 117 | void evictCurrentQuarterAtMidnight() {} | |
| 118 | ||
| 119 | // This says to cache the value in a cache called | |
| 120 | // currentQuarter, and only allow one call at a time | |
| 121 | @Cacheable(value = "currentQuarter", sync = true) | |
| 122 | public UCSBAPIQuarter getCurrentQuarter() throws Exception { | |
| 123 |
1
1. getCurrentQuarter : negated conditional → KILLED |
if (cachedCurrentQuarter != null |
| 124 |
1
1. getCurrentQuarter : negated conditional → KILLED |
&& Instant.now().isBefore(cacheTime.plus(CACHE_DURATION_HOURS, ChronoUnit.HOURS))) { |
| 125 |
1
1. getCurrentQuarter : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED |
return cachedCurrentQuarter; |
| 126 | } | |
| 127 | ||
| 128 | HttpHeaders headers = new HttpHeaders(); | |
| 129 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 130 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 131 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 132 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 133 | ||
| 134 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 135 | ||
| 136 | String url = CURRENT_QUARTER_ENDPOINT; | |
| 137 | ||
| 138 | log.info("url=" + url); | |
| 139 | ||
| 140 | String retVal = ""; | |
| 141 | MediaType contentType = null; | |
| 142 | HttpStatus statusCode = null; | |
| 143 | ||
| 144 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 145 | contentType = re.getHeaders().getContentType(); | |
| 146 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 147 | retVal = re.getBody(); | |
| 148 | ||
| 149 | log.info("contentType: {} statusCode: {} entity: {}", contentType, statusCode, entity); | |
| 150 | log.trace("json: {}", retVal); | |
| 151 | UCSBAPIQuarter quarter = null; | |
| 152 | quarter = objectMapper.readValue(retVal, UCSBAPIQuarter.class); | |
| 153 | cachedCurrentQuarter = quarter; | |
| 154 | cacheTime = Instant.now(); | |
| 155 |
1
1. getCurrentQuarter : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED |
return quarter; |
| 156 | } | |
| 157 | ||
| 158 | public List<UCSBAPIQuarter> getAllQuarters() throws Exception { | |
| 159 | List<UCSBAPIQuarter> quarters = ucsbApiQuarterRepository.findAll(); | |
| 160 |
1
1. getAllQuarters : negated conditional → KILLED |
if (quarters.isEmpty()) { |
| 161 | quarters = this.loadAllQuarters(); | |
| 162 | } | |
| 163 |
1
1. getAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuarters → KILLED |
return quarters; |
| 164 | } | |
| 165 | ||
| 166 | public List<UCSBAPIQuarter> getAllQuartersFromAPI() throws Exception { | |
| 167 | HttpHeaders headers = new HttpHeaders(); | |
| 168 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 169 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 170 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 171 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 172 | ||
| 173 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 174 | ||
| 175 | String url = ALL_QUARTERS_ENDPOINT; | |
| 176 | ||
| 177 | log.info("url=" + url); | |
| 178 | ||
| 179 | String retVal = ""; | |
| 180 | MediaType contentType = null; | |
| 181 | HttpStatus statusCode = null; | |
| 182 | ||
| 183 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 184 | contentType = re.getHeaders().getContentType(); | |
| 185 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 186 | retVal = re.getBody(); | |
| 187 | ||
| 188 | log.info( | |
| 189 | "json: {} contentType: {} statusCode: {} entity: {}", | |
| 190 | retVal, | |
| 191 | contentType, | |
| 192 | statusCode, | |
| 193 | entity); | |
| 194 | List<UCSBAPIQuarter> quarters = null; | |
| 195 | quarters = objectMapper.readValue(retVal, new TypeReference<List<UCSBAPIQuarter>>() {}); | |
| 196 |
1
1. getAllQuartersFromAPI : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuartersFromAPI → KILLED |
return quarters; |
| 197 | } | |
| 198 | ||
| 199 | public boolean quarterYYYYQInRange(String quarterYYYYQ) { | |
| 200 |
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED |
boolean dateGEStart = quarterYYYYQ.compareTo(startQtrYYYYQ) >= 0; |
| 201 |
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED |
boolean dateLEEnd = quarterYYYYQ.compareTo(getEndQtrYYYYQ()) <= 0; |
| 202 |
3
1. quarterYYYYQInRange : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::quarterYYYYQInRange → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED 3. quarterYYYYQInRange : negated conditional → KILLED |
return (dateGEStart && dateLEEnd); |
| 203 | } | |
| 204 | ||
| 205 | public List<UCSBAPIQuarter> loadAllQuarters() throws Exception { | |
| 206 | List<UCSBAPIQuarter> quarters = this.getAllQuartersFromAPI(); | |
| 207 | List<UCSBAPIQuarter> savedQuarters = new ArrayList<UCSBAPIQuarter>(); | |
| 208 |
1
1. loadAllQuarters : removed call to java/util/List::forEach → KILLED |
quarters.forEach( |
| 209 | (quarter) -> { | |
| 210 |
1
1. lambda$loadAllQuarters$0 : negated conditional → KILLED |
if (quarterYYYYQInRange(quarter.getQuarter())) { |
| 211 | ucsbApiQuarterRepository.save(quarter); | |
| 212 | savedQuarters.add(quarter); | |
| 213 | } | |
| 214 | }); | |
| 215 | log.info("savedQuarters.size={}", savedQuarters.size()); | |
| 216 |
1
1. loadAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::loadAllQuarters → KILLED |
return savedQuarters; |
| 217 | } | |
| 218 | ||
| 219 | public List<String> getActiveQuarters() throws Exception { | |
| 220 | List<String> activeQuarters = new ArrayList<>(); | |
| 221 | String currQtr = getCurrentQuarterYYYYQ(); | |
| 222 | String endQtr = getEndQtrYYYYQ(); | |
| 223 | ||
| 224 |
2
1. getActiveQuarters : negated conditional → KILLED 2. getActiveQuarters : changed conditional boundary → KILLED |
if (currQtr.compareTo(endQtr) <= 0) { |
| 225 | Quarter.quarterList(currQtr, endQtr) | |
| 226 |
1
1. getActiveQuarters : removed call to java/util/List::forEach → KILLED |
.forEach(quarter -> activeQuarters.add(quarter.getYYYYQ())); |
| 227 | } | |
| 228 | ||
| 229 |
1
1. getActiveQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarters → KILLED |
return activeQuarters; |
| 230 | } | |
| 231 | ||
| 232 | public LocalDateTime lastDayToRegister(UCSBAPIQuarter ucsbApiQuarter) { | |
| 233 |
1
1. lastDayToRegister : negated conditional → KILLED |
if (ucsbApiQuarter == null) { |
| 234 | return null; | |
| 235 | } | |
| 236 | ||
| 237 | LocalDateTime lastDayToAddUndergrad = ucsbApiQuarter.getLastDayToAddUnderGrad(); | |
| 238 | LocalDateTime lastDayToAddGrad = ucsbApiQuarter.getLastDayToAddGrad(); | |
| 239 | ||
| 240 |
2
1. lastDayToRegister : negated conditional → KILLED 2. lastDayToRegister : negated conditional → KILLED |
if (lastDayToAddUndergrad == null || lastDayToAddGrad == null) { |
| 241 | return null; | |
| 242 | } | |
| 243 | ||
| 244 |
2
1. lastDayToRegister : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::lastDayToRegister → KILLED 2. lastDayToRegister : negated conditional → KILLED |
return lastDayToAddUndergrad.isAfter(lastDayToAddGrad) |
| 245 | ? lastDayToAddUndergrad | |
| 246 | : lastDayToAddGrad; | |
| 247 | } | |
| 248 | ||
| 249 | public boolean isQuarterInRegistrationPass(String quarterYYYYQ) { | |
| 250 | UCSBAPIQuarter quarter = ucsbApiQuarterRepository.findById(quarterYYYYQ).orElse(null); | |
| 251 | LocalDateTime lastDay = lastDayToRegister(quarter); | |
| 252 | ||
| 253 |
1
1. isQuarterInRegistrationPass : negated conditional → KILLED |
if (quarter == null) { |
| 254 |
1
1. isQuarterInRegistrationPass : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::isQuarterInRegistrationPass → KILLED |
return false; |
| 255 | } | |
| 256 | ||
| 257 |
1
1. isQuarterInRegistrationPass : negated conditional → KILLED |
if (lastDay == null) { |
| 258 |
1
1. isQuarterInRegistrationPass : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::isQuarterInRegistrationPass → KILLED |
return false; |
| 259 | } | |
| 260 | ||
| 261 | LocalDateTime pass1Begin = quarter.getPass1Begin(); | |
| 262 | ||
| 263 |
1
1. isQuarterInRegistrationPass : negated conditional → KILLED |
if (pass1Begin == null) { |
| 264 |
1
1. isQuarterInRegistrationPass : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::isQuarterInRegistrationPass → KILLED |
return false; |
| 265 | } | |
| 266 | ||
| 267 | LocalDateTime currentDate = LocalDateTime.now(); | |
| 268 |
3
1. isQuarterInRegistrationPass : negated conditional → KILLED 2. isQuarterInRegistrationPass : negated conditional → KILLED 3. isQuarterInRegistrationPass : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::isQuarterInRegistrationPass → KILLED |
return currentDate.isAfter(pass1Begin) && currentDate.isBefore(lastDay); |
| 269 | } | |
| 270 | ||
| 271 | public List<String> getActiveRegistrationQuarters() throws Exception { | |
| 272 | ||
| 273 | List<String> activeQuarters = getActiveQuarters(); | |
| 274 | ||
| 275 | List<String> registrationQuarters = | |
| 276 |
2
1. lambda$getActiveRegistrationQuarters$2 : replaced boolean return with false for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::lambda$getActiveRegistrationQuarters$2 → KILLED 2. lambda$getActiveRegistrationQuarters$2 : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::lambda$getActiveRegistrationQuarters$2 → KILLED |
activeQuarters.stream().filter(yyyyq -> isQuarterInRegistrationPass(yyyyq)).toList(); |
| 277 |
1
1. getActiveRegistrationQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveRegistrationQuarters → KILLED |
return registrationQuarters; |
| 278 | } | |
| 279 | } | |
Mutations | ||
| 74 |
1.1 |
|
| 78 |
1.1 |
|
| 87 |
1.1 |
|
| 92 |
1.1 |
|
| 96 |
1.1 |
|
| 110 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 125 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |
|
| 132 |
1.1 |
|
| 155 |
1.1 |
|
| 160 |
1.1 |
|
| 163 |
1.1 |
|
| 168 |
1.1 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 196 |
1.1 |
|
| 200 |
1.1 2.2 |
|
| 201 |
1.1 2.2 |
|
| 202 |
1.1 2.2 3.3 |
|
| 208 |
1.1 |
|
| 210 |
1.1 |
|
| 216 |
1.1 |
|
| 224 |
1.1 2.2 |
|
| 226 |
1.1 |
|
| 229 |
1.1 |
|
| 233 |
1.1 |
|
| 240 |
1.1 2.2 |
|
| 244 |
1.1 2.2 |
|
| 253 |
1.1 |
|
| 254 |
1.1 |
|
| 257 |
1.1 |
|
| 258 |
1.1 |
|
| 263 |
1.1 |
|
| 264 |
1.1 |
|
| 268 |
1.1 2.2 3.3 |
|
| 276 |
1.1 2.2 |
|
| 277 |
1.1 |