RecommendationRequestsController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
4
import edu.ucsb.cs156.example.repositories.RecommendationRequestRepository;
5
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 com.fasterxml.jackson.core.JsonProcessingException;
12
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.format.annotation.DateTimeFormat;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.*;
17
18
import jakarta.validation.Valid;
19
20
import edu.ucsb.cs156.example.entities.RecommendationRequest;
21
22
import java.time.LocalDateTime;
23
24
@Tag(name = "recommendationRequests")
25
@RequestMapping("/api/recommendationrequests")
26
@RestController
27
@Slf4j
28
public class RecommendationRequestsController extends ApiController {
29
30
    @Autowired
31
    private RecommendationRequestRepository recommendationRequestRepository;
32
33
    @Operation(summary = "List all recommendation requests")
34
    @PreAuthorize("hasRole('ROLE_USER')")
35
    @GetMapping("/all")
36
    public Iterable<RecommendationRequest> getAllRequests() {
37 1 1. getAllRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::getAllRequests → KILLED
        return recommendationRequestRepository.findAll();
38
    }
39
40
    @Operation(summary = "Create a new recommendation request")
41
    @PreAuthorize("hasRole('ROLE_ADMIN')")
42
    @PostMapping("/post")
43
    public RecommendationRequest createRecommendationRequest(
44
            @Parameter(description = "Email of the requester") @RequestParam String requesterEmail,
45
            @Parameter(description = "Email of the professor") @RequestParam String professorEmail,
46
            @Parameter(description = "Explanation for the request") @RequestParam String explanation,
47
            @Parameter(description = "Date when the recommendation was requested (ISO 8601 format)") 
48
            @RequestParam("dateRequested") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateRequested,
49
            @Parameter(description = "Date when the recommendation is needed (ISO 8601 format)") 
50
            @RequestParam("dateNeeded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateNeeded,
51
            @Parameter(description = "Status of whether the request is completed") @RequestParam boolean doneBool
52
    ) throws JsonProcessingException {
53
54
        log.info("Request date: {}", dateRequested);
55
        log.info("Needed by: {}", dateNeeded);
56
57
        RecommendationRequest newRequest = RecommendationRequest.builder()
58
                .requesterEmail(requesterEmail)
59
                .professorEmail(professorEmail)
60
                .explanation(explanation)
61
                .dateRequested(dateRequested)
62
                .dateNeeded(dateNeeded)
63
                .done(doneBool)
64
                .build();
65
66 1 1. createRecommendationRequest : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::createRecommendationRequest → KILLED
        return recommendationRequestRepository.save(newRequest);
67
    }
68
69
    /**
70
     * Get a specific recommendation request by its ID
71
     *
72
     * @param id the ID of the recommendation request
73
     * @return the corresponding RecommendationRequest
74
     */
75
    @Operation(summary = "Get a single recommendation request by ID")
76
    @PreAuthorize("hasRole('ROLE_USER')")
77
    @GetMapping("")
78
    public RecommendationRequest getRequestById(
79
            @Parameter(description = "ID of the recommendation request") @RequestParam Long id) {
80 1 1. getRequestById : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::getRequestById → KILLED
        return recommendationRequestRepository.findById(id)
81 1 1. lambda$getRequestById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::lambda$getRequestById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
82
    }
83
84
    /**
85
     * Delete a recommendation request by its ID
86
     *
87
     * @param id the ID of the recommendation request to be deleted
88
     * @return a message confirming the deletion
89
     */
90
    @Operation(summary = "Delete a recommendation request")
91
    @PreAuthorize("hasRole('ROLE_ADMIN')")
92
    @DeleteMapping("")
93
    public Object deleteRecommendationRequest(
94
            @Parameter(description = "ID of the recommendation request to delete") @RequestParam Long id) {
95
        RecommendationRequest request = recommendationRequestRepository.findById(id)
96 1 1. lambda$deleteRecommendationRequest$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::lambda$deleteRecommendationRequest$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
97
98 1 1. deleteRecommendationRequest : removed call to edu/ucsb/cs156/example/repositories/RecommendationRequestRepository::delete → KILLED
        recommendationRequestRepository.delete(request);
99 1 1. deleteRecommendationRequest : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::deleteRecommendationRequest → KILLED
        return genericMessage(String.format("RecommendationRequest with id %s deleted", id));
100
    }
101
102
    /**
103
     * Update a recommendation request by its ID
104
     *
105
     * @param id       the ID of the recommendation request to be updated
106
     * @param incoming the new request data
107
     * @return the updated RecommendationRequest
108
     */
109
    @Operation(summary = "Update a recommendation request")
110
    @PreAuthorize("hasRole('ROLE_ADMIN')")
111
    @PutMapping("")
112
    public RecommendationRequest updateRecommendationRequest(
113
               @Parameter(name="id") @RequestParam Long id,
114
            @RequestBody @Valid RecommendationRequest incoming) {
115
116
        RecommendationRequest req = recommendationRequestRepository.findById(id)
117 1 1. lambda$updateRecommendationRequest$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::lambda$updateRecommendationRequest$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
118
119 1 1. updateRecommendationRequest : removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setDateNeeded → KILLED
        req.setDateNeeded(incoming.getDateNeeded());
120 1 1. updateRecommendationRequest : removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setDateRequested → KILLED
        req.setDateRequested(incoming.getDateRequested());
121 1 1. updateRecommendationRequest : removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setDone → KILLED
        req.setDone(incoming.getDone());
122 1 1. updateRecommendationRequest : removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setProfessorEmail → KILLED
        req.setProfessorEmail(incoming.getProfessorEmail());
123 1 1. updateRecommendationRequest : removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setRequesterEmail → KILLED
        req.setRequesterEmail(incoming.getRequesterEmail());
124 1 1. updateRecommendationRequest : removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setExplanation → KILLED
        req.setExplanation(incoming.getExplanation());
125
126
        recommendationRequestRepository.save(req);
127
128 1 1. updateRecommendationRequest : replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::updateRecommendationRequest → KILLED
        return req;
129
    
130
    }
131
}

Mutations

37

1.1
Location : getAllRequests
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:logged_in_users_can_get_all_recommendation_requests()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::getAllRequests → KILLED

66

1.1
Location : createRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_user_can_submit_new_request()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::createRecommendationRequest → KILLED

80

1.1
Location : getRequestById
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:logged_in_users_can_get_request_by_id_if_exists()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::getRequestById → KILLED

81

1.1
Location : lambda$getRequestById$0
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:logged_in_users_cannot_get_request_by_id_if_not_exists()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::lambda$getRequestById$0 → KILLED

96

1.1
Location : lambda$deleteRecommendationRequest$1
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_cannot_delete_non_existing_request()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::lambda$deleteRecommendationRequest$1 → KILLED

98

1.1
Location : deleteRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_delete_existing_request()]
removed call to edu/ucsb/cs156/example/repositories/RecommendationRequestRepository::delete → KILLED

99

1.1
Location : deleteRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_delete_existing_request()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::deleteRecommendationRequest → KILLED

117

1.1
Location : lambda$updateRecommendationRequest$2
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_cannot_edit_non_existing_request()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::lambda$updateRecommendationRequest$2 → KILLED

119

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setDateNeeded → KILLED

120

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setDateRequested → KILLED

121

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setDone → KILLED

122

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setProfessorEmail → KILLED

123

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setRequesterEmail → KILLED

124

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
removed call to edu/ucsb/cs156/example/entities/RecommendationRequest::setExplanation → KILLED

128

1.1
Location : updateRecommendationRequest
Killed by : edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RecommendationRequestsControllerTests]/[method:admin_can_edit_existing_request()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RecommendationRequestsController::updateRecommendationRequest → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0