DriverAvailabilityController.java

1
package edu.ucsb.cs156.gauchoride.controllers;
2
3
import jakarta.validation.Valid;
4
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.security.access.prepost.PreAuthorize;
8
import org.springframework.web.bind.annotation.DeleteMapping;
9
import org.springframework.web.bind.annotation.GetMapping;
10
import org.springframework.web.bind.annotation.PostMapping;
11
import org.springframework.web.bind.annotation.PutMapping;
12
import org.springframework.web.bind.annotation.RequestBody;
13
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestParam;
15
import org.springframework.web.bind.annotation.RestController;
16
17
import com.fasterxml.jackson.core.JsonProcessingException;
18
19
import edu.ucsb.cs156.gauchoride.entities.DriverAvailability;
20
import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException;
21
import edu.ucsb.cs156.gauchoride.repositories.DriverAvailabilityRepository;
22
import io.swagger.v3.oas.annotations.Operation;
23
import io.swagger.v3.oas.annotations.Parameter;
24
import io.swagger.v3.oas.annotations.tags.Tag;
25
import lombok.extern.slf4j.Slf4j;
26
27
@Tag(name = "DriverAvailability")
28
@RequestMapping("/api/driverAvailability")
29
@RestController
30
@Slf4j
31
public class DriverAvailabilityController extends ApiController{
32
    
33
    @Autowired
34
    DriverAvailabilityRepository driverAvailabilityRepository;
35
36
    //Allows driver to create and post new availability
37
    @Operation(summary= "Create a new driver availability")
38
    @PreAuthorize("hasRole('ROLE_DRIVER')")
39
    @PostMapping("/new")
40
    public DriverAvailability postDriverAvailability(
41
            @Parameter(name="day") @RequestParam String day,
42
            @Parameter(name="startTime") @RequestParam String startTime,
43
            @Parameter(name="endTime") @RequestParam String endTime,
44
            @Parameter(name="notes") @RequestParam String notes)
45
            throws JsonProcessingException {
46
47
48
        log.info("notes={}", notes);
49
50
        DriverAvailability driverAvailability = new DriverAvailability();
51 1 1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDriverId → KILLED
        driverAvailability.setDriverId(getCurrentUser().getUser().getId());
52 1 1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED
        driverAvailability.setDay(day);
53 1 1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED
        driverAvailability.setStartTime(startTime);
54 1 1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED
        driverAvailability.setEndTime(endTime);
55 1 1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED
        driverAvailability.setNotes(notes);
56
57
        DriverAvailability savedDriverAvailability = driverAvailabilityRepository.save(driverAvailability);
58
59 1 1. postDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::postDriverAvailability → KILLED
        return savedDriverAvailability;
60
    }
61
62
    //GET all availability submissions for the current user.
63
    @Operation(summary = "Get all driver availabilites owned by the current user")
64
    @PreAuthorize("hasRole('ROLE_DRIVER')")
65
    @GetMapping("")
66
    public Iterable<DriverAvailability> allApplications() {
67
        Iterable<DriverAvailability> availabilities;
68
        availabilities = driverAvailabilityRepository.findAllByDriverId(getCurrentUser().getUser().getId());
69 1 1. allApplications : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allApplications → KILLED
        return availabilities;
70
    }
71
72
    //GET a single availability by ID if owned by the current user
73
    @Operation(summary = "Get a single availability but only if owned by the current user")
74
    @PreAuthorize("hasRole('ROLE_DRIVER')")
75
    @GetMapping("id")
76
    public DriverAvailability getById(
77
                    @Parameter(name="id", description = "Long, Id of the driver availability to get", 
78
                    required = true)  
79
                    @RequestParam Long id) 
80
    {
81
        DriverAvailability availability;
82
        availability = driverAvailabilityRepository.findById(id)
83 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$getById$0 → KILLED
                    .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));
84 1 1. getById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::getById → KILLED
        return availability;
85
    }
86
87
    //Edits an availability if owned by current user
88
    @Operation(summary = "Edit an existing driver availability but only if it is owned by the current user")
89
    @PreAuthorize("hasRole('ROLE_DRIVER')")
90
    @PutMapping("")
91
    public ResponseEntity<Object> updateDriverAvailability(
92
                            @Parameter(name="id", description = "Long, Id of the driver availability to get", 
93
                                        required = true)  
94
                            @RequestParam Long id,
95
                            @RequestBody @Valid DriverAvailability incoming)
96
    {
97
        DriverAvailability availability;
98
        availability = driverAvailabilityRepository.findById(id)
99 1 1. lambda$updateDriverAvailability$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$updateDriverAvailability$1 → KILLED
                    .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));
100
101 1 1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED
        availability.setDay(incoming.getDay());
102 1 1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED
        availability.setStartTime(incoming.getStartTime());
103 1 1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED
        availability.setEndTime(incoming.getEndTime());
104 1 1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED
        availability.setNotes(incoming.getNotes());
105
106
        driverAvailabilityRepository.save(availability);
107 1 1. updateDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::updateDriverAvailability → KILLED
        return ResponseEntity.ok(availability);
108
109
    }
110
111
    // Edits an availability of any user if the current user is an admin
112
    @Operation(summary = "Edit an existing driver availability if the current user is an admin")
113
    @PreAuthorize("hasRole('ROLE_ADMIN')")
114
    @PutMapping("/admin")
115
    public ResponseEntity<Object> updateAnyDriverAvailability(
116
                            @Parameter(name = "id", description = "Long, Id of the driver availability to get", 
117
                                        required = true)  
118
                            @RequestParam Long id,
119
                            @RequestBody @Valid DriverAvailability incoming)
