UCSBSubjectsController.java

1
package edu.ucsb.cs156.courses.controllers;
2
3
import edu.ucsb.cs156.courses.entities.UCSBSubject;
4
import edu.ucsb.cs156.courses.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.courses.repositories.UCSBSubjectRepository;
6
import edu.ucsb.cs156.courses.services.UCSBSubjectsService;
7
import io.swagger.annotations.Api;
8
import io.swagger.annotations.ApiOperation;
9
import io.swagger.annotations.ApiParam;
10
import lombok.extern.slf4j.Slf4j;
11
12
import com.fasterxml.jackson.core.JsonProcessingException;
13
import com.fasterxml.jackson.databind.ObjectMapper;
14
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
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RestController;
25
26
import java.util.ArrayList;
27
import java.util.List;
28
29
@Slf4j
30
@Api(description = "API to handle CRUD operations for UCSB Subjects database")
31
@RequestMapping("/api/UCSBSubjects")
32
@RestController
33
public class UCSBSubjectsController extends ApiController {
34
    @Autowired
35
    UCSBSubjectRepository subjectRepository;
36
37
    @Autowired
38
    ObjectMapper mapper;
39
40
    @Autowired
41
    UCSBSubjectsService ucsbSubjectsService;
42
43
    @ApiOperation(value = "Get all UCSB Subjects")
44
    @GetMapping("/all")
45
    public Iterable<UCSBSubject> allSubjects() {
46
        Iterable<UCSBSubject> subjects = subjectRepository.findAll();
47 1 1. allSubjects : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::allSubjects → KILLED
        return subjects;
48
    }
49
50
    @ApiOperation(value = "Load subjects into database from UCSB API")
51
    @PreAuthorize("hasRole('ROLE_ADMIN')")
52
    @PostMapping("/load")
53
    public List<UCSBSubject> loadSubjects() throws JsonProcessingException{
54
       
55
        List<UCSBSubject> subjects = ucsbSubjectsService.get();
56
        
57
        List<UCSBSubject> savedSubjects = new ArrayList<UCSBSubject>();
58
59 1 1. loadSubjects : removed call to java/util/List::forEach → KILLED
        subjects.forEach((ucsbSubject)->{
60
            try {
61
              subjectRepository.save(ucsbSubject);
62
              savedSubjects.add(ucsbSubject);
63
            } catch (DuplicateKeyException dke) {
64
                log.info("Skipping duplicate entity %s".formatted(ucsbSubject.getSubjectCode()));
65
            }
66
        });
67
        log.info("subjects={}",subjects);
68 1 1. loadSubjects : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::loadSubjects → KILLED
        return savedSubjects;
69
    }
70
71
    @ApiOperation(value = "Get a single UCSB Subject by id if it is in the database")
72
    @PreAuthorize("hasRole('ROLE_USER') || hasRole('ROLE_ADMIN')")
73
    @GetMapping("")
74
    public UCSBSubject getSubjectById(
75
            @ApiParam("subjectCode") @RequestParam String subjectCode) throws JsonProcessingException {
76
77
        UCSBSubject subject = subjectRepository.findById(subjectCode)
78 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));
79
80 1 1. getSubjectById : replaced return value with null for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::getSubjectById → KILLED
        return subject;
81
    }
82
83
    @ApiOperation(value = "Delete a UCSB Subject by id")
84
    @PreAuthorize("hasRole('ROLE_ADMIN')")
85
    @DeleteMapping("")
86
    public Object deleteSubject(
87
            @ApiParam("subjectCode") @RequestParam String subjectCode) {
88
89
        UCSBSubject subject = subjectRepository.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
    @ApiOperation(value = "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
}

Mutations

47

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

59

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_with_duplicates()]
removed call to java/util/List::forEach → KILLED

68

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_with_duplicates()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UCSBSubjectsController::loadSubjects → KILLED

78

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

80

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