StaffController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.Staff;
4
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.happiercows.models.CsvUploadResult;
6
import edu.ucsb.cs156.happiercows.models.StaffDTO;
7
import edu.ucsb.cs156.happiercows.repositories.StaffRepository;
8
import edu.ucsb.cs156.happiercows.utilities.CanonicalFormConverter;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import io.swagger.v3.oas.annotations.Operation;
11
import io.swagger.v3.oas.annotations.Parameter;
12
import lombok.extern.slf4j.Slf4j;
13
import org.apache.commons.csv.CSVFormat;
14
import org.apache.commons.csv.CSVParser;
15
import org.apache.commons.csv.CSVRecord;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.http.MediaType;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.web.bind.annotation.*;
20
import org.springframework.web.multipart.MultipartFile;
21
22
import java.io.IOException;
23
import java.io.InputStreamReader;
24
import java.nio.charset.StandardCharsets;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
28
29
@Slf4j
30
@Tag(name = "Staff")
31
@RequestMapping("/api/staff")
32
@RestController
33
public class StaffController extends ApiController {
34
    @Autowired
35
    private StaffRepository staffRepository;
36
37
    @Operation(summary = "List all course staff")
38
    @PreAuthorize("hasRole('ROLE_ADMIN')")
39
    @GetMapping("/all")
40
    public Iterable<Staff> allStaff() {
41 1 1. allStaff : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/StaffController::allStaff → KILLED
        return staffRepository.findAll();
42
    }
43
44
    @Operation(summary = "List the staff for a single course")
45
    @PreAuthorize("hasRole('ROLE_ADMIN')")
46
    @GetMapping("/course/{courseId}")
47
    public Iterable<Staff> staffForCourse(
48
            @Parameter(name = "courseId") @PathVariable Long courseId) {
49 1 1. staffForCourse : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/StaffController::staffForCourse → KILLED
        return staffRepository.findByCourseId(courseId);
50
    }
51
52
    @Operation(summary = "Get a staff member by id")
53
    @PreAuthorize("hasRole('ROLE_ADMIN')")
54
    @GetMapping("/{id}")
55
    public Staff getStaffById(
56
            @Parameter(name = "id") @PathVariable Long id) {
57 1 1. getStaffById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::getStaffById → KILLED
        return staffRepository.findById(id)
58 1 1. lambda$getStaffById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::lambda$getStaffById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Staff.class, id));
59
    }
60
61
    @Operation(summary = "Add a staff member to a course")
62
    @PreAuthorize("hasRole('ROLE_ADMIN')")
63
    @PostMapping("")
64
    public Staff postStaff(
65
            @Parameter(name = "staff") @RequestBody StaffDTO staffDTO) {
66 1 1. postStaff : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::postStaff → KILLED
        return staffRepository.save(staffDTO.toStaff());
67
    }
68
69
    @Operation(summary = "Update a single staff member")
70
    @PreAuthorize("hasRole('ROLE_ADMIN')")
71
    @PutMapping("/{id}")
72
    public Staff updateStaff(
73
            @Parameter(name = "id") @PathVariable Long id, @RequestBody StaffDTO staffDTO) {
74
        Staff staff = staffRepository.findById(id)
75 1 1. lambda$updateStaff$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::lambda$updateStaff$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Staff.class, id));
76
77 1 1. updateStaff : removed call to edu/ucsb/cs156/happiercows/entities/Staff::setLastName → KILLED
        staff.setLastName(staffDTO.getLastName());
78 1 1. updateStaff : removed call to edu/ucsb/cs156/happiercows/entities/Staff::setFirstMiddleName → KILLED
        staff.setFirstMiddleName(staffDTO.getFirstMiddleName());
79 1 1. updateStaff : removed call to edu/ucsb/cs156/happiercows/entities/Staff::setEmail → KILLED
        staff.setEmail(CanonicalFormConverter.convertToValidEmail(staffDTO.getEmail()));
