ShiftController.java

1
package edu.ucsb.cs156.gauchoride.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
6
import edu.ucsb.cs156.gauchoride.entities.Shift;
7
import edu.ucsb.cs156.gauchoride.repositories.ShiftRepository;
8
import edu.ucsb.cs156.gauchoride.repositories.UserRepository;
9
import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException;
10
import edu.ucsb.cs156.gauchoride.models.CurrentUser;
11
12
import java.time.LocalTime;
13
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.security.access.prepost.PreAuthorize;
17
import org.springframework.web.bind.annotation.DeleteMapping;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.PostMapping;
20
import org.springframework.web.bind.annotation.PathVariable;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24
import org.springframework.web.bind.annotation.PutMapping;
25
import org.springframework.web.bind.annotation.RequestBody;
26
27
import javax.validation.Valid;
28
29
import io.swagger.v3.oas.annotations.tags.Tag;
30
import io.swagger.v3.oas.annotations.Operation;
31
import io.swagger.v3.oas.annotations.Parameter;
32
33
34
@Tag(name = "Shift information")
35
@RequestMapping("/api/shift")
36
@RestController
37
public class ShiftController extends ApiController {
38
    @Autowired
39
    ShiftRepository shiftRepository;
40
41
    @Autowired
42
    ObjectMapper mapper;
43
44
    @Operation(summary = "Get a list of all shifts")
45
    @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_USER')")
46
    @GetMapping("/all")
47
    public ResponseEntity<String> allShifts()
48
            throws JsonProcessingException {
49
        Iterable<Shift> shifts = shiftRepository.findAll();
50
        String body = mapper.writeValueAsString(shifts);
51 1 1. allShifts : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::allShifts → KILLED
        return ResponseEntity.ok().body(body);
52
    }
53
54
    @Operation(summary = "Get shift by id")
55
    @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_USER')")
56
    @GetMapping("")
57
    public Shift shiftByID(
58
            @Parameter(name = "id", description = "Long, id number of shift to get", example = "1", required = true) @RequestParam Long id)
59
            throws JsonProcessingException {
60
        Shift shift = shiftRepository.findById(id)
61 1 1. lambda$shiftByID$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$shiftByID$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Shift.class, id));
62 1 1. shiftByID : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::shiftByID → KILLED
        return shift;
63
    }
64
65
    @Operation(summary = "Get a list of all shifts for given driverId")
66
    @PreAuthorize("hasRole('ROLE_DRIVER')")
67
    @GetMapping("/drivershifts")
68
    public ResponseEntity<String> driverShifts()
69
            throws JsonProcessingException {
70
        Long driverID = super.getCurrentUser().getUser().getId();
71
        Iterable<Shift> shifts = shiftRepository.findByDriverID(driverID);
72
        String body = mapper.writeValueAsString(shifts);
73 1 1. driverShifts : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::driverShifts → KILLED
        return ResponseEntity.ok().body(body);
74
    }
75
    
76
77
    @Operation(summary = "Create a new shift for the table")
78
    @PreAuthorize("hasRole('ROLE_ADMIN')")
79
    @PostMapping("/post")
80
    public Shift postShift(
81
        @Parameter(name="day") @RequestParam String day,
82
        @Parameter(name="shiftStart") @RequestParam String shiftStart,
83
        @Parameter(name="shiftEnd") @RequestParam String shiftEnd,
84
        @Parameter(name="driverID") @RequestParam long driverID ,
85
        @Parameter(name="driverBackupID") @RequestParam long driverBackupID
86
        )
87
        {
88
89
        Shift shift = new Shift();
90
91 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED
        shift.setDriverID(getCurrentUser().getUser().getId());
92 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED
        shift.setDay(day);
93 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED
        shift.setShiftStart(shiftStart);
94 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED
        shift.setShiftEnd(shiftEnd);
95 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED
        shift.setDriverBackupID(driverBackupID);
96
97
        Shift savedShift = shiftRepository.save(shift);
98
99 1 1. postShift : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::postShift → KILLED
        return savedShift;
100
    }
101
102
    
103
    @Operation(summary= "Delete a shift")
104
    @PreAuthorize("hasRole('ROLE_ADMIN')")
105
    @DeleteMapping("")
106
    public Object deleteShift(
107
            @Parameter(name="id") @RequestParam long id) {
108
        Shift shift = shiftRepository.findById(id)
109 1 1. lambda$deleteShift$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$deleteShift$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Shift.class, id));
110
111 1 1. deleteShift : removed call to edu/ucsb/cs156/gauchoride/repositories/ShiftRepository::delete → KILLED
        shiftRepository.delete(shift);
112 1 1. deleteShift : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::deleteShift → KILLED
        return genericMessage("Shift with id %s deleted".formatted(id));
113
    }
114
115
    @Operation(summary= "Update a single shift")
116
    @PreAuthorize("hasRole('ROLE_ADMIN')")
117
    @PutMapping("")
118
    public Shift updateReview(
119
            @Parameter(name="id") @RequestParam long id,
120
            @RequestBody @Valid Shift incoming) {
121
122
        Shift shift = shiftRepository.findById(id)
123 1 1. lambda$updateReview$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$updateReview$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Shift.class, id));
124
125 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED
        shift.setDay(incoming.getDay());
126 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED
        shift.setShiftStart(incoming.getShiftStart());
127 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED
        shift.setShiftEnd(incoming.getShiftEnd());
128 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED
        shift.setDriverID(incoming.getDriverID());
129 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED
        shift.setDriverBackupID(incoming.getDriverBackupID());
130
131
        shiftRepository.save(shift);
132
133 1 1. updateReview : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::updateReview → KILLED
        return shift;
134
    }
135
136
}

Mutations

51

1.1
Location : allShifts
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:logged_in_user_can_get_all_shifts()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::allShifts → KILLED

61

1.1
Location : lambda$shiftByID$0
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[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/gauchoride/controllers/ShiftController::lambda$shiftByID$0 → KILLED

62

1.1
Location : shiftByID
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:test_that_logged_in_driver_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::shiftByID → KILLED

73

1.1
Location : driverShifts
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:logged_in_driver_can_get_all_their_shifts()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::driverShifts → KILLED

91

1.1
Location : postShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:an_admin_can_post_a_new_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED

92

1.1
Location : postShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:an_admin_can_post_a_new_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED

93

1.1
Location : postShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:an_admin_can_post_a_new_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED

94

1.1
Location : postShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:an_admin_can_post_a_new_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED

95

1.1
Location : postShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:an_admin_can_post_a_new_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED

99

1.1
Location : postShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:an_admin_can_post_a_new_shift()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::postShift → KILLED

109

1.1
Location : lambda$deleteShift$1
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_tries_to_delete_non_existant_shift_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$deleteShift$1 → KILLED

111

1.1
Location : deleteShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_delete_a_shift()]
removed call to edu/ucsb/cs156/gauchoride/repositories/ShiftRepository::delete → KILLED

112

1.1
Location : deleteShift
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_delete_a_shift()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::deleteShift → KILLED

123

1.1
Location : lambda$updateReview$2
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_cannot_edit_shift_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$updateReview$2 → KILLED

125

1.1
Location : updateReview
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_edit_an_existing_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED

126

1.1
Location : updateReview
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_edit_an_existing_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED

127

1.1
Location : updateReview
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_edit_an_existing_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED

128

1.1
Location : updateReview
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_edit_an_existing_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED

129

1.1
Location : updateReview
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_edit_an_existing_shift()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED

133

1.1
Location : updateReview
Killed by : edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.ShiftControllerTests]/[method:admin_can_edit_an_existing_shift()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::updateReview → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3