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.List;
13
import lombok.extern.slf4j.Slf4j;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.DeleteMapping;
17
import org.springframework.web.bind.annotation.GetMapping;
18
import org.springframework.web.bind.annotation.PostMapping;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestParam;
21
import org.springframework.web.bind.annotation.RestController;
22
23
@Slf4j
24
@Tag(name = "API to handle CRUD operations for UCSB Subjects database")
25
@RequestMapping("/api/UCSBSubjects")
26
@RestController
27
public class UCSBSubjectsController extends ApiController /* implements ApplicationRunner */ {
28
  @Autowired UCSBSubjectRepository subjectRepository;
29
30
  @Autowired ObjectMapper mapper;
31
32
  @Autowired UCSBSubjectsService ucsbSubjectsService;
33
34
  @Operation(summary = "Get all UCSB Subjects")
35
  @GetMapping("/all")
36
  public Iterable<UCSBSubject> allSubjects() {
37
    Iterable<UCSBSubject> subjects = subjectRepository.findAll();
38 1 1. allSubjects : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::allSubjects → KILLED
    return subjects;
39
  }
40
41
  @Operation(summary = "Load subjects into database from UCSB API")
42
  @PreAuthorize("hasRole('ROLE_ADMIN')")
43
  @PostMapping("/load")
44
  public List<UCSBSubject> loadSubjects() throws JsonProcessingException {
45
    List<UCSBSubject> savedSubjects = ucsbSubjectsService.loadAllSubjects();
46 1 1. loadSubjects : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::loadSubjects → KILLED
    return savedSubjects;
47
  }
48
49
  @Operation(summary = "Get a single UCSB Subject by id if it is in the database")
50
  @PreAuthorize("hasRole('ROLE_USER') || hasRole('ROLE_ADMIN')")
51
  @GetMapping("")
52
  public UCSBSubject getSubjectById(
53
      @Parameter(name = "subjectCode") @RequestParam String subjectCode)
54
      throws JsonProcessingException {
55
56
    UCSBSubject subject =
57
        subjectRepository
58
            .findById(subjectCode)
59 1 1. lambda$getSubjectById$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::lambda$getSubjectById$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UCSBSubject.class, subjectCode));
60
61 1 1. getSubjectById : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::getSubjectById → KILLED
    return subject;
62
  }
63
64
  @Operation(summary = "Delete a UCSB Subject by id")
65
  @PreAuthorize("hasRole('ROLE_ADMIN')")
66
  @DeleteMapping("")
67
  public Object deleteSubject(@Parameter(name = "subjectCode") @RequestParam String subjectCode) {
68
69
    UCSBSubject subject =
70
        subjectRepository
71
            .findById(subjectCode)
72 1 1. lambda$deleteSubject$1 : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::lambda$deleteSubject$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UCSBSubject.class, subjectCode));
73
74 1 1. deleteSubject : removed call to edu/ucsb/cs156/courses/repositories/UCSBSubjectRepository::delete → KILLED
    subjectRepository.delete(subject);
75
76 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));
77
  }
78
79
  @Operation(summary = "Delete all UCSB Subjects in the table")
80
  @PreAuthorize("hasRole('ROLE_ADMIN')")
81
  @DeleteMapping("/all")
82
  public Object deleteAllSubjects() {
83 1 1. deleteAllSubjects : removed call to edu/ucsb/cs156/courses/repositories/UCSBSubjectRepository::deleteAll → KILLED
    subjectRepository.deleteAll();
84 1 1. deleteAllSubjects : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::deleteAllSubjects → KILLED
    return genericMessage("All UCSBSubject records deleted");
85
  }
86
}

Mutations

38

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 Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::allSubjects → KILLED

46

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

59

1.1
Location : lambda$getSubjectById$0
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$0 → KILLED

61

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

72

1.1
Location : lambda$deleteSubject$1
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$1 → KILLED

74

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

76

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

83

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

84

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.17.0