ApiController.java

1
package edu.ucsb.cs156.organic.controllers;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
5
import edu.ucsb.cs156.organic.entities.Staff;
6
import edu.ucsb.cs156.organic.errors.EntityNotFoundException;
7
import edu.ucsb.cs156.organic.models.CurrentUser;
8
import edu.ucsb.cs156.organic.services.CurrentUserService;
9
import liquibase.pro.packaged.a;
10
import lombok.extern.slf4j.Slf4j;
11
12
import org.springframework.http.HttpStatus;
13
import org.springframework.security.access.AccessDeniedException;
14
import org.springframework.web.bind.annotation.ExceptionHandler;
15
import org.springframework.web.bind.annotation.ResponseStatus;
16
17
import com.fasterxml.jackson.databind.ObjectMapper;
18
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
19
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
20
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
21
import java.util.Map;
22
23
@Slf4j
24
public abstract class ApiController {
25
  @Autowired
26
  private CurrentUserService currentUserService;
27
28
  protected CurrentUser getCurrentUser() {
29 1 1. getCurrentUser : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getCurrentUser → KILLED
    return currentUserService.getCurrentUser();
30
  }
31
32
  /**
33
   * This creates a plain old java object that can be returned as a JSON response
34
   * @return a Map object with a single key/value pair: "message" => message
35
   */
36
  protected Object genericMessage(String message) {
37 1 1. genericMessage : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::genericMessage → KILLED
    return Map.of("message", message);
38
  }
39
40
  @ExceptionHandler({ IllegalArgumentException.class })
41
  @ResponseStatus(HttpStatus.BAD_REQUEST)
42
  public Object handleIllegalArgumentException(Throwable e) {
43
    Map<String, String> map = Map.of(
44
        "type", e.getClass().getSimpleName(),
45
        "message", e.getMessage());
46
    log.error("Exception thrown: {}", map);
47 1 1. handleIllegalArgumentException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleIllegalArgumentException → KILLED
    return map;
48
  }
49
50
  @ExceptionHandler({ EntityNotFoundException.class })
51
  @ResponseStatus(HttpStatus.NOT_FOUND)
52
  public Object handleGenericException(Throwable e) {
53 1 1. handleGenericException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleGenericException → KILLED
    return Map.of(
54
        "type", e.getClass().getSimpleName(),
55
        "message", e.getMessage());
56
  }
57
58
  /**
59
   * Exception handler to return HTTP status code 403 Forbidden
60
   * when an AccessDeniedException is thrown
61
   * 
62
   * @param e AccessDeniedException
63
   * @return map with type and message
64
   */
65
  @ExceptionHandler({ AccessDeniedException.class })
66
  @ResponseStatus(HttpStatus.FORBIDDEN)
67
  public Object handleAccessDeniedException(Throwable e) {
68 1 1. handleAccessDeniedException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleAccessDeniedException → KILLED
    return Map.of(
69
        "type", e.getClass().getSimpleName(),
70
        "message", e.getMessage());
71
  }
72
73
  private ObjectMapper mapper;
74
75
  /**
76
   * Special ObjectMapper that ignores Mockito mocks
77
   * 
78
   * @return ObjectMapper mapper
79
   */
80
  public ObjectMapper getMapper() {
81 1 1. getMapper : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getMapper → KILLED
    return mapper;
82
  }
83
84
  public ApiController() {
85
    mapper = mapperThatIgnoresMockitoMocks();
86
  }
87
88
  public static ObjectMapper mapperThatIgnoresMockitoMocks() {
89
    ObjectMapper mapper = new ObjectMapper();
90
    mapper.registerModule(new JavaTimeModule());
91
    mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
92
      @Override
93
      public boolean hasIgnoreMarker(final AnnotatedMember m) {
94 3 1. hasIgnoreMarker : negated conditional → KILLED
2. hasIgnoreMarker : negated conditional → KILLED
3. hasIgnoreMarker : replaced boolean return with true for edu/ucsb/cs156/organic/controllers/ApiController$1::hasIgnoreMarker → KILLED
        return super.hasIgnoreMarker(m) || m.getName().contains("Mockito");
95
      }
96
    });
97 1 1. mapperThatIgnoresMockitoMocks : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::mapperThatIgnoresMockitoMocks → KILLED
    return mapper;
98
  }
99
}

Mutations

29

1.1
Location : getCurrentUser
Killed by : edu.ucsb.cs156.organic.controllers.UserInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UserInfoControllerTests]/[method:currentUser__get_emails()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getCurrentUser → KILLED

37

1.1
Location : genericMessage
Killed by : edu.ucsb.cs156.organic.controllers.SchoolsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SchoolsControllerTests]/[method:an_admin_user_can_delete_a_school()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::genericMessage → KILLED

47

1.1
Location : handleIllegalArgumentException
Killed by : edu.ucsb.cs156.organic.controllers.JobsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.JobsControllerTests]/[method:admin_launch_test_job_with_invalid_parameter_60001()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleIllegalArgumentException → KILLED

53

1.1
Location : handleGenericException
Killed by : edu.ucsb.cs156.organic.controllers.UsersControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UsersControllerTests]/[method:admin_tries_to_toggleAdmin_non_existant_user_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleGenericException → KILLED

68

1.1
Location : handleAccessDeniedException
Killed by : edu.ucsb.cs156.organic.controllers.CoursesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.CoursesControllerTests]/[method:a_user_cannot_get_course_if_not_on_course_staff()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleAccessDeniedException → KILLED

81

1.1
Location : getMapper
Killed by : edu.ucsb.cs156.organic.controllers.UserInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UserInfoControllerTests]/[method:currentUser__logged_in()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getMapper → KILLED

94

1.1
Location : hasIgnoreMarker
Killed by : edu.ucsb.cs156.organic.controllers.UserInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UserInfoControllerTests]/[method:currentUser__logged_in()]
negated conditional → KILLED

2.2
Location : hasIgnoreMarker
Killed by : edu.ucsb.cs156.organic.controllers.UserInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UserInfoControllerTests]/[method:currentUser__logged_in()]
negated conditional → KILLED

3.3
Location : hasIgnoreMarker
Killed by : edu.ucsb.cs156.organic.controllers.UserInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UserInfoControllerTests]/[method:currentUser__logged_in()]
replaced boolean return with true for edu/ucsb/cs156/organic/controllers/ApiController$1::hasIgnoreMarker → KILLED

97

1.1
Location : mapperThatIgnoresMockitoMocks
Killed by : edu.ucsb.cs156.organic.controllers.UserInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.UserInfoControllerTests]/[method:currentUser__logged_in()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::mapperThatIgnoresMockitoMocks → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3