BooksController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Book;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.BookRepository;
6
import io.swagger.annotations.Api;
7
import io.swagger.annotations.ApiOperation;
8
import io.swagger.annotations.ApiParam;
9
import lombok.extern.slf4j.Slf4j;
10
11
import com.fasterxml.jackson.core.JsonProcessingException;
12
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.format.annotation.DateTimeFormat;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.DeleteMapping;
17
import org.springframework.web.bind.annotation.GetMapping;
18
import org.springframework.web.bind.annotation.PostMapping;
19
import org.springframework.web.bind.annotation.PutMapping;
20
import org.springframework.web.bind.annotation.RequestBody;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24
25
import javax.validation.Valid;
26
27
28
@Api(description = "Books")
29
@RequestMapping("/api/books")
30
@RestController
31
@Slf4j
32
public class BooksController extends ApiController {
33
34
    @Autowired
35
    BookRepository BookRepository;
36
37
    @ApiOperation(value = "List all books")
38
    @PreAuthorize("hasRole('ROLE_USER')")
39
    @GetMapping("/all")
40
    public Iterable<Book> allBooks() {
41
        Iterable<Book> books = BookRepository.findAll();
42 1 1. allBooks : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::allBooks → KILLED
        return books;
43
    }
44
    
45
    @ApiOperation(value = "Get a single date")
46
    @PreAuthorize("hasRole('ROLE_USER')")
47
    @GetMapping("")
48
    public Book getById(
49
            @ApiParam("id") @RequestParam Long id) {
50
        Book book = BookRepository.findById(id)
51 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Book.class, id));
52
53 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::getById → KILLED
        return book;
54
    }
55
    
56
    @ApiOperation(value = "Create a new book")
57
    @PreAuthorize("hasRole('ROLE_ADMIN')")
58
    @PostMapping("/post")
59
    public Book postBook(
60
            @ApiParam("title") @RequestParam String title,
61
            @ApiParam("author") @RequestParam String author,
62
            @ApiParam("description") @RequestParam String description)
63
            throws JsonProcessingException {
64
65
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
66
        // See: https://www.baeldung.com/spring-date-parameters
67
68
69
        Book book = new Book();
70 1 1. postBook : removed call to edu/ucsb/cs156/example/entities/Book::setTitle → KILLED
        book.setTitle(title);
71 1 1. postBook : removed call to edu/ucsb/cs156/example/entities/Book::setAuthor → KILLED
        book.setAuthor(author);
72 1 1. postBook : removed call to edu/ucsb/cs156/example/entities/Book::setDescription → KILLED
        book.setDescription(description);
73
74
        Book savedBook = BookRepository.save(book);
75
76 1 1. postBook : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::postBook → KILLED
        return savedBook;
77
    }
78
    
79
    @ApiOperation(value = "Delete a Book")
80
    @PreAuthorize("hasRole('ROLE_ADMIN')")
81
    @DeleteMapping("")
82
    public Object deleteBook(
83
            @ApiParam("id") @RequestParam Long id) {
84
        Book book = BookRepository.findById(id)
85 1 1. lambda$deleteBook$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::lambda$deleteBook$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Book.class, id));
86
87 1 1. deleteBook : removed call to edu/ucsb/cs156/example/repositories/BookRepository::delete → KILLED
        BookRepository.delete(book);
88 1 1. deleteBook : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::deleteBook → KILLED
        return genericMessage("Book with id %s deleted".formatted(id));
89
    }
90
91
    @ApiOperation(value = "Update a single book")
92
    @PreAuthorize("hasRole('ROLE_ADMIN')")
93
    @PutMapping("")
94
    public Book updateBook(
95
            @ApiParam("id") @RequestParam Long id,
96
            @RequestBody @Valid Book incoming) {
97
98
        Book book = BookRepository.findById(id)
99 1 1. lambda$updateBook$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::lambda$updateBook$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Book.class, id));
100
101 1 1. updateBook : removed call to edu/ucsb/cs156/example/entities/Book::setTitle → KILLED
        book.setTitle(incoming.getTitle());
102 1 1. updateBook : removed call to edu/ucsb/cs156/example/entities/Book::setAuthor → KILLED
        book.setAuthor(incoming.getAuthor());
103 1 1. updateBook : removed call to edu/ucsb/cs156/example/entities/Book::setDescription → KILLED
        book.setDescription(incoming.getDescription());
104
105
        BookRepository.save(book);
106
107 1 1. updateBook : replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::updateBook → KILLED
        return book;
108
    
109
    }
110
    
111
}

Mutations

42

1.1
Location : allBooks
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:logged_in_user_can_get_all_books()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::allBooks → KILLED

51

1.1
Location : lambda$getById$0
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:test_that_logged_in_user_can_get_by_id_when_the_id_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::lambda$getById$0 → KILLED

53

1.1
Location : getById
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:test_that_logged_in_user_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::getById → KILLED

70

1.1
Location : postBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:an_admin_user_can_post_a_new_book()]
removed call to edu/ucsb/cs156/example/entities/Book::setTitle → KILLED

71

1.1
Location : postBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:an_admin_user_can_post_a_new_book()]
removed call to edu/ucsb/cs156/example/entities/Book::setAuthor → KILLED

72

1.1
Location : postBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:an_admin_user_can_post_a_new_book()]
removed call to edu/ucsb/cs156/example/entities/Book::setDescription → KILLED

76

1.1
Location : postBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:an_admin_user_can_post_a_new_book()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::postBook → KILLED

85

1.1
Location : lambda$deleteBook$1
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_tries_to_delete_non_existant_book_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::lambda$deleteBook$1 → KILLED

87

1.1
Location : deleteBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_can_delete_a_date()]
removed call to edu/ucsb/cs156/example/repositories/BookRepository::delete → KILLED

88

1.1
Location : deleteBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_can_delete_a_date()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::deleteBook → KILLED

99

1.1
Location : lambda$updateBook$2
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_cannot_edit_book_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::lambda$updateBook$2 → KILLED

101

1.1
Location : updateBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_can_edit_an_existing_book()]
removed call to edu/ucsb/cs156/example/entities/Book::setTitle → KILLED

102

1.1
Location : updateBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_can_edit_an_existing_book()]
removed call to edu/ucsb/cs156/example/entities/Book::setAuthor → KILLED

103

1.1
Location : updateBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_can_edit_an_existing_book()]
removed call to edu/ucsb/cs156/example/entities/Book::setDescription → KILLED

107

1.1
Location : updateBook
Killed by : edu.ucsb.cs156.example.controllers.BooksControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BooksControllerTests]/[method:admin_can_edit_an_existing_book()]
replaced return value with null for edu/ucsb/cs156/example/controllers/BooksController::updateBook → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3