RequestTypeController.java

1
package edu.ucsb.cs156.rec.controllers;
2
3
import edu.ucsb.cs156.rec.entities.RequestType;
4
import edu.ucsb.cs156.rec.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.rec.repositories.RequestTypeRepository;
6
import io.swagger.v3.oas.annotations.Operation;
7
import io.swagger.v3.oas.annotations.Parameter;
8
import io.swagger.v3.oas.annotations.tags.Tag;
9
import lombok.extern.slf4j.Slf4j;
10
11
import java.util.Map;
12
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.http.ResponseEntity;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.*;
17
18
import jakarta.validation.Valid;
19
20
/**
21
 * This is a REST controller for Request Types.
22
 */
23
24
@Tag(name = "Request Types")
25
@RequestMapping("/api/request-types")
26
@RestController
27
@Slf4j
28
public class RequestTypeController extends ApiController {
29
30
    @Autowired
31
    RequestTypeRepository requestTypeRepository;
32
33
    /**
34
     * This method returns a list of all request types.
35
     * @return a list of all request types
36
     */
37
    @Operation(summary = "List all request types")
38
    @PreAuthorize("hasRole('ROLE_USER')")
39
    @GetMapping("/all")
40
    public Iterable<RequestType> allRequestTypes() {
41 1 1. allRequestTypes : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RequestTypeController::allRequestTypes → KILLED
        return requestTypeRepository.findAll();
42
    }
43
44
    /**
45
     * This method returns a single request type.
46
     * @param id id of the request type to get
47
     * @return a single request type
48
     */
49
    @Operation(summary = "Get a single request type")
50
    @PreAuthorize("hasRole('ROLE_USER')")
51
    @GetMapping("")
52
    public RequestType getById(
53
            @Parameter(name = "id") @RequestParam Long id) {
54 1 1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::getById → KILLED
        return requestTypeRepository.findById(id)
55 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RequestType.class, id));
56
    }
57
58
    /**
59
     * This method creates a new request type. Accessible only to users with the role "ROLE_ADMIN".
60
     * @param description description of the request type
61
     * @return the saved request type (with its id field set by the database)
62
     */
63
    @Operation(summary = "Create a new request type")
64
    @PreAuthorize("hasRole('ROLE_ADMIN')")
65
    @PostMapping("/post")
66
    public ResponseEntity<Object> postRequestType(
67
            @Parameter(name = "description") @RequestParam String description) {
68 1 1. postRequestType : negated conditional → KILLED
        if (requestTypeRepository.findByRequestType(description).isPresent()) {
69 1 1. postRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::postRequestType → KILLED
            return ResponseEntity.badRequest().body(Map.of(
70
                "type", "IllegalArgumentException",
71
                "message", "Request Type with this description already exists"
72
            ));
73
        }
74
75
        RequestType requestType = RequestType.builder()
76
                .requestType(description)
77
                .build();
78
79
        RequestType savedRequestType = requestTypeRepository.save(requestType);
80 1 1. postRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::postRequestType → KILLED
        return ResponseEntity.ok(savedRequestType);
81
    }
82
83
84
    /**
85
     * Deletes a request type. Accessible only to users with the role "ROLE_ADMIN".
86
     * @param id id of the request type to delete
87
     * @return a message indicating that the request type was deleted
88
     */
89
    @Operation(summary = "Delete a Request Type")
90
    @PreAuthorize("hasRole('ROLE_ADMIN')")
91
    @DeleteMapping("")
92
    public Object deleteRequestType(
93
            @Parameter(name = "id") @RequestParam Long id) {
94
        RequestType requestType = requestTypeRepository.findById(id)
95 1 1. lambda$deleteRequestType$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$deleteRequestType$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RequestType.class, id));
96
97 1 1. deleteRequestType : removed call to edu/ucsb/cs156/rec/repositories/RequestTypeRepository::delete → KILLED
        requestTypeRepository.delete(requestType);
98 1 1. deleteRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::deleteRequestType → KILLED
        return genericMessage("Request Type with id %s deleted".formatted(id));
99
    }
100
101
    /**
102
     * Update a single request type. Accessible only to users with the role "ROLE_ADMIN".
103
     * @param id id of the request type to update
104
     * @param description the new description for the request type
105
     * @return the updated request type object
106
     */
107
    @Operation(summary = "Update a single request type")
108
    @PreAuthorize("hasRole('ROLE_ADMIN')")
109
    @PutMapping("")
110
    public ResponseEntity<Object> updateRequestType(
111
            @Parameter(name = "id") @RequestParam Long id,
112
            @Parameter(name = "description") @RequestParam String description) {
113
114
        RequestType requestType = requestTypeRepository.findById(id)
115 1 1. lambda$updateRequestType$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$updateRequestType$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RequestType.class, id));
116
117 1 1. updateRequestType : negated conditional → KILLED
        if (requestTypeRepository.findByRequestType(description).isPresent() && 
118 1 1. updateRequestType : negated conditional → KILLED
            !requestType.getRequestType().equals(description)) {
119 1 1. updateRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::updateRequestType → KILLED
            return ResponseEntity.badRequest().body(Map.of(
120
                "type", "IllegalArgumentException",
121
                "message", "Request Type with this description already exists"
122
            ));
123
        }
124
125 1 1. updateRequestType : removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED
        requestType.setRequestType(description);
126
127
        RequestType updatedRequestType = requestTypeRepository.save(requestType);
128 1 1. updateRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::updateRequestType → KILLED
        return ResponseEntity.ok(updatedRequestType);
129
    }
130
131
}

Mutations

41

1.1
Location : allRequestTypes
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:logged_in_user_can_get_all_request_types()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RequestTypeController::allRequestTypes → KILLED

54

1.1
Location : getById
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:logged_in_user_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::getById → KILLED

55

1.1
Location : lambda$getById$0
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:logged_in_user_cannot_get_by_id_when_the_id_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$getById$0 → KILLED

68

1.1
Location : postRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_user_can_post_a_new_request_type()]
negated conditional → KILLED

69

1.1
Location : postRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_cannot_post_duplicate_request_type()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::postRequestType → KILLED

80

1.1
Location : postRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_user_can_post_a_new_request_type()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::postRequestType → KILLED

95

1.1
Location : lambda$deleteRequestType$1
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_cannot_delete_non_existent_request_type()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$deleteRequestType$1 → KILLED

97

1.1
Location : deleteRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_can_delete_a_request_type()]
removed call to edu/ucsb/cs156/rec/repositories/RequestTypeRepository::delete → KILLED

98

1.1
Location : deleteRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_can_delete_a_request_type()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::deleteRequestType → KILLED

115

1.1
Location : lambda$updateRequestType$2
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_cannot_update_nonexistent_request_type()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$updateRequestType$2 → KILLED

117

1.1
Location : updateRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_cannot_update_request_type_to_duplicate_description()]
negated conditional → KILLED

118

1.1
Location : updateRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_cannot_update_request_type_to_duplicate_description()]
negated conditional → KILLED

119

1.1
Location : updateRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_cannot_update_request_type_to_duplicate_description()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::updateRequestType → KILLED

125

1.1
Location : updateRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_can_update_existing_request_type()]
removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED

128

1.1
Location : updateRequestType
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:admin_can_update_existing_request_type()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::updateRequestType → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0