ApiController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException;
4
import edu.ucsb.cs156.frontiers.errors.NoLinkedOrganizationException;
5
import edu.ucsb.cs156.frontiers.models.CurrentUser;
6
import edu.ucsb.cs156.frontiers.services.CurrentUserService;
7
import jakarta.validation.ConstraintViolation;
8
import jakarta.validation.ConstraintViolationException;
9
import java.util.Collection;
10
import java.util.List;
11
import java.util.Map;
12
import lombok.extern.slf4j.Slf4j;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.http.HttpStatus;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
17
import org.springframework.security.core.GrantedAuthority;
18
import org.springframework.web.bind.annotation.ExceptionHandler;
19
import org.springframework.web.bind.annotation.ResponseStatus;
20
21
/** This is an abstract class that provides common functionality for all API controllers. */
22
@Slf4j
23
public abstract class ApiController {
24
  @Autowired private CurrentUserService currentUserService;
25
26
  @Autowired RoleHierarchy roleHierarchy;
27
28
  /**
29
   * This method returns the current user.
30
   *
31
   * @return the current user
32
   */
33
  protected CurrentUser getCurrentUser() {
34 1 1. getCurrentUser : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::getCurrentUser → KILLED
    return currentUserService.getCurrentUser();
35
  }
36
37
  /**
38
   * This method checks if the current user has the given role
39
   *
40
   * @return true if the current user has the role, false otherwise
41
   * @param role the role to check
42
   */
43
  protected boolean doesCurrentUserHaveRole(String roleToCheck) {
44
    CurrentUser currentUser = getCurrentUser();
45
    Collection<? extends GrantedAuthority> authorities = currentUser.getRoles();
46
47
    Collection<? extends GrantedAuthority> extendedAuthorities =
48
        roleHierarchy.getReachableGrantedAuthorities(authorities);
49
50 4 1. lambda$doesCurrentUserHaveRole$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/ApiController::lambda$doesCurrentUserHaveRole$0 → KILLED
2. lambda$doesCurrentUserHaveRole$0 : replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/ApiController::lambda$doesCurrentUserHaveRole$0 → KILLED
3. doesCurrentUserHaveRole : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/ApiController::doesCurrentUserHaveRole → KILLED
4. doesCurrentUserHaveRole : replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/ApiController::doesCurrentUserHaveRole → KILLED
    return extendedAuthorities.stream().anyMatch(role -> role.getAuthority().equals(roleToCheck));
51
  }
52
53
  /**
54
   * This method checks if the current user is an admin.
55
   *
56
   * @return true if the current user is an admin, false otherwise
57
   */
58
  protected boolean isCurrentUserAdmin() {
59 2 1. isCurrentUserAdmin : replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/ApiController::isCurrentUserAdmin → KILLED
2. isCurrentUserAdmin : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/ApiController::isCurrentUserAdmin → KILLED
    return doesCurrentUserHaveRole("ROLE_ADMIN");
60
  }
61
62
  /**
63
   * This method returns a generic message.
64
   *
65
   * @param message the message
66
   * @return a map with the message
67
   */
68
  protected Object genericMessage(String message) {
69 1 1. genericMessage : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::genericMessage → KILLED
    return Map.of("message", message);
70
  }
71
72
  /**
73
   * This method handles the EntityNotFoundException. This maps to a 404/Not Found.
74
   *
75
   * @param e the exception
76
   * @return a map with the type and message of the exception
77
   */
78
  @ExceptionHandler({EntityNotFoundException.class})
79
  @ResponseStatus(HttpStatus.NOT_FOUND)
80
  public Object handleEntityNotFoundException(Throwable e) {
81 1 1. handleEntityNotFoundException : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleEntityNotFoundException → KILLED
    return Map.of(
82
        "type", e.getClass().getSimpleName(),
83
        "message", e.getMessage());
84
  }
85
86
  /**
87
   * This method handles the NoLinkedOrganizationException. This maps to a 400/Bad Request.
88
   *
89
   * @param e the exception
90
   * @return a map with the type and message of the exception
91
   */
92
  @ExceptionHandler({NoLinkedOrganizationException.class})
93
  @ResponseStatus(HttpStatus.BAD_REQUEST)
94
  public Object handleNoLinkedOrgException(Throwable e) {
95 1 1. handleNoLinkedOrgException : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleNoLinkedOrgException → KILLED
    return Map.of(
96
        "type", e.getClass().getSimpleName(),
97
        "message", e.getMessage());
98
  }
99
100
  /**
101
   * This method handles the UnsupportedOperationException. This maps to a 403/Forbidden.
102
   *
103
   * @param e the exception
104
   * @return a map with the type and message of the exception
105
   */
106
  @ExceptionHandler(UnsupportedOperationException.class)
107
  public ResponseEntity<Map<String, String>> handleUnsupportedOperation(
108
      UnsupportedOperationException ex) {
109 1 1. handleUnsupportedOperation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleUnsupportedOperation → KILLED
    return ResponseEntity.status(HttpStatus.FORBIDDEN).body(Map.of("message", ex.getMessage()));
110
  }
111
112
  /**
113
   * This method handles the IllegalArgumentException. This maps to a 400/Bad Request.
114
   *
115
   * @param e the exception
116
   * @return a map with the type and message of the exception
117
   */
118
  @ExceptionHandler({IllegalArgumentException.class})
119
  @ResponseStatus(HttpStatus.BAD_REQUEST)
120
  public Object handleIllegalArgument(Throwable e) {
121 1 1. handleIllegalArgument : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleIllegalArgument → KILLED
    return Map.of(
122
        "type", e.getClass().getSimpleName(),
123
        "message", e.getMessage());
124
  }
125
126
  /**
127
   * This method handles the ConstraintViolationException. This maps to a 400/Bad Request.
128
   *
129
   * @param e the exception
130
   * @return a map with the type and message of the exception
131
   */
132
  @ExceptionHandler({ConstraintViolationException.class})
133
  @ResponseStatus(HttpStatus.BAD_REQUEST)
134
  public Map<String, String> handleValidationException(ConstraintViolationException e) {
135
    List<String> messages =
136
        e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).toList();
137 1 1. handleValidationException : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/ApiController::handleValidationException → KILLED
    return Map.of("message", String.join(", ", messages));
138
  }
139
}

Mutations

34

1.1
Location : getCurrentUser
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_false()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::getCurrentUser → KILLED

50

1.1
Location : lambda$doesCurrentUserHaveRole$0
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_false()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/ApiController::lambda$doesCurrentUserHaveRole$0 → KILLED

2.2
Location : lambda$doesCurrentUserHaveRole$0
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_true()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/ApiController::lambda$doesCurrentUserHaveRole$0 → KILLED

3.3
Location : doesCurrentUserHaveRole
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_false()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/ApiController::doesCurrentUserHaveRole → KILLED

4.4
Location : doesCurrentUserHaveRole
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_true()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/ApiController::doesCurrentUserHaveRole → KILLED

59

1.1
Location : isCurrentUserAdmin
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_true()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/ApiController::isCurrentUserAdmin → KILLED

2.2
Location : isCurrentUserAdmin
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_doesCurrentUserHaveRole_false()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/ApiController::isCurrentUserAdmin → KILLED

69

1.1
Location : genericMessage
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:generic_message_test()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::genericMessage → KILLED

81

1.1
Location : handleEntityNotFoundException
Killed by : edu.ucsb.cs156.frontiers.controllers.TeamsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.TeamsControllerTests]/[method:testGetTeamById_teamDoesNotExist()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleEntityNotFoundException → KILLED

95

1.1
Location : handleNoLinkedOrgException
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:test_dummy_controller_returns_no_linked_org()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleNoLinkedOrgException → KILLED

109

1.1
Location : handleUnsupportedOperation
Killed by : edu.ucsb.cs156.frontiers.controllers.AdminsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.AdminsControllerTests]/[method:admin_tries_to_delete_an_ADMIN_EMAIL_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleUnsupportedOperation → KILLED

121

1.1
Location : handleIllegalArgument
Killed by : edu.ucsb.cs156.frontiers.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CoursesControllerTests]/[method:delete_course_with_staff_throws_illegal_argument()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleIllegalArgument → KILLED

137

1.1
Location : handleValidationException
Killed by : edu.ucsb.cs156.frontiers.controllers.ApiControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.ApiControllerTests]/[method:validation_exception_handling()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/ApiController::handleValidationException → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0