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