ChatFilterWordsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.ChatFilterWord;
4
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.happiercows.repositories.ChatFilterWordRepository;
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 java.util.Locale;
10
import lombok.extern.slf4j.Slf4j;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpStatus;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.web.bind.annotation.*;
16
17
@Slf4j
18
@Tag(name = "Chat Filter Words")
19
@RequestMapping("/api/chatfilterwords")
20
@RestController
21
public class ChatFilterWordsController extends ApiController {
22
23
    /** Matches the VARCHAR(255) size of the WORD column in the migration. */
24
    static final int MAX_WORD_LENGTH = 255;
25
26
    @Autowired
27
    private ChatFilterWordRepository chatFilterWordRepository;
28
29
    @Operation(summary = "List all inappropriate words that are censored in chat")
30
    @PreAuthorize("hasRole('ROLE_ADMIN')")
31
    @GetMapping("/all")
32
    public Iterable<ChatFilterWord> allChatFilterWords() {
33 1 1. allChatFilterWords : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::allChatFilterWords → KILLED
        return chatFilterWordRepository.findAll();
34
    }
35
36
    @Operation(summary = "Add an inappropriate word to be censored in chat")
37
    @PreAuthorize("hasRole('ROLE_ADMIN')")
38
    @PostMapping("")
39
    public ResponseEntity<Object> postChatFilterWord(
40
            @Parameter(description = "The word or phrase to censor. It is trimmed, runs of whitespace are collapsed to a single space, and it is lowercased.") @RequestParam String word) {
41
        String normalized = word.trim().replaceAll("\\s+", " ").toLowerCase(Locale.ROOT);
42
43 1 1. postChatFilterWord : negated conditional → KILLED
        if (normalized.isEmpty()) {
44 1 1. postChatFilterWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
45
                    .body(genericMessage("Word must not be blank"));
46
        }
47
48 2 1. postChatFilterWord : changed conditional boundary → KILLED
2. postChatFilterWord : negated conditional → KILLED
        if (normalized.length() > MAX_WORD_LENGTH) {
49 1 1. postChatFilterWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
50
                    .body(genericMessage("Word must be at most %d characters".formatted(MAX_WORD_LENGTH)));
51
        }
52
53 1 1. postChatFilterWord : negated conditional → KILLED
        if (chatFilterWordRepository.findByWord(normalized).isPresent()) {
54 1 1. postChatFilterWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED
            return ResponseEntity.status(HttpStatus.CONFLICT)
55
                    .body(genericMessage("\"%s\" is already on the list".formatted(normalized)));
56
        }
57
58
        ChatFilterWord saved = chatFilterWordRepository.save(
59
                ChatFilterWord.builder().word(normalized).build());
60 1 1. postChatFilterWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED
        return ResponseEntity.ok(saved);
61
    }
62
63
    @Operation(summary = "Remove a word from the list of inappropriate words")
64
    @PreAuthorize("hasRole('ROLE_ADMIN')")
65
    @DeleteMapping("")
66
    public Object deleteChatFilterWord(
67
            @Parameter(description = "id of the word to remove") @RequestParam Long id) {
68
        ChatFilterWord chatFilterWord = chatFilterWordRepository.findById(id)
69 1 1. lambda$deleteChatFilterWord$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::lambda$deleteChatFilterWord$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(ChatFilterWord.class, id));
70 1 1. deleteChatFilterWord : removed call to edu/ucsb/cs156/happiercows/repositories/ChatFilterWordRepository::delete → KILLED
        chatFilterWordRepository.delete(chatFilterWord);
71 1 1. deleteChatFilterWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::deleteChatFilterWord → KILLED
        return genericMessage("ChatFilterWord with id %s deleted".formatted(id));
72
    }
73
}

Mutations

33

1.1
Location : allChatFilterWords
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCanListWords()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::allChatFilterWords → KILLED

43

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

44

1.1
Location : postChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCannotAddBlankWord()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED

48

1.1
Location : postChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCanAddWordOfMaxLength()]
changed conditional boundary → KILLED

2.2
Location : postChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCannotAddWordOverMaxLength()]
negated conditional → KILLED

49

1.1
Location : postChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCannotAddWordOverMaxLength()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED

53

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

54

1.1
Location : postChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCannotAddDuplicateWord()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED

60

1.1
Location : postChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCanAddWordAndItIsTrimmedAndLowercased()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::postChatFilterWord → KILLED

69

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

70

1.1
Location : deleteChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCanDeleteWord()]
removed call to edu/ucsb/cs156/happiercows/repositories/ChatFilterWordRepository::delete → KILLED

71

1.1
Location : deleteChatFilterWord
Killed by : edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ChatFilterWordsControllerTests]/[method:adminCanDeleteWord()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatFilterWordsController::deleteChatFilterWord → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1