BookController.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 org.springframework.beans.factory.annotation.Autowired;
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.PutMapping;
17
import org.springframework.web.bind.annotation.RequestBody;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20
import org.springframework.web.bind.annotation.RestController;
21
22
import javax.validation.Valid;
23
24
25
@Api(description = "Book")
26
@RequestMapping("/api/books")
27
@RestController
28
@Slf4j
29
public class BookController extends ApiController {
30
31
    @Autowired
32
    BookRepository bookRepository;
33
34
    @ApiOperation(value = "List all books")
35
    @PreAuthorize("hasRole('ROLE_USER')")
36
    @GetMapping("/all")
37
    public Iterable<Book> allBooks() {
38
        Iterable<Book> books = bookRepository.findAll();
39 1 1. allBooks : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::allBooks → KILLED
        return books;
40
    }
41
42
    @ApiOperation(value = "Get a single book")
43
    @PreAuthorize("hasRole('ROLE_USER')")
44
    @GetMapping("")
45
    public Book getById(
46
            @ApiParam("id") @RequestParam Long id) {
47
        Book books = bookRepository.findById(id)
48 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Book.class, id));
49
50 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::getById → KILLED
        return books;
51
    }
52
53
    @ApiOperation(value = "Create a new book")
54
    @PreAuthorize("hasRole('ROLE_ADMIN')")
55
    @PostMapping("/post")
56
    public Book postBooks(
57
        @ApiParam("name") @RequestParam String name,
58
        @ApiParam("author") @RequestParam String author,
59
        @ApiParam("genre") @RequestParam String genre
60
        )
61
        {
62
63
        Book books = new Book();
64 1 1. postBooks : removed call to edu/ucsb/cs156/example/entities/Book::setName → KILLED
        books.setName(name);
65 1 1. postBooks : removed call to edu/ucsb/cs156/example/entities/Book::setAuthor → KILLED
        books.setAuthor(author);
66 1 1. postBooks : removed call to edu/ucsb/cs156/example/entities/Book::setGenre → KILLED
        books.setGenre(genre);
67
        Book savedBook = bookRepository.save(books);
68
69 1 1. postBooks : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::postBooks → KILLED
        return savedBook;
70
    }
71
    @ApiOperation(value = "Update a single book")
72
    @PreAuthorize("hasRole('ROLE_ADMIN')")
73
    @PutMapping("")
74
    public Book updateBooks(
75
            @ApiParam("id") @RequestParam Long id,
76
            @RequestBody @Valid Book incoming) {
77
78
        Book books = bookRepository.findById(id)
79 1 1. lambda$updateBooks$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::lambda$updateBooks$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Book.class, id));
80
81
82 1 1. updateBooks : removed call to edu/ucsb/cs156/example/entities/Book::setName → KILLED
        books.setName(incoming.getName());  
83 1 1. updateBooks : removed call to edu/ucsb/cs156/example/entities/Book::setAuthor → KILLED
        books.setAuthor(incoming.getAuthor());
84 1 1. updateBooks : removed call to edu/ucsb/cs156/example/entities/Book::setGenre → KILLED
        books.setGenre(incoming.getGenre());
85
86
        bookRepository.save(books);
87
88 1 1. updateBooks : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::updateBooks → KILLED
        return books;
89
    }
90
91
    @ApiOperation(value = "Delete a book")
92
    @PreAuthorize("hasRole('ROLE_ADMIN')")
93
    @DeleteMapping("")
94
    public Object deleteBook(
95
            @ApiParam("code") @RequestParam Long id) {
96
        Book books = bookRepository.findById(id)
97 1 1. lambda$deleteBook$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::lambda$deleteBook$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Book.class, id));
98
99 1 1. deleteBook : removed call to edu/ucsb/cs156/example/repositories/BookRepository::delete → KILLED
        bookRepository.delete(books);
100 1 1. deleteBook : replaced return value with null for edu/ucsb/cs156/example/controllers/BookController::deleteBook → KILLED
        return genericMessage("Book with id %s deleted".formatted(id));
101
    }
102
}

Mutations

39

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

48

1.1
Location : lambda$getById$0
Killed by : edu.ucsb.cs156.example.controllers.BookControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BookControllerTests]/[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/BookController::lambda$getById$0 → KILLED

50

1.1
Location : getById
Killed by : edu.ucsb.cs156.example.controllers.BookControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.BookControllerTests]/[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/BookController::getById → KILLED

64

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

65

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

66

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

69

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

79

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

82

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

83

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

84

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

88

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

97

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

99

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

100

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

Active mutators

Tests examined


Report generated by PIT 1.7.3