UsersController.java

1
package edu.ucsb.cs156.gauchoride.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
6
import edu.ucsb.cs156.gauchoride.entities.User;
7
import edu.ucsb.cs156.gauchoride.repositories.UserRepository;
8
9
import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException;
10
11
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.web.bind.annotation.DeleteMapping;
16
import org.springframework.web.bind.annotation.GetMapping;
17
import org.springframework.web.bind.annotation.PostMapping;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20
import org.springframework.web.bind.annotation.RestController;
21
22
import io.swagger.v3.oas.annotations.tags.Tag;
23
import io.swagger.v3.oas.annotations.Operation;
24
import io.swagger.v3.oas.annotations.Parameter;
25
26
27
@Tag(name = "User information (admin only)")
28
@RequestMapping("/api/admin/users")
29
@RestController
30
public class UsersController extends ApiController {
31
    @Autowired
32
    UserRepository userRepository;
33
34
    @Autowired
35
    ObjectMapper mapper;
36
37
    @Operation(summary = "Get a list of all users")
38
    @PreAuthorize("hasRole('ROLE_ADMIN')")
39
    @GetMapping("")
40
    public ResponseEntity<String> users()
41
            throws JsonProcessingException {
42
        Iterable<User> users = userRepository.findAll();
43
        String body = mapper.writeValueAsString(users);
44 1 1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED
        return ResponseEntity.ok().body(body);
45
    }
46
47
    @Operation(summary = "Get user by id")
48
    @PreAuthorize("hasRole('ROLE_ADMIN')")
49
    @GetMapping("/get")
50
    public User users(
51
            @Parameter(name = "id", description = "Long, id number of user to get", example = "1", required = true) @RequestParam Long id)
52
            throws JsonProcessingException {
53
        User user = userRepository.findById(id)
54 1 1. lambda$users$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$users$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(User.class, id));
55 1 1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED
        return user;
56
    }
57
58
    @Operation(summary = "Delete a user (admin)")
59
    @PreAuthorize("hasRole('ROLE_ADMIN')")
60
    @DeleteMapping("/delete")
61
    public Object deleteUser_Admin(
62
            @Parameter(name = "id", description = "Long, id number of user to delete", example = "1", required = true) @RequestParam Long id) {
63
              User user = userRepository.findById(id)
64 1 1. lambda$deleteUser_Admin$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$deleteUser_Admin$1 → KILLED
          .orElseThrow(() -> new EntityNotFoundException(User.class, id));
65
66 1 1. deleteUser_Admin : removed call to edu/ucsb/cs156/gauchoride/repositories/UserRepository::delete → KILLED
          userRepository.delete(user);
67
68 1 1. deleteUser_Admin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::deleteUser_Admin → KILLED
        return genericMessage("User with id %s deleted".formatted(id));
69
    }
70
71
    
72
    @Operation(summary = "Toggle the admin field")
73
    @PreAuthorize("hasRole('ROLE_ADMIN')")
74
    @PostMapping("/toggleAdmin")
75
    public Object toggleAdmin( @Parameter(name = "id", description = "Long, id number of user to toggle their admin field", example = "1", required = true) @RequestParam Long id){
76
        User user = userRepository.findById(id)
77 1 1. lambda$toggleAdmin$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleAdmin$2 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(User.class, id));
78
79 2 1. toggleAdmin : negated conditional → KILLED
2. toggleAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED
        user.setAdmin(!user.getAdmin());
80
        userRepository.save(user);
81 1 1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleAdmin → KILLED
        return genericMessage("User with id %s has toggled admin status to %s".formatted(id, user.getAdmin()));
82
    }
83
84
85
    @Operation(summary = "Toggle the driver field")
86
    @PreAuthorize("hasRole('ROLE_ADMIN')")
87
    @PostMapping("/toggleDriver")
88
    public Object toggleDriver( @Parameter(name = "id", description = "Long, id number of user to toggle their driver field", example = "1", required = true) @RequestParam Long id){
89
90
        User user = userRepository.findById(id)
91 1 1. lambda$toggleDriver$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleDriver$3 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(User.class, id));
92
93 2 1. toggleDriver : negated conditional → KILLED
2. toggleDriver : removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED
        user.setDriver(!user.getDriver());
94
        userRepository.save(user);
95 1 1. toggleDriver : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleDriver → KILLED
        return genericMessage("User with id %s has toggled driver status to %s".formatted(id, user.getDriver()));
96
    }
97
98
    @Operation(summary = "Toggle the rider field")
99
    @PreAuthorize("hasRole('ROLE_ADMIN')")
100
    @PostMapping("/toggleRider")
101
    public Object toggleRider( @Parameter(name = "id") @RequestParam Long id){
102
        User user = userRepository.findById(id)
103 1 1. lambda$toggleRider$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleRider$4 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(User.class, id));
104
105 2 1. toggleRider : negated conditional → KILLED
2. toggleRider : removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED
        user.setRider(!user.getRider());
106
        userRepository.save(user);
107 1 1. toggleRider : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleRider → KILLED
        return genericMessage("User with id %s has toggled rider status to %s".formatted(id, user.getRider()));
108
    }
109
}

Mutations

44

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

54

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

55

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

64

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

66

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

68

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

77

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

79

1.1
Location : toggleAdmin
Killed by : edu.ucsb.cs156.gauchoride.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.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.gauchoride.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.UsersControllerTests]/[method:admin_can_toggle_admin_status_of_a_user_from_true_to_false()]
removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED

81

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

91

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

93

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

2.2
Location : toggleDriver
Killed by : edu.ucsb.cs156.gauchoride.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.UsersControllerTests]/[method:admin_can_toggle_driver_status_of_a_user_from_true_to_false()]
removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED

95

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

103

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

105

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

2.2
Location : toggleRider
Killed by : edu.ucsb.cs156.gauchoride.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.UsersControllerTests]/[method:admin_can_toggle_rider_status_of_a_user_from_true_to_false()]
removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED

107

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

Active mutators

Tests examined


Report generated by PIT 1.7.3