RecommendationRequestController.java

1
package edu.ucsb.cs156.rec.controllers;
2
3
import edu.ucsb.cs156.rec.entities.RecommendationRequest;
4
import edu.ucsb.cs156.rec.entities.RequestType;
5
import edu.ucsb.cs156.rec.entities.User;
6
import edu.ucsb.cs156.rec.errors.EntityNotFoundException;
7
import edu.ucsb.cs156.rec.repositories.RecommendationRequestRepository;
8
import edu.ucsb.cs156.rec.repositories.RequestTypeRepository;
9
import edu.ucsb.cs156.rec.repositories.UserRepository;
10
import edu.ucsb.cs156.rec.services.CurrentUserService;
11
import io.swagger.v3.oas.annotations.Operation;
12
import io.swagger.v3.oas.annotations.Parameter;
13
import io.swagger.v3.oas.annotations.tags.Tag;
14
import lombok.extern.slf4j.Slf4j;
15
16
import com.fasterxml.jackson.core.JsonProcessingException;
17
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.format.annotation.DateTimeFormat;
20
import org.springframework.security.access.prepost.PreAuthorize;
21
import org.springframework.web.bind.annotation.GetMapping;
22
import org.springframework.web.bind.annotation.PostMapping;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestParam;
25
import org.springframework.web.bind.annotation.RestController;
26
27
import java.time.LocalDateTime;
28
29
@Tag(name = "RecommendationRequest")
30
@RequestMapping("/api/recommendationrequest")
31
@RestController
32
@Slf4j
33
public class RecommendationRequestController extends ApiController {
34
    @Autowired
35
    RecommendationRequestRepository recommendationRequestRepository;
36
    @Autowired
37
    CurrentUserService currentUserService;
38
    @Autowired
39
    UserRepository userRepository;
40
    @Autowired
41
    RequestTypeRepository requestTypeRepository;
42
43
    /**
44
     * List all recommendation requests
45
     * 
46
     * @return an iterable of RecommendationRequest
47
     */
48
    @Operation(summary= "List all recommendation requests")
49
    @PreAuthorize("hasRole('ROLE_ADMIN')")
50
    @GetMapping("/alladmin")
51
    public Iterable<RecommendationRequest> allRecommendationRequests() {
52
        Iterable<RecommendationRequest> requests = recommendationRequestRepository.findAll();
53 1 1. allRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allRecommendationRequests → KILLED
        return requests;
54
    }
55
	
56
	/**
57
     * List all recommendation requests created by a user with requesterId
58
     * 
59
     * @return an iterable of RecommendationRequest
60
     */
61
    @Operation(summary= "List all recommendation requests created by a user with requesterId")
62
    @PreAuthorize("hasRole('ROLE_USER')")
63
    @GetMapping("/all")
64
    public Iterable<RecommendationRequest> getAllCurrentUser() {
65
        User currentUser = currentUserService.getUser();
66
        Long requesterId = currentUser.getId();
67
        Iterable<RecommendationRequest> requests = recommendationRequestRepository.findAllByRequesterId(requesterId);
68
69 1 1. getAllCurrentUser : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getAllCurrentUser → KILLED
        return requests;
70
    }
71
72
    /**
73
     * Create a new recommendation request
74
     * 
75
     * @param professorId the id of the professor
76
     * @param requestType the request type
77
     * @param details the details of the recommendation request
78
     * @param neededByDate the date the request should be fulfilled by
79
     * @return a RecommendationRequest
80
     */
81
    @Operation(summary= "Create a new request")
82
    @PreAuthorize("hasRole('ROLE_USER')")
83
    @PostMapping("/post")
84
85
    public RecommendationRequest postRecommendationRequest(
86
            @Parameter(name="professorId") @RequestParam Long professorId,
87
            @Parameter(name="requestType") @RequestParam String requestType,
88
            @Parameter(name="details") @RequestParam String details,
89
            @Parameter(name="neededByDate", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("neededByDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime neededByDate
90
            )
91
            throws JsonProcessingException {
92
93
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
94
        // See: https://www.baeldung.com/spring-date-parameters
95
96
        RecommendationRequest recommendationRequest = new RecommendationRequest();
97
        User currentUser = currentUserService.getUser();
98
        Long requesterId = currentUser.getId();
99
100
        User professor = userRepository.findById(professorId)
101 1 1. lambda$postRecommendationRequest$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$postRecommendationRequest$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(User.class, professorId));
102
103 1 1. postRecommendationRequest : negated conditional → KILLED
        if (!professor.getAdmin()) {
104
            throw new IllegalArgumentException("Requested professor is not an admin.");
105
        }
106
107
        requestTypeRepository.findByRequestType(requestType)
108 1 1. lambda$postRecommendationRequest$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$postRecommendationRequest$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RequestType.class, requestType));
109
110 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequesterId → KILLED
        recommendationRequest.setRequesterId(requesterId);
111 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setProfessorId → KILLED
        recommendationRequest.setProfessorId(professorId);
112 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequestType → KILLED
        recommendationRequest.setRequestType(requestType);
113 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED
        recommendationRequest.setDetails(details);
114
115
        // completionDate is unassigned until completed, so we set that as null
116
        LocalDateTime submissionDate = LocalDateTime.now();
117
        submissionDate = submissionDate.minusNanos(submissionDate.getNano());
118
119
        String status = "Pending";
120
121 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setNeededByDate → KILLED
        recommendationRequest.setNeededByDate(neededByDate);
122 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setSubmissionDate → KILLED
        recommendationRequest.setSubmissionDate(submissionDate);
123 1 1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED
        recommendationRequest.setStatus(status);
124
125
        RecommendationRequest savedRecommendationRequest = recommendationRequestRepository.save(recommendationRequest);
126
127 1 1. postRecommendationRequest : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::postRecommendationRequest → KILLED
        return savedRecommendationRequest;
128
    }
