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

Mutations

55

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

65

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

66

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

84

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

85

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

86

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

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()]
removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → 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()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::postShift → KILLED

100

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

102

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

104

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

115

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

117

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

118

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

119

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

120

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

121

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

126

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

Active mutators

Tests examined


Report generated by PIT 1.7.3