| 1 | package edu.ucsb.cs156.courses.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.courses.entities.UCSBSubject; | |
| 7 | import edu.ucsb.cs156.courses.repositories.UCSBSubjectRepository; | |
| 8 | import java.time.Duration; | |
| 9 | import java.util.ArrayList; | |
| 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.MediaType; | |
| 19 | import org.springframework.http.ResponseEntity; | |
| 20 | import org.springframework.stereotype.Service; | |
| 21 | import org.springframework.web.client.RestTemplate; | |
| 22 | ||
| 23 | @Slf4j | |
| 24 | @Service("UCSBSubjects") | |
| 25 | public class UCSBSubjectsService { | |
| 26 | ||
| 27 | @Autowired private ObjectMapper mapper; | |
| 28 | @Autowired private UCSBSubjectRepository subjectRepository; | |
| 29 | ||
| 30 | @Value("${app.ucsb.api.consumer_key}") | |
| 31 | private String apiKey; | |
| 32 | ||
| 33 | @Value("${app.ucsb.api.host}") | |
| 34 | private String apiHost; | |
| 35 | ||
| 36 | public static final String ENDPOINT = | |
| 37 | "{apiHost}/students/lookups/v1/subjects?includeInactive=false"; | |
| 38 | ||
| 39 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 40 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 41 | ||
| 42 | private final RestTemplate restTemplate; | |
| 43 | ||
| 44 | public UCSBSubjectsService(RestTemplateBuilder restTemplateBuilder) { | |
| 45 | restTemplate = | |
| 46 | restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 47 | } | |
| 48 | ||
| 49 | public List<UCSBSubject> get() throws JsonProcessingException { | |
| 50 | ||
| 51 | HttpHeaders headers = new HttpHeaders(); | |
| 52 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 53 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 54 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 55 | ||
| 56 | String url = ENDPOINT.replace("{apiHost}", apiHost); | |
| 57 | ||
| 58 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 59 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 60 | ||
| 61 | String retBody = re.getBody(); | |
| 62 | List<UCSBSubject> subjects = | |
| 63 | mapper.readValue(retBody, new TypeReference<List<UCSBSubject>>() {}); | |
| 64 | ||
| 65 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBSubjectsService::get → KILLED |
return subjects; |
| 66 | } | |
| 67 | ||
| 68 | public List<UCSBSubject> loadAllSubjects() throws JsonProcessingException { | |
| 69 | List<UCSBSubject> subjects = this.get(); | |
| 70 | List<UCSBSubject> savedSubjects = new ArrayList<UCSBSubject>(); | |
| 71 | ||
| 72 |
1
1. loadAllSubjects : removed call to java/util/List::forEach → KILLED |
subjects.forEach( |
| 73 | (ucsbSubject) -> { | |
| 74 | subjectRepository.save(ucsbSubject); | |
| 75 | savedSubjects.add(ucsbSubject); | |
| 76 | }); | |
| 77 | log.info("subjects={}", subjects); | |
| 78 |
1
1. loadAllSubjects : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBSubjectsService::loadAllSubjects → KILLED |
return savedSubjects; |
| 79 | } | |
| 80 | } | |
Mutations | ||
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 65 |
1.1 |
|
| 72 |
1.1 |
|
| 78 |
1.1 |