UCSBDiningCommonsMenuItemController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.UCSBDiningCommonsMenuItem;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.UCSBDiningCommonsMenuItemRepository;
6
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.Parameter;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import lombok.extern.slf4j.Slf4j;
11
import java.util.Map;
12
13
import com.fasterxml.jackson.core.JsonProcessingException;
14
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.format.annotation.DateTimeFormat;
17
import org.springframework.http.ResponseEntity;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.web.bind.annotation.DeleteMapping;
20
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestBody;
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.http.HttpStatus;
29
30
import jakarta.validation.Valid;
31
32
import java.time.LocalDateTime;
33
34
35
@Tag(name = "UCSBDiningCommonsMenuItem")
36
@RequestMapping("/api/ucsbdiningcommonsmenuitem")
37
@RestController
38
@Slf4j
39
public class UCSBDiningCommonsMenuItemController extends ApiController {
40
41
    @Autowired
42
    UCSBDiningCommonsMenuItemRepository ucsbDiningCommonsMenuItemRepository;
43
44
 
45
    @Operation(summary= "List all ucsb dining commons menu items")
46
    @PreAuthorize("hasRole('ROLE_USER')")
47
    @GetMapping("/all")
48
    public Iterable<UCSBDiningCommonsMenuItem> allItem() {
49
        Iterable<UCSBDiningCommonsMenuItem> menuitem = ucsbDiningCommonsMenuItemRepository.findAll();
50 1 1. allItem : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::allItem → KILLED
        return menuitem;
51
    }
52
53
54
    /**
55
     * Create a new UCSBDiningCommonsMenuItem
56
     * 
57
     * @param diningCommonsCode  the code for the dining commons
58
     * @param name               the name of the menu item
59
     * @param station            the station where the item is served
60
     * @return the saved item
61
     */
62
    @Operation(summary = "Create a new UCSBDiningCommonsMenuItem")
63
    @PreAuthorize("hasRole('ROLE_ADMIN')")
64
    @PostMapping("/post")
65
    public UCSBDiningCommonsMenuItem postItem(
66
            @Parameter(name = "diningCommonsCode") @RequestParam String diningCommonsCode,
67
            @Parameter(name = "name") @RequestParam String name,
68
            @Parameter(name = "station") @RequestParam String station) {
69
70
        UCSBDiningCommonsMenuItem item = UCSBDiningCommonsMenuItem.builder()
71
                .diningCommonsCode(diningCommonsCode)
72
                .name(name)
73
                .station(station)
74
                .build();
75
76 1 1. postItem : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::postItem → KILLED
        return ucsbDiningCommonsMenuItemRepository.save(item);
77
    }
78
79
    @Operation(summary= "Get a single menu item by id")
80
    @PreAuthorize("hasRole('ROLE_USER')")
81
    @GetMapping("")
82
    public UCSBDiningCommonsMenuItem getById(@RequestParam Long id) {
83 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::getById → KILLED
        return ucsbDiningCommonsMenuItemRepository.findById(id)
84 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::lambda$getById$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));
85
}
86
    @Operation(summary= "Update a single UCSBDiningCommonsMenuItem")
87
    @PreAuthorize("hasRole('ROLE_ADMIN')")
88
    @PutMapping("")
89
        public UCSBDiningCommonsMenuItem updateUCSBDiningCommonsMenuItem(
90
        @Parameter(name="id") @RequestParam Long id,
91
        @RequestBody @Valid UCSBDiningCommonsMenuItem incoming
92
        ) {
93
            UCSBDiningCommonsMenuItem item = ucsbDiningCommonsMenuItemRepository.findById(id)
94 1 1. lambda$updateUCSBDiningCommonsMenuItem$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::lambda$updateUCSBDiningCommonsMenuItem$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));
95
96 1 1. updateUCSBDiningCommonsMenuItem : removed call to edu/ucsb/cs156/example/entities/UCSBDiningCommonsMenuItem::setDiningCommonsCode → KILLED
            item.setDiningCommonsCode(incoming.getDiningCommonsCode());
97 1 1. updateUCSBDiningCommonsMenuItem : removed call to edu/ucsb/cs156/example/entities/UCSBDiningCommonsMenuItem::setName → KILLED
            item.setName(incoming.getName());
