InstructorsController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import edu.ucsb.cs156.frontiers.entities.Instructor;
5
import edu.ucsb.cs156.frontiers.repositories.InstructorRepository;
6
import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter;
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.tags.Tag;
9
import lombok.extern.slf4j.Slf4j;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.ResponseEntity;
12
import org.springframework.security.access.prepost.PreAuthorize;
13
import org.springframework.web.bind.annotation.DeleteMapping;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RestController;
19
20
/**
21
 * This is a REST controller for getting information about the instructors. These endpoints are only
22
 * accessible to instructors with the role "ROLE_ADMIN".
23
 */
24
@Tag(name = "Instructors")
25
@RequestMapping("/api/admin/instructors")
26
@RestController
27
@Slf4j
28
public class InstructorsController extends ApiController {
29
  @Autowired InstructorRepository instructorRepository;
30
31
  @Autowired ObjectMapper mapper;
32
33
  /**
34
   * Create a new Instructor, available only to Admins.
35
   *
36
   * @param email the email of the instructor
37
   * @return the created Instructor
38
   */
39
  @Operation(summary = "Create a new Instructor")
40
  @PreAuthorize("hasRole('ROLE_ADMIN')")
41
  @PostMapping("/post")
42
  public Instructor postInstructor(@RequestParam String email) {
43
    String convertedEmail = CanonicalFormConverter.convertToValidEmail(email);
44
    Instructor instructor = Instructor.builder().email(convertedEmail).build();
45
    instructorRepository.save(instructor);
46 1 1. postInstructor : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::postInstructor → KILLED
    return instructor;
47
  }
48
49
  /**
50
   * Get a list of all instructors, available only to Admins.
51
   *
52
   * @return a list of all instructors
53
   */
54
  @Operation(summary = "List all Instructors")
55
  @PreAuthorize("hasRole('ROLE_ADMIN')")
56
  @GetMapping("/get")
57
  public Iterable<Instructor> allInstructors() {
58
    Iterable<Instructor> instructors = instructorRepository.findAll();
59 1 1. allInstructors : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/InstructorsController::allInstructors → KILLED
    return instructors;
60
  }
61
62
  /** Delete an instructor by email, available only to Admins. */
63
  @Operation(summary = "Delete an Instructor by email")
64
  @PreAuthorize("hasRole('ROLE_ADMIN')")
65
  @DeleteMapping("/delete")
66
  public ResponseEntity<String> deleteInstructor(@RequestParam String email) {
67
    Instructor instructor = instructorRepository.findById(email).orElse(null);
68
69 1 1. deleteInstructor : negated conditional → KILLED
    if (instructor == null) {
70 1 1. deleteInstructor : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::deleteInstructor → KILLED
      return ResponseEntity.status(404)
71
          .body(String.format("Instructor with email %s not found.", email));
72
    }
73
74 1 1. deleteInstructor : removed call to edu/ucsb/cs156/frontiers/repositories/InstructorRepository::delete → KILLED
    instructorRepository.delete(instructor);
75 1 1. deleteInstructor : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::deleteInstructor → KILLED
    return ResponseEntity.status(200)
76
        .body(String.format("Instructor with email %s deleted.", email));
77
  }
78
}

Mutations

46

1.1
Location : postInstructor
Killed by : edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests]/[method:logged_in_admins_can_post()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::postInstructor → KILLED

59

1.1
Location : allInstructors
Killed by : edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests]/[method:logged_in_admins_can_get()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/InstructorsController::allInstructors → KILLED

69

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

70

1.1
Location : deleteInstructor
Killed by : edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests]/[method:admin_try_to_delete_a_instructor_not_found()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::deleteInstructor → KILLED

74

1.1
Location : deleteInstructor
Killed by : edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests]/[method:logged_in_admins_can_delete()]
removed call to edu/ucsb/cs156/frontiers/repositories/InstructorRepository::delete → KILLED

75

1.1
Location : deleteInstructor
Killed by : edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests]/[method:logged_in_admins_can_delete()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::deleteInstructor → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0