UCSBGradeHistoryServiceImpl.java

1
package edu.ucsb.cs156.courses.services;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import com.opencsv.CSVReader;
5
import edu.ucsb.cs156.courses.entities.GradeHistory;
6
import edu.ucsb.cs156.courses.models.ApiResult;
7
import edu.ucsb.cs156.courses.models.Quarter;
8
import edu.ucsb.cs156.courses.models.TreeElement;
9
import java.io.Reader;
10
import java.io.StringReader;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.stream.Collectors;
15
import lombok.extern.slf4j.Slf4j;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.boot.web.client.RestTemplateBuilder;
18
import org.springframework.http.HttpEntity;
19
import org.springframework.http.HttpHeaders;
20
import org.springframework.http.HttpMethod;
21
import org.springframework.http.MediaType;
22
import org.springframework.http.ResponseEntity;
23
import org.springframework.stereotype.Service;
24
import org.springframework.web.client.RestTemplate;
25
26
@Service("UCSBGradeHistoryService")
27
@Slf4j
28
public class UCSBGradeHistoryServiceImpl implements UCSBGradeHistoryService {
29
30
  private final RestTemplate restTemplate;
31
32
  public UCSBGradeHistoryServiceImpl(RestTemplateBuilder restTemplateBuilder) {
33
    restTemplate = restTemplateBuilder.build();
34
  }
35
36
  @Autowired ObjectMapper mapper;
37
38
  public static final String REPO_OWNER_AND_NAME = "ucsb-cs156/UCSB_Grades";
39
  public static final String API_ENDPOINT =
40
      "https://api.github.com/repos/" + REPO_OWNER_AND_NAME + "/git/trees/main?recursive=1";
41
42
  @Override
43
  public List<String> getUrls() throws Exception {
44
45
    log.info("getting data from {}", API_ENDPOINT);
46
47
    HttpHeaders headers = new HttpHeaders();
48 1 1. getUrls : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
49 1 1. getUrls : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
50
51
    HttpEntity<String> entity = new HttpEntity<>(headers);
52
    Map<String, String> uriVariables = Map.of("recursive", "1");
53
54
    ResponseEntity<String> re =
55
        restTemplate.exchange(API_ENDPOINT, HttpMethod.GET, entity, String.class, uriVariables);
56
57
    ApiResult apiResult = mapper.readValue(re.getBody(), ApiResult.class);
58
59
    List<TreeElement> treeElements = apiResult.getTree();
60
    List<String> urls =
61
        treeElements.stream()
62 1 1. lambda$getUrls$0 : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::lambda$getUrls$0 → KILLED
            .map(treeElement -> treeElement.getPath())
63 3 1. lambda$getUrls$1 : negated conditional → KILLED
2. lambda$getUrls$1 : negated conditional → KILLED
3. lambda$getUrls$1 : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::lambda$getUrls$1 → KILLED
            .filter(path -> (path.startsWith("quarters/") && path.endsWith(".csv")))
64
            .collect(Collectors.toList());
65
    List<String> rawUrls =
66
        urls.stream()
67 1 1. lambda$getUrls$2 : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::lambda$getUrls$2 → KILLED
            .map(url -> "https://raw.githubusercontent.com/" + REPO_OWNER_AND_NAME + "/main/" + url)
68
            .collect(Collectors.toList());
69 1 1. getUrls : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::getUrls → KILLED
    return rawUrls;
70
  }
71
72
  @Override
73
  public List<GradeHistory> getGradeData(String url) throws Exception {
74
    log.info("getting data from {}", url);
75
    HttpHeaders headers = new HttpHeaders();
76 1 1. getGradeData : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
77 1 1. getGradeData : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
78
79
    HttpEntity<String> entity = new HttpEntity<>(headers);
80
    ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
81
    String csvData = re.getBody();
82 1 1. getGradeData : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::getGradeData → KILLED
    return parse(new StringReader(csvData));
83
  }
84
85
  @Override
86
  public List<GradeHistory> parse(Reader reader) throws Exception {
87
    List<GradeHistory> gradeHistoryList = new ArrayList<GradeHistory>();
88
    log.info("Parsing CSV file with grade history... ");
89
    CSVReader csvReader = new CSVReader(reader);
90
    csvReader.skip(1);
91
    List<String[]> myEntries = csvReader.readAll();
92
    for (String[] row : myEntries) {
93
      String yyyyq = Integer.toString(Quarter.qyyToyyyyQ(row[0]));
94
      GradeHistory gradeHistory =
95
          GradeHistory.builder()
96
              .yyyyq(yyyyq)
97
              .course(row[2])
98
              .instructor(row[3])
99
              .grade(row[4])
100
              .count(Integer.parseInt(row[5]))
101
              .build();
102
      log.info("Parsed: " + gradeHistory.toString());
103
      gradeHistoryList.add(gradeHistory);
104
    }
105
    csvReader.close();
106 1 1. parse : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::parse → KILLED
    return gradeHistoryList;
107
  }
108
}

Mutations

48

1.1
Location : getUrls
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
removed call to org/springframework/http/HttpHeaders::setAccept → KILLED

49

1.1
Location : getUrls
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
removed call to org/springframework/http/HttpHeaders::setContentType → KILLED

62

1.1
Location : lambda$getUrls$0
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::lambda$getUrls$0 → KILLED

63

1.1
Location : lambda$getUrls$1
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
negated conditional → KILLED

2.2
Location : lambda$getUrls$1
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
negated conditional → KILLED

3.3
Location : lambda$getUrls$1
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::lambda$getUrls$1 → KILLED

67

1.1
Location : lambda$getUrls$2
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::lambda$getUrls$2 → KILLED

69

1.1
Location : getUrls
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getUrls()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::getUrls → KILLED

76

1.1
Location : getGradeData
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getGradeData()]
removed call to org/springframework/http/HttpHeaders::setAccept → KILLED

77

1.1
Location : getGradeData
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getGradeData()]
removed call to org/springframework/http/HttpHeaders::setContentType → KILLED

82

1.1
Location : getGradeData
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getGradeData()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::getGradeData → KILLED

106

1.1
Location : parse
Killed by : edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UCSBGradeHistoryServiceTests]/[method:test_getGradeData()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeHistoryServiceImpl::parse → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0