120
    {
121
        DriverAvailability availability = driverAvailabilityRepository.findById(id)
122 1 1. lambda$updateAnyDriverAvailability$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$updateAnyDriverAvailability$2 → KILLED
                    .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));
123
124 1 1. updateAnyDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED
        availability.setDay(incoming.getDay());
125 1 1. updateAnyDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED
        availability.setStartTime(incoming.getStartTime());
126 1 1. updateAnyDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED
        availability.setEndTime(incoming.getEndTime());
127 1 1. updateAnyDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED
        availability.setNotes(incoming.getNotes());
128
129
        driverAvailabilityRepository.save(availability);
130 1 1. updateAnyDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::updateAnyDriverAvailability → KILLED
        return ResponseEntity.ok(availability);
131
    }
132
133
    //DELETE for driver Availability
134
    @Operation(summary= "Delete a driver availability")
135
    @PreAuthorize("hasRole('ROLE_DRIVER')")
136
    @DeleteMapping("")
137
    public Object deleteDriverAvailability(
138
            @Parameter(name="id") @RequestParam Long id) {
139
        DriverAvailability availability = driverAvailabilityRepository.findById(id)
140 1 1. lambda$deleteDriverAvailability$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$deleteDriverAvailability$3 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));
141 1 1. deleteDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/repositories/DriverAvailabilityRepository::delete → KILLED
        driverAvailabilityRepository.delete(availability);
142 1 1. deleteDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::deleteDriverAvailability → KILLED
        return genericMessage("DriverAvailability with id %s deleted".formatted(id));
143
    }
144
145
    //Lets Admin GET a single availability by ID
146
    @Operation(summary = "Admin can get a single availability by id")
147
    @PreAuthorize("hasRole('ROLE_ADMIN')")
148
    @GetMapping("admin")
149
    public DriverAvailability adminGetById(
150
                    @Parameter(name="id", description = "Long, Id of the driver availability to get", 
151
                    required = true)  
152
                    @RequestParam Long id) 
153
    {
154
        DriverAvailability availability;
155
        availability = driverAvailabilityRepository.findById(id)
156 1 1. lambda$adminGetById$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$adminGetById$4 → KILLED
                    .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));
157 1 1. adminGetById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::adminGetById → KILLED
        return availability;
158
    }
159
160
    //Lets admins get all driver availabilties
161
    @Operation(summary= "List all driver availabilities")
162
    @PreAuthorize("hasRole('ROLE_ADMIN')")
163
    @GetMapping("/admin/all")
164
    public Iterable<DriverAvailability> allDriverAvailabilities() {
165
        Iterable<DriverAvailability> availabilities = driverAvailabilityRepository.findAll();
166 1 1. allDriverAvailabilities : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allDriverAvailabilities → KILLED
        return availabilities;
167
    }
168
169
}

Mutations

51

1.1
Location : postDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:a_driver_can_post_a_new_driverAvailability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDriverId → KILLED

52

1.1
Location : postDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:a_driver_can_post_a_new_driverAvailability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED

53

1.1
Location : postDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:a_driver_can_post_a_new_driverAvailability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED

54

1.1
Location : postDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:a_driver_can_post_a_new_driverAvailability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED

55

1.1
Location : postDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:a_driver_can_post_a_new_driverAvailability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED

59

1.1
Location : postDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:a_driver_can_post_a_new_driverAvailability()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::postDriverAvailability → KILLED

69

1.1
Location : allApplications
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:logged_in_driver_can_get_all_their_own_availabilities()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allApplications → KILLED

83

1.1
Location : lambda$getById$0
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:test_that_logged_in_driver_can_get_by_id_when_the_id_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$getById$0 → KILLED

84

1.1
Location : getById
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:test_that_logged_in_driver_can_get_by_id_when_the_id_exists_and_user_id_matches()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::getById → KILLED

99

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

101

1.1
Location : updateDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_edit_their_own_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED

102

1.1
Location : updateDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_edit_their_own_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED

103

1.1
Location : updateDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_edit_their_own_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED

104

1.1
Location : updateDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_edit_their_own_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED

107

1.1
Location : updateDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_edit_their_own_availability()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::updateDriverAvailability → KILLED

122

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

124

1.1
Location : updateAnyDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:admin_can_edit_any_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED

125

1.1
Location : updateAnyDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:admin_can_edit_any_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED

126

1.1
Location : updateAnyDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:admin_can_edit_any_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED

127

1.1
Location : updateAnyDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:admin_can_edit_any_availability()]
removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED

130

1.1
Location : updateAnyDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:admin_can_edit_any_availability()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::updateAnyDriverAvailability → KILLED

140

1.1
Location : lambda$deleteDriverAvailability$3
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_tries_to_delete_non_existant_availability_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$deleteDriverAvailability$3 → KILLED

141

1.1
Location : deleteDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_delete_an_availability()]
removed call to edu/ucsb/cs156/gauchoride/repositories/DriverAvailabilityRepository::delete → KILLED

142

1.1
Location : deleteDriverAvailability
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:driver_can_delete_an_availability()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::deleteDriverAvailability → KILLED

156

1.1
Location : lambda$adminGetById$4
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[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/DriverAvailabilityController::lambda$adminGetById$4 → KILLED

157

1.1
Location : adminGetById
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:test_that_logged_in_admin_can_get_by_id_when_the_id_exists_and_user_id_matches()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::adminGetById → KILLED

166

1.1
Location : allDriverAvailabilities
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriverAvailabilityControllerTests]/[method:test_that_logged_in_admin_can_get_all_applications()]
replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allDriverAvailabilities → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3