UsersController.java

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

Mutations

40

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

51

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

53

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

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

56

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

64

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

66

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

68

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

Active mutators

Tests examined


Report generated by PIT 1.7.3