CourseStaffCSVController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import com.opencsv.CSVReader;
4
import com.opencsv.exceptions.CsvException;
5
import edu.ucsb.cs156.frontiers.entities.Course;
6
import edu.ucsb.cs156.frontiers.entities.CourseStaff;
7
import edu.ucsb.cs156.frontiers.enums.InsertStatus;
8
import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException;
9
import edu.ucsb.cs156.frontiers.models.StaffLoadResult;
10
import edu.ucsb.cs156.frontiers.models.StaffUpsertResponse;
11
import edu.ucsb.cs156.frontiers.repositories.CourseRepository;
12
import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository;
13
import edu.ucsb.cs156.frontiers.services.UpdateUserService;
14
import io.swagger.v3.oas.annotations.Operation;
15
import io.swagger.v3.oas.annotations.Parameter;
16
import io.swagger.v3.oas.annotations.tags.Tag;
17
import java.io.BufferedInputStream;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.io.InputStreamReader;
21
import java.util.List;
22
import lombok.extern.slf4j.Slf4j;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.http.HttpStatus;
25
import org.springframework.http.ResponseEntity;
26
import org.springframework.security.access.prepost.PreAuthorize;
27
import org.springframework.web.bind.annotation.PostMapping;
28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestParam;
30
import org.springframework.web.bind.annotation.RestController;
31
import org.springframework.web.multipart.MultipartFile;
32
import org.springframework.web.server.ResponseStatusException;
33
34
@Tag(name = "CourseStaff")
35
@RequestMapping("/api/coursestaff")
36
@RestController("CourseStaffCSVController")
37
@Slf4j
38
public class CourseStaffCSVController extends ApiController {
39
40
  @Autowired private CourseStaffRepository courseStaffRepository;
41
42
  @Autowired private CourseRepository courseRepository;
43
44
  @Autowired private UpdateUserService updateUserService;
45
46
  public static final String STAFF_CSV_HEADERS = "firstName,lastName,email";
47
48
  @Operation(summary = "Upload Course Staff from CSV")
49
  @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)")
50
  @PostMapping(
51
      value = "/upload/csv",
52
      consumes = {"multipart/form-data"})
53
  public ResponseEntity<StaffLoadResult> uploadStaffCSV(
54
      @Parameter(name = "courseId") @RequestParam Long courseId,
55
      @Parameter(name = "file") @RequestParam("file") MultipartFile file)
56
      throws IOException, CsvException {
57
58
    Course course =
59
        courseRepository
60
            .findById(courseId)
61 1 1. lambda$uploadStaffCSV$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::lambda$uploadStaffCSV$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString()));
62
63
    int counts[] = {0, 0};
64
65
    try (InputStream inputStream = new BufferedInputStream(file.getInputStream());
66
        InputStreamReader reader = new InputStreamReader(inputStream);
67
        CSVReader csvReader = new CSVReader(reader)) {
68
69
      String[] headers = csvReader.readNext();
70 1 1. uploadStaffCSV : negated conditional → KILLED
      if (headers == null) {
71
        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "CSV file is empty");
72
      }
73
74 1 1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::validateHeaders → KILLED
      validateHeaders(headers);
75
76
      List<String[]> myEntries = csvReader.readAll();
77
      for (String[] row : myEntries) {
78 2 1. uploadStaffCSV : changed conditional boundary → KILLED
2. uploadStaffCSV : negated conditional → KILLED
        if (row.length < 3) {
79
          throw new ResponseStatusException(
80
              HttpStatus.BAD_REQUEST,
81
              String.format(
82
                  "CSV row does not have enough columns. Expected at least 3, got %d", row.length));
83
        }
84
85
        CourseStaff courseStaff =
86
            CourseStaff.builder()
87
                .firstName(row[0].trim())
88
                .lastName(row[1].trim())
89
                .email(row[2].trim())
90
                .course(course)
91
                .build();
92
93
        StaffUpsertResponse upsertResponse = CourseStaffController.upsertStaff(courseStaff, course);
94
        InsertStatus s = upsertResponse.getInsertStatus();
95 1 1. uploadStaffCSV : negated conditional → KILLED
        if (s == InsertStatus.INSERTED) {
96
          course.getCourseStaff().add(upsertResponse.courseStaff());
97
        }
98 1 1. uploadStaffCSV : Replaced integer addition with subtraction → KILLED
        counts[s.ordinal()]++;
99
      }
100
    }
101
102
    StaffLoadResult successfulResult =
103
        new StaffLoadResult(
104
            counts[InsertStatus.INSERTED.ordinal()], counts[InsertStatus.UPDATED.ordinal()]);
105
    courseStaffRepository.saveAll(course.getCourseStaff());
106 1 1. uploadStaffCSV : removed call to java/util/List::forEach → KILLED
    course.getCourseStaff().forEach(updateUserService::attachUserToCourseStaff);
107 1 1. uploadStaffCSV : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::uploadStaffCSV → KILLED
    return ResponseEntity.ok(successfulResult);
108
  }
109
110
  static void validateHeaders(String[] headers) {
111
    String[] expectedHeaders = STAFF_CSV_HEADERS.split(",");
112 2 1. validateHeaders : changed conditional boundary → KILLED
2. validateHeaders : negated conditional → KILLED
    if (headers.length < expectedHeaders.length) {
113
      throw new ResponseStatusException(
114
          HttpStatus.BAD_REQUEST,
115
          String.format(
116
              "CSV headers do not match expected format. Expected: %s", STAFF_CSV_HEADERS));
117
    }
118 2 1. validateHeaders : changed conditional boundary → KILLED
2. validateHeaders : negated conditional → KILLED
    for (int i = 0; i < expectedHeaders.length; i++) {
119 1 1. validateHeaders : negated conditional → KILLED
      if (!expectedHeaders[i].trim().equalsIgnoreCase(headers[i].trim())) {
120
        throw new ResponseStatusException(
121
            HttpStatus.BAD_REQUEST,
122
            String.format(
123
                "CSV headers do not match expected format. Expected: %s", STAFF_CSV_HEADERS));
124
      }
125
    }
126
  }
127
}

Mutations

61

1.1
Location : lambda$uploadStaffCSV$0
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_not_found_for_missing_course()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::lambda$uploadStaffCSV$0 → KILLED

70

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_empty_csv()]
negated conditional → KILLED

74

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_invalid_headers()]
removed call to edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::validateHeaders → KILLED

78

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_strips_whitespace_from_fields()]
changed conditional boundary → KILLED

2.2
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_short_row()]
negated conditional → KILLED

95

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_strips_whitespace_from_fields()]
negated conditional → KILLED

98

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:instructor_can_upload_staff_csv()]
Replaced integer addition with subtraction → KILLED

106

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:instructor_can_upload_staff_csv()]
removed call to java/util/List::forEach → KILLED

107

1.1
Location : uploadStaffCSV
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:instructor_can_upload_staff_csv()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::uploadStaffCSV → KILLED

112

1.1
Location : validateHeaders
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_strips_whitespace_from_fields()]
changed conditional boundary → KILLED

2.2
Location : validateHeaders
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_too_few_headers()]
negated conditional → KILLED

118

1.1
Location : validateHeaders
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_short_row()]
changed conditional boundary → KILLED

2.2
Location : validateHeaders
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_invalid_headers()]
negated conditional → KILLED

119

1.1
Location : validateHeaders
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseStaffCSVControllerTests]/[method:upload_returns_bad_request_for_invalid_headers()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support