StudentsController.java

1
package edu.ucsb.cs156.organic.controllers;
2
3
import edu.ucsb.cs156.organic.entities.Course;
4
import edu.ucsb.cs156.organic.entities.Student;
5
import edu.ucsb.cs156.organic.repositories.CourseRepository;
6
import edu.ucsb.cs156.organic.repositories.StaffRepository;
7
import edu.ucsb.cs156.organic.repositories.StudentRepository;
8
import edu.ucsb.cs156.organic.repositories.UserRepository;
9
import io.swagger.v3.oas.annotations.Operation;
10
import io.swagger.v3.oas.annotations.Parameter;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import lombok.extern.slf4j.Slf4j;
13
14
import com.fasterxml.jackson.core.JsonProcessingException;
15
import com.opencsv.CSVReader;
16
import com.opencsv.exceptions.CsvException;
17
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.security.access.prepost.PreAuthorize;
20
import org.springframework.web.bind.annotation.GetMapping;
21
import org.springframework.web.bind.annotation.PostMapping;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RestController;
25
import org.springframework.web.multipart.MultipartFile;
26
27
import edu.ucsb.cs156.organic.errors.EntityNotFoundException;
28
29
import java.io.BufferedInputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.InputStreamReader;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Optional;
36
37
@Tag(name = "Students")
38
@RequestMapping("/api/students")
39
@RestController
40
@Slf4j
41
public class StudentsController extends ApiController {
42
43
        public enum Status {
44
                INSERTED, UPDATED
45
        };
46
47
        @Autowired
48
        CourseRepository courseRepository;
49
50
        @Autowired
51
        StaffRepository staffRepository;
52
53
        @Autowired
54
        StudentRepository studentRepository;
55
56
        @Autowired
57
        UserRepository userRepository;
58
59
        @Operation(summary = "Get Students for course")
60
        @PreAuthorize("hasRole('ROLE_ADMIN')")
61
        @GetMapping("/all")
62
        public Iterable<Student> getStaff(
63
                        @Parameter(name = "courseId") @RequestParam Long courseId)
64
                        throws JsonProcessingException {
65
66
                Course course = courseRepository.findById(courseId)
67 1 1. lambda$getStaff$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::lambda$getStaff$0 → KILLED
                                .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString()));
68
69
                Iterable<Student> students = studentRepository.findByCourseId(course.getId());
70 1 1. getStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::getStaff → KILLED
                return students;
71
        }
72
73
        @Operation(summary = "Upload Students for Course in UCSB Egrades Format")
74
        @PreAuthorize("hasRole('ROLE_ADMIN')")
75
        @PostMapping(value = "/upload/egrades", consumes = { "multipart/form-data" })
76
        public Map<String, String> getStaff(
77
                        @Parameter(name = "courseId") @RequestParam Long courseId,
78
                        @Parameter(name = "file") @RequestParam("file") MultipartFile file)
79
                        throws JsonProcessingException, IOException, CsvException {
80
81
                Course course = courseRepository.findById(courseId)
82 1 1. lambda$getStaff$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::lambda$getStaff$1 → KILLED
                                .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString()));
83
84
                int counts[] = { 0, 0 };
85
86
                try (InputStream inputStream = new BufferedInputStream(file.getInputStream());
87
                                InputStreamReader reader = new InputStreamReader(inputStream);
88
                                CSVReader csvReader = new CSVReader(reader);) {
89 1 1. getStaff : removed call to com/opencsv/CSVReader::skip → KILLED
                        csvReader.skip(2);
90
                        List<String[]> myEntries = csvReader.readAll();
91
                        for (String[] row : myEntries) {
92
                                Student student = fromEgradesCSVRow(row);
93 1 1. getStaff : removed call to edu/ucsb/cs156/organic/entities/Student::setCourseId → KILLED
                                student.setCourseId(course.getId());
94
                                Status s = upsertStudent(student, course);
95 1 1. getStaff : Replaced integer addition with subtraction → KILLED
                                counts[s.ordinal()]++;
96
                        }
97
                }
