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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
* Copyright © 2016-2018 European Support Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openecomp.sdc.logging.servlet.jaxrs;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
import static org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter.START_TIME_KEY;
import java.lang.reflect.Method;
import java.util.UUID;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceInfo;
import org.easymock.EasyMock;
import org.openecomp.sdc.logging.LoggingConstants;
import org.openecomp.sdc.logging.api.ContextData;
import org.openecomp.sdc.logging.api.LoggingContext;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
/**
* Unit testing JAX-RS request filter.
*
* @author evitaliy
* @since 19 Mar 2018
*/
@PrepareForTest({LoggingContext.class, ContextData.class})
public class LoggingRequestFilterTest extends PowerMockTestCase {
private static final Class RESOURCE_CLASS = MockResource.class;
private static final Method RESOURCE_METHOD = MockResource.class.getDeclaredMethods()[0];
private static final String RESOURCE_NAME = RESOURCE_CLASS.getName() + "." + RESOURCE_METHOD.getName();
private static final String RANDOM_REQUEST_ID = UUID.randomUUID().toString();
private static final String RANDOM_PARTNER_NAME = UUID.randomUUID().toString();
/**
* Verify all mocks after each test.
*/
@AfterMethod
public void verifyMocks(ITestResult result) {
try {
PowerMock.verifyAll();
} catch (AssertionError e) {
throw new AssertionError("Expectations failed in: " + result.getMethod().getMethodName(), e);
}
}
@Test
public void notHandledWhenNoMatchingResource() {
PowerMock.mockStatic(LoggingContext.class);
PowerMock.replay(LoggingContext.class);
new LoggingRequestFilter().filter(mockEmptyContainerRequestContext());
}
@Test
public void serviceNamePopulatedWhenThereIsMatchingResource() {
mockContextDataBuilder(null, RESOURCE_NAME, null);
mockLoggingContext();
LoggingRequestFilter filter = new LoggingRequestFilter();
filter.setResource(mockResource());
filter.filter(mockContainerRequestContext(
new RequestIdHeader(null),
new PartnerHeader(null)));
}
@Test
public void partnerNamePopulatedWhenPresentInDefaultHeader() {
mockContextDataBuilder(null, RESOURCE_NAME, RANDOM_PARTNER_NAME);
mockLoggingContext();
LoggingRequestFilter filter = new LoggingRequestFilter();
filter.setResource(mockResource());
filter.filter(mockContainerRequestContext(
new RequestIdHeader(null),
new PartnerHeader(RANDOM_PARTNER_NAME)));
}
@Test
public void partnerNamePopulatedWhenPresentInCustomHeader() {
final String partnerHeader = "x-partner-header";
mockContextDataBuilder(null, RESOURCE_NAME, RANDOM_PARTNER_NAME);
mockLoggingContext();
LoggingRequestFilter filter = new LoggingRequestFilter();
filter.setResource(mockResource());
filter.setPartnerNameHeaders(partnerHeader);
filter.filter(mockContainerRequestContext(
new RequestIdHeader(null),
new PartnerHeader(partnerHeader, RANDOM_PARTNER_NAME)));
}
@Test
public void requestIdPopulatedWhenPresentInDefaultHeader() {
mockContextDataBuilder(RANDOM_REQUEST_ID, RESOURCE_NAME, null);
mockLoggingContext();
LoggingRequestFilter filter = new LoggingRequestFilter();
filter.setResource(mockResource());
filter.filter(mockContainerRequestContext(
new RequestIdHeader(RANDOM_REQUEST_ID),
new PartnerHeader(null)));
}
@Test
public void requestIdPopulatedWhenPresentInCustomHeader() {
final String requestIdHeader = "x-request-id";
mockContextDataBuilder(RANDOM_REQUEST_ID, RESOURCE_NAME, null);
mockLoggingContext();
LoggingRequestFilter filter = new LoggingRequestFilter();
filter.setResource(mockResource());
filter.setRequestIdHeaders(requestIdHeader);
filter.filter(mockContainerRequestContext(
new RequestIdHeader(requestIdHeader, RANDOM_REQUEST_ID),
new PartnerHeader(null)));
}
private ResourceInfo mockResource() {
ResourceInfo resource = EasyMock.mock(ResourceInfo.class);
//noinspection unchecked
EasyMock.expect(resource.getResourceClass()).andReturn(RESOURCE_CLASS);
EasyMock.expect(resource.getResourceMethod()).andReturn(RESOURCE_METHOD);
EasyMock.replay(resource);
return resource;
}
private ContainerRequestContext mockEmptyContainerRequestContext() {
ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class);
EasyMock.replay(requestContext);
return requestContext;
}
private ContainerRequestContext mockContainerRequestContext(Header... headers) {
ContainerRequestContext requestContext = EasyMock.mock(ContainerRequestContext.class);
for (Header h : headers) {
EasyMock.expect(requestContext.getHeaderString(h.key)).andReturn(h.value);
}
requestContext.setProperty(EasyMock.eq(START_TIME_KEY), EasyMock.anyLong());
EasyMock.expectLastCall();
EasyMock.replay(requestContext);
return requestContext;
}
private void mockContextDataBuilder(String requestId, String serviceName, String partnerName) {
ContextData.ContextDataBuilder mockBuilder = EasyMock.mock(ContextData.ContextDataBuilder.class);
if (requestId != null) {
EasyMock.expect(mockBuilder.requestId(requestId)).andReturn(mockBuilder);
} else {
EasyMock.expect(mockBuilder.requestId(anyString())).andReturn(mockBuilder);
}
if (serviceName != null) {
EasyMock.expect(mockBuilder.serviceName(serviceName)).andReturn(mockBuilder);
}
if (partnerName != null) {
EasyMock.expect(mockBuilder.partnerName(partnerName)).andReturn(mockBuilder);
}
EasyMock.expect(mockBuilder.build()).andReturn(EasyMock.mock(ContextData.class));
EasyMock.replay(mockBuilder);
PowerMock.mockStatic(ContextData.class);
ContextData.builder();
PowerMock.expectLastCall().andReturn(mockBuilder);
PowerMock.replay(ContextData.class);
}
private void mockLoggingContext() {
PowerMock.mockStatic(LoggingContext.class);
LoggingContext.clear();
EasyMock.expectLastCall().once();
LoggingContext.put(anyObject(ContextData.class));
EasyMock.expectLastCall().once();
PowerMock.replay(LoggingContext.class);
}
private abstract static class Header {
private final String key;
private final String value;
private Header(String key, String value) {
this.key = key;
this.value = value;
}
}
private static class PartnerHeader extends Header {
private PartnerHeader(String value) {
super(LoggingConstants.DEFAULT_PARTNER_NAME_HEADER, value);
}
private PartnerHeader(String key, String value) {
super(key, value);
}
}
private static class RequestIdHeader extends Header {
private RequestIdHeader(String value) {
super(LoggingConstants.DEFAULT_REQUEST_ID_HEADER, value);
}
private RequestIdHeader(String key, String value) {
super(key, value);
}
}
private static class MockResource {
@SuppressWarnings("EmptyMethod")
void process() {
// no-op
}
}
}
|