| 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.util.ArrayList; | |
| 9 | import java.util.Arrays; | |
| 10 | import java.util.List; | |
| 11 | import lombok.extern.slf4j.Slf4j; | |
| 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.RestTemplate; | |
| 23 | ||
| 24 | /** Service object that wraps the UCSB Academic Curriculum API */ | |
| 25 | @Service | |
| 26 | @Slf4j | |
| 27 | public class UCSBAPIQuarterService { | |
| 28 | ||
| 29 |   @Value("${app.startQtrYYYYQ:20221}") | |
| 30 |   private String startQtrYYYYQ; | |
| 31 | ||
| 32 |   @Value("${app.endQtrYYYYQ:20222}") | |
| 33 |   private String endQtrYYYYQ; | |
| 34 | ||
| 35 |   @Autowired private ObjectMapper objectMapper; | |
| 36 | ||
| 37 |   @Autowired UCSBAPIQuarterRepository ucsbApiQuarterRepository; | |
| 38 | ||
| 39 |   @Value("${app.ucsb.api.consumer_key}") | |
| 40 |   private String apiKey; | |
| 41 | ||
| 42 |   private RestTemplate restTemplate = new RestTemplate(); | |
| 43 | ||
| 44 |   public UCSBAPIQuarterService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 45 |     restTemplate = restTemplateBuilder.build(); | |
| 46 |   } | |
| 47 | ||
| 48 |   public static final String CURRENT_QUARTER_ENDPOINT = | |
| 49 |       "https://api.ucsb.edu/academics/quartercalendar/v1/quarters/current"; | |
| 50 | ||
| 51 |   public static final String ALL_QUARTERS_ENDPOINT = | |
| 52 |       "https://api.ucsb.edu/academics/quartercalendar/v1/quarters"; | |
| 53 | ||
| 54 |   public String getStartQtrYYYYQ() { | |
| 55 | 
1
1. getStartQtrYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getStartQtrYYYYQ → KILLED | 
    return startQtrYYYYQ; | 
| 56 |   } | |
| 57 | ||
| 58 |   public String getEndQtrYYYYQ() { | |
| 59 | 
1
1. getEndQtrYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getEndQtrYYYYQ → KILLED | 
    return endQtrYYYYQ; | 
| 60 |   } | |
| 61 | ||
| 62 |   public String getCurrentQuarterYYYYQ() throws Exception { | |
| 63 |     UCSBAPIQuarter quarter = getCurrentQuarter(); | |
| 64 | 
1
1. getCurrentQuarterYYYYQ : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarterYYYYQ → KILLED | 
    return quarter.getQuarter(); | 
| 65 |   } | |
| 66 | ||
| 67 |   public ArrayList<String> getActiveQuarterList() throws Exception { | |
| 68 | ||
| 69 |     String current = getCurrentQuarterYYYYQ(); | |
| 70 |     String end = getEndQtrYYYYQ(); | |
| 71 |     List<Quarter> quarters = Quarter.quarterList(current, end); | |
| 72 |     ArrayList<String> results = new ArrayList<>(); | |
| 73 |     for (Quarter quarter : quarters) { | |
| 74 |       results.add(quarter.getYYYYQ()); | |
| 75 |     } | |
| 76 | ||
| 77 | 
1
1. getActiveQuarterList : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarterList → KILLED | 
    return results; | 
| 78 |   } | |
| 79 | ||
| 80 |   public UCSBAPIQuarter getCurrentQuarter() throws Exception { | |
| 81 |     HttpHeaders headers = new HttpHeaders(); | |
| 82 | 
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 83 | 
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 84 | 
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "1.0"); | 
| 85 | 
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 86 | ||
| 87 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 88 | ||
| 89 |     String url = CURRENT_QUARTER_ENDPOINT; | |
| 90 | ||
| 91 |     log.info("url=" + url); | |
| 92 | ||
| 93 |     String retVal = ""; | |
| 94 |     MediaType contentType = null; | |
| 95 |     HttpStatus statusCode = null; | |
| 96 | ||
| 97 |     ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 98 |     contentType = re.getHeaders().getContentType(); | |
| 99 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 100 |     retVal = re.getBody(); | |
| 101 | ||
| 102 |     log.info( | |
| 103 |         "json: {} contentType: {} statusCode: {} entity: {}", | |
| 104 |         retVal, | |
| 105 |         contentType, | |
| 106 |         statusCode, | |
| 107 |         entity); | |
| 108 |     UCSBAPIQuarter quarter = null; | |
| 109 |     quarter = objectMapper.readValue(retVal, UCSBAPIQuarter.class); | |
| 110 | 
1
1. getCurrentQuarter : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED | 
    return quarter; | 
