HelpRequestController.java

1
package edu.ucsb.cs156.example.controllers; // not sure if i need this
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import edu.ucsb.cs156.example.entities.HelpRequest;
5
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.example.repositories.HelpRequestRepository;
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 java.time.LocalDateTime;
11
import lombok.extern.slf4j.Slf4j;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.format.annotation.DateTimeFormat;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.web.bind.annotation.DeleteMapping;
16
import org.springframework.web.bind.annotation.GetMapping;
17
import org.springframework.web.bind.annotation.PostMapping;
18
import org.springframework.web.bind.annotation.PutMapping;
19
import org.springframework.web.bind.annotation.RequestBody;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestParam;
22
import org.springframework.web.bind.annotation.RestController;
23
24
/** This is a REST controller for HelpRequest */
25
@Tag(name = "HelpRequests")
26
@RequestMapping("/api/helprequests")
27
@RestController
28
@Slf4j
29
public class HelpRequestController extends ApiController {
30
31
  @Autowired
32
  HelpRequestRepository
33
      helpRequestRepository; // Uppercase = class, lowercase = variable (convention)
34
35
  /**
36
   * List all HelpRequests
37
   *
38
   * @return an iterable of HelpRequests
39
   */
40
  @Operation(summary = "List all help requests")
41
  @PreAuthorize("hasRole('ROLE_USER')")
42
  @GetMapping("/all")
43
  public Iterable<HelpRequest> allHelpRequests() {
44
    Iterable<HelpRequest> helpRequests = helpRequestRepository.findAll();
45 1 1. allHelpRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/HelpRequestController::allHelpRequests → KILLED
    return helpRequests;
46
  }
47
48
  /**
49
   * Create a new helpRequest.
50
   *
51
   * @param requesterEmail requester’s email
52
   * @param teamId team id string (e.g., s22-5pm-3)
53
   * @param tableOrBreakoutRoom table or room number
54
   * @param requestTime ISO-8601 timestamp
55
   * @param explanation free-text explanation
56
   * @param solved whether it’s solved
57
   * @return the saved HelpRequest
58
   */
59
  @Operation(summary = "Create a new helpRequest")
60
  @PreAuthorize("hasRole('ROLE_ADMIN')")
61
  @PostMapping("/post")
62
  public HelpRequest postHelpRequest(
63
      @Parameter(name = "requesterEmail") @RequestParam String requesterEmail,
64
      @Parameter(name = "teamId") @RequestParam String teamId,
65
      @Parameter(name = "tableOrBreakoutRoom") @RequestParam String tableOrBreakoutRoom,
66
      @Parameter(
67
              name = "requestTime",
68
              description = "date and time in ISO format, e.g. 2025-10-28T17:35:00 (no timezone)")
69
          @RequestParam("requestTime")
70
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
71
          LocalDateTime requestTime,
72
      @Parameter(name = "explanation") @RequestParam String explanation,
73
      @Parameter(name = "solved") @RequestParam boolean solved)
74
      throws JsonProcessingException {
75
76
    // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
77
    // See: https://www.baeldung.com/spring-date-parameters
78
    log.info("requestTime={}", requestTime);
79
80
    HelpRequest helpRequest = new HelpRequest();
81 1 1. postHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequesterEmail → KILLED
    helpRequest.setRequesterEmail(requesterEmail);
82 1 1. postHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTeamId → KILLED
    helpRequest.setTeamId(teamId);
83 1 1. postHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTableOrBreakoutRoom → KILLED
    helpRequest.setTableOrBreakoutRoom(tableOrBreakoutRoom);
84 1 1. postHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequestTime → KILLED
    helpRequest.setRequestTime(requestTime);
85 1 1. postHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setExplanation → KILLED
    helpRequest.setExplanation(explanation);
86 1 1. postHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setSolved → KILLED
    helpRequest.setSolved(solved);
87
88
    HelpRequest saved = helpRequestRepository.save(helpRequest);
89 1 1. postHelpRequest : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::postHelpRequest → KILLED
    return saved;
90
  }
91
92
  /**
93
   * Get a single help request by id
94
   *
95
   * @param id the id of the help request
96
   * @return a HelpRequest
97
   */
98
  @Operation(summary = "Get a single help request")
99
  @PreAuthorize("hasRole('ROLE_USER')")
100
  @GetMapping("")
101
  public HelpRequest getById(@Parameter(name = "id") @RequestParam Long id) {
102
    HelpRequest helpRequest =
103
        helpRequestRepository
104
            .findById(id)
105 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::lambda$getById$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(HelpRequest.class, id));
106
107 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::getById → KILLED
    return helpRequest;
108
  }
