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.validation.Valid;
25
26
import java.time.LocalDateTime;
27
import java.util.Optional;
28
29
import com.fasterxml.jackson.core.JsonProcessingException;
30
import com.fasterxml.jackson.databind.ObjectMapper;
31
32
import edu.ucsb.cs156.organic.entities.School;
33
import edu.ucsb.cs156.organic.entities.User;
34
import edu.ucsb.cs156.organic.repositories.SchoolRepository;
35
import edu.ucsb.cs156.organic.repositories.UserRepository;
36
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
77
    @Operation(summary= "List all schools")
78
    @PreAuthorize("hasRole('ROLE_USER')")
79
    @GetMapping("/all")
80
    public Iterable<School> allSchools() {
81
        Iterable<School> schools = schoolRepository.findAll();
82 1 1. allSchools : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::allSchools → KILLED
        return schools;
83
    }
84
85
    @Operation(summary= "Get a single school by abbreviation")
86
    @PreAuthorize("hasRole('ROLE_USER')")
87
    @GetMapping("")
88
    public School getById(
89
        @Parameter(name="abbrev") @RequestParam String abbrev) {
90
        Optional<School> schoolOptional = schoolRepository.findById(abbrev);
91 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));
92 1 1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::getById → KILLED
        return school;
93
    }
94
95
    @Operation(summary= "Delete a school")
96
    @PreAuthorize("hasRole('ROLE_ADMIN')")
97
    @DeleteMapping("")
98
    public Object deleteSchool(
99
            @Parameter(name="abbrev") @RequestParam String abbrev) {
100
                School school = schoolRepository.findById(abbrev)
101 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));
102
103 1 1. deleteSchool : removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED
        schoolRepository.delete(school);
104 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));
105
    }
106
107
108
    @Operation(summary= "Create a new school")
109
110
  @PreAuthorize("hasRole('ROLE_ADMIN')")
111
    @PostMapping("/post")
112
    public School postSchool(
113
        @Parameter(name = "school", description="school in json format") @RequestBody School school
114
        )
115
    {
116
117 1 1. postSchool : negated conditional → KILLED
        if (!school.getAbbrev().equals(school.getAbbrev().toLowerCase())){
118
            throw new IllegalArgumentException("Invalid abbrev format. Abbrev must be all lowercase");
119
        }
120
121
        School savedSchool = schoolRepository.save(school);
122
123 1 1. postSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::postSchool → KILLED
        return savedSchool;
124
    }
125
126
}

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

82

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

91

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

92

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

101

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

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()]
removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED

104

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

117

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

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()]
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