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