HotelsController.java

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

Mutations

39

1.1
Location : allHotels
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:logged_in_user_can_get_all_hotels()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HotelsController::allHotels → KILLED

48

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

49

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

66

1.1
Location : postHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:an_admin_user_can_post_a_new_hotel()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HotelsController::postHotel → KILLED

75

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

77

1.1
Location : deleteHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:admin_can_delete_a_hotel()]
removed call to edu/ucsb/cs156/example/repositories/HotelRepository::delete → KILLED

78

1.1
Location : deleteHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:admin_can_delete_a_hotel()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HotelsController::deleteHotel → KILLED

89

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

91

1.1
Location : updateHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:admin_can_edit_an_existing_hotel()]
removed call to edu/ucsb/cs156/example/entities/Hotel::setName → KILLED

92

1.1
Location : updateHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:admin_can_edit_an_existing_hotel()]
removed call to edu/ucsb/cs156/example/entities/Hotel::setAddress → KILLED

93

1.1
Location : updateHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:admin_can_edit_an_existing_hotel()]
removed call to edu/ucsb/cs156/example/entities/Hotel::setDescription → KILLED

97

1.1
Location : updateHotel
Killed by : edu.ucsb.cs156.example.controllers.HotelsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.HotelsControllerTests]/[method:admin_can_edit_an_existing_hotel()]
replaced return value with null for edu/ucsb/cs156/example/controllers/HotelsController::updateHotel → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3