98 1 1. updateUCSBDiningCommonsMenuItem : removed call to edu/ucsb/cs156/example/entities/UCSBDiningCommonsMenuItem::setStation → KILLED
            item.setStation(incoming.getStation());
99
100
            try {
101
                ucsbDiningCommonsMenuItemRepository.save(item);
102
            } catch (Exception e) {
103
                log.error("Error saving UCSBDiningCommonsMenuItem with id {}", id, e);
104
                throw new RuntimeException("Failed to update UCSBDiningCommonsMenuItem", e);
105
            }
106 1 1. updateUCSBDiningCommonsMenuItem : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::updateUCSBDiningCommonsMenuItem → KILLED
            return item;
107
            
108
        }
109
110
    @Operation(summary = "Delete a UCSBDiningCommonsMenuItem")
111
    @PreAuthorize("hasRole('ROLE_ADMIN')")
112
    @DeleteMapping("")
113
    public Object deleteUCSBDiningCommonsMenuItem(
114
            @Parameter(name = "id") @RequestParam Long id) {
115
            UCSBDiningCommonsMenuItem item = ucsbDiningCommonsMenuItemRepository.findById(id)
116 1 1. lambda$deleteUCSBDiningCommonsMenuItem$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::lambda$deleteUCSBDiningCommonsMenuItem$2 → KILLED
                    .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));
117
        
118 1 1. deleteUCSBDiningCommonsMenuItem : removed call to edu/ucsb/cs156/example/repositories/UCSBDiningCommonsMenuItemRepository::delete → KILLED
            ucsbDiningCommonsMenuItemRepository.delete(item);
119 1 1. deleteUCSBDiningCommonsMenuItem : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::deleteUCSBDiningCommonsMenuItem → KILLED
            return genericMessage("UCSBDiningCommonsMenuItem with id %s deleted".formatted(id));
120
        }
121
    @ExceptionHandler(RuntimeException.class)
122
    public ResponseEntity<Object> handleRuntimeException(RuntimeException ex) {
123 1 1. handleRuntimeException : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::handleRuntimeException → KILLED
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
124
                             .body(Map.of(
125
                                 "type", "RuntimeException",
126
                                 "message", ex.getMessage()
127
                             ));
128
    }
129
        
130
        
131
132
133
134
   
135
}
136
137

Mutations

50

1.1
Location : allItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:test_logged_in_user_can_get_all_items()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::allItem → KILLED

76

1.1
Location : postItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:test_admin_can_post_item()]
replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::postItem → KILLED

83

1.1
Location : getById
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:test_getById_returns_item_when_exists()]
replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::getById → KILLED

84

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

94

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

96

1.1
Location : updateUCSBDiningCommonsMenuItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:admin_can_edit_an_existing_item()]
removed call to edu/ucsb/cs156/example/entities/UCSBDiningCommonsMenuItem::setDiningCommonsCode → KILLED

97

1.1
Location : updateUCSBDiningCommonsMenuItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:admin_can_edit_an_existing_item()]
removed call to edu/ucsb/cs156/example/entities/UCSBDiningCommonsMenuItem::setName → KILLED

98

1.1
Location : updateUCSBDiningCommonsMenuItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:admin_can_edit_an_existing_item()]
removed call to edu/ucsb/cs156/example/entities/UCSBDiningCommonsMenuItem::setStation → KILLED

106

1.1
Location : updateUCSBDiningCommonsMenuItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:admin_can_edit_an_existing_item()]
replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::updateUCSBDiningCommonsMenuItem → KILLED

116

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

118

1.1
Location : deleteUCSBDiningCommonsMenuItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:admin_can_delete_item()]
removed call to edu/ucsb/cs156/example/repositories/UCSBDiningCommonsMenuItemRepository::delete → KILLED

119

1.1
Location : deleteUCSBDiningCommonsMenuItem
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:admin_can_delete_item()]
replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::deleteUCSBDiningCommonsMenuItem → KILLED

123

1.1
Location : handleRuntimeException
Killed by : edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.UCSBDiningCommonsMenuItemControllerTests]/[method:test_update_throws_exception_when_save_fails()]
replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDiningCommonsMenuItemController::handleRuntimeException → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0