CoursesController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.Courses;
4
5
import edu.ucsb.cs156.happiercows.repositories.CoursesRepository;
6
import edu.ucsb.cs156.happiercows.repositories.UserRepository;
7
8
import io.swagger.v3.oas.annotations.Operation;
9
import io.swagger.v3.oas.annotations.Parameter;
10
import io.swagger.v3.oas.annotations.tags.Tag;
11
import lombok.extern.slf4j.Slf4j;
12
13
import com.fasterxml.jackson.core.JsonProcessingException;
14
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.security.access.prepost.PreAuthorize;
17
import org.springframework.web.bind.annotation.DeleteMapping;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.PostMapping;
20
import org.springframework.web.bind.annotation.PutMapping;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24
25
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
26
27
@Tag(name = "Courses")
28
@RequestMapping("/api/courses")
29
@RestController
30
@Slf4j
31
public class CoursesController extends ApiController {
32
33
    @Autowired
34
    CoursesRepository courseRepository;
35
36
    @Autowired
37
    UserRepository userRepository;
38
39
    @Operation(summary = "List all courses")
40
    @PreAuthorize("hasAnyRole('ROLE_USER')")
41
    @GetMapping("/all")
42
    public Iterable<Courses> allCourses() {
43
        Iterable<Courses> courses = courseRepository.findAll();
44 1 1. allCourses : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::allCourses → KILLED
        return courses;
45
    }
46
47
    @Operation(summary = "Get a single course by id")
48
    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
49
    @GetMapping("/get")
50
    public Courses getById(
51
            @Parameter(name = "id") @RequestParam Long id) {
52
53
        Courses course = courseRepository.findById(id)
54 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Courses.class, id));
55
56 1 1. getById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::getById → KILLED
        return course;
57
    }
58
59
    @Operation(summary = "Create a new course")
60
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
61
    @PostMapping("/post")
62
    public Courses postCourse(
63
            @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name,
64
            @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term)
65
            throws JsonProcessingException {
66
67
        Courses course = Courses.builder()
68
                .name(name)
69
                .term(term)
70
                .build();
71
72
        Courses savedCourse = courseRepository.save(course);
73
74 1 1. postCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::postCourse → KILLED
        return savedCourse;
75
    }
76
77
    @Operation(summary = "Update information for a course")
78
    // allow for roles of ADMIN
79
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
80
    @PutMapping("/update")
81
    public Courses updateCourse(
82
            @Parameter(name = "id") @RequestParam Long id,
83
            @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name,
84
            @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term)
85
            throws JsonProcessingException {
86
87
        Courses course = courseRepository.findById(id)
88 1 1. lambda$updateCourse$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::lambda$updateCourse$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Courses.class,
89
                        id.toString()));
90
91 1 1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setName → KILLED
        course.setName(name);
92 1 1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setTerm → SURVIVED
        course.setTerm(term);
93
94
        course = courseRepository.save(course);
95
        log.info("course={}", course);
96
97 1 1. updateCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::updateCourse → KILLED
        return course;
98
    }
99
100
    // delete a course if the user is an admin for the course
101
    @Operation(summary = "Delete a course")
102
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
103
    @DeleteMapping("/delete")
104
    public Courses deleteCourse(
105
            @Parameter(name = "id") @RequestParam Long id)
106
            throws JsonProcessingException {
107
108
        Courses course = courseRepository.findById(id)
109 1 1. lambda$deleteCourse$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::lambda$deleteCourse$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Courses.class, id.toString()));
110
111 1 1. deleteCourse : removed call to edu/ucsb/cs156/happiercows/repositories/CoursesRepository::delete → KILLED
        courseRepository.delete(course);
112 1 1. deleteCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::deleteCourse → KILLED
        return course;
113
    }
114
115
}

Mutations

44

1.1
Location : allCourses
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:logged_in_user_can_get_all_courses()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::allCourses → KILLED

54

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

56

1.1
Location : getById
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:test_that_admin_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::getById → KILLED

74

1.1
Location : postCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_can_post_a_new_course()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::postCourse → KILLED

88

1.1
Location : lambda$updateCourse$1
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_cannot_update_non_existing_course()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::lambda$updateCourse$1 → KILLED

91

1.1
Location : updateCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_can_update_a_course()]
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setName → KILLED

92

1.1
Location : updateCourse
Killed by : none
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setTerm → SURVIVED

97

1.1
Location : updateCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_can_update_a_course()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::updateCourse → KILLED

109

1.1
Location : lambda$deleteCourse$2
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_cannot_delete_non_existing_course()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::lambda$deleteCourse$2 → KILLED

111

1.1
Location : deleteCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_can_delete_a_course()]
removed call to edu/ucsb/cs156/happiercows/repositories/CoursesRepository::delete → KILLED

112

1.1
Location : deleteCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CoursesControllerTests]/[method:an_admin_user_can_delete_a_course()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::deleteCourse → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3