UsersController.java

1
package edu.ucsb.cs156.organic.controllers;
2
import com.fasterxml.jackson.core.JsonProcessingException;
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.http.ResponseEntity;
6
import org.springframework.security.access.prepost.PreAuthorize;
7
import org.springframework.web.bind.annotation.GetMapping;
8
import org.springframework.web.bind.annotation.PostMapping;
9
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.RequestParam;
11
import org.springframework.web.bind.annotation.RestController;
12
13
import edu.ucsb.cs156.organic.entities.User;
14
import edu.ucsb.cs156.organic.repositories.UserRepository;
15
import edu.ucsb.cs156.organic.errors.EntityNotFoundException;
16
17
import io.swagger.v3.oas.annotations.tags.Tag;
18
import io.swagger.v3.oas.annotations.Operation;
19
import io.swagger.v3.oas.annotations.Parameter;
20
21
22
23
@Tag(name = "User information (admin only)")
24
@RequestMapping("/api/admin/users")
25
@RestController
26
public class UsersController extends ApiController {
27
    @Autowired
28
    UserRepository userRepository;
29
    @Autowired
30
    ObjectMapper mapper;
31
    @Operation(summary = "Get a list of all users")
32
    @PreAuthorize("hasRole('ROLE_ADMIN')")
33
    @GetMapping("")
34
    public ResponseEntity<String> users()
35
            throws JsonProcessingException {
36
        Iterable<User> users = userRepository.findAll();
37
        String body = mapper.writeValueAsString(users);
38 1 1. users : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::users → KILLED
        return ResponseEntity.ok().body(body);
39
    }
40
41
42
    @Operation(summary = "Toggle the admin status")
43
    @PreAuthorize("hasRole('ROLE_ADMIN')")
44
    @PostMapping("/toggleAdmin")
45
    public Object toggleAdmin( @Parameter(name = "githubId", description = "Integer, github id of user to toggle their admin status", example = "1", required = true) @RequestParam Integer githubId){
46 1 1. lambda$toggleAdmin$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::lambda$toggleAdmin$0 → KILLED
        User user = userRepository.findByGithubId(githubId).orElseThrow(() -> new EntityNotFoundException(User.class, githubId));
47 2 1. toggleAdmin : negated conditional → KILLED
2. toggleAdmin : removed call to edu/ucsb/cs156/organic/entities/User::setAdmin → KILLED
        user.setAdmin(!user.isAdmin());
48
        userRepository.save(user);
49 1 1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::toggleAdmin → KILLED
        return genericMessage("User with id %s has toggled admin status to %s".formatted(githubId, user.isAdmin()));
50
51
    }
52
53
    @Operation(summary = "Toggle the instructor status")
54
    @PreAuthorize("hasRole('ROLE_ADMIN')")
55
    @PostMapping("/toggleInstructor")
56
    public Object toggleInstructor( @Parameter(name = "githubId", description = "Integer, github id of user to toggle their instructor status", example = "1", required = true) @RequestParam Integer githubId){
57 1 1. lambda$toggleInstructor$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::lambda$toggleInstructor$1 → KILLED
        User user = userRepository.findByGithubId(githubId).orElseThrow(() -> new EntityNotFoundException(User.class, githubId));
58 2 1. toggleInstructor : negated conditional → KILLED
2. toggleInstructor : removed call to edu/ucsb/cs156/organic/entities/User::setInstructor → KILLED
        user.setInstructor(!user.isInstructor());
59
        userRepository.save(user);
60 1 1. toggleInstructor : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::toggleInstructor → KILLED
        return genericMessage("User with id %s has toggled instructor status to %s".formatted(githubId, user.isInstructor()));
61
    }
62
63
64
65
}

Mutations

38

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

46

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

47

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

49

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

57

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

58

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

2.2
Location : toggleInstructor
Killed by : edu.ucsb.cs156.organic.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UsersControllerTests]/[method:admin_can_toggle_instructor_status_of_a_user_from_false_to_true()]
removed call to edu/ucsb/cs156/organic/entities/User::setInstructor → KILLED

60

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

Active mutators

Tests examined


Report generated by PIT 1.7.3