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.PathVariable;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestParam;
21
import org.springframework.web.bind.annotation.RestController;
22
23
import io.swagger.annotations.Api;
24
import io.swagger.annotations.ApiOperation;
25
import io.swagger.annotations.ApiParam;
26
27
28
@Api(description = "User information (admin only)")
29
@RequestMapping("/api/admin/users")
30
@RestController
31
public class UsersController extends ApiController {
32
    @Autowired
33
    UserRepository userRepository;
34
35
    @Autowired
36
    ObjectMapper mapper;
37
38
    @ApiOperation(value = "Get a list of all users")
39
    @PreAuthorize("hasRole('ROLE_ADMIN')")
40
    @GetMapping("")
41
    public ResponseEntity<String> users()
42
            throws JsonProcessingException {
43
        Iterable<User> users = userRepository.findAll();
44
        String body = mapper.writeValueAsString(users);
45 1 1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED
        return ResponseEntity.ok().body(body);
46
    }
47
48
    @ApiOperation(value = "Get user by id")
49
    @PreAuthorize("hasRole('ROLE_ADMIN')")
50
    @GetMapping("/get")
51
    public User users(
52
            @ApiParam("id") @RequestParam Long id)
53
            throws JsonProcessingException {
54
        User user = userRepository.findById(id)
55 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));
56 1 1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED
        return user;
57
    }
58
59
    @ApiOperation(value = "Delete a user (admin)")
60
    @PreAuthorize("hasRole('ROLE_ADMIN')")
61
    @DeleteMapping("/delete")
62
    public Object deleteUser_Admin(
63
            @ApiParam("id") @RequestParam Long id) {
64
              User user = userRepository.findById(id)
65 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));
66
67 1 1. deleteUser_Admin : removed call to edu/ucsb/cs156/gauchoride/repositories/UserRepository::delete → KILLED
          userRepository.delete(user);
68
69 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));
70
    }
71
72
    
73
    @ApiOperation(value = "Toggle the admin field")
74
    @PreAuthorize("hasRole('ROLE_ADMIN')")
75
    @PostMapping("/toggleAdmin")
76
    public Object toggleAdmin( @ApiParam("id") @RequestParam Long id){
77
        User user = userRepository.findById(id)
78 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));
79
80 2 1. toggleAdmin : negated conditional → KILLED
2. toggleAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED
        user.setAdmin(!user.getAdmin());
81
        userRepository.save(user);
82 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".formatted(id));
83
    }
84
85
    @ApiOperation(value = "Toggle the driver field")
86
    @PreAuthorize("hasRole('ROLE_ADMIN')")
87
    @PostMapping("/toggleDriver")
88
    public Object toggleDriver( @ApiParam("id") @RequestParam Long id){
89
        User user = userRepository.findById(id)
90 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));
91
92 2 1. toggleDriver : negated conditional → KILLED
2. toggleDriver : removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED
        user.setDriver(!user.getDriver());
93
        userRepository.save(user);
94 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".formatted(id));
95
    }
96
    
97
    @ApiOperation(value = "Toggle the rider field")
98
    @PreAuthorize("hasRole('ROLE_ADMIN')")
99
    @PostMapping("/toggleRider")
100
    public Object toggleRider( @ApiParam("id") @RequestParam Long id){
101
        User user = userRepository.findById(id)
102 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));
103
104 2 1. toggleRider : negated conditional → KILLED
2. toggleRider : removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED
        user.setRider(!user.getRider());
105
        userRepository.save(user);
106 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".formatted(id));
107
    }
108
}

Mutations

45

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

55

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

56

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

65

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

67

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

69

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

78

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_toggle_non_existant_user_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleAdmin$2 → KILLED

80

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_false_to_true()]
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_false_to_true()]
removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED

82

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_false_to_true()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleAdmin → KILLED

90

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

92

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_false_to_true()]
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_false_to_true()]
removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED

94

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_false_to_true()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleDriver → KILLED

102

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

104

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_false_to_true()]
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_false_to_true()]
removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED

106

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_false_to_true()]
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