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 lombok.extern.slf4j.Slf4j;
10
11
import org.springframework.http.HttpStatus;
12
import org.springframework.security.access.AccessDeniedException;
13
import org.springframework.web.bind.annotation.ExceptionHandler;
14
import org.springframework.web.bind.annotation.ResponseStatus;
15
16
import com.fasterxml.jackson.databind.ObjectMapper;
17
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
18
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
19
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
20
import java.util.Map;
21
22
/**
23
 * Base class for all API controllers.  Provides a getCurrentUser() method
24
 * and a genericMessage() method.
25
 *
26
 */
27
28
@Slf4j
29
public abstract class ApiController {
30
  @Autowired
31
  private CurrentUserService currentUserService;
32
33
  /**
34
   * Returns the current user, or null if no user is logged in.
35
   * @return CurrentUser current user or null
36
   */
37
  protected CurrentUser getCurrentUser() {
38 1 1. getCurrentUser : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getCurrentUser → KILLED
    return currentUserService.getCurrentUser();
39
  }
40
41
  
42
  /**
43
   * Exception handler to return HTTP status code 400 Bad Request
44
   * when an IllegalArgumentException is thrown
45
   * @param e IllegalArgumentException
46
   * @return map with type and message
47
   */
48
  @ExceptionHandler({ IllegalArgumentException.class})
49
  @ResponseStatus(HttpStatus.BAD_REQUEST)
50
  public Object handleIllegalArgumentException(Throwable e) {
51
    Map<String,String> map =  Map.of(
52
      "type", e.getClass().getSimpleName(),
53
      "message", e.getMessage()
54
    );
55
    log.error("Exception thrown: {}", map);
56 1 1. handleIllegalArgumentException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleIllegalArgumentException → KILLED
    return map;
57
  }
58
59
  /**
60
   * Exception handler to return HTTP status code 404 Not Found
61
   * when an EntityNotFoundException is thrown
62
   * @param e EntityNotFoundException
63
   * @return map with type and message
64
   */
65
  @ExceptionHandler({ EntityNotFoundException.class })
66
  @ResponseStatus(HttpStatus.NOT_FOUND)
67
  public Object handleGenericException(Throwable e) {
68 1 1. handleGenericException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleGenericException → KILLED
    return Map.of(
69
      "type", e.getClass().getSimpleName(),
70
      "message", e.getMessage()
71
    );
72
  }
73
74
  /**
75
   * Exception handler to return HTTP status code 403 Forbidden 
76
   * when an AccessDeniedException is thrown
77
   * @param e AccessDeniedException
78
   * @return map with type and message
79
   */
80
  @ExceptionHandler({ AccessDeniedException.class })
81
  @ResponseStatus(HttpStatus.FORBIDDEN)
82
  public Object handleAccessDeniedException(Throwable e) {
83 1 1. handleAccessDeniedException : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::handleAccessDeniedException → KILLED
    return Map.of(
84
      "type", e.getClass().getSimpleName(),
85
      "message", e.getMessage()
86
    );
87
  }
88
89
  /**
90
   * Generic message to return in a controller method
91
   * @param message
92
   * @return map with message
93
   */
94
  protected Object genericMessage(String message) {
95 1 1. genericMessage : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::genericMessage → KILLED
    return Map.of("message", message);
96
  }
97
98
  private ObjectMapper mapper;
99
100
  /**
101
   * Special ObjectMapper that ignores Mockito mocks
102
   * @return ObjectMapper mapper
103
   */
104
  public ObjectMapper getMapper() {
105 1 1. getMapper : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::getMapper → KILLED
    return mapper;
106
  }
107
108
  /**
109
   * Constructor that initializes the ObjectMapper
110
   */
111
112
  public ApiController() {
113
   mapper = mapperThatIgnoresMockitoMocks();
114
  }
115
116
  /**
117
   * Special ObjectMapper that ignores Mockito mocks
118
   * @return ObjectMapper mapper
119
   */
120
121
  public static ObjectMapper mapperThatIgnoresMockitoMocks() {
122
    ObjectMapper mapper = new ObjectMapper();
123
    mapper.registerModule(new JavaTimeModule());
124
    mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
125
      @Override
126
      public boolean hasIgnoreMarker(final AnnotatedMember m) {
127 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");
128
      }
129
    });
130 1 1. mapperThatIgnoresMockitoMocks : replaced return value with null for edu/ucsb/cs156/organic/controllers/ApiController::mapperThatIgnoresMockitoMocks → KILLED
    return mapper;
131
  }
132
}

Mutations

38

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

56

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

68

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

83

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

95

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

105

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

127

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

130

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