UsersController.java

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

Mutations

39

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

47

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

51

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

59

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

61

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

63

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