a247d119c844449b45cad7596409e017479034e9
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.logging.servlet;
18
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;
27
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;
36
37 /**
38  * Populating context from request data.
39  *
40  * @author evitaliy
41  * @since 01 Aug 2018
42  */
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest(LoggingContext.class)
45 public class ContextTrackerTest {
46
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});
49
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});
52
53     @Test(expected = NullPointerException.class)
54     public void throwExceptionWhenPartnerNamesNull() {
55         new ContextTracker(null, REQUEST_ID_HEADER);
56     }
57
58     @Test(expected = NullPointerException.class)
59     public void throwExceptionWhenRequestIdsNull() {
60         new ContextTracker(PARTNER_NAME_HEADER, null);
61     }
62
63     @Test
64     public void requestIdCopiedWhenGiven() {
65
66         mockStatic(LoggingContext.class);
67
68         final String requestId = "request-id-for-unit-testing";
69         HttpServletRequest request = mock(HttpServletRequest.class);
70         when(request.getHeader(X_REQUEST_ID)).thenReturn(requestId);
71
72         ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
73         tracker.preRequest(request);
74
75         ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
76         verifyStatic(LoggingContext.class);
77
78         LoggingContext.put(contextDataCaptor.capture());
79
80         assertEquals(requestId, contextDataCaptor.getValue().getRequestId());
81     }
82
83     @Test
84     public void requestIdGeneratedWhenNotGiven() {
85
86         mockStatic(LoggingContext.class);
87
88         ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
89         tracker.preRequest(mock(HttpServletRequest.class));
90
91         ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
92         verifyStatic(LoggingContext.class);
93
94         LoggingContext.put(contextDataCaptor.capture());
95
96         String requestId = contextDataCaptor.getValue().getRequestId();
97         assertNotNull(requestId);
98         assertFalse(requestId.isEmpty());
99     }
100
101     @Test
102     public void partnerNameCopiedWhenGiven() {
103
104         mockStatic(LoggingContext.class);
105
106         final String partner = "partner-name-for-unit-testing";
107         HttpServletRequest request = mock(HttpServletRequest.class);
108         when(request.getHeader(X_PARTNER_NAME)).thenReturn(partner);
109
110         ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
111         tracker.preRequest(request);
112
113         ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
114         verifyStatic(LoggingContext.class);
115
116         LoggingContext.put(contextDataCaptor.capture());
117
118         assertEquals(partner, contextDataCaptor.getValue().getPartnerName());
119     }
120
121     @Test
122     public void partnerNameAbsentWhenNotGiven() {
123
124         mockStatic(LoggingContext.class);
125
126         ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
127         tracker.preRequest(mock(HttpServletRequest.class));
128
129         ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
130         verifyStatic(LoggingContext.class);
131
132         LoggingContext.put(contextDataCaptor.capture());
133
134         assertNull(contextDataCaptor.getValue().getPartnerName());
135     }
136
137     @Test
138     public void serviceNameGenerated() {
139
140         mockStatic(LoggingContext.class);
141
142         ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
143         tracker.preRequest(mock(HttpServletRequest.class));
144
145         ArgumentCaptor<ContextData> contextDataCaptor = ArgumentCaptor.forClass(ContextData.class);
146         verifyStatic(LoggingContext.class);
147
148         LoggingContext.put(contextDataCaptor.capture());
149
150         assertNotNull(contextDataCaptor.getValue().getServiceName());
151     }
152
153     @Test
154     public void contextClearedWhenRequestFinished() {
155
156         mockStatic(LoggingContext.class);
157
158         ContextTracker tracker = new ContextTracker(PARTNER_NAME_HEADER, REQUEST_ID_HEADER);
159         tracker.postRequest(mock(RequestProcessingResult.class));
160
161         verifyStatic(LoggingContext.class);
162         LoggingContext.clear();
163     }
164 }
165