BannedWordsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.BannedWord;
4
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.happiercows.repositories.BannedWordRepository;
6
import io.swagger.v3.oas.annotations.Operation;
7
import io.swagger.v3.oas.annotations.Parameter;
8
import io.swagger.v3.oas.annotations.tags.Tag;
9
import lombok.extern.slf4j.Slf4j;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.ResponseEntity;
12
import org.springframework.security.access.prepost.PreAuthorize;
13
import org.springframework.web.bind.annotation.DeleteMapping;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RestController;
19
20
import org.springframework.dao.DataIntegrityViolationException;
21
22
/** Admin-only API for maintaining the list of words censored in chat. */
23
@Slf4j
24
@Tag(name = "Banned Words")
25
@RequestMapping("/api/admin/bannedwords")
26
@RestController
27
public class BannedWordsController extends ApiController {
28
29
    @Autowired
30
    private BannedWordRepository bannedWordRepository;
31
32
    @Operation(summary = "List all banned words")
33
    @PreAuthorize("hasRole('ROLE_ADMIN')")
34
    @GetMapping("/all")
35
    public Iterable<BannedWord> allBannedWords() {
36 1 1. allBannedWords : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::allBannedWords → KILLED
        return bannedWordRepository.findAll();
37
    }
38
39
    @Operation(summary = "Add a banned word (stored trimmed and lowercase)")
40
    @PreAuthorize("hasRole('ROLE_ADMIN')")
41
    @PostMapping("/post")
42
    public ResponseEntity<Object> postBannedWord(
43
            @Parameter(name = "word") @RequestParam String word) {
44
        String normalized = word.trim().toLowerCase();
45
46 1 1. postBannedWord : negated conditional → KILLED
        if (normalized.isEmpty()) {
47 1 1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED
            return ResponseEntity.badRequest().body(genericMessage("Word must not be blank"));
48
        }
49 1 1. postBannedWord : negated conditional → KILLED
        if (bannedWordRepository.findByWord(normalized).isPresent()) {
50 1 1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED
            return ResponseEntity.badRequest()
51
                    .body(genericMessage("Word '%s' is already banned".formatted(normalized)));
52
        }
53
54
        try {
55
            BannedWord saved = bannedWordRepository.save(BannedWord.builder().word(normalized).build());
56 1 1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED
            return ResponseEntity.ok(saved);
57
        } catch (DataIntegrityViolationException e) {
58
            // Another request inserted the same word between findByWord() and save()
59 1 1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED
            return ResponseEntity.badRequest()
60
                    .body(genericMessage("Word '%s' is already banned".formatted(normalized)));
61
        }
62
    }
63
64
    @Operation(summary = "Remove a banned word")
65
    @PreAuthorize("hasRole('ROLE_ADMIN')")
66
    @DeleteMapping("")
67
    public Object deleteBannedWord(
68
            @Parameter(name = "id") @RequestParam Long id) {
69
        BannedWord bannedWord = bannedWordRepository.findById(id)
70 1 1. lambda$deleteBannedWord$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::lambda$deleteBannedWord$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(BannedWord.class, id));
71
72 1 1. deleteBannedWord : removed call to edu/ucsb/cs156/happiercows/repositories/BannedWordRepository::delete → KILLED
        bannedWordRepository.delete(bannedWord);
73 1 1. deleteBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::deleteBannedWord → KILLED
        return genericMessage("BannedWord with id %s deleted".formatted(id));
74
    }
75
}

Mutations

36

1.1
Location : allBannedWords
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_can_get_all_banned_words()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::allBannedWords → KILLED

46

1.1
Location : postBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_cannot_post_duplicate_word()]
negated conditional → KILLED

47

1.1
Location : postBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_cannot_post_blank_word()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED

49

1.1
Location : postBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_cannot_post_duplicate_word()]
negated conditional → KILLED

50

1.1
Location : postBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_cannot_post_duplicate_word()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED

56

1.1
Location : postBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_can_post_word_and_it_is_trimmed_and_lowercased()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED

59

1.1
Location : postBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_gets_400_when_concurrent_insert_violates_unique_constraint()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED

70

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

72

1.1
Location : deleteBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_can_delete_existing_word()]
removed call to edu/ucsb/cs156/happiercows/repositories/BannedWordRepository::delete → KILLED

73

1.1
Location : deleteBannedWord
Killed by : edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.BannedWordsControllerTests]/[method:admin_can_delete_existing_word()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::deleteBannedWord → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1