ApiController.java

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

Mutations

35

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

51

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

60

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

70

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

82

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

96

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

110

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

122

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_students_throws_illegal_argument()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/ApiController::handleIllegalArgument → KILLED

138

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

150

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

Active mutators

Tests examined


Report generated by PIT 1.17.0