DiningCommonsController.java

1
package edu.ucsb.cs156.dining.controllers;
2
3
import edu.ucsb.cs156.dining.services.DiningCommonsService;
4
5
import java.time.LocalDateTime;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Optional;
9
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.json.JSONArray;
12
import org.json.JSONObject;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PathVariable;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RestController;
18
import io.swagger.v3.oas.annotations.Parameter;
19
import edu.ucsb.cs156.dining.entities.MenuItem;
20
import edu.ucsb.cs156.dining.repositories.MenuItemRepository;
21
22
import org.springframework.web.bind.annotation.PathVariable;
23
import io.swagger.v3.oas.annotations.Parameter;
24
25
@RestController
26
@RequestMapping("/api/diningcommons")
27
public class DiningCommonsController {
28
29
  @Autowired DiningCommonsService diningCommonsService;
30
  @Autowired MenuItemRepository menuItemRepository;
31
32
  @GetMapping(value = "/all", produces = "application/json")
33
  public ResponseEntity<String> getAllDiningCommons() throws Exception {
34
35
    String body = diningCommonsService.getDiningCommonsJSON();
36
37 1 1. getAllDiningCommons : replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::getAllDiningCommons → KILLED
    return ResponseEntity.ok().body(body);
38
  }
39
40
  @GetMapping(value = "/{date-time}/{dining-commons-code}", produces = "application/json")
41
  public ResponseEntity<String> getMealsByDateTimeAndDiningCommonsCode(
42
    @Parameter(name = "date-time", required = true) @PathVariable("date-time") LocalDateTime date_time,
43
    @Parameter(name = "dining-commons-code", required = true) @PathVariable("dining-commons-code") String dining_commons_code) throws Exception {
44
        
45
        String body = diningCommonsService.getMealsByDateJSON(date_time, dining_commons_code);
46
47 1 1. getMealsByDateTimeAndDiningCommonsCode : replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::getMealsByDateTimeAndDiningCommonsCode → KILLED
        return ResponseEntity.ok().body(body);
48
  }
49
50
51
  public static class MenuItemDTO {
52
    private Long id;
53
    private MenuItem menuItem;
54
55
    // Constructor
56
    public MenuItemDTO(Long id, MenuItem menuItem) {
57
        this.id = id;
58
        this.menuItem = menuItem;
59
    }
60
61
    // Getters and Setters
62
    public Long getId() {
63 1 1. getId : replaced Long return value with 0L for edu/ucsb/cs156/dining/controllers/DiningCommonsController$MenuItemDTO::getId → KILLED
        return id;
64
    }
65
66
    public MenuItem getMenuItem() {
67 1 1. getMenuItem : replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController$MenuItemDTO::getMenuItem → KILLED
        return menuItem;
68
    }
69
}
70
71
72
  @GetMapping(value = "/{date-time}/{dining-commons-code}/{meal}", produces = "application/json")
73
  //@PostMapping(value = "/{date-time}/{dining-commons-code}/{meal}", produces = "application/json")
74
  public ResponseEntity<List<MenuItemDTO>> getMenuItemsByMealDateTimeAndDiningCommonsCode(
75
    @Parameter(name = "date-time", required = true) @PathVariable("date-time") LocalDateTime dateTime,
76
    @Parameter(name = "dining-commons-code", required = true) @PathVariable("dining-commons-code") String diningCommonsCode,
77
    @Parameter(name = "meal", required = true) @PathVariable("meal") String meal) throws Exception {
78
79
    String body = diningCommonsService.getMenuItemsByMealAndDateJSON(dateTime, diningCommonsCode, meal);
80
    JSONArray retrievedMenuItems = new JSONArray(body);
81
82
    List<MenuItemDTO> menuItemDTOs = new ArrayList<>();
83
84
    Optional<MenuItem> existingMenuItem;
85
    MenuItem returnedMenuItem = new MenuItem();
86
87 2 1. getMenuItemsByMealDateTimeAndDiningCommonsCode : changed conditional boundary → KILLED
2. getMenuItemsByMealDateTimeAndDiningCommonsCode : negated conditional → KILLED
    for (int i = 0; i < retrievedMenuItems.length(); i++) {
88
        JSONObject retrievedMenuItem = retrievedMenuItems.getJSONObject(i);
89
        String name = retrievedMenuItem.getString("name");
90
        String station = retrievedMenuItem.getString("station");
91
92
        // Check if record already exists
93
        existingMenuItem = menuItemRepository.findFirstByDiningCommonsCodeAndMealAndNameAndStation(
94
            diningCommonsCode, meal, name, station);
95
96
        
97
        MenuItem newMenuItem = new MenuItem();
98 1 1. getMenuItemsByMealDateTimeAndDiningCommonsCode : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setDiningCommonsCode → KILLED
        newMenuItem.setDiningCommonsCode(diningCommonsCode);
99 1 1. getMenuItemsByMealDateTimeAndDiningCommonsCode : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setMeal → KILLED
        newMenuItem.setMeal(meal);
100 1 1. getMenuItemsByMealDateTimeAndDiningCommonsCode : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setName → KILLED
        newMenuItem.setName(name);
101 1 1. getMenuItemsByMealDateTimeAndDiningCommonsCode : removed call to edu/ucsb/cs156/dining/entities/MenuItem::setStation → KILLED
        newMenuItem.setStation(station);
102
103 1 1. lambda$getMenuItemsByMealDateTimeAndDiningCommonsCode$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::lambda$getMenuItemsByMealDateTimeAndDiningCommonsCode$0 → KILLED
        returnedMenuItem = existingMenuItem.orElseGet(() -> menuItemRepository.save(newMenuItem));
104
        // We must use orElseGet (and not orElse) to ensure lazy evaluation of the Optional<MenuItem>, that is,
105
        // we only want to save a new MenuItem if NO existing menu item is returned.
106
107
        // Add to DTO list
108
        menuItemDTOs.add(new MenuItemDTO(returnedMenuItem.getId(), returnedMenuItem));
109
    }
110
111 1 1. getMenuItemsByMealDateTimeAndDiningCommonsCode : replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::getMenuItemsByMealDateTimeAndDiningCommonsCode → KILLED
    return ResponseEntity.ok(menuItemDTOs);
112
  }
113
}

