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

Mutations

40

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

52

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

62

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

75

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

77

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

88

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

90

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

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()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::deleteSubject → KILLED

100

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

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()]
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