SchoolController.java

1
package edu.ucsb.cs156.organic.controllers;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.format.annotation.DateTimeFormat;
5
import org.springframework.security.access.prepost.PreAuthorize;
6
import org.springframework.web.bind.annotation.DeleteMapping;
7
import org.springframework.web.bind.annotation.GetMapping;
8
import org.springframework.web.bind.annotation.RequestBody;
9
import org.springframework.web.bind.annotation.PostMapping;
10
import org.springframework.web.bind.annotation.PutMapping;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.bind.annotation.RequestParam;
13
import org.springframework.web.bind.annotation.RestController;
14
15
import edu.ucsb.cs156.organic.errors.EntityNotFoundException;
16
import io.swagger.v3.oas.annotations.Operation;
17
18
import io.swagger.v3.oas.annotations.Parameter;
19
import io.swagger.v3.oas.annotations.tags.Tag;
20
import lombok.extern.slf4j.Slf4j;
21
import javax.validation.Valid;
22
23
import java.net.URLDecoder;
24
import java.nio.charset.StandardCharsets;
25
import java.time.LocalDateTime;
26
import java.util.Optional;
27
28
import com.fasterxml.jackson.core.JsonProcessingException;
29
import com.fasterxml.jackson.databind.ObjectMapper;
30
31
import edu.ucsb.cs156.organic.entities.Course;
32
import edu.ucsb.cs156.organic.entities.School;
33
import edu.ucsb.cs156.organic.entities.Staff;
34
import edu.ucsb.cs156.organic.entities.User;
35
import edu.ucsb.cs156.organic.repositories.SchoolRepository;
36
import edu.ucsb.cs156.organic.repositories.UserRepository;
37
38
@Tag(name = "school")
39
@RequestMapping("/api/schools")
40
@RestController
41
@Slf4j // what does this do
42
public class SchoolController extends ApiController{
43
    @Autowired
44
    SchoolRepository schoolRepository;
45
46
    @Autowired
47
    ObjectMapper mapper;
48
    
49
    @Autowired
50
    UserRepository userRepository;
51
52
    @Operation(summary = "Update information for a school")
53
    // allow for roles of ADMIN or INSTRUCTOR but only if the user is a staff member
54
    // for the course
55
    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')")
56
    @PutMapping("/update")
57
    public School updateSchool(
58
            @Parameter(name = "abbrev") @RequestParam String abbrev,
59
            @RequestBody @Valid School incoming) {
60
61
                School school = schoolRepository.findById(abbrev)
62 1 1. lambda$updateSchool$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$updateSchool$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(School.class, abbrev));
63
64 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED
        school.setName(incoming.getName());
65 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED
        school.setTermRegex(incoming.getTermRegex());
66 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED
        school.setTermDescription(incoming.getTermDescription());
67 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED
        school.setTermError(incoming.getTermError());
68
69
        schoolRepository.save(school);
70
71
        log.info("school={}", school);
72
73 1 1. updateSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::updateSchool → KILLED
        return school;
74
    }
75
76
    @Operation(summary= "List all schools")
77
    @PreAuthorize("hasRole('ROLE_USER')")
78
    @GetMapping("/all")
79
    public Iterable<School> allSchools() {
80
        Iterable<School> schools = schoolRepository.findAll();
81 1 1. allSchools : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::allSchools → KILLED
        return schools;
82
    }
83
84
    @Operation(summary= "Get a single school by abbreviation")
85
    @PreAuthorize("hasRole('ROLE_USER')")
86
    @GetMapping("")
87
    public School getById(
88
        @Parameter(name="abbrev") @RequestParam String abbrev) {
89
        Optional<School> schoolOptional = schoolRepository.findById(abbrev);
90 1 1. lambda$getById$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$getById$1 → KILLED
        School school = schoolOptional.orElseThrow(() -> new EntityNotFoundException(School.class, abbrev));
91 1 1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::getById → KILLED
        return school;
92
    }
93
94
    @Operation(summary= "Delete a school")