| 111 |   } | |
| 112 | ||
| 113 |   public List<UCSBAPIQuarter> getAllQuarters() throws Exception { | |
| 114 |     List<UCSBAPIQuarter> quarters = ucsbApiQuarterRepository.findAll(); | |
| 115 | 
1
1. getAllQuarters : negated conditional → KILLED | 
    if (quarters.isEmpty()) { | 
| 116 |       quarters = this.loadAllQuarters(); | |
| 117 |     } | |
| 118 | 
1
1. getAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuarters → KILLED | 
    return quarters; | 
| 119 |   } | |
| 120 | ||
| 121 |   public List<UCSBAPIQuarter> getAllQuartersFromAPI() throws Exception { | |
| 122 |     HttpHeaders headers = new HttpHeaders(); | |
| 123 | 
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED | 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | 
| 124 | 
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED | 
    headers.setContentType(MediaType.APPLICATION_JSON); | 
| 125 | 
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-version", "1.0"); | 
| 126 | 
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED | 
    headers.set("ucsb-api-key", this.apiKey); | 
| 127 | ||
| 128 |     HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 129 | ||
| 130 |     String url = ALL_QUARTERS_ENDPOINT; | |
| 131 | ||
| 132 |     log.info("url=" + url); | |
| 133 | ||
| 134 |     String retVal = ""; | |
| 135 |     MediaType contentType = null; | |
| 136 |     HttpStatus statusCode = null; | |
| 137 | ||
| 138 |     ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 139 |     contentType = re.getHeaders().getContentType(); | |
| 140 |     statusCode = (HttpStatus) re.getStatusCode(); | |
| 141 |     retVal = re.getBody(); | |
| 142 | ||
| 143 |     log.info( | |
| 144 |         "json: {} contentType: {} statusCode: {} entity: {}", | |
| 145 |         retVal, | |
| 146 |         contentType, | |
| 147 |         statusCode, | |
| 148 |         entity); | |
| 149 |     List<UCSBAPIQuarter> quarters = null; | |
| 150 |     quarters = objectMapper.readValue(retVal, new TypeReference<List<UCSBAPIQuarter>>() {}); | |
| 151 | 
1
1. getAllQuartersFromAPI : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuartersFromAPI → KILLED | 
    return quarters; | 
| 152 |   } | |
| 153 | ||
| 154 |   public boolean quarterYYYYQInRange(String quarterYYYYQ) { | |
| 155 | 
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED  | 
    boolean dateGEStart = quarterYYYYQ.compareTo(startQtrYYYYQ) >= 0; | 
| 156 | 
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED  | 
    boolean dateLEEnd = quarterYYYYQ.compareTo(endQtrYYYYQ) <= 0; | 
| 157 | 
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); | 
| 158 |   } | |
| 159 | ||
| 160 |   public List<UCSBAPIQuarter> loadAllQuarters() throws Exception { | |
| 161 |     List<UCSBAPIQuarter> quarters = this.getAllQuartersFromAPI(); | |
| 162 |     List<UCSBAPIQuarter> savedQuarters = new ArrayList<UCSBAPIQuarter>(); | |
| 163 | 
1
1. loadAllQuarters : removed call to java/util/List::forEach → KILLED | 
    quarters.forEach( | 
| 164 |         (quarter) -> { | |
| 165 | 
1
1. lambda$loadAllQuarters$0 : negated conditional → KILLED | 
          if (quarterYYYYQInRange(quarter.getQuarter())) { | 
| 166 |             ucsbApiQuarterRepository.save(quarter); | |
| 167 |             savedQuarters.add(quarter); | |
| 168 |           } | |
| 169 |         }); | |
| 170 |     log.info("savedQuarters.size={}", savedQuarters.size()); | |
| 171 | 
1
1. loadAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::loadAllQuarters → KILLED | 
    return savedQuarters; | 
| 172 |   } | |
| 173 | } | |
Mutations | ||
| 55 | 
 
 1.1  | 
|
| 59 | 
 
 1.1  | 
|
| 64 | 
 
 1.1  | 
|
| 77 | 
 
 1.1  | 
|
| 82 | 
 
 1.1  | 
|
| 83 | 
 
 1.1  | 
|
| 84 | 
 
 1.1  | 
|
| 85 | 
 
 1.1  | 
|
| 110 | 
 
 1.1  | 
|
| 115 | 
 
 1.1  | 
|
| 118 | 
 
 1.1  | 
|
| 123 | 
 
 1.1  | 
|
| 124 | 
 
 1.1  | 
|
| 125 | 
 
 1.1  | 
|
| 126 | 
 
 1.1  | 
|
| 151 | 
 
 1.1  | 
|
| 155 | 
 
 1.1 2.2  | 
|
| 156 | 
 
 1.1 2.2  | 
|
| 157 | 
 
 1.1 2.2 3.3  | 
|
| 163 | 
 
 1.1  | 
|
| 165 | 
 
 1.1  | 
|
| 171 | 
 
 1.1  |