RestaurantsController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Restaurant;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.RestaurantRepository;
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
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.security.access.prepost.PreAuthorize;
12
import org.springframework.web.bind.annotation.*;
13
14
import javax.validation.Valid;
15
16
@Api(description = "Restaurants")
17
@RequestMapping("/api/restaurants")
18
@RestController
19
@Slf4j
20
public class RestaurantsController extends ApiController {
21
22
    @Autowired
23
    RestaurantRepository restaurantRepository;
24
25
    @ApiOperation(value = "List all restaurants")
26
    @PreAuthorize("hasRole('ROLE_USER')")
27
    @GetMapping("/all")
28
    public Iterable<Restaurant> allRestaurants() {
29 1 1. allRestaurants : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::allRestaurants → KILLED
        return restaurantRepository.findAll();
30
    }
31
32
33
    @ApiOperation(value = "Get a single restaurant")
34
    @PreAuthorize("hasRole('ROLE_USER')")
35
    @GetMapping("")
36
    public Restaurant getById(
37
            @ApiParam("id") @RequestParam Long id
38
    ) {
39
40 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::getById → KILLED
        return restaurantRepository.findById(id)
41 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));
42
    }
43
44
    @ApiOperation(value = "Create a restaurant")
45
    @PreAuthorize("hasRole('ROLE_ADMIN')")
46
    @PostMapping("/post")
47
    public Restaurant postRestaurant(
48
            @ApiParam("name") @RequestParam String name,
49
            @ApiParam("address") @RequestParam String address,
50
            @ApiParam("description") @RequestParam String description) {
51
52
        var restaurant = Restaurant.builder()
53
                .name(name)
54
                .address(address)
55
                .description(description)
56
                .build();
57
58 1 1. postRestaurant : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::postRestaurant → KILLED
        return restaurantRepository.save(restaurant);
59
    }
60
61
    @ApiOperation(value = "Delete a restaurant")
62
    @PreAuthorize("hasRole('ROLE_ADMIN')")
63
    @DeleteMapping("")
64
    public Object deleteRestaurant(
65
            @ApiParam("id") @RequestParam Long id) {
66
        Restaurant restaurant = restaurantRepository.findById(id)
67 1 1. lambda$deleteRestaurant$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::lambda$deleteRestaurant$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));
68
69 1 1. deleteRestaurant : removed call to edu/ucsb/cs156/example/repositories/RestaurantRepository::delete → KILLED
        restaurantRepository.delete(restaurant);
70 1 1. deleteRestaurant : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::deleteRestaurant → KILLED
        return genericMessage("Restaurant with id %s deleted".formatted(id));
71
    }
72
73
    @ApiOperation(value = "Update a single restaurant")
74
    @PreAuthorize("hasRole('ROLE_ADMIN')")
75
    @PutMapping("")
76
    public Restaurant updateRestaurant(
77
            @ApiParam("id") @RequestParam Long id,
78
            @RequestBody @Valid Restaurant incoming) {
79
80
        Restaurant restaurant = restaurantRepository.findById(id)
81 1 1. lambda$updateRestaurant$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::lambda$updateRestaurant$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));
82
83 1 1. updateRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setName → KILLED
        restaurant.setName(incoming.getName());
84 1 1. updateRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setAddress → KILLED
        restaurant.setAddress(incoming.getAddress());
85 1 1. updateRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setDescription → KILLED
        restaurant.setDescription(incoming.getDescription());
86
87
        restaurantRepository.save(restaurant);
88
89 1 1. updateRestaurant : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::updateRestaurant → KILLED
        return restaurant;
90
    }
91
}

Mutations

29

1.1
Location : allRestaurants
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:logged_in_user_can_get_all_restaurants()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::allRestaurants → KILLED

40

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

41

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

58

1.1
Location : postRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:an_admin_user_can_post_a_new_restaurant()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::postRestaurant → KILLED

67

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

69

1.1
Location : deleteRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:admin_can_delete_a_restaurant()]
removed call to edu/ucsb/cs156/example/repositories/RestaurantRepository::delete → KILLED

70

1.1
Location : deleteRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:admin_can_delete_a_restaurant()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::deleteRestaurant → KILLED

81

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

83

1.1
Location : updateRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:admin_can_edit_an_existing_restaurant()]
removed call to edu/ucsb/cs156/example/entities/Restaurant::setName → KILLED

84

1.1
Location : updateRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:admin_can_edit_an_existing_restaurant()]
removed call to edu/ucsb/cs156/example/entities/Restaurant::setAddress → KILLED

85

1.1
Location : updateRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:admin_can_edit_an_existing_restaurant()]
removed call to edu/ucsb/cs156/example/entities/Restaurant::setDescription → KILLED

89

1.1
Location : updateRestaurant
Killed by : edu.ucsb.cs156.example.controllers.RestaurantsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.RestaurantsControllerTests]/[method:admin_can_edit_an_existing_restaurant()]
replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::updateRestaurant → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3