| 1 | package edu.ucsb.cs156.example.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.example.entities.User; | |
| 4 | import edu.ucsb.cs156.example.models.CurrentUser; | |
| 5 | import java.util.Collection; | |
| 6 | import java.util.List; | |
| 7 | import org.springframework.security.core.GrantedAuthority; | |
| 8 | ||
| 9 | /** | |
| 10 | * This is a service that provides information about the current user. | |
| 11 | * | |
| 12 | * <p>It is an abstract class because we have different implementations for testing and production. | |
| 13 | */ | |
| 14 | public abstract class CurrentUserService { | |
| 15 | ||
| 16 | /** | |
| 17 | * This method returns the current user as a User object. | |
| 18 | * | |
| 19 | * @return the current user | |
| 20 | */ | |
| 21 | public abstract User getUser(); | |
| 22 | ||
| 23 | /** | |
| 24 | * This method returns the current user as a CurrentUser object | |
| 25 | * | |
| 26 | * @return the current user | |
| 27 | */ | |
| 28 | public abstract CurrentUser getCurrentUser(); | |
| 29 | ||
| 30 | /** | |
| 31 | * This method returns the roles of the current user. | |
| 32 | * | |
| 33 | * @return a collection of roles | |
| 34 | */ | |
| 35 | public abstract Collection<? extends GrantedAuthority> getRoles(); | |
| 36 | ||
| 37 | /** | |
| 38 | * This method returns whether the current user is logged in. | |
| 39 | * | |
| 40 | * @return whether the current user is logged in | |
| 41 | */ | |
| 42 | public final boolean isLoggedIn() { | |
| 43 |
2
1. isLoggedIn : negated conditional → KILLED 2. isLoggedIn : replaced boolean return with true for edu/ucsb/cs156/example/services/CurrentUserService::isLoggedIn → KILLED |
return getUser() != null; |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * This method returns the roles of the current user as a sorted list of strings. | |
| 48 | * | |
| 49 | * @return a collection of roles | |
| 50 | */ | |
| 51 | public List<String> getRolesSorted() { | |
| 52 | Collection<? extends GrantedAuthority> authorities = getRoles(); | |
| 53 | List<String> roles = authorities.stream().map(GrantedAuthority::getAuthority).sorted().toList(); | |
| 54 |
1
1. getRolesSorted : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/services/CurrentUserService::getRolesSorted → KILLED |
return roles; |
| 55 | } | |
| 56 | } | |
Mutations | ||
| 43 |
1.1 2.2 |
|
| 54 |
1.1 |