CourseController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.Course;
4
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.happiercows.models.CourseDTO;
6
import edu.ucsb.cs156.happiercows.repositories.CourseRepository;
7
import io.swagger.v3.oas.annotations.tags.Tag;
8
import io.swagger.v3.oas.annotations.Operation;
9
import io.swagger.v3.oas.annotations.Parameter;
10
import lombok.extern.slf4j.Slf4j;
11
import jakarta.validation.Valid;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.security.access.prepost.PreAuthorize;
14
import org.springframework.web.bind.annotation.*;
15
16
@Slf4j
17
@Tag(name = "Course")
18
@RequestMapping("/api/course")
19
@RestController
20
public class CourseController extends ApiController {
21
    @Autowired
22
    private CourseRepository courseRepository;
23
24
    /**
25
     * This method returns a list of all courses.
26
     * 
27
     * @return a list of all courses
28
     */
29
    @Operation(summary = "List all courses")
30
    @PreAuthorize("hasRole('ROLE_USER')")
31
    @GetMapping("/all")
32
    public Iterable<Course> allOrganisations() {
33
        Iterable<Course> courses = courseRepository.findAll();
34 1 1. allOrganisations : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/CourseController::allOrganisations → KILLED
        return courses;
35
    }
36
37
    @Operation(summary = "Get a course by id")
38
    @PreAuthorize("hasRole('ROLE_ADMIN')")
39
    @GetMapping("/{id}")
40
    public Course getCourseById(
41
            @Parameter(name = "id") @PathVariable Long id) {
42
        Course course = courseRepository.findById(id)
43 1 1. lambda$getCourseById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::lambda$getCourseById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Course.class, id));
44 1 1. getCourseById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::getCourseById → KILLED
        return course;
45
    }
46
47
    @Operation(summary = "Create a course")
48
    @PreAuthorize("hasRole('ROLE_ADMIN')")
49
    @PostMapping("")
50
    public Course postCourse(
51
            @Parameter(name = "course") @RequestBody CourseDTO courseDTO) {
52
        Course savedCourse = courseRepository.save(courseDTO.toCourse());
53 1 1. postCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::postCourse → KILLED
        return savedCourse;
54
    }
55
56
    /**
57
     * Update a single course.
58
     * 
59
     * @param id id of the course to update
60
     * @param incoming the new course information
61
     * @return the updated course object
62
     */
63
    @Operation(summary = "Update a single course")
64
    @PreAuthorize("hasRole('ROLE_ADMIN')")
65
    @PutMapping("")
66
    public Course updateCourse(
67
            @Parameter(name = "id") @RequestParam Long id,
68
            @RequestBody @Valid Course incoming) {
69
        
70
        Course currCourse = courseRepository
71
                .findById(id)
72 1 1. lambda$updateCourse$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::lambda$updateCourse$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Course.class, id));
73
74 1 1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED
        currCourse.setCode(incoming.getCode());
75 1 1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED
        currCourse.setName(incoming.getName());
76 1 1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED
        currCourse.setTerm(incoming.getTerm());
77
78
        courseRepository.save(currCourse);
79
80 1 1. updateCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::updateCourse → KILLED
        return currCourse;  
81
82
    }
83
    
84
85
}

Mutations

34

1.1
Location : allOrganisations
Killed by : edu.ucsb.cs156.happiercows.controllers.CourseControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CourseControllerTests]/[method:logged_in_user_can_get_all_courses()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/CourseController::allOrganisations → KILLED

43

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

44

1.1
Location : getCourseById
Killed by : edu.ucsb.cs156.happiercows.controllers.CourseControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CourseControllerTests]/[method:admin_can_get_course_by_id()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::getCourseById → KILLED

53

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

72

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

74

1.1
Location : updateCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CourseControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CourseControllerTests]/[method:admin_can_edit_an_existing_course()]
removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED

75

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

76

1.1
Location : updateCourse
Killed by : edu.ucsb.cs156.happiercows.controllers.CourseControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.CourseControllerTests]/[method:admin_can_edit_an_existing_course()]
removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED

80

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

Active mutators

Tests examined


Report generated by PIT 1.17.0