1ba418f317ec4cff4e29f0a0825f8e007074af63
[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 org.easymock.EasyMock;
20 import org.junit.After;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.TestName;
24 import org.junit.runner.RunWith;
25 import org.openecomp.sdc.logging.api.ContextData;
26 import org.openecomp.sdc.logging.api.LoggingContext;
27 import org.powermock.api.easymock.PowerMock;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.modules.junit4.PowerMockRunner;
30
31 import javax.servlet.FilterChain;
32 import javax.servlet.FilterConfig;
33 import javax.servlet.ServletException;
34 import javax.servlet.ServletRequest;
35 import javax.servlet.ServletResponse;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import java.io.IOException;
39 import java.util.UUID;
40
41 import static org.easymock.EasyMock.anyObject;
42 import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_PARTNER_NAME_HEADER;
43 import static org.openecomp.sdc.logging.LoggingConstants.DEFAULT_REQUEST_ID_HEADER;
44 import static org.openecomp.sdc.logging.servlet.LoggingFilter.PARTNER_NAME_HEADERS_PARAM;
45 import static org.openecomp.sdc.logging.servlet.LoggingFilter.REQUEST_ID_HEADERS_PARAM;
46
47 /**
48  * Unit-tests logging filter for initialization and data retrieval.
49  *
50  * @author evitaliy
51  * @since 17 Aug 2016
52  */
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest(LoggingContext.class)
55 public class LoggingFilterTest {
56
57     private static final String RANDOM_REQUEST_URI = UUID.randomUUID().toString();
58     private static final String RANDOM_REQUEST_ID = UUID.randomUUID().toString();
59     private static final String RANDOM_PARTNER_NAME = UUID.randomUUID().toString();
60
61     @Rule
62     public TestName testName = new TestName();
63
64     /**
65      * Verify all mocks after each test.
66      */
67     @After
68     public void verifyMocks() {
69
70         try {
71             PowerMock.verifyAll();
72         } catch (AssertionError e) {
73             throw new AssertionError("Expectations failed in " + testName.getMethodName() + "()", e);
74         }
75     }
76
77
78     @Test
79     public void filterPopulatesValuesWhenNoInitParamsAndNoHeaders() throws IOException, ServletException {
80
81         mockLoggingContext();
82         LoggingFilter loggingFilter = new LoggingFilter();
83         loggingFilter.init(mockFilterConfig(null, null));
84         loggingFilter.doFilter(new MockRequestBuilder().build(), mockResponse(), mockChain());
85     }
86
87     @Test
88     public void filterPopulatesValuesWhenNoInitParamsAndExistingHeaders() throws IOException, ServletException {
89
90         mockLoggingContext();
91
92         LoggingFilter loggingFilter = new LoggingFilter();
93         loggingFilter.init(mockFilterConfig(null, null));
94
95         HttpServletRequest mockRequest = new MockRequestBuilder().partnerName(RANDOM_PARTNER_NAME)
96                                                                  .requestId(RANDOM_REQUEST_ID).build();
97         loggingFilter.doFilter(mockRequest, mockResponse(), mockChain());
98     }
99
100     @Test
101     public void filterPopulatesValuesWhenCustomInitParamsAndNoHeaders() throws IOException, ServletException {
102
103         mockLoggingContext();
104
105         final String requestIdHeader = "x-request";
106         final String partnerNameHeader = "x-partner";
107
108         LoggingFilter loggingFilter = new LoggingFilter();
109         FilterConfig mockConfig = mockFilterConfig(requestIdHeader, partnerNameHeader);
110         loggingFilter.init(mockConfig);
111
112         HttpServletRequest mockRequest = new MockRequestBuilder().requestIdHeader(requestIdHeader)
113                                                                  .partnerNameHeader(partnerNameHeader).build();
114         loggingFilter.doFilter(mockRequest, mockResponse(), mockChain());
115     }
116
117     @Test
118     public void filterPopulatesValuesWhenCustomInitParamsAndExistingHeaders() throws IOException, ServletException {
119
120         mockLoggingContext();
121
122         final String requestIdHeader = "x-request-id";
123         final String partnerNameHeader = "x-partner-name";
124
125         LoggingFilter loggingFilter = new LoggingFilter();
126         FilterConfig mockConfig = mockFilterConfig(requestIdHeader, partnerNameHeader);
127         loggingFilter.init(mockConfig);
128
129         HttpServletRequest mockRequest = new MockRequestBuilder()
130                 .partnerNameHeader(partnerNameHeader).partnerName(RANDOM_PARTNER_NAME)
131                 .requestIdHeader(requestIdHeader).requestId(RANDOM_REQUEST_ID).build();
132         loggingFilter.doFilter(mockRequest, mockResponse(), mockChain());
133     }
134
135     private FilterConfig mockFilterConfig(String requestIdHeader, String partnerNameHeader) {
136         FilterConfig config = EasyMock.mock(FilterConfig.class);
137         EasyMock.expect(config.getInitParameter(REQUEST_ID_HEADERS_PARAM)).andReturn(requestIdHeader);
138         EasyMock.expect(config.getInitParameter(PARTNER_NAME_HEADERS_PARAM)).andReturn(partnerNameHeader);
139         EasyMock.replay(config);
140         return config;
141     }
142
143     private FilterChain mockChain() throws IOException, ServletException {
144         FilterChain chain = EasyMock.mock(FilterChain.class);
145         chain.doFilter(anyObject(ServletRequest.class), anyObject(ServletResponse.class));
146         EasyMock.expectLastCall().once();
147         EasyMock.replay(chain);
148         return chain;
149     }
150
151     private ServletResponse mockResponse() {
152         HttpServletResponse servletResponse = EasyMock.mock(HttpServletResponse.class);
153         EasyMock.replay(servletResponse);
154         return servletResponse;
155     }
156
157     private void mockLoggingContext() {
158
159         PowerMock.mockStatic(LoggingContext.class);
160
161         LoggingContext.clear();
162         EasyMock.expectLastCall().times(2);
163
164         LoggingContext.put(anyObject(ContextData.class));
165         EasyMock.expectLastCall().once();
166
167         PowerMock.replay(LoggingContext.class);
168     }
169
170     private static class MockRequestBuilder {
171
172         private String requestIdHeader = DEFAULT_REQUEST_ID_HEADER;
173         private String partnerNameHeader = DEFAULT_PARTNER_NAME_HEADER;
174         private String requestId = null;
175         private String partnerName = null;
176
177         MockRequestBuilder requestIdHeader(String h) {
178             this.requestIdHeader = h;
179             return this;
180         }
181
182         MockRequestBuilder requestId(String id) {
183             this.requestId = id;
184             return this;
185         }
186
187         MockRequestBuilder partnerNameHeader(String h) {
188             this.partnerNameHeader = h;
189             return this;
190         }
191
192         MockRequestBuilder partnerName(String name) {
193             this.partnerName = name;
194             return this;
195         }
196
197         HttpServletRequest build() {
198             HttpServletRequest mockRequest = EasyMock.mock(HttpServletRequest.class);
199             EasyMock.expect(mockRequest.getRequestURI()).andReturn(RANDOM_REQUEST_URI);
200             EasyMock.expect(mockRequest.getHeader(requestIdHeader)).andReturn(requestId);
201             EasyMock.expect(mockRequest.getHeader(partnerNameHeader)).andReturn(partnerName);
202             EasyMock.replay(mockRequest);
203             return mockRequest;
204         }
205     }
206 }