2 * Copyright © 2016-2017 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;
20 import org.testng.Assert;
21 import org.testng.annotations.Test;
23 import javax.servlet.FilterChain;
24 import javax.servlet.RequestDispatcher;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletInputStream;
27 import javax.servlet.ServletOutputStream;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.io.UnsupportedEncodingException;
34 import java.net.InetAddress;
35 import java.util.Enumeration;
36 import java.util.Locale;
38 import java.util.UUID;
39 import java.util.concurrent.atomic.AtomicInteger;
41 import static org.testng.Assert.assertEquals;
42 import static org.testng.Assert.assertNull;
45 * TODO: Add more tests
50 public class LoggingFilterTest {
52 private static final String REMOTE_HOST = UUID.randomUUID().toString();
55 public void testDoFilter() throws Exception {
56 LoggingFilter loggingFilter = new LoggingFilter();
57 ServletRequest mockRequest = new TestServletRequest();
58 ServletResponse mockResponse = new TestServletResponse();
59 TestFilterChain mockChain = new TestFilterChain();
60 loggingFilter.doFilter(mockRequest, mockResponse, mockChain);
61 assertEquals(1, mockChain.getCount());
62 assertNull(MDC.getCopyOfContextMap());
65 private static class TestServletRequest implements ServletRequest {
68 public Object getAttribute(String s) {
73 public Enumeration getAttributeNames() {
78 public String getCharacterEncoding() {
83 public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
88 public int getContentLength() {
93 public String getContentType() {
98 public ServletInputStream getInputStream() throws IOException {
103 public String getParameter(String s) {
108 public Enumeration getParameterNames() {
113 public String[] getParameterValues(String s) {
114 return new String[0];
118 public Map getParameterMap() {
123 public String getProtocol() {
128 public String getScheme() {
133 public String getServerName() {
138 public int getServerPort() {
143 public BufferedReader getReader() throws IOException {
148 public String getRemoteAddr() {
153 public String getRemoteHost() {
158 public void setAttribute(String s, Object o) {
163 public void removeAttribute(String s) {
168 public Locale getLocale() {
173 public Enumeration getLocales() {
178 public boolean isSecure() {
183 public RequestDispatcher getRequestDispatcher(String s) {
188 public String getRealPath(String s) {
193 public int getRemotePort() {
198 public String getLocalName() {
203 public String getLocalAddr() {
208 public int getLocalPort() {
213 private static class TestFilterChain implements FilterChain {
215 private AtomicInteger count = new AtomicInteger(0);
218 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
219 throws IOException, ServletException {
221 Assert.assertNotNull(MDC.get("RequestId"));
222 Assert.assertEquals(MDC.get("ServiceInstanceId"), "N/A");
223 Assert.assertEquals(MDC.get("ServiceName"), "ASDC");
224 Assert.assertEquals(MDC.get("InstanceUUID"), "N/A");
225 Assert.assertEquals(MDC.get("RemoteHost"), REMOTE_HOST);
227 InetAddress host = InetAddress.getLocalHost();
228 Assert.assertEquals(MDC.get("ServerIPAddress"), host.getHostAddress());
229 Assert.assertEquals(MDC.get("ServerFQDN"), host.getHostName());
231 count.incrementAndGet();
234 public int getCount() {
239 private static class TestServletResponse implements ServletResponse {
242 public String getCharacterEncoding() {
247 public void setCharacterEncoding(String s) {
252 public String getContentType() {
257 public void setContentType(String s) {
262 public ServletOutputStream getOutputStream() throws IOException {
267 public PrintWriter getWriter() throws IOException {
272 public void setContentLength(int i) {
277 public int getBufferSize() {
282 public void setBufferSize(int i) {
287 public void flushBuffer() throws IOException {
292 public void resetBuffer() {
297 public boolean isCommitted() {
302 public void reset() {
307 public Locale getLocale() {
312 public void setLocale(Locale locale) {