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