UCSBSubjectsController.java

1
package edu.ucsb.cs156.courses.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import edu.ucsb.cs156.courses.entities.UCSBSubject;
6
import edu.ucsb.cs156.courses.errors.EntityNotFoundException;
7
import edu.ucsb.cs156.courses.repositories.UCSBSubjectRepository;
8
import edu.ucsb.cs156.courses.services.UCSBSubjectsService;
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.ArrayList;
13
import java.util.List;
14
import lombok.extern.slf4j.Slf4j;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.boot.ApplicationArguments;
17
import org.springframework.boot.ApplicationRunner;
18
import org.springframework.dao.DuplicateKeyException;
19
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestParam;
25
import org.springframework.web.bind.annotation.RestController;
26
27
@Slf4j
28
@Tag(name = "API to handle CRUD operations for UCSB Subjects database")
29
@RequestMapping("/api/UCSBSubjects")
30
@RestController
31
public class UCSBSubjectsController extends ApiController implements ApplicationRunner {
32
  @Autowired UCSBSubjectRepository subjectRepository;
33
34
  @Autowired ObjectMapper mapper;
35
36
  @Autowired UCSBSubjectsService ucsbSubjectsService;
37
38
  @Operation(summary = "Get all UCSB Subjects")
39
  @GetMapping("/all")
40
  public Iterable<UCSBSubject> allSubjects() {
41
    Iterable<UCSBSubject> subjects = subjectRepository.findAll();
42 1 1. allSubjects : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::allSubjects → KILLED
    return subjects;
43
  }
44
45
  @Operation(summary = "Load subjects into database from UCSB API")
46
  @PreAuthorize("hasRole('ROLE_ADMIN')")
47
  @PostMapping("/load")
48
  public List<UCSBSubject> loadSubjects() throws JsonProcessingException {
49
50
    List<UCSBSubject> subjects = ucsbSubjectsService.get();
51
52
    List<UCSBSubject> savedSubjects = new ArrayList<UCSBSubject>();
53
54 1 1. loadSubjects : removed call to java/util/List::forEach → KILLED
    subjects.forEach(
55
        (ucsbSubject) -> {
56
          try {
57
            subjectRepository.save(ucsbSubject);
58
            savedSubjects.add(ucsbSubject);
59
          } catch (DuplicateKeyException dke) {
60
            log.info("Skipping duplicate entity %s".formatted(ucsbSubject.getSubjectCode()));
61
          }
62
        });
63
    log.info("subjects={}", subjects);
64 1 1. loadSubjects : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::loadSubjects → KILLED
    return savedSubjects;
65
  }
66
67
  @Operation(summary = "Get a single UCSB Subject by id if it is in the database")
68
  @PreAuthorize("hasRole('ROLE_USER') || hasRole('ROLE_ADMIN')")
69
  @GetMapping("")
70
  public UCSBSubject getSubjectById(
71
      @Parameter(name = "subjectCode") @RequestParam String subjectCode)
72
      throws JsonProcessingException {
73
74
    UCSBSubject subject =
75
        subjectRepository
76
            .findById(subjectCode)
77 1 1. lambda$getSubjectById$1 : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::lambda$getSubjectById$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UCSBSubject.class, subjectCode));
78
79 1 1. getSubjectById : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::getSubjectById → KILLED
    return subject;
80
  }
81
82
  @Operation(summary = "Delete a UCSB Subject by id")
83
  @PreAuthorize("hasRole('ROLE_ADMIN')")
84
  @DeleteMapping("")
85
  public Object deleteSubject(@Parameter(name = "subjectCode") @RequestParam String subjectCode) {
86
87
    UCSBSubject subject =
88
        subjectRepository
89
            .findById(subjectCode)
90 1 1. lambda$deleteSubject$2 : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::lambda$deleteSubject$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UCSBSubject.class, subjectCode));
91
92 1 1. deleteSubject : removed call to edu/ucsb/cs156/courses/repositories/UCSBSubjectRepository::delete → KILLED
    subjectRepository.delete(subject);
93
94 1 1. deleteSubject : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::deleteSubject → KILLED
    return genericMessage("UCSBSubject with id %s deleted".formatted(subjectCode));
95
  }
96
97
  @Operation(summary = "Delete all UCSB Subjects in the table")
98
  @PreAuthorize("hasRole('ROLE_ADMIN')")
99
  @DeleteMapping("/all")
100
  public Object deleteAllSubjects() {
101
102 1 1. deleteAllSubjects : removed call to edu/ucsb/cs156/courses/repositories/UCSBSubjectRepository::deleteAll → KILLED
    subjectRepository.deleteAll();
103
104 1 1. deleteAllSubjects : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::deleteAllSubjects → KILLED
    return genericMessage("All UCSBSubject records deleted");
105
  }
106
107
  @Override
108
  public void run(ApplicationArguments args) throws Exception {
109
    loadSubjects();
110
    log.info("Got all subjects on launch!");
111
  }
112
}

Mutations

42

1.1
Location : allSubjects
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_todos_admin_all__admin_logged_in__returns_all_records()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::allSubjects → KILLED

54

1.1
Location : loadSubjects
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_UCSBSubjects_load_returns_200()]
removed call to java/util/List::forEach → KILLED

64

1.1
Location : loadSubjects
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_UCSBSubjects_load_returns_200()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::loadSubjects → KILLED

77

1.1
Location : lambda$getSubjectById$1
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_UCSBSubjects__user_logged_in__search_for_id_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::lambda$getSubjectById$1 → KILLED

79

1.1
Location : getSubjectById
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_UCSBSubjects__user_logged_in__returns_a_subject_that_exists()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::getSubjectById → KILLED

90

1.1
Location : lambda$deleteSubject$2
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_todos__user_logged_in__delete_by_id_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::lambda$deleteSubject$2 → KILLED

92

1.1
Location : deleteSubject
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_todos__user_logged_in__delete_by_id()]
removed call to edu/ucsb/cs156/courses/repositories/UCSBSubjectRepository::delete → KILLED

94

1.1
Location : deleteSubject
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_todos__user_logged_in__delete_by_id()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::deleteSubject → KILLED

102

1.1
Location : deleteAllSubjects
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_todos__admin_logged_in__delete_all()]
removed call to edu/ucsb/cs156/courses/repositories/UCSBSubjectRepository::deleteAll → KILLED

104

1.1
Location : deleteAllSubjects
Killed by : edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.UCSBSubjectsControllerTests]/[method:api_todos__admin_logged_in__delete_all()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::deleteAllSubjects → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3