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.OrgStatus;
8
import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException;
9
import edu.ucsb.cs156.frontiers.repositories.CourseRepository;
10
import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository;
11
import edu.ucsb.cs156.frontiers.services.UpdateUserService;
12
import io.swagger.v3.oas.annotations.Operation;
13
import io.swagger.v3.oas.annotations.Parameter;
14
import io.swagger.v3.oas.annotations.tags.Tag;
15
import java.io.BufferedInputStream;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.InputStreamReader;
19
import java.util.ArrayList;
20
import java.util.HashSet;
21
import java.util.List;
22
import java.util.Locale;
23
import java.util.Map;
24
import java.util.Set;
25
import lombok.extern.slf4j.Slf4j;
26
import org.springframework.beans.factory.annotation.Autowired;
27
import org.springframework.http.HttpStatus;
28
import org.springframework.http.ResponseEntity;
29
import org.springframework.security.access.prepost.PreAuthorize;
30
import org.springframework.web.bind.annotation.PostMapping;
31
import org.springframework.web.bind.annotation.RequestMapping;
32
import org.springframework.web.bind.annotation.RequestParam;
33
import org.springframework.web.bind.annotation.RestController;
34
import org.springframework.web.multipart.MultipartFile;
35
import org.springframework.web.server.ResponseStatusException;
36
37
@Tag(name = "CourseStaff")
38
@RequestMapping("/api/coursestaff")
39
@RestController("CourseStaffCSVController")
40
@Slf4j
41
public class CourseStaffCSVController extends ApiController {
42
43
  @Autowired private CourseStaffRepository courseStaffRepository;
44
45
  @Autowired private CourseRepository courseRepository;
46
47
  @Autowired private UpdateUserService updateUserService;
48
49
  public static final String STAFF_CSV_HEADERS = "firstName,lastName,email";
50
51
  /**
52
   * Upload course staff from a CSV file with the headers firstName,lastName,email. Rows whose email
53
   * is already on the course's staff, or that repeat an email from an earlier row of the same file,
54
   * are not added (emails are compared ignoring upper and lower case); every other row is still
55
   * added. If any row was rejected, the response status is 409 (Conflict) and the body lists the
56
   * rejected emails.
57
   *
58
   * @param courseId id of the course
59
   * @param file the CSV file
60
   * @return the number of staff added, and the rejected emails if there were any
61
   */
62
  @Operation(summary = "Upload Course Staff from CSV")
63
  @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)")
64
  @PostMapping(
65
      value = "/upload/csv",
66
      consumes = {"multipart/form-data"})
67
  public ResponseEntity<Map<String, Object>> uploadStaffCSV(
68
      @Parameter(name = "courseId") @RequestParam Long courseId,
69
      @Parameter(name = "file") @RequestParam("file") MultipartFile file)
70
      throws IOException, CsvException {
71
72
    Course course =
73
        courseRepository
74
            .findById(courseId)
75 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()));
76
77
    int count = 0;
78
    List<String> rejected = new ArrayList<>();
79
    Set<String> seenEmails = new HashSet<>();
80
81
    try (InputStream inputStream = new BufferedInputStream(file.getInputStream());
82
        InputStreamReader reader = new InputStreamReader(inputStream);
83
        CSVReader csvReader = new CSVReader(reader)) {
84
85
      String[] headers = csvReader.readNext();
86 1 1. uploadStaffCSV : negated conditional → KILLED
      if (headers == null) {
87
        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "CSV file is empty");
88
      }
89
90 1 1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::validateHeaders → KILLED
      validateHeaders(headers);
91
92
      String[] row;
93 1 1. uploadStaffCSV : negated conditional → KILLED
      while ((row = csvReader.readNext()) != null) {
94 2 1. uploadStaffCSV : changed conditional boundary → KILLED
2. uploadStaffCSV : negated conditional → KILLED
        if (row.length < 3) {
95
          throw new ResponseStatusException(
96
              HttpStatus.BAD_REQUEST,
97
              String.format(
98
                  "CSV row does not have enough columns. Expected at least 3, got %d", row.length));
99
        }
100
101
        String email = row[2].trim();
102 1 1. uploadStaffCSV : negated conditional → KILLED
        if (!seenEmails.add(email.toLowerCase(Locale.ROOT))
103 1 1. uploadStaffCSV : negated conditional → KILLED
            || courseStaffRepository.existsByCourseAndEmailIgnoreCase(course, email)) {
104
          rejected.add(email);
105
          continue;
106
        }
107
108
        CourseStaff courseStaff =
109
            CourseStaff.builder()
110
                .firstName(row[0].trim())
111
                .lastName(row[1].trim())
112
                .email(email)
113
                .course(course)
114
                .build();
115
116 1 1. uploadStaffCSV : negated conditional → KILLED
        if (course.getInstallationId() != null) {
117 1 1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED
          courseStaff.setOrgStatus(OrgStatus.JOINCOURSE);
118
        } else {
119 1 1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED
          courseStaff.setOrgStatus(OrgStatus.PENDING);
120
        }
121
122
        CourseStaff savedCourseStaff = courseStaffRepository.save(courseStaff);
123 1 1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToCourseStaff → KILLED
        updateUserService.attachUserToCourseStaff(savedCourseStaff);
124 1 1. uploadStaffCSV : Changed increment from 1 to -1 → KILLED
        count++;
125
      }
126
    }
127
128 1 1. uploadStaffCSV : negated conditional → KILLED
    if (rejected.isEmpty()) {
129 1 1. uploadStaffCSV : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::uploadStaffCSV → KILLED
      return ResponseEntity.ok(Map.of("count", count));
130
    }
131 1 1. uploadStaffCSV : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::uploadStaffCSV → KILLED
    return ResponseEntity.status(HttpStatus.CONFLICT)
132
        .body(Map.of("count", count, "rejected", rejected));
133
  }
134
135
  static void validateHeaders(String[] headers) {
136
    String[] expectedHeaders = STAFF_CSV_HEADERS.split(",");
137 2 1. validateHeaders : changed conditional boundary → KILLED
2. validateHeaders : negated conditional → KILLED
    if (headers.length < expectedHeaders.length) {
138
      throw new ResponseStatusException(
139
          HttpStatus.BAD_REQUEST,
140
          String.format(
141
              "CSV headers do not match expected format. Expected: %s", STAFF_CSV_HEADERS));
142
    }
143 2 1. validateHeaders : changed conditional boundary → KILLED
2. validateHeaders : negated conditional → KILLED
    for (int i = 0; i < expectedHeaders.length; i++) {
144 1 1. validateHeaders : negated conditional → KILLED
      if (!expectedHeaders[i].trim().equalsIgnoreCase(headers[i].trim())) {
145
        throw new ResponseStatusException(
146
            HttpStatus.BAD_REQUEST,
147
            String.format(
148
                "CSV headers do not match expected format. Expected: %s", STAFF_CSV_HEADERS));
149
      }
150
    }
151
  }
152
}

Mutations

75

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

86

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

90

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

93

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_short_row()]
negated conditional → KILLED

94

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

102

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

103

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

116

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_with_installation_id()]
negated conditional → KILLED

117

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_with_installation_id()]
removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED

119

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 edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED

123

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 edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToCourseStaff → KILLED

124

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()]
Changed increment from 1 to -1 → KILLED

128

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

129

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

131

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

137

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

143

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

144

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