109
110
  /**
111
   * Update a single help request. Accessible only to users with the role "ROLE_ADMIN".
112
   *
113
   * @param id the id of the help request to update
114
   * @param incoming the new help request data
115
   * @return the updated help request
116
   */
117
  @Operation(summary = "Update a single help request")
118
  @PreAuthorize("hasRole('ROLE_ADMIN')")
119
  @PutMapping("")
120
  public HelpRequest updateHelpRequest(
121
      @Parameter(name = "id") @RequestParam Long id, @RequestBody HelpRequest incoming) {
122
123
    HelpRequest helpRequest =
124
        helpRequestRepository
125
            .findById(id)
126 1 1. lambda$updateHelpRequest$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::lambda$updateHelpRequest$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(HelpRequest.class, id));
127
128 1 1. updateHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequesterEmail → KILLED
    helpRequest.setRequesterEmail(incoming.getRequesterEmail());
129 1 1. updateHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTeamId → KILLED
    helpRequest.setTeamId(incoming.getTeamId());
130 1 1. updateHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTableOrBreakoutRoom → KILLED
    helpRequest.setTableOrBreakoutRoom(incoming.getTableOrBreakoutRoom());
131 1 1. updateHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequestTime → KILLED
    helpRequest.setRequestTime(incoming.getRequestTime());
132 1 1. updateHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setExplanation → KILLED
    helpRequest.setExplanation(incoming.getExplanation());
133 1 1. updateHelpRequest : removed call to edu/ucsb/cs156/example/entities/HelpRequest::setSolved → KILLED
    helpRequest.setSolved(incoming.getSolved());
134
135
    helpRequestRepository.save(helpRequest);
136
137 1 1. updateHelpRequest : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::updateHelpRequest → KILLED
    return helpRequest;
138
  }
139
140
  /**
141
   * Delete a help request. Accessible only to users with the role "ROLE_ADMIN".
142
   *
143
   * @param id the id of the help request to delete
144
   * @return a message indicating the request was deleted
145
   */
146
  @Operation(summary = "Delete a help request")
147
  @PreAuthorize("hasRole('ROLE_ADMIN')")
148
  @DeleteMapping("")
149
  public Object deleteHelpRequest(@Parameter(name = "id") @RequestParam Long id) {
150
    HelpRequest helpRequest =
151
        helpRequestRepository
152
            .findById(id)
153 1 1. lambda$deleteHelpRequest$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::lambda$deleteHelpRequest$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(HelpRequest.class, id));
154
155 1 1. deleteHelpRequest : removed call to edu/ucsb/cs156/example/repositories/HelpRequestRepository::delete → KILLED
    helpRequestRepository.delete(helpRequest);
156 1 1. deleteHelpRequest : replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::deleteHelpRequest → KILLED
    return genericMessage("HelpRequest with id %s deleted".formatted(id));
157
  }
158
}

Mutations

45

1.1
Location : allHelpRequests
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:logged_in_user_can_get_all_helprequest()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/HelpRequestController::allHelpRequests → KILLED

81

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequesterEmail → KILLED

82

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTeamId → KILLED

83

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTableOrBreakoutRoom → KILLED

84

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequestTime → KILLED

85

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setExplanation → KILLED

86

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setSolved → KILLED

89

1.1
Location : postHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:an_admin_user_can_post_a_new_helprequest()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::postHelpRequest → KILLED

105

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

107

1.1
Location : getById
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:test_that_logged_in_user_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::getById → KILLED

126

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

128

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequesterEmail → KILLED

129

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTeamId → KILLED

130

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setTableOrBreakoutRoom → KILLED

131

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setRequestTime → KILLED

132

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setExplanation → KILLED

133

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
removed call to edu/ucsb/cs156/example/entities/HelpRequest::setSolved → KILLED

137

1.1
Location : updateHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_edit_an_existing_helprequest()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::updateHelpRequest → KILLED

153

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

155

1.1
Location : deleteHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_delete_a_helprequest()]
removed call to edu/ucsb/cs156/example/repositories/HelpRequestRepository::delete → KILLED

156

1.1
Location : deleteHelpRequest
Killed by : edu.ucsb.cs156.example.controllers.HelpRequestControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HelpRequestControllerTests]/[method:admin_can_delete_a_helprequest()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HelpRequestController::deleteHelpRequest → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0