CourseOptionsController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import edu.ucsb.cs156.frontiers.entities.Course;
4
import edu.ucsb.cs156.frontiers.entities.CourseOption;
5
import edu.ucsb.cs156.frontiers.enums.CourseOptions;
6
import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException;
7
import edu.ucsb.cs156.frontiers.repositories.CourseOptionRepository;
8
import edu.ucsb.cs156.frontiers.repositories.CourseRepository;
9
import io.swagger.v3.oas.annotations.Operation;
10
import io.swagger.v3.oas.annotations.Parameter;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import java.util.LinkedHashMap;
13
import java.util.Map;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.GetMapping;
17
import org.springframework.web.bind.annotation.PostMapping;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20
import org.springframework.web.bind.annotation.RestController;
21
22
@Tag(name = "CourseOptions")
23
@RequestMapping("/api/course/options")
24
@RestController
25
public class CourseOptionsController extends ApiController {
26
27
  @Autowired private CourseOptionRepository courseOptionRepository;
28
29
  @Autowired private CourseRepository courseRepository;
30
31
  /**
32
   * The options are readable by anyone who can manage the course (its instructor, its staff and
33
   * admins), because what the course pages show depends on them; they can only be changed by the
34
   * instructor or an admin.
35
   */
36
  @Operation(summary = "Get toggleable options for a course")
37
  @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)")
38
  @GetMapping("")
39
  public Map<String, Boolean> getCourseOptions(
40
      @Parameter(name = "courseId") @RequestParam Long courseId,
41
      @Parameter(name = "option") @RequestParam(required = false) String option) {
42 1 1. getCourseOptions : removed call to edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::ensureCourseExists → KILLED
    ensureCourseExists(courseId);
43
44 2 1. getCourseOptions : negated conditional → KILLED
2. getCourseOptions : negated conditional → KILLED
    if (option != null && !option.isBlank()) {
45
      String normalizedOption = normalizeAndValidateOption(option);
46
      boolean enabled =
47
          courseOptionRepository
48
              .findByCourseIdAndOption(courseId, normalizedOption)
49
              .map(CourseOption::getEnabled)
50
              .orElse(false);
51 1 1. getCourseOptions : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::getCourseOptions → KILLED
      return Map.of(normalizedOption, enabled);
52
    }
53
54
    LinkedHashMap<String, Boolean> options = new LinkedHashMap<>();
55
    for (CourseOptions courseOption : CourseOptions.values()) {
56
      options.put(courseOption.name(), false);
57
    }
58
    for (CourseOption courseOption : courseOptionRepository.findByCourseId(courseId)) {
59 1 1. getCourseOptions : negated conditional → KILLED
      if (options.containsKey(courseOption.getOption())) {
60
        options.put(courseOption.getOption(), courseOption.getEnabled());
61
      }
62
    }
63 1 1. getCourseOptions : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::getCourseOptions → KILLED
    return options;
64
  }
65
66
  @Operation(summary = "Set toggleable option for a course")
67
  @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)")
68
  @PostMapping("")
69
  public Map<String, Boolean> setCourseOption(
70
      @Parameter(name = "courseId") @RequestParam Long courseId,
71
      @Parameter(name = "option") @RequestParam String option,
72
      @Parameter(name = "enabled") @RequestParam Boolean enabled) {
73 1 1. setCourseOption : removed call to edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::ensureCourseExists → KILLED
    ensureCourseExists(courseId);
74
75
    String normalizedOption = normalizeAndValidateOption(option);
76
    CourseOption courseOption =
77
        courseOptionRepository
78
            .findByCourseIdAndOption(courseId, normalizedOption)
79
            .orElse(CourseOption.builder().courseId(courseId).option(normalizedOption).build());
80 1 1. setCourseOption : removed call to edu/ucsb/cs156/frontiers/entities/CourseOption::setEnabled → KILLED
    courseOption.setEnabled(enabled);
81
    courseOptionRepository.save(courseOption);
82
83 1 1. setCourseOption : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::setCourseOption → KILLED
    return Map.of(normalizedOption, enabled);
84
  }
85
86
  private void ensureCourseExists(Long courseId) {
87
    courseRepository
88
        .findById(courseId)
89 1 1. lambda$ensureCourseExists$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::lambda$ensureCourseExists$0 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId));
90
  }
91
92
  private String normalizeAndValidateOption(String option) {
93
    String normalizedOption = option.strip().toUpperCase();
94
    try {
95
      CourseOptions.valueOf(normalizedOption);
96 1 1. normalizeAndValidateOption : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::normalizeAndValidateOption → KILLED
      return normalizedOption;
97
    } catch (IllegalArgumentException e) {
98
      throw new IllegalArgumentException("Invalid course option: " + option);
99
    }
100
  }
101
}

Mutations

42

1.1
Location : getCourseOptions
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:getCourseOptions_missingCourse_returnsNotFound()]
removed call to edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::ensureCourseExists → KILLED

44

1.1
Location : getCourseOptions
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:getCourseOptions_invalidOption_returnsBadRequest()]
negated conditional → KILLED

2.2
Location : getCourseOptions
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:adminCanGetCourseOptions()]
negated conditional → KILLED

51

1.1
Location : getCourseOptions
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:getCourseOptions_withOptionParam_returnsSingleOption()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::getCourseOptions → KILLED

59

1.1
Location : getCourseOptions
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:staffCanGetCourseOptions()]
negated conditional → KILLED

63

1.1
Location : getCourseOptions
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:getCourseOptions_withBlankOptionParam_returnsAllOptions()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::getCourseOptions → KILLED

73

1.1
Location : setCourseOption
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:setCourseOption_missingCourse_returnsNotFound()]
removed call to edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::ensureCourseExists → KILLED

80

1.1
Location : setCourseOption
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:setCourseOption_updatesExistingValue()]
removed call to edu/ucsb/cs156/frontiers/entities/CourseOption::setEnabled → KILLED

83

1.1
Location : setCourseOption
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:setCourseOption_createsNewSlackIntegrationValue()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::setCourseOption → KILLED

89

1.1
Location : lambda$ensureCourseExists$0
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:getCourseOptions_missingCourse_returnsNotFound()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::lambda$ensureCourseExists$0 → KILLED

96

1.1
Location : normalizeAndValidateOption
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseOptionsControllerTests]/[method:getCourseOptions_withOptionParam_returnsSingleOption()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/CourseOptionsController::normalizeAndValidateOption → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support