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
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.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.DeleteMapping;
17
import org.springframework.web.bind.annotation.GetMapping;
18
import org.springframework.web.bind.annotation.PostMapping;
19
import org.springframework.web.bind.annotation.PutMapping;
20
import org.springframework.web.bind.annotation.RequestBody;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24
25
import jakarta.validation.Valid;
26
27
/**
28
 * This is a REST controller for RequestType
29
 */
30
31
@Tag(name = "RequestType")
32
@RequestMapping("/api/requesttypes")
33
@RestController
34
@Slf4j
35
public class RequestTypeController extends ApiController {
36
37
    @Autowired
38
    RequestTypeRepository requestTypeRepository;
39
40
41
    /**
42
     * This method returns a list of all Request Types.
43
     * @return a list of all Request Types.
44
     */
45
    @Operation(summary = "List all request types")
46
    @PreAuthorize("hasRole('ROLE_USER')")
47
    @GetMapping("/all")
48
    public Iterable<RequestType> allRequestTypes(
49
    ) {
50
        Iterable<RequestType> requestTypes = requestTypeRepository.findAll();
51 1 1. allRequestTypes : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RequestTypeController::allRequestTypes → KILLED
        return requestTypes;
52
    }
53
54
        /**
55
     * Get a single request type by id
56
     * 
57
     * @param id the id of the request type
58
     * @return a RequestTYpe
59
     */
60
    @Operation(summary= "Get a single request type")
61
    @PreAuthorize("hasRole('ROLE_USER')")
62
    @GetMapping("")
63
    public RequestType getById(
64
            @Parameter(name="id") @RequestParam Long id) {
65
        RequestType requestType = requestTypeRepository.findById(id)
66 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));
67
68 1 1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::getById → KILLED
        return requestType;
69
    }
70
71
    /**
72
     * Create a new request type
73
     * 
74
     * @param requestType   the title of the request type
75
     * @return the saved requesttype
76
     */
77
    @Operation(summary= "Create a new request type")
78
    @PreAuthorize("hasRole('ROLE_ADMIN')")
79
    @PostMapping("/post")
80
    public RequestType postRequestType(
81
            @Parameter(name="requestType") @RequestParam String requestType)
82
            throws JsonProcessingException {
83
84
        log.info("requestType={}", requestType);
85
86
        // Check for duplicates
87
        Iterable<RequestType> existingRequestTypes = requestTypeRepository.findAll();
88
        for (RequestType existing : existingRequestTypes) {
89 1 1. postRequestType : negated conditional → KILLED
            if (existing.getRequestType().equals(requestType)) {
90
                throw new IllegalArgumentException("Duplicate request type: " + requestType);
91
            }
92
        }
93
94
        // Create new request type
95
        RequestType requestTypeNew = new RequestType();
96 1 1. postRequestType : removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED
        requestTypeNew.setRequestType(requestType);
97
98
        RequestType savedRequestType = requestTypeRepository.save(requestTypeNew);
99
100 1 1. postRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::postRequestType → KILLED
        return savedRequestType;
101
    }
102
103
    /**
104
     * Delete a UCSBDate
105
     * 
106
     * @param id the id of the request type to delete
107
     * @return a message indicating the date was deleted
108
     */
109
    @Operation(summary= "Delete a request type")
110
    @PreAuthorize("hasRole('ROLE_ADMIN')")
111
    @DeleteMapping("")
112
    public Object deleteRequestType(
113
            @Parameter(name="id") @RequestParam Long id) {
114
        RequestType requestType = requestTypeRepository.findById(id)
115 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));
116
117 1 1. deleteRequestType : removed call to edu/ucsb/cs156/rec/repositories/RequestTypeRepository::delete → KILLED
        requestTypeRepository.delete(requestType);
118 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));
119
    }
120
121
    /**
122
     * Update a single request type
123
     * 
124
     * @param id       id of the request to update
125
     * @param incoming the new request type
126
     * @return the updated request type object
127
     */
128
    @Operation(summary= "Update a single request type")
129
    @PreAuthorize("hasRole('ROLE_ADMIN')")
130
    @PutMapping("")
131
    public RequestType updateRequestType(
132
            @Parameter(name="id") @RequestParam Long id,
133
            @RequestBody @Valid RequestType incoming) {
134
135
        RequestType requestType = requestTypeRepository.findById(id)
136 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));
137
138
139
        // Check for duplicates
140
        Iterable<RequestType> existingRequestTypes = requestTypeRepository.findAll();
141
        for (RequestType existing : existingRequestTypes) {
142 1 1. updateRequestType : negated conditional → KILLED
            if (incoming.getRequestType().isEmpty()){
143
                throw new IllegalArgumentException("Request type cannot be empty");
144
            }
145 1 1. updateRequestType : negated conditional → KILLED
            if (existing.getRequestType().equals(incoming.getRequestType())) {
146
                throw new IllegalArgumentException("Duplicate request type: " + incoming);
147
            }
148
        }
149
150
        // Update and save the request type
151 1 1. updateRequestType : removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED
        requestType.setRequestType(incoming.getRequestType());
152
        requestTypeRepository.save(requestType);
153
154 1 1. updateRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::updateRequestType → KILLED
        return requestType;
155
    }
156
}

Mutations

51

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

66

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:test_that_logged_in_user_can_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 : getById
Killed by : edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RequestTypeControllerTests]/[method:test_that_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

89

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

96

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

100

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

115

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_tries_to_delete_non_existant_requesttype_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$deleteRequestType$1 → KILLED

117

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

118

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_requesttype()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::deleteRequestType → KILLED

136

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_edit_requesttype_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$updateRequestType$2 → KILLED

142

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

145

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

151

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_edit_an_existing_requesttype()]
removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED

154

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