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 UCSBAPIQuarter getCurrentQuarter() throws Exception { | |
68 | HttpHeaders headers = new HttpHeaders(); | |
69 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
70 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
71 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
72 |
1
1. getCurrentQuarter : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
73 | ||
74 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
75 | ||
76 | String url = CURRENT_QUARTER_ENDPOINT; | |
77 | ||
78 | log.info("url=" + url); | |
79 | ||
80 | String retVal = ""; | |
81 | MediaType contentType = null; | |
82 | HttpStatus statusCode = null; | |
83 | ||
84 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
85 | contentType = re.getHeaders().getContentType(); | |
86 | statusCode = (HttpStatus) re.getStatusCode(); | |
87 | retVal = re.getBody(); | |
88 | ||
89 | log.info( | |
90 | "json: {} contentType: {} statusCode: {} entity: {}", | |
91 | retVal, | |
92 | contentType, | |
93 | statusCode, | |
94 | entity); | |
95 | UCSBAPIQuarter quarter = null; | |
96 | quarter = objectMapper.readValue(retVal, UCSBAPIQuarter.class); | |
97 |
1
1. getCurrentQuarter : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getCurrentQuarter → KILLED |
return quarter; |
98 | } | |
99 | ||
100 | public List<UCSBAPIQuarter> getAllQuarters() throws Exception { | |
101 | List<UCSBAPIQuarter> quarters = ucsbApiQuarterRepository.findAll(); | |
102 |
1
1. getAllQuarters : negated conditional → KILLED |
if (quarters.isEmpty()) { |
103 | quarters = this.loadAllQuarters(); | |
104 | } | |
105 |
1
1. getAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuarters → KILLED |
return quarters; |
106 | } | |
107 | ||
108 | public List<UCSBAPIQuarter> getAllQuartersFromAPI() throws Exception { | |
109 | HttpHeaders headers = new HttpHeaders(); | |
110 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
111 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
112 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
113 |
1
1. getAllQuartersFromAPI : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
114 | ||
115 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
116 | ||
117 | String url = ALL_QUARTERS_ENDPOINT; | |
118 | ||
119 | log.info("url=" + url); | |
120 | ||
121 | String retVal = ""; | |
122 | MediaType contentType = null; | |
123 | HttpStatus statusCode = null; | |
124 | ||
125 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
126 | contentType = re.getHeaders().getContentType(); | |
127 | statusCode = (HttpStatus) re.getStatusCode(); | |
128 | retVal = re.getBody(); | |
129 | ||
130 | log.info( | |
131 | "json: {} contentType: {} statusCode: {} entity: {}", | |
132 | retVal, | |
133 | contentType, | |
134 | statusCode, | |
135 | entity); | |
136 | List<UCSBAPIQuarter> quarters = null; | |
137 | quarters = objectMapper.readValue(retVal, new TypeReference<List<UCSBAPIQuarter>>() {}); | |
138 |
1
1. getAllQuartersFromAPI : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getAllQuartersFromAPI → KILLED |
return quarters; |
139 | } | |
140 | ||
141 | public boolean quarterYYYYQInRange(String quarterYYYYQ) { | |
142 |
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED |
boolean dateGEStart = quarterYYYYQ.compareTo(startQtrYYYYQ) >= 0; |
143 |
2
1. quarterYYYYQInRange : changed conditional boundary → KILLED 2. quarterYYYYQInRange : negated conditional → KILLED |
boolean dateLEEnd = quarterYYYYQ.compareTo(endQtrYYYYQ) <= 0; |
144 |
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); |
145 | } | |
146 | ||
147 | public List<UCSBAPIQuarter> loadAllQuarters() throws Exception { | |
148 | List<UCSBAPIQuarter> quarters = this.getAllQuartersFromAPI(); | |
149 | List<UCSBAPIQuarter> savedQuarters = new ArrayList<UCSBAPIQuarter>(); | |
150 |
1
1. loadAllQuarters : removed call to java/util/List::forEach → KILLED |
quarters.forEach( |
151 | (quarter) -> { | |
152 |
1
1. lambda$loadAllQuarters$0 : negated conditional → KILLED |
if (quarterYYYYQInRange(quarter.getQuarter())) { |
153 | ucsbApiQuarterRepository.save(quarter); | |
154 | savedQuarters.add(quarter); | |
155 | } | |
156 | }); | |
157 | log.info("savedQuarters.size={}", savedQuarters.size()); | |
158 |
1
1. loadAllQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::loadAllQuarters → KILLED |
return savedQuarters; |
159 | } | |
160 | ||
161 | public List<String> getActiveQuarters() throws Exception { | |
162 | List<String> activeQuarters = new ArrayList<>(); | |
163 | Quarter.quarterList(getCurrentQuarterYYYYQ(), getEndQtrYYYYQ()) | |
164 |
1
1. getActiveQuarters : removed call to java/util/List::forEach → KILLED |
.forEach(quarter -> activeQuarters.add(quarter.getYYYYQ())); |
165 |
1
1. getActiveQuarters : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBAPIQuarterService::getActiveQuarters → KILLED |
return activeQuarters; |
166 | } | |
167 | } | |
Mutations | ||
55 |
1.1 |
|
59 |
1.1 |
|
64 |
1.1 |
|
69 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
72 |
1.1 |
|
97 |
1.1 |
|
102 |
1.1 |
|
105 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
112 |
1.1 |
|
113 |
1.1 |
|
138 |
1.1 |
|
142 |
1.1 2.2 |
|
143 |
1.1 2.2 |
|
144 |
1.1 2.2 3.3 |
|
150 |
1.1 |
|
152 |
1.1 |
|
158 |
1.1 |
|
164 |
1.1 |
|
165 |
1.1 |