RestaurantsController.java

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

Mutations

46

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 Collections.emptyList for edu/ucsb/cs156/example/controllers/RestaurantsController::allRestaurants → KILLED

60

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

62

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

79

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()]
removed call to edu/ucsb/cs156/example/entities/Restaurant::setName → KILLED

80

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()]
removed call to edu/ucsb/cs156/example/entities/Restaurant::setDescription → KILLED

83

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

97

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

99

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

100

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

117

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

119

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

120

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

124

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.17.0