UsersController.java

1
package edu.ucsb.cs156.rec.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.DeleteMapping;
10
import org.springframework.web.bind.annotation.GetMapping;
11
import org.springframework.web.bind.annotation.PostMapping;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.RequestParam;
14
import org.springframework.web.bind.annotation.RestController;
15
16
import edu.ucsb.cs156.rec.errors.EntityNotFoundException;
17
18
import edu.ucsb.cs156.rec.entities.User;
19
import edu.ucsb.cs156.rec.repositories.UserRepository;
20
import io.swagger.v3.oas.annotations.Operation;
21
import io.swagger.v3.oas.annotations.tags.Tag;
22
import io.swagger.v3.oas.annotations.Parameter;
23
24
/**
25
 * This is a REST controller for getting information about the users.
26
 * 
27
 * These endpoints are only accessible to users with the role "ROLE_ADMIN".
28
 */
29
30
@Tag(name="User information (admin only)")
31
@RequestMapping("/api/admin/users")
32
@RestController
33
public class UsersController extends ApiController {
34
    @Autowired
35
    UserRepository userRepository;
36
37
    @Autowired
38
    ObjectMapper mapper;
39
40
    /**
41
     * This method returns a list of all users.  Accessible only to users with the role "ROLE_ADMIN".
42
     * @return a list of all users
43
     * @throws JsonProcessingException if there is an error processing the JSON
44
     */
45
    @Operation(summary= "Get a list of all users")
46
    @PreAuthorize("hasRole('ROLE_ADMIN')")
47
    @GetMapping("")
48
    public ResponseEntity<String> users()
49
            throws JsonProcessingException {
50
        Iterable<User> users = userRepository.findAll();
51
        String body = mapper.writeValueAsString(users);
52 1 1. users : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::users → KILLED
        return ResponseEntity.ok().body(body);
53
    }
54
55
    @Operation(summary = "Get user by id")
56
    @PreAuthorize("hasRole('ROLE_ADMIN')")
57
    @GetMapping("/get")
58
    public User users(
59
            @Parameter(name = "id", description = "Long, id number of user to get", example = "1", required = true) @RequestParam Long id)
60
            throws JsonProcessingException {
61
        User user = userRepository.findById(id)
62 1 1. lambda$users$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$users$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(User.class, id));
63 1 1. users : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::users → KILLED
        return user;
64
    }
65
66
    @Operation(summary = "Delete a user (admin)")
67
    @PreAuthorize("hasRole('ROLE_ADMIN')")
68
    @DeleteMapping("/delete")
69
    public Object deleteUser_Admin(
70
            @Parameter(name = "id", description = "Long, id number of user to delete", example = "1", required = true) @RequestParam Long id) {
71
              User user = userRepository.findById(id)
72 1 1. lambda$deleteUser_Admin$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$deleteUser_Admin$1 → KILLED
          .orElseThrow(() -> new EntityNotFoundException(User.class, id));
73
74 1 1. deleteUser_Admin : removed call to edu/ucsb/cs156/rec/repositories/UserRepository::delete → KILLED
          userRepository.delete(user);
75
76 1 1. deleteUser_Admin : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::deleteUser_Admin → KILLED
        return genericMessage("User with id %s deleted".formatted(id));
77
    }
78
79
    
80
    @Operation(summary = "Toggle the admin field") 
81
    @PreAuthorize("hasRole('ROLE_ADMIN')")
82
    @PostMapping("/toggleAdmin")
83
    public Object toggleAdmin( @Parameter(name = "id", description = "Long, id number of user to toggle their admin field", example = "1", required = true) @RequestParam Long id){
84
        User user = userRepository.findById(id)
85 1 1. lambda$toggleAdmin$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$toggleAdmin$2 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(User.class, id));
86
87 2 1. toggleAdmin : negated conditional → KILLED
2. toggleAdmin : removed call to edu/ucsb/cs156/rec/entities/User::setAdmin → KILLED
        user.setAdmin(!user.getAdmin());
88
        userRepository.save(user);
89 1 1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::toggleAdmin → KILLED
        return genericMessage("User with id %s has toggled admin status to %s".formatted(id, user.getAdmin()));
90
    }
91
92
93
    @Operation(summary = "Toggle the professor field") 
94
    @PreAuthorize("hasRole('ROLE_ADMIN')")
95
    @PostMapping("/toggleProfessor")
96
    public Object toggleProfessor( @Parameter(name = "id", description = "Long, id number of user to toggle their professor field", example = "1", required = true) @RequestParam Long id){
97
98
        User user = userRepository.findById(id)
99 1 1. lambda$toggleProfessor$3 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$toggleProfessor$3 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(User.class, id));
100
101 2 1. toggleProfessor : negated conditional → KILLED
2. toggleProfessor : removed call to edu/ucsb/cs156/rec/entities/User::setProfessor → KILLED
        user.setProfessor(!user.getProfessor());
102
        userRepository.save(user);
103 1 1. toggleProfessor : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::toggleProfessor → KILLED
        return genericMessage("User with id %s has toggled professor status to %s".formatted(id, user.getProfessor()));
104
    }
105
106
    @Operation(summary = "Toggle the student field") 
107
    @PreAuthorize("hasRole('ROLE_ADMIN')")
108
    @PostMapping("/toggleStudent")
109
    public Object toggleStudent( @Parameter(name = "id") @RequestParam Long id){
110
        User user = userRepository.findById(id)
111 1 1. lambda$toggleStudent$4 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$toggleStudent$4 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(User.class, id));
112
113 2 1. toggleStudent : negated conditional → KILLED
2. toggleStudent : removed call to edu/ucsb/cs156/rec/entities/User::setStudent → KILLED
        user.setStudent(!user.getStudent());
114
        userRepository.save(user);
115 1 1. toggleStudent : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::toggleStudent → KILLED
        return genericMessage("User with id %s has toggled student status to %s".formatted(id, user.getStudent()));
116
    }
117
}

Mutations

52

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

62

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

63

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

72

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

74

1.1
Location : deleteUser_Admin
Killed by : edu.ucsb.cs156.rec.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.UsersControllerTests]/[method:admin_can_delete_a_user()]
removed call to edu/ucsb/cs156/rec/repositories/UserRepository::delete → KILLED

76

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

85

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

87

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

2.2
Location : toggleAdmin
Killed by : edu.ucsb.cs156.rec.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.UsersControllerTests]/[method:admin_can_toggle_admin_status_of_a_user_from_true_to_false()]
removed call to edu/ucsb/cs156/rec/entities/User::setAdmin → KILLED

89

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

99

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

101

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

2.2
Location : toggleProfessor
Killed by : edu.ucsb.cs156.rec.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.UsersControllerTests]/[method:admin_can_toggle_professor_status_of_a_user_from_false_to_true()]
removed call to edu/ucsb/cs156/rec/entities/User::setProfessor → KILLED

103

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

111

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

113

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

2.2
Location : toggleStudent
Killed by : edu.ucsb.cs156.rec.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.rec.controllers.UsersControllerTests]/[method:admin_can_toggle_student_status_of_a_user_from_true_to_false()]
removed call to edu/ucsb/cs156/rec/entities/User::setStudent → KILLED

115

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

Active mutators

Tests examined


Report generated by PIT 1.17.0