UsersController.java

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

Mutations

41

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

49

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

51

1.1
Location : suspendUser
Killed by : edu.ucsb.cs156.happiercows.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UsersControllerTests]/[method:testSuspendUser_success()]
removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED

53

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

61

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

63

1.1
Location : restoreUser
Killed by : edu.ucsb.cs156.happiercows.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.UsersControllerTests]/[method:testRestoreUser_success()]
removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED

65

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

Active mutators

Tests examined


Report generated by PIT 1.7.3