SchoolController.java

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

Mutations

63

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

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::setName → 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::setTermRegex → 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::setTermDescription → KILLED

68

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

74

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

83

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

93

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

94

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

103

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

105

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

106

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

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::setAbbrev → 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::setName → 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_bad_format_termRegex()]
removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED

126

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

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

129

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

139

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