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.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.organic.models.CurrentUser;
7
import edu.ucsb.cs156.organic.services.CurrentUserService;
8
import lombok.extern.slf4j.Slf4j;
9
10
import org.springframework.http.HttpStatus;
11
import org.springframework.web.bind.annotation.ExceptionHandler;
12
import org.springframework.web.bind.annotation.ResponseStatus;
13
import org.springframework.security.access.AccessDeniedException;
14
15
import com.fasterxml.jackson.databind.ObjectMapper;
16
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
17
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
18
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
19
import java.util.Map;
20
21
@Slf4j
22
public abstract class ApiController {
23
  @Autowired
24
  private CurrentUserService currentUserService;
25
26
  protected CurrentUser getCurrentUser() {
27 1 1. getCurrentUser : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getCurrentUser → KILLED
    return currentUserService.getCurrentUser();
28
  }
29
  
30
  @ExceptionHandler({ IllegalArgumentException.class})
31
  @ResponseStatus(HttpStatus.BAD_REQUEST)
32
  public Object handleIllegalArgumentException(Throwable e) {
33
    Map<String,String> map =  Map.of(
34
      "type", e.getClass().getSimpleName(),
35
      "message", e.getMessage()
36
    );
37
    log.error("Exception thrown: {}", map);
38 1 1. handleIllegalArgumentException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleIllegalArgumentException → KILLED
    return map;
39
  }
40
41
  @ExceptionHandler({ EntityNotFoundException.class })
42
  @ResponseStatus(HttpStatus.NOT_FOUND)
43
  public Object handleGenericException(Throwable e) {
44 1 1. handleGenericException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleGenericException → KILLED
    return Map.of(
45
      "type", e.getClass().getSimpleName(),
46
      "message", e.getMessage()
47
    );
48
  }
49
50
  private ObjectMapper mapper;
51
52
  /**
53
   * Special ObjectMapper that ignores Mockito mocks
54
   * @return ObjectMapper mapper
55
   */
56
  public ObjectMapper getMapper() {
57 1 1. getMapper : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getMapper → KILLED
    return mapper;
58
  }
59
60
61
  protected Object genericMessage(String message) {
62 1 1. genericMessage : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::genericMessage → KILLED
    return Map.of("message", message);
63
  }
64
65
66
  public ApiController() {
67
   mapper = mapperThatIgnoresMockitoMocks();
68
  }
69
70
  public static ObjectMapper mapperThatIgnoresMockitoMocks() {
71
    ObjectMapper mapper = new ObjectMapper();
72
    mapper.registerModule(new JavaTimeModule());
73
    mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
74
      @Override
75
      public boolean hasIgnoreMarker(final AnnotatedMember m) {
76 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");
77
      }
78
    });
79 1 1. mapperThatIgnoresMockitoMocks : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::mapperThatIgnoresMockitoMocks → KILLED
    return mapper;
80
  }
81
82
83
  @ExceptionHandler({ AccessDeniedException.class })
84
  @ResponseStatus(HttpStatus.FORBIDDEN)
85
  public Object handleAccessDeniedException(Throwable err) {
86 1 1. handleAccessDeniedException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleAccessDeniedException → KILLED
    return Map.of(
87
      "type", err.getClass().getSimpleName(),
88
      "message", err.getMessage()
89
    );
90
  }
91
92
}

Mutations

27

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

38

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

44

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_toggle_instructor_for_non_existant_user_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleGenericException → KILLED

57

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

62

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

76

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

79

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

86

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

Active mutators

Tests examined


Report generated by PIT 1.7.3