Mutations

37

1.1
Location : getAllDiningCommons
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getAllCommons_userNotLoggedIn()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::getAllDiningCommons → KILLED

47

1.1
Location : getMealsByDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMealsByDate()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::getMealsByDateTimeAndDiningCommonsCode → KILLED

63

1.1
Location : getId
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
replaced Long return value with 0L for edu/ucsb/cs156/dining/controllers/DiningCommonsController$MenuItemDTO::getId → KILLED

67

1.1
Location : getMenuItem
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController$MenuItemDTO::getMenuItem → KILLED

87

1.1
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
changed conditional boundary → KILLED

2.2
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
negated conditional → KILLED

98

1.1
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
removed call to edu/ucsb/cs156/dining/entities/MenuItem::setDiningCommonsCode → KILLED

99

1.1
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
removed call to edu/ucsb/cs156/dining/entities/MenuItem::setMeal → KILLED

100

1.1
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
removed call to edu/ucsb/cs156/dining/entities/MenuItem::setName → KILLED

101

1.1
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
removed call to edu/ucsb/cs156/dining/entities/MenuItem::setStation → KILLED

103

1.1
Location : lambda$getMenuItemsByMealDateTimeAndDiningCommonsCode$0
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::lambda$getMenuItemsByMealDateTimeAndDiningCommonsCode$0 → KILLED

111

1.1
Location : getMenuItemsByMealDateTimeAndDiningCommonsCode
Killed by : edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.DiningCommonsControllerTests]/[method:getMenuItemsByMealDateTimeAndDiningCommonsCode()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/DiningCommonsController::getMenuItemsByMealDateTimeAndDiningCommonsCode → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0