UsersController.java

1
package edu.ucsb.cs156.dining.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.http.ResponseEntity;
8
import org.springframework.security.access.prepost.PreAuthorize;
9
import org.springframework.web.bind.annotation.GetMapping;
10
import org.springframework.web.bind.annotation.RequestMapping;
11
import org.springframework.web.bind.annotation.RestController;
12
13
import edu.ucsb.cs156.dining.entities.User;
14
import edu.ucsb.cs156.dining.repositories.UserRepository;
15
import io.swagger.v3.oas.annotations.Operation;
16
import io.swagger.v3.oas.annotations.tags.Tag;
17
18
19
import edu.ucsb.cs156.dining.models.CurrentUser;
20
import edu.ucsb.cs156.dining.entities.User;
21
import edu.ucsb.cs156.dining.errors.EntityNotFoundException;
22
import org.springframework.web.bind.annotation.*;
23
import org.springframework.http.HttpStatus;
24
import org.springframework.web.server.ResponseStatusException;
25
import java.util.List;
26
27
import java.time.LocalDate;
28
29
/**
30
 * This is a REST controller for getting information about the users.
31
 * 
32
 * These endpoints are only accessible to users with the role "ROLE_ADMIN".
33
 */
34
35
@Tag(name="User information (admin only)")
36
@RequestMapping("/api")
37
@RestController
38
public class UsersController extends ApiController {
39
    @Autowired
40
    UserRepository userRepository;
41
42
    @Autowired
43
    ObjectMapper mapper;
44
45
    /**
46
     * This method returns a list of all users.  Accessible only to users with the role "ROLE_ADMIN".
47
     * @return a list of all users
48
     * @throws JsonProcessingException if there is an error processing the JSON
49
     */
50
    @Operation(summary= "Get a list of all users")
51
    @PreAuthorize("hasRole('ROLE_ADMIN')")
52
    @GetMapping("/admin/users")
53
    public ResponseEntity<String> users()
54
            throws JsonProcessingException {
55
56
57
        Iterable<User> users = userRepository.findAll();
58
        String body = mapper.writeValueAsString(users);
59 1 1. users : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::users → KILLED
        return ResponseEntity.ok().body(body);
60
    }
61
62
    /**
63
     * This method returns list of all users with a proposed alias.
64
     * @return a list of users with a proposed alias
65
     * @throws JsonProcessingException if there is an error processing the JSON
66
     */
67
    @Operation(summary = "Get a list of all users with a proposed alias")
68
    @PreAuthorize("hasRole('ROLE_ADMIN')")
69
    @GetMapping("/admin/usersWithProposedAlias")
70
    public ResponseEntity<String> getUsersWithProposedAlias()
71
            throws JsonProcessingException {
72
        Iterable<User> users = userRepository.findByProposedAliasNotNull();
73
        String body = mapper.writeValueAsString(users);
74 1 1. getUsersWithProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::getUsersWithProposedAlias → KILLED
        return ResponseEntity.ok().body(body);
75
    }
76
77
    /**
78
     * This method allows the user to update their alias.
79
     * @param proposedAlias the new alias
80
     * @return the updated user
81
     */
82
    @Operation(summary = "Update proposed alias of the current user")
83
    @PreAuthorize("hasRole('ROLE_USER')")
84
    @PostMapping("/currentUser/updateAlias")
85
    public ResponseEntity<User> updateProposedAlias(@RequestParam String proposedAlias) {
86
        CurrentUser currentUser = super.getCurrentUser();
87
        User user = currentUser.getUser();
88
89 1 1. updateProposedAlias : negated conditional → KILLED
        if (userRepository.findByAlias(proposedAlias).isPresent()) {
90
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Alias already in use.");
91
        }
92
    
93 1 1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED
        user.setProposedAlias(proposedAlias);
94 1 1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED
        user.setStatus("Awaiting Moderation");
95
        User savedUser = userRepository.save(user);
96
    
97 1 1. updateProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateProposedAlias → KILLED
        return ResponseEntity.ok(savedUser);
98
    }
99
    
100
    /**
101
     * This method allows an admin to update the moderation status of a user's alias.
102
     * @param id the id of the user to update
103
     * @param approved the new moderation status 
104
     * @return the updated user
105
     */
106
    @PreAuthorize("hasRole('ROLE_ADMIN')")
107
    @PutMapping("/currentUser/updateAliasModeration")
108
    public User updateAliasModeration(
109
            @RequestParam long id, 
110
            @RequestParam Boolean approved) {
111
        
112
        User user = userRepository.findById(id)
113 1 1. lambda$updateAliasModeration$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::lambda$updateAliasModeration$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(User.class, id));
114
        
115
116 1 1. updateAliasModeration : negated conditional → KILLED
        if (approved) {
117 1 1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setAlias → KILLED
            user.setAlias(user.getProposedAlias());  
118 1 1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED
            user.setStatus("Approved");
119 1 1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setDateApproved → KILLED
            user.setDateApproved(LocalDate.now());
120 1 1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED
            user.setProposedAlias(null);
121
        } else {
122 1 1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED
            user.setStatus("Rejected");
123 1 1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED
            user.setProposedAlias(null);
124
        }
125
        
126
        userRepository.save(user);
127
128 1 1. updateAliasModeration : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateAliasModeration → KILLED
        return user;
129
    }
130
}

Mutations

59

1.1
Location : users
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:users__admin_logged_in()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::users → KILLED

74

1.1
Location : getUsersWithProposedAlias
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_can_get_all_users_with_proposed_alias()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::getUsersWithProposedAlias → KILLED

89

1.1
Location : updateProposedAlias
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:user_cannot_post_existing_alias()]
negated conditional → KILLED

93

1.1
Location : updateProposedAlias
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:a_user_can_post_a_new_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED

94

1.1
Location : updateProposedAlias
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:a_user_can_post_a_new_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED

97

1.1
Location : updateProposedAlias
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:a_user_can_post_a_new_alias()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateProposedAlias → KILLED

113

1.1
Location : lambda$updateAliasModeration$0
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_cannot_approve_nonexistent_user()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::lambda$updateAliasModeration$0 → KILLED

116

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_approves_alias()]
negated conditional → KILLED

117

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_approves_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setAlias → KILLED

118

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_approves_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED

119

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_approves_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setDateApproved → KILLED

120

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_approves_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED

122

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_does_not_approve_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED

123

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_does_not_approve_alias()]
removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED

128

1.1
Location : updateAliasModeration
Killed by : edu.ucsb.cs156.dining.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.UsersControllerTests]/[method:admin_approves_alias()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateAliasModeration → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0