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

Mutations

69

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

72

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

84

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

91

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

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()]
removed call to edu/ucsb/cs156/organic/entities/Student::setCourseId → KILLED

97

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

100

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

108

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

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

122

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

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()]
removed call to edu/ucsb/cs156/organic/entities/Student::setEmail → KILLED

125

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

128

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