2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.logging.servlet;
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
27 import javax.servlet.*;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.io.UnsupportedEncodingException;
32 import java.net.InetAddress;
33 import java.util.Enumeration;
34 import java.util.Locale;
36 import java.util.UUID;
37 import java.util.concurrent.atomic.AtomicInteger;
39 import static org.testng.Assert.assertEquals;
40 import static org.testng.Assert.assertNull;
43 * TODO: Add more tests
48 public class LoggingFilterTest {
50 private static final String REMOTE_HOST = UUID.randomUUID().toString();
53 public void testDoFilter() throws Exception {
54 LoggingFilter loggingFilter = new LoggingFilter();
55 ServletRequest mockRequest = new TestServletRequest();
56 ServletResponse mockResponse = new TestServletResponse();
57 TestFilterChain mockChain = new TestFilterChain();
58 loggingFilter.doFilter(mockRequest, mockResponse, mockChain);
59 assertEquals(1, mockChain.getCount());
60 assertNull(MDC.getCopyOfContextMap());
63 private static class TestServletRequest implements ServletRequest {
66 public Object getAttribute(String s) {
71 public Enumeration getAttributeNames() {
76 public String getCharacterEncoding() {
81 public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
86 public int getContentLength() {
91 public String getContentType() {
96 public ServletInputStream getInputStream() throws IOException {
101 public String getParameter(String s) {
106 public Enumeration getParameterNames() {
111 public String[] getParameterValues(String s) {
112 return new String[0];
116 public Map getParameterMap() {
121 public String getProtocol() {
126 public String getScheme() {
131 public String getServerName() {
136 public int getServerPort() {
141 public BufferedReader getReader() throws IOException {
146 public String getRemoteAddr() {
151 public String getRemoteHost() {
156 public void setAttribute(String s, Object o) {
161 public void removeAttribute(String s) {
166 public Locale getLocale() {
171 public Enumeration getLocales() {
176 public boolean isSecure() {
181 public RequestDispatcher getRequestDispatcher(String s) {
186 public String getRealPath(String s) {
191 public int getRemotePort() {
196 public String getLocalName() {
201 public String getLocalAddr() {
206 public int getLocalPort() {
211 private static class TestFilterChain implements FilterChain {
213 private AtomicInteger count = new AtomicInteger(0);
216 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
217 throws IOException, ServletException {
219 Assert.assertNotNull(MDC.get("RequestId"));
220 Assert.assertEquals(MDC.get("ServiceInstanceId"), "N/A");
221 Assert.assertEquals(MDC.get("ServiceName"), "SDC");
222 Assert.assertEquals(MDC.get("InstanceUUID"), "N/A");
223 Assert.assertEquals(MDC.get("RemoteHost"), REMOTE_HOST);
225 InetAddress host = InetAddress.getLocalHost();
226 Assert.assertEquals(MDC.get("ServerIPAddress"), host.getHostAddress());
227 Assert.assertEquals(MDC.get("ServerFQDN"), host.getHostName());
229 count.incrementAndGet();
232 public int getCount() {
237 private static class TestServletResponse implements ServletResponse {
240 public String getCharacterEncoding() {
245 public void setCharacterEncoding(String s) {
250 public String getContentType() {
255 public void setContentType(String s) {
260 public ServletOutputStream getOutputStream() throws IOException {
265 public PrintWriter getWriter() throws IOException {
270 public void setContentLength(int i) {
275 public int getBufferSize() {
280 public void setBufferSize(int i) {
285 public void flushBuffer() throws IOException {
290 public void resetBuffer() {
295 public boolean isCommitted() {
300 public void reset() {
305 public Locale getLocale() {
310 public void setLocale(Locale locale) {