RestaurantsController.java

1
package edu.ucsb.cs156.rec.controllers;
2
3
import edu.ucsb.cs156.rec.entities.Restaurant;
4
import edu.ucsb.cs156.rec.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.rec.repositories.RestaurantRepository;
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
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 jakarta.validation.Valid;
23
24
/**
25
 * This is a REST controller for Restaurants
26
 */
27
28
@Tag(name = "Restaurants")
29
@RequestMapping("/api/restaurants")
30
@RestController
31
public class RestaurantsController extends ApiController {
32
33
    @Autowired
34
    RestaurantRepository restaurantRepository;
35
36
    /**
37
     * This method returns a list of all restaurants.
38
     * @return a list of all restaurants
39
     */
40
    @Operation(summary = "List all restaurants")
41
    @PreAuthorize("hasRole('ROLE_USER')")
42
    @GetMapping("/all")
43
    public Iterable<Restaurant> allRestaurants() {
44
        Iterable<Restaurant> restaurants = restaurantRepository.findAll();
45 1 1. allRestaurants : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RestaurantsController::allRestaurants → KILLED
        return restaurants;
46
    }
47
48
    /**
49
     * This method returns a single restaurant.
50
     * @param id id of the restaurant to get
51
     * @return a single restaurant
52
     */
53
    @Operation(summary = "Get a single restaurant")
54
    @PreAuthorize("hasRole('ROLE_USER')")
55
    @GetMapping("")
56
    public Restaurant getById(
57
            @Parameter(name = "id") @RequestParam Long id) {
58
        Restaurant restaurant = restaurantRepository.findById(id)
59 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::lambda$getById$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));
60
61 1 1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::getById → KILLED
        return restaurant;
62
    }
63
64
    /**
65
     * This method creates a new restaurant. Accessible only to users with the role "ROLE_ADMIN".
66
     * @param name name of the restaurant
67
     * @param description description of the restaurant
68
     * @return the save restaurant (with it's id field set by the database)
69
     */
70
    @Operation(summary = "Create a new restaurant")
71
    @PreAuthorize("hasRole('ROLE_ADMIN')")
72
    @PostMapping("/post")
73
    public Restaurant postRestaurant(
74
            @Parameter(name = "name") @RequestParam String name,
75
            @Parameter(name = "description") @RequestParam String description) {
76
        Restaurant restaurant = new Restaurant();
77
78 1 1. postRestaurant : removed call to edu/ucsb/cs156/rec/entities/Restaurant::setName → KILLED
        restaurant.setName(name);
79 1 1. postRestaurant : removed call to edu/ucsb/cs156/rec/entities/Restaurant::setDescription → KILLED
        restaurant.setDescription(description);
80
        
81
        Restaurant savedrestaurant = restaurantRepository.save(restaurant);
82 1 1. postRestaurant : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::postRestaurant → KILLED
        return savedrestaurant;
83
    }
84
85
    /**
86
     * Deletes a restaurant. Accessible only to users with the role "ROLE_ADMIN".
87
     * @param id id of the restaurant to delete
88
     * @return a message indicating that the restaurant was deleted
89
     */
90
    @Operation(summary = "Delete a Restaurant")
91
    @PreAuthorize("hasRole('ROLE_ADMIN')")
92
    @DeleteMapping("")
93
    public Object deleteRestaurant(
94
            @Parameter(name = "id") @RequestParam Long id) {
95
        Restaurant restaurant = restaurantRepository.findById(id)
96 1 1. lambda$deleteRestaurant$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::lambda$deleteRestaurant$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));
97
98 1 1. deleteRestaurant : removed call to edu/ucsb/cs156/rec/repositories/RestaurantRepository::delete → KILLED
        restaurantRepository.delete(restaurant);
99 1 1. deleteRestaurant : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::deleteRestaurant → KILLED
        return genericMessage("Restaurant with id %s deleted".formatted(id));
100
    }
101
102
    /**
103
     * Update a single restaurant. Accessible only to users with the role "ROLE_ADMIN".
104
     * @param id id of the restaurant to update
105
     * @param incoming the new restaurant contents
106
     * @return the updated restaurant object
107
     */
108
    @Operation(summary = "Update a single restaurant")
109
    @PreAuthorize("hasRole('ROLE_ADMIN')")
110
    @PutMapping("")
111
    public Restaurant updateRestaurant(
112
            @Parameter(name = "id") @RequestParam Long id,
113
            @RequestBody @Valid Restaurant incoming) {
114
115
        Restaurant restaurant = restaurantRepository.findById(id)
116 1 1. lambda$updateRestaurant$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::lambda$updateRestaurant$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));
117
118 1 1. updateRestaurant : removed call to edu/ucsb/cs156/rec/entities/Restaurant::setName → KILLED
        restaurant.setName(incoming.getName());
119 1 1. updateRestaurant : removed call to edu/ucsb/cs156/rec/entities/Restaurant::setDescription → KILLED
        restaurant.setDescription(incoming.getDescription());
120
121
        restaurantRepository.save(restaurant);
122
123 1 1. updateRestaurant : replaced return value with null for edu/ucsb/cs156/rec/controllers/RestaurantsController::updateRestaurant → KILLED
        return restaurant;
124
    }
125
}

Mutations

45

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

59

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

61

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

78

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

79

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

82

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

96

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

98

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

99

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

116

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

118

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

119

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

123

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

Active mutators

Tests examined


Report generated by PIT 1.17.0