1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.rest; |
35 |
|
|
36 |
|
import static javax.ws.rs.core.Response.Status.*; |
37 |
|
import static org.junit.Assert.*; |
38 |
|
import static org.mockito.Mockito.when; |
39 |
|
|
40 |
|
import javax.jcr.RepositoryException; |
41 |
|
import javax.ws.rs.NotAllowedException; |
42 |
|
import javax.ws.rs.NotFoundException; |
43 |
|
import javax.ws.rs.container.ResourceInfo; |
44 |
|
import javax.ws.rs.core.Response; |
45 |
|
|
46 |
|
import org.junit.Before; |
47 |
|
import org.junit.Test; |
48 |
|
import org.mockito.InjectMocks; |
49 |
|
import org.mockito.Mock; |
50 |
|
import org.mockito.MockitoAnnotations; |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (39) |
Complexity: 8 |
Complexity Density: 0.26 |
|
52 |
|
public class RestExceptionMapperTest { |
53 |
|
|
54 |
|
@InjectMocks |
55 |
|
private final RestExceptionMapper restExceptionMapper = new RestExceptionMapper(); |
56 |
|
|
57 |
|
@Mock |
58 |
|
private ResourceInfo resourceInfo; |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
60 |
6 |
@Before... |
61 |
|
public void setUp() { |
62 |
|
|
63 |
6 |
MockitoAnnotations.initMocks(this); |
64 |
|
} |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
66 |
1 |
@Test... |
67 |
|
public void testThatRestExceptionMapperDoesNotProvideAStackTrace() { |
68 |
|
|
69 |
1 |
String errorMessage = "This is a test-exception."; |
70 |
1 |
Exception exception = new Exception(errorMessage); |
71 |
|
|
72 |
|
|
73 |
1 |
Response response = restExceptionMapper.toResponse(exception); |
74 |
|
|
75 |
|
|
76 |
1 |
assertErrorResponse(response, INTERNAL_SERVER_ERROR, errorMessage, "unknown"); |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
79 |
5 |
private void assertErrorResponse(Response response, Response.Status expectedStatusCode, String expectedErrorMessage, String expectedErrorCode) {... |
80 |
5 |
assertEquals(expectedStatusCode, response.getStatusInfo()); |
81 |
5 |
assertNotNull(response.getEntity()); |
82 |
5 |
assertTrue(response.getEntity() instanceof RestError); |
83 |
5 |
RestError restError = (RestError) response.getEntity(); |
84 |
5 |
assertNotNull(restError.getErrorContainer().getCode()); |
85 |
5 |
assertEquals(expectedErrorCode, restError.getErrorContainer().getCode()); |
86 |
5 |
assertEquals(expectedErrorMessage, restError.getErrorContainer().getMessage()); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
89 |
1 |
@Test... |
90 |
|
public void testMapsRepositoryExceptionToInternalServerError() { |
91 |
|
|
92 |
1 |
Response response = restExceptionMapper.toResponse(new RepositoryException("")); |
93 |
|
|
94 |
|
|
95 |
1 |
assertEquals(INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); |
96 |
|
} |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
98 |
1 |
@Test... |
99 |
|
public void notFoundException() { |
100 |
|
|
101 |
1 |
String errorMessage = "Not found"; |
102 |
1 |
NotFoundException exception = new NotFoundException(errorMessage); |
103 |
|
|
104 |
|
|
105 |
1 |
Response response = restExceptionMapper.toResponse(exception); |
106 |
|
|
107 |
|
|
108 |
1 |
assertErrorResponse(response, NOT_FOUND, errorMessage, "notFound"); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void methodNotAllowedException() { |
113 |
|
|
114 |
1 |
String errorMessage = "Method not allowed"; |
115 |
1 |
NotAllowedException exception = new NotAllowedException(errorMessage, "GET", new String[] { "POST" }); |
116 |
|
|
117 |
|
|
118 |
1 |
Response response = restExceptionMapper.toResponse(exception); |
119 |
|
|
120 |
|
|
121 |
1 |
assertErrorResponse(response, METHOD_NOT_ALLOWED, errorMessage, "methodNotAllowed"); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void notFoundWhenNoResourceMethodCalled() { |
126 |
|
|
127 |
1 |
when(resourceInfo.getResourceMethod()).thenReturn(null); |
128 |
1 |
String errorMessage = "Not found"; |
129 |
1 |
NotFoundException exception = new NotFoundException(errorMessage); |
130 |
|
|
131 |
|
|
132 |
1 |
Response response = restExceptionMapper.toResponse(exception); |
133 |
|
|
134 |
|
|
135 |
1 |
assertErrorResponse(response, NOT_FOUND, errorMessage, "notFound"); |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
138 |
1 |
@Test... |
139 |
|
public void trimJaxRsInformation() { |
140 |
|
|
141 |
1 |
String errorMessage = "RESTEASY003210: Could not find resource for full path: http://localhost:8080/magnoliaAuthor/.rest/delivery/tours"; |
142 |
1 |
NotFoundException exception = new NotFoundException(errorMessage); |
143 |
|
|
144 |
|
|
145 |
1 |
Response response = restExceptionMapper.toResponse(exception); |
146 |
|
|
147 |
|
|
148 |
1 |
assertErrorResponse(response, NOT_FOUND, "Could not find resource for full path: http://localhost:8080/magnoliaAuthor/.rest/delivery/tours", "notFound"); |
149 |
|
} |
150 |
|
} |