95
    @PreAuthorize("hasRole('ROLE_ADMIN')")
96
    @DeleteMapping("")
97
    public Object deleteSchool(
98
            @Parameter(name="abbrev") @RequestParam String abbrev) {
99
                School school = schoolRepository.findById(abbrev)
100 1 1. lambda$deleteSchool$2 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$deleteSchool$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(School.class, abbrev));
101
102 1 1. deleteSchool : removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED
        schoolRepository.delete(school);
103 1 1. deleteSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::deleteSchool → KILLED
        return genericMessage("School with id %s deleted".formatted(abbrev));
104
    }
105
106
107
    @Operation(summary= "Create a new school")
108
    @PreAuthorize("hasRole('ROLE_ADMIN')")
109
    @PostMapping("/post")
110
    public School postSchool(
111
        @Parameter(name="abbrev", description="university domain name", example="ucsb") @RequestParam String abbrev,
112
        @Parameter(name="name", description="University name") @RequestParam String name,
113
        @Parameter(name="termRegex", description="Format: Example [WSMF]\\d\\d") @RequestParam String termRegex,
114
        @Parameter(name="termDescription", description="Enter quarter, e.g. F23, W24, S24, M24") @RequestParam String termDescription,
115
        @Parameter(name="termError", description="input error?") @RequestParam(required = false) String termError)
116
        throws JsonProcessingException {
117
118
        String decodedTermRegex = URLDecoder.decode(termRegex, StandardCharsets.UTF_8);
119
120
        School school = School.builder().build();
121 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setAbbrev → KILLED
        school.setAbbrev(abbrev);
122 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED
        school.setName(name);
123 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED
        school.setTermRegex(decodedTermRegex);
124 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED
        school.setTermDescription(termDescription);
125 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED
        school.setTermError(termError);
126
127 1 1. postSchool : negated conditional → KILLED
        if (!abbrev.equals(abbrev.toLowerCase())){
128
            throw new IllegalArgumentException("Invalid abbrev format. Abbrev must be all lowercase");
129
        }
130
131
        School savedSchool = schoolRepository.save(school);
132
133 1 1. postSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::postSchool → KILLED
        return savedSchool;
134
    }
135
136
}

Mutations

62

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

64

1.1
Location : updateSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_instructor_user_can_update_a_school_if_they_are_admin()]
removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED

65

1.1
Location : updateSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_instructor_user_can_update_a_school_if_they_are_admin()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED

66

1.1
Location : updateSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_instructor_user_can_update_a_school_if_they_are_admin()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED

67

1.1
Location : updateSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_instructor_user_can_update_a_school_if_they_are_admin()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED

73

1.1
Location : updateSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_instructor_user_can_update_a_school_if_they_are_admin()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::updateSchool → KILLED

81

1.1
Location : allSchools
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:logged_in_user_can_get_all_schools()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::allSchools → KILLED

90

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

91

1.1
Location : getById
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:test_that_logged_in_user_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::getById → KILLED

100

1.1
Location : lambda$deleteSchool$2
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:admin_tries_to_delete_non_existant_school_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$deleteSchool$2 → KILLED

102

1.1
Location : deleteSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:admin_can_delete_a_school()]
removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED

103

1.1
Location : deleteSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:admin_can_delete_a_school()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::deleteSchool → KILLED

121

1.1
Location : postSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_admin_user_can_post_a_new_school()]
removed call to edu/ucsb/cs156/organic/entities/School::setAbbrev → KILLED

122

1.1
Location : postSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_admin_user_can_post_a_new_school()]
removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED

123

1.1
Location : postSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_admin_user_can_post_a_new_school()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED

124

1.1
Location : postSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_admin_user_can_post_a_new_school()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED

125

1.1
Location : postSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_admin_user_can_post_a_new_school()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED

127

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

133

1.1
Location : postSchool
Killed by : edu.ucsb.cs156.organic.controllers.SchoolControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolControllerTests]/[method:an_admin_user_can_post_a_new_school()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::postSchool → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3