129
130
    /**
131
     * Get a single request by id
132
     * 
133
     * CHECK if recommendation request belongs to user
134
     * 
135
     * @param id the id of the request
136
     * @return a RecommendationRequest
137
     */
138
    @Operation(summary= "Get a single request")
139
    @PreAuthorize("hasRole('ROLE_USER')")
140
    @GetMapping("")
141
    public RecommendationRequest getById(
142
            @Parameter(name="id") @RequestParam Long id
143
        ) {
144
        User currentUser = currentUserService.getUser();
145
        Long requesterId = currentUser.getId();
146
147
        RecommendationRequest recommendationRequest = recommendationRequestRepository.findById(id)
148 1 1. lambda$getById$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$getById$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
149
150 1 1. getById : negated conditional → KILLED
		if (requesterId != recommendationRequest.getRequesterId()) {
151
			// throw entity not found to reveal less information to any malicious user
152
			throw new EntityNotFoundException(RecommendationRequest.class, id);
153
		}
154
155 1 1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getById → KILLED
        return recommendationRequest;
156
    }
157
}

Mutations

53

1.1
Location : allRecommendationRequests
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:admin_can_get_all_recommendationrequests()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allRecommendationRequests → KILLED

69

1.1
Location : getAllCurrentUser
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:logged_in_user_can_get_all_recommendationrequests()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getAllCurrentUser → KILLED

101

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

103

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

108

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

110

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequesterId → KILLED

111

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setProfessorId → KILLED

112

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequestType → KILLED

113

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED

121

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setNeededByDate → KILLED

122

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setSubmissionDate → KILLED

123

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED

127

1.1
Location : postRecommendationRequest
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[method:a_user_can_post_a_new_recommendationrequest()]
replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::postRecommendationRequest → KILLED

148

1.1
Location : lambda$getById$2
Killed by : edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.RecommendationRequestControllerTests]/[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/RecommendationRequestController::lambda$getById$2 → KILLED

150

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

155

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

Active mutators

Tests examined


Report generated by PIT 1.17.0