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 |
|
38 |
1.1 |
|
44 |
1.1 |
|
57 |
1.1 |
|
62 |
1.1 |
|
76 |
1.1 2.2 3.3 |
|
79 |
1.1 |
|
86 |
1.1 |