SchoolsController.java

1
package edu.ucsb.cs156.organic.controllers;
2
3
import edu.ucsb.cs156.organic.entities.School;
4
import edu.ucsb.cs156.organic.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.organic.repositories.SchoolRepository;
6
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.Parameter;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import lombok.extern.slf4j.Slf4j;
11
12
import com.fasterxml.jackson.core.JsonProcessingException;
13
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.format.annotation.DateTimeFormat;
16
import org.springframework.security.access.prepost.PreAuthorize;
17
import org.springframework.web.bind.annotation.DeleteMapping;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.PostMapping;
20
import org.springframework.web.bind.annotation.PutMapping;
21
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RestController;
25
26
import javax.validation.Valid;
27
28
import java.time.LocalDateTime;
29
30
@Tag(name = "Schools")
31
@RequestMapping("/api/Schools")
32
@RestController
33
@Slf4j
34
public class SchoolsController extends ApiController {
35
36
    @Autowired
37
    SchoolRepository schoolRepository;
38
39
    @Operation(summary= "List all schools")
40
    @PreAuthorize("hasRole('ROLE_USER')")
41
    @GetMapping("/all")
42
    public Iterable<School> allSchools() {
43
        Iterable<School> schools = schoolRepository.findAll();
44 1 1. allSchools : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::allSchools → KILLED
        return schools;
45
    }
46
47
    @Operation(summary= "Create a new school")
48
    @PreAuthorize("hasRole('ROLE_ADMIN')")
49
    @PostMapping("/post")
50
    public School postSchool(
51
            @Parameter(name="abbrev") @RequestParam String abbrev,
52
            @Parameter(name="name") @RequestParam String name,
53
            @Parameter(name="termRegex") @RequestParam String termRegex,
54
            @Parameter(name="termDescription") @RequestParam String termDescription,
55
            @Parameter(name="termError") @RequestParam String termError)
56
            throws JsonProcessingException {
57
58
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
59
        // See: https://www.baeldung.com/spring-date-parameters
60
61
        // log.info("localDateTime={}", localDateTime);
62
63
        School school = new School();
64 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setAbbrev → KILLED
        school.setAbbrev(abbrev);
65 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED
        school.setName(name);
66 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED
        school.setTermRegex(termRegex);
67 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED
        school.setTermDescription(termDescription);
68 1 1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED
        school.setTermError(termError);
69
70
        School savedSchool = schoolRepository.save(school);
71
72 1 1. postSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::postSchool → KILLED
        return savedSchool;
73
    }
74
75
    @Operation(summary= "Get a single school")
76
    @PreAuthorize("hasRole('ROLE_USER')")
77
    @GetMapping("")
78
    public School getById(
79
            @Parameter(name="id") @RequestParam Long id) {
80
        School school = schoolRepository.findById(id)
81 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(School.class, id));
82
83 1 1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::getById → KILLED
        return school;
84
    }
85
86
    @Operation(summary= "Delete a school")
87
    @PreAuthorize("hasRole('ROLE_ADMIN')")
88
    @DeleteMapping("")
89
    public Object deleteSchool(
90
            @Parameter(name="id") @RequestParam Long id) {
91
        School school = schoolRepository.findById(id)
92 1 1. lambda$deleteSchool$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::lambda$deleteSchool$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(School.class, id));
93
94 1 1. deleteSchool : removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED
        schoolRepository.delete(school);
95 1 1. deleteSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::deleteSchool → KILLED
        return genericMessage("School with id %s deleted".formatted(id));
96
    }
97
98
    @Operation(summary= "Update a school")
99
    @PreAuthorize("hasRole('ROLE_ADMIN')")
100
    @PutMapping("")
101
    public School updateSchool(
102
            @Parameter(name="id") @RequestParam Long id,
103
            @RequestBody @Valid School incoming) {
104
105
                School school = schoolRepository.findById(id)
106 1 1. lambda$updateSchool$2 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::lambda$updateSchool$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(School.class, id));
107
108 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setAbbrev → KILLED
                school.setAbbrev(incoming.getAbbrev());
109 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED
                school.setName(incoming.getName());
110 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED
                school.setTermRegex(incoming.getTermRegex());
111 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED
                school.setTermDescription(incoming.getTermDescription());
112 1 1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED
                school.setTermError(incoming.getTermError());
113
114
        schoolRepository.save(school);
115
116 1 1. updateSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolsController::updateSchool → KILLED
        return school;
117
    }
118
119
}

Mutations

44

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

64

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

65

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

66

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

67

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

68

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

72

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

81

1.1
Location : lambda$getById$0
Killed by : edu.ucsb.cs156.organic.controllers.SchoolsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolsControllerTests]/[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/SchoolsController::lambda$getById$0 → KILLED

83

1.1
Location : getById
Killed by : edu.ucsb.cs156.organic.controllers.SchoolsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolsControllerTests]/[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/SchoolsController::getById → KILLED

92

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

94

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

95

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

106

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

108

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

109

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

110

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

111

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

112

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

116

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

Active mutators

Tests examined


Report generated by PIT 1.7.3