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 = "Create a new shift for the table")
66
    @PreAuthorize("hasRole('ROLE_ADMIN')")
67
    @PostMapping("/post")
68
    public Shift postShift(
69
        @Parameter(name="day") @RequestParam String day,
70
        @Parameter(name="shiftStart") @RequestParam String shiftStart,
71
        @Parameter(name="shiftEnd") @RequestParam String shiftEnd,
72
        @Parameter(name="driverID") @RequestParam long driverID ,
73
        @Parameter(name="driverBackupID") @RequestParam long driverBackupID
74
        )
75
        {
76
77
        Shift shift = new Shift();
78
79 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED
        shift.setDriverID(getCurrentUser().getUser().getId());
80 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED
        shift.setDay(day);
81 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED
        shift.setShiftStart(shiftStart);
82 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED
        shift.setShiftEnd(shiftEnd);
83 1 1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED
        shift.setDriverBackupID(driverBackupID);
84
85
        Shift savedShift = shiftRepository.save(shift);
86
87 1 1. postShift : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::postShift → KILLED
        return savedShift;
88
    }
89
90
    
91
    @Operation(summary= "Delete a shift")
92
    @PreAuthorize("hasRole('ROLE_ADMIN')")
93
    @DeleteMapping("")
94
    public Object deleteShift(
95
            @Parameter(name="id") @RequestParam long id) {
96
        Shift shift = shiftRepository.findById(id)
97 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));
98
99 1 1. deleteShift : removed call to edu/ucsb/cs156/gauchoride/repositories/ShiftRepository::delete → KILLED
        shiftRepository.delete(shift);
100 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));
101
    }
102
103
    @Operation(summary= "Update a single shift")
104
    @PreAuthorize("hasRole('ROLE_ADMIN')")
105
    @PutMapping("")
106
    public Shift updateReview(
107
            @Parameter(name="id") @RequestParam long id,
108
            @RequestBody @Valid Shift incoming) {
109
110
        Shift shift = shiftRepository.findById(id)
111 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));
112
113 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED
        shift.setDay(incoming.getDay());
114 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED
        shift.setShiftStart(incoming.getShiftStart());
115 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED
        shift.setShiftEnd(incoming.getShiftEnd());
116 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED
        shift.setDriverID(incoming.getDriverID());
117 1 1. updateReview : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED
        shift.setDriverBackupID(incoming.getDriverBackupID());
118
119
        shiftRepository.save(shift);
120
121 1 1. updateReview : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::updateReview → KILLED
        return shift;
122
    }
123
124
}

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_admin_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_admin_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_user_can_get_by_id_when_the_id_exists_and_user_id_matches()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::shiftByID → KILLED

79

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

80

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

81

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

82

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

83

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

87

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

97

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

99

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

100

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

111

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

113

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

114

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

115

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

116

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

117

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

121

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