98 1 1. getStaff : replaced return value with Collections.emptyMap for edu/ucsb/cs156/organic/controllers/StudentsController::getStaff → KILLED
                return Map.of(
99
                                "filename", file.getOriginalFilename(),
100
                                "message", String.format("Inserted %d new students, Updated %d students",
101
                                                counts[Status.INSERTED.ordinal()], counts[Status.UPDATED.ordinal()]));
102
103
        }
104
105
        public Student fromEgradesCSVRow(String[] row) {
106 1 1. fromEgradesCSVRow : replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::fromEgradesCSVRow → KILLED
                return Student.builder()
107
                                .fname(row[5])
108
                                .lname(row[4])
109
                                .studentId(row[1])
110
                                .email(row[10])
111
                                .build();
112
        }
113
114
        public Status upsertStudent(Student student, Course course) {
115
                Optional<Student> existingStudent = studentRepository.findByCourseIdAndStudentId(course.getId(),
116
                                student.getStudentId());
117 1 1. upsertStudent : negated conditional → KILLED
                if (existingStudent.isPresent()) {
118
                        Student existingStudentObj = existingStudent.get();
119 1 1. upsertStudent : removed call to edu/ucsb/cs156/organic/entities/Student::setFname → KILLED
                        existingStudentObj.setFname(student.getFname());
120 1 1. upsertStudent : removed call to edu/ucsb/cs156/organic/entities/Student::setLname → KILLED
                        existingStudentObj.setLname(student.getLname());
121 1 1. upsertStudent : removed call to edu/ucsb/cs156/organic/entities/Student::setEmail → KILLED
                        existingStudentObj.setEmail(student.getEmail());
122
                        studentRepository.save(existingStudentObj);
123 1 1. upsertStudent : replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::upsertStudent → KILLED
                        return Status.UPDATED;
124
                } else {
125
                        studentRepository.save(student);
126 1 1. upsertStudent : replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::upsertStudent → KILLED
                        return Status.INSERTED;
127
                }
128
129
        }
130
131
}

Mutations

67

1.1
Location : lambda$getStaff$0
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_cannot_get_all_students_for_a_non_existing_course()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::lambda$getStaff$0 → KILLED

70

1.1
Location : getStaff
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_get_all_students_for_a_course()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::getStaff → KILLED

82

1.1
Location : lambda$getStaff$1
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_cannot_upload_students_for_a_non_existing_course()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::lambda$getStaff$1 → KILLED

89

1.1
Location : getStaff
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
removed call to com/opencsv/CSVReader::skip → KILLED

93

1.1
Location : getStaff
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
removed call to edu/ucsb/cs156/organic/entities/Student::setCourseId → KILLED

95

1.1
Location : getStaff
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
Replaced integer addition with subtraction → KILLED

98

1.1
Location : getStaff
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/organic/controllers/StudentsController::getStaff → KILLED

106

1.1
Location : fromEgradesCSVRow
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::fromEgradesCSVRow → KILLED

117

1.1
Location : upsertStudent
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
negated conditional → KILLED

119

1.1
Location : upsertStudent
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
removed call to edu/ucsb/cs156/organic/entities/Student::setFname → KILLED

120

1.1
Location : upsertStudent
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
removed call to edu/ucsb/cs156/organic/entities/Student::setLname → KILLED

121

1.1
Location : upsertStudent
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
removed call to edu/ucsb/cs156/organic/entities/Student::setEmail → KILLED

123

1.1
Location : upsertStudent
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::upsertStudent → KILLED

126

1.1
Location : upsertStudent
Killed by : edu.ucsb.cs156.organic.controllers.StudentsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.StudentsControllerTests]/[method:admin_can_upload_students_for_an_existing_course()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/StudentsController::upsertStudent → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3