CoursesController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
4
import com.fasterxml.jackson.core.JsonProcessingException;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import edu.ucsb.cs156.happiercows.entities.Courses;
7
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
8
import edu.ucsb.cs156.happiercows.models.HealthUpdateStrategyList;
9
import edu.ucsb.cs156.happiercows.repositories.CoursesRepository;
10
import io.swagger.v3.oas.annotations.tags.Tag;
11
import io.swagger.v3.oas.annotations.Operation;
12
import io.swagger.v3.oas.annotations.Parameter;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.format.annotation.DateTimeFormat;
15
16
import lombok.extern.slf4j.Slf4j;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.http.HttpStatus;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.security.access.prepost.PreAuthorize;
21
import org.springframework.web.bind.annotation.*;
22
import edu.ucsb.cs156.happiercows.services.CommonsPlusBuilderService;
23
import java.util.Optional;
24
import java.time.LocalDateTime;
25
26
import javax.transaction.Transactional;
27
import javax.validation.Valid;
28
29
30
@Slf4j
31
@Tag(name = "Courses")
32
@RequestMapping("/api/courses")
33
@RestController
34
public class CoursesController extends ApiController{
35
    @Autowired
36
    CoursesRepository coursesRepository;
37
38
    // Get all records in the table and return as a JSON array
39
    @Operation(summary= "List all courses")
40
    @PreAuthorize("hasRole('ROLE_USER')")
41
    @GetMapping("/all")
42
    public Iterable<Courses> allCourses() {
43
        Iterable<Courses> items = coursesRepository.findAll();
44 1 1. allCourses : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::allCourses → KILLED
        return items;
45
    }
46
47
48
    // Use the data in the input parameters to create a new row in the table and return the data as JSON
49
    @Operation(summary= "Create a new course")
50
    @PreAuthorize("hasRole('ROLE_ADMIN')")
51
    @PostMapping("/post")
52
    public Courses postCourses(
53
            @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name,
54
            @Parameter(name = "school", description = "school abbreviation e.g. UCSB") @RequestParam String school,
55
            @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term,
56
            @Parameter(name = "startDate", description = "in iso format, i.e. YYYY-mm-ddTHH:MM:SS; e.g. 2023-10-01T00:00:00 see https://en.wikipedia.org/wiki/ISO_8601") @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
57
            @Parameter(name = "endDate", description = "in iso format, i.e. YYYY-mm-ddTHH:MM:SS; e.g. 2023-12-31T11:59:59 see https://en.wikipedia.org/wiki/ISO_8601") @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate)
58
            throws JsonProcessingException {
59
60
61
        Courses course = new Courses();
62 1 1. postCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setName → KILLED
        course.setName(name);
63 1 1. postCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setSchool → KILLED
        course.setSchool(school);
64 1 1. postCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setTerm → KILLED
        course.setTerm(term);
65 1 1. postCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setStartDate → KILLED
        course.setStartDate(startDate);
66 1 1. postCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setEndDate → KILLED
        course.setEndDate(endDate);
67
68
        Courses savedCourse = coursesRepository.save(course);
69
70 1 1. postCourses : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::postCourses → KILLED
        return savedCourse;
71
    }
72
73
    @Operation(summary = "Update a course")
74
    @PreAuthorize("hasRole('ROLE_ADMIN')")
75
    @PutMapping("")
76
    public Courses updateCourses(
77
               @Parameter(name="id") @RequestParam Long id,
78
            @RequestBody @Valid Courses incoming) {
79
80
        Courses course1 = coursesRepository.findById(id)
81 1 1. lambda$updateCourses$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::lambda$updateCourses$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Courses.class, id));
82
83 1 1. updateCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setName → KILLED
       course1.setName(incoming.getName());
84 1 1. updateCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setSchool → KILLED
       course1.setSchool(incoming.getSchool());
85 1 1. updateCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setTerm → KILLED
       course1.setTerm(incoming.getTerm());
86 1 1. updateCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setStartDate → KILLED
       course1.setStartDate(incoming.getStartDate());
87 1 1. updateCourses : removed call to edu/ucsb/cs156/happiercows/entities/Courses::setEndDate → KILLED
       course1.setEndDate(incoming.getEndDate());
88
89
        coursesRepository.save(course1);
90
91 1 1. updateCourses : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CoursesController::updateCourses → KILLED
        return course1;
92
    
93
    }
94
}   

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

62

1.1
Location : postCourses
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()]
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setName → KILLED

63

1.1
Location : postCourses
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()]
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setSchool → KILLED

64

1.1
Location : postCourses
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()]
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setTerm → KILLED

65

1.1
Location : postCourses
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()]
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setStartDate → KILLED

66

1.1
Location : postCourses
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()]
removed call to edu/ucsb/cs156/happiercows/entities/Courses::setEndDate → KILLED

70

1.1
Location : postCourses
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::postCourses → KILLED

81

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

83

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

84

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

85

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

86

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

87

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

91

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

Active mutators

Tests examined


Report generated by PIT 1.7.3