80 1 1. updateStaff : removed call to edu/ucsb/cs156/happiercows/entities/Staff::setCourseId → KILLED
        staff.setCourseId(staffDTO.getCourseId());
81
82
        staffRepository.save(staff);
83
84 1 1. updateStaff : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::updateStaff → KILLED
        return staff;
85
    }
86
87
    @Operation(summary = "Remove a staff member from a course")
88
    @PreAuthorize("hasRole('ROLE_ADMIN')")
89
    @DeleteMapping("/{id}")
90
    public Object deleteStaff(
91
            @Parameter(name = "id") @PathVariable Long id) {
92
        Staff staff = staffRepository.findById(id)
93 1 1. lambda$deleteStaff$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::lambda$deleteStaff$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Staff.class, id));
94
95 1 1. deleteStaff : removed call to edu/ucsb/cs156/happiercows/repositories/StaffRepository::delete → KILLED
        staffRepository.delete(staff);
96 1 1. deleteStaff : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::deleteStaff → KILLED
        return genericMessage("Staff with id %s deleted".formatted(id));
97
    }
98
99
    private static final List<String> STAFF_CSV_HEADERS = List.of("lastName", "firstMiddleName", "email");
100
101
    /**
102
     * Bulk-adds staff to a course from an uploaded CSV file, with a single
103
     * fixed header format ({@code lastName,firstMiddleName,email}). Rows
104
     * whose email is already on the course's staff list are skipped rather
105
     * than duplicated.
106
     *
107
     * @param courseId the course to add the staff to
108
     * @param file the uploaded CSV file
109
     * @return a summary of how many staff were created, and which emails
110
     *         were skipped as already-on-roster
111
     */
112
    @Operation(summary = "Upload a CSV file of staff for a course")
113
    @PreAuthorize("hasRole('ROLE_ADMIN')")
114
    @PostMapping(value = "/upload/csv", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
115
    public CsvUploadResult uploadStaffCsv(
116
            @Parameter(name = "courseId") @RequestParam Long courseId,
117
            @Parameter(name = "file") @RequestParam("file") MultipartFile file) throws IOException {
118
119
        List<CSVRecord> records;
120
        try (CSVParser parser = CSVFormat.DEFAULT.parse(
121
                new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
122
            records = parser.getRecords();
123
        }
124
125 1 1. uploadStaffCsv : negated conditional → KILLED
        if (records.isEmpty()) {
126
            throw new IllegalArgumentException("CSV file is empty");
127
        }
128
129
        Iterator<CSVRecord> rows = records.iterator();
130
131
        List<String> headerValues = new ArrayList<>();
132
        for (String value : rows.next()) {
133
            headerValues.add(value.trim());
134
        }
135
136 1 1. uploadStaffCsv : negated conditional → KILLED
        if (headerValues.size() != STAFF_CSV_HEADERS.size()) {
137
            throw new IllegalArgumentException("Unrecognized CSV header format");
138
        }
139 2 1. uploadStaffCsv : negated conditional → KILLED
2. uploadStaffCsv : changed conditional boundary → KILLED
        for (int i = 0; i < STAFF_CSV_HEADERS.size(); i++) {
140 1 1. uploadStaffCsv : negated conditional → KILLED
            if (!STAFF_CSV_HEADERS.get(i).equalsIgnoreCase(headerValues.get(i))) {
141
                throw new IllegalArgumentException("Unrecognized CSV header format");
142
            }
143
        }
144
145
        int created = 0;
146
        List<String> skippedEmails = new ArrayList<>();
147
148 1 1. uploadStaffCsv : negated conditional → KILLED
        while (rows.hasNext()) {
149
            CSVRecord row = rows.next();
150 1 1. uploadStaffCsv : negated conditional → KILLED
            if (row.size() != STAFF_CSV_HEADERS.size()) {
151
                throw new IllegalArgumentException(
152
                        "Row %d does not have the expected number of columns".formatted(row.getRecordNumber()));
153
            }
154
155
            Staff staff = Staff.builder()
156
                    .lastName(row.get(0).trim())
157
                    .firstMiddleName(row.get(1).trim())
158
                    .email(CanonicalFormConverter.convertToValidEmail(row.get(2).trim()))
159
                    .courseId(courseId)
160
                    .build();
161
162 1 1. uploadStaffCsv : negated conditional → KILLED
            if (staffRepository.findByCourseIdAndEmail(courseId, staff.getEmail()).iterator().hasNext()) {
163
                skippedEmails.add(staff.getEmail());
164
                continue;
165
            }
166
167
            staffRepository.save(staff);
168 1 1. uploadStaffCsv : Changed increment from 1 to -1 → KILLED
            created++;
169
        }
170
171 1 1. uploadStaffCsv : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::uploadStaffCsv → KILLED
        return CsvUploadResult.builder()
172
                .created(created)
173
                .skippedEmails(skippedEmails)
174
                .build();
175
    }
176
}

Mutations

41

1.1
Location : allStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_get_all_staff()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/StaffController::allStaff → KILLED

49

1.1
Location : staffForCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_get_staff_for_a_course()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/StaffController::staffForCourse → KILLED

57

1.1
Location : getStaffById
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_get_staff_by_id()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::getStaffById → KILLED

58

1.1
Location : lambda$getStaffById$0
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_cannot_get_staff_when_it_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::lambda$getStaffById$0 → KILLED

66

1.1
Location : postStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_post_new_staff()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::postStaff → KILLED

75

1.1
Location : lambda$updateStaff$0
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_cannot_edit_staff_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::lambda$updateStaff$0 → KILLED

77

1.1
Location : updateStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_edit_an_existing_staff_member()]
removed call to edu/ucsb/cs156/happiercows/entities/Staff::setLastName → KILLED

78

1.1
Location : updateStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_edit_an_existing_staff_member()]
removed call to edu/ucsb/cs156/happiercows/entities/Staff::setFirstMiddleName → KILLED

79

1.1
Location : updateStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_edit_an_existing_staff_member()]
removed call to edu/ucsb/cs156/happiercows/entities/Staff::setEmail → KILLED

80

1.1
Location : updateStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_edit_an_existing_staff_member()]
removed call to edu/ucsb/cs156/happiercows/entities/Staff::setCourseId → KILLED

84

1.1
Location : updateStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_edit_an_existing_staff_member()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::updateStaff → KILLED

93

1.1
Location : lambda$deleteStaff$0
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_tries_to_delete_non_existant_staff_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::lambda$deleteStaff$0 → KILLED

95

1.1
Location : deleteStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_delete_a_staff_member()]
removed call to edu/ucsb/cs156/happiercows/repositories/StaffRepository::delete → KILLED

96

1.1
Location : deleteStaff
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_delete_a_staff_member()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::deleteStaff → KILLED

125

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:empty_csv_file_is_rejected()]
negated conditional → KILLED

136

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_upload_staff_csv_with_umail_emails_and_they_are_canonicalized()]
negated conditional → KILLED

139

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:unrecognized_header_format_is_rejected()]
negated conditional → KILLED

2.2
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:malformed_row_with_too_few_columns_is_rejected()]
changed conditional boundary → KILLED

140

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:unrecognized_header_format_is_rejected()]
negated conditional → KILLED

148

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:malformed_row_with_too_few_columns_is_rejected()]
negated conditional → KILLED

150

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:malformed_row_with_too_few_columns_is_rejected()]
negated conditional → KILLED

162

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_upload_staff_csv_with_umail_emails_and_they_are_canonicalized()]
negated conditional → KILLED

168

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_upload_staff_csv()]
Changed increment from 1 to -1 → KILLED

171

1.1
Location : uploadStaffCsv
Killed by : edu.ucsb.cs156.happiercows.controllers.StaffControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.StaffControllerTests]/[method:admin_can_upload_staff_csv()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/StaffController::uploadStaffCsv → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1