2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.servlet;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.powermock.api.mockito.PowerMockito.mock;
24 import static org.powermock.api.mockito.PowerMockito.mockStatic;
25 import static org.powermock.api.mockito.PowerMockito.verifyStatic;
26 import static org.powermock.api.mockito.PowerMockito.when;
28 import javax.servlet.http.HttpServletRequest;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.ArgumentCaptor;
32 import org.openecomp.sdc.logging.api.ContextData;
33 import org.openecomp.sdc.logging.api.LoggingContext;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
38 * Populating context from request data.
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest(LoggingContext.class)
45 public class ContextTrackerTest {
47 private static final String X_REQUEST_ID = "X-REQUEST-ID";
48 private static final HttpHeader REQUEST_ID_HEADER = new HttpHeader(new String[] {X_REQUEST_ID});
50 private static final String X_PARTNER_NAME = "X-PARTNER-NAME";
51 private static final HttpHeader PARTNER_NAME_HEADER = new HttpHeader(new String[] {X_PARTNER_NAME});
53 @Test(expected = NullPointerException.class)
54 public void throwExceptionWhenPartnerNamesNull() {
55 new ContextTracker(null, REQUEST_ID_HEADER);
58 @Test(expected = NullPointerException.class)
59 public void throwExceptionWhenRequestIdsNull() {
60 new ContextTracker(PARTNER_NAME_HEADER, null);
64 public void requestIdCopiedWhenGiven() {
66 mockStatic(LoggingContext.class);
68 final String requestId = "request-id-for-unit-testing";
69 HttpServletRequest request = mock(HttpServletRequest.class);
70 when(request.getHeader(X_REQUEST_ID)).thenReturn(requestId);
72 ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
73 tracker.preRequest(request);
75 ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
76 verifyStatic(LoggingContext.class);
78 LoggingContext.put(contextDataCaptor.capture());
80 assertEquals(requestId, contextDataCaptor.getValue().getRequestId());
84 public void requestIdGeneratedWhenNotGiven() {
86 mockStatic(LoggingContext.class);
88 ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
89 tracker.preRequest(mock(HttpServletRequest.class));
91 ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
92 verifyStatic(LoggingContext.class);
94 LoggingContext.put(contextDataCaptor.capture());
96 String requestId = contextDataCaptor.getValue().getRequestId();
97 assertNotNull(requestId);
98 assertFalse(requestId.isEmpty());
102 public void partnerNameCopiedWhenGiven() {
104 mockStatic(LoggingContext.class);
106 final String partner = "partner-name-for-unit-testing";
107 HttpServletRequest request = mock(HttpServletRequest.class);
108 when(request.getHeader(X_PARTNER_NAME)).thenReturn(partner);
110 ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
111 tracker.preRequest(request);
113 ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
114 verifyStatic(LoggingContext.class);
116 LoggingContext.put(contextDataCaptor.capture());
118 assertEquals(partner, contextDataCaptor.getValue().getPartnerName());
122 public void partnerNameAbsentWhenNotGiven() {
124 mockStatic(LoggingContext.class);
126 ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
127 tracker.preRequest(mock(HttpServletRequest.class));
129 ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
130 verifyStatic(LoggingContext.class);
132 LoggingContext.put(contextDataCaptor.capture());
134 assertNull(contextDataCaptor.getValue().getPartnerName());
138 public void serviceNameGenerated() {
140 mockStatic(LoggingContext.class);
142 ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
143 tracker.preRequest(mock(HttpServletRequest.class));
145 ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
146 verifyStatic(LoggingContext.class);
148 LoggingContext.put(contextDataCaptor.capture());
150 assertNotNull(contextDataCaptor.getValue().getServiceName());
154 public void contextClearedWhenRequestFinished() {
156 mockStatic(LoggingContext.class);
158 ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
159 tracker.postRequest(mock(RequestProcessingResult.class));
161 verifyStatic(LoggingContext.class);
162 LoggingContext.clear();