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