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.*;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.io.UnsupportedEncodingException;
28 import java.net.InetAddress;
29 import java.util.Enumeration;
30 import java.util.Locale;
32 import java.util.UUID;
33 import java.util.concurrent.atomic.AtomicInteger;
35 import static org.testng.Assert.assertEquals;
36 import static org.testng.Assert.assertNull;
39 * TODO: Add more tests
44 public class LoggingFilterTest {
46 private static final String REMOTE_HOST = UUID.randomUUID().toString();
49 public void testDoFilter() throws Exception {
50 LoggingFilter loggingFilter = new LoggingFilter();
51 ServletRequest mockRequest = new TestServletRequest();
52 ServletResponse mockResponse = new TestServletResponse();
53 TestFilterChain mockChain = new TestFilterChain();
54 loggingFilter.doFilter(mockRequest, mockResponse, mockChain);
55 assertEquals(1, mockChain.getCount());
56 assertNull(MDC.getCopyOfContextMap());
59 private static class TestServletRequest implements ServletRequest {
62 public Object getAttribute(String s) {
67 public Enumeration getAttributeNames() {
72 public String getCharacterEncoding() {
77 public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
82 public int getContentLength() {
87 public String getContentType() {
92 public ServletInputStream getInputStream() throws IOException {
97 public String getParameter(String s) {
102 public Enumeration getParameterNames() {
107 public String[] getParameterValues(String s) {
108 return new String[0];
112 public Map getParameterMap() {
117 public String getProtocol() {
122 public String getScheme() {
127 public String getServerName() {
132 public int getServerPort() {
137 public BufferedReader getReader() throws IOException {
142 public String getRemoteAddr() {
147 public String getRemoteHost() {
152 public void setAttribute(String s, Object o) {
157 public void removeAttribute(String s) {
162 public Locale getLocale() {
167 public Enumeration getLocales() {
172 public boolean isSecure() {
177 public RequestDispatcher getRequestDispatcher(String s) {
182 public String getRealPath(String s) {
187 public int getRemotePort() {
192 public String getLocalName() {
197 public String getLocalAddr() {
202 public int getLocalPort() {
207 private static class TestFilterChain implements FilterChain {
209 private AtomicInteger count = new AtomicInteger(0);
212 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
213 throws IOException, ServletException {
215 Assert.assertNotNull(MDC.get("RequestId"));
216 Assert.assertEquals(MDC.get("ServiceInstanceId"), "N/A");
217 Assert.assertEquals(MDC.get("ServiceName"), "ASDC");
218 Assert.assertEquals(MDC.get("InstanceUUID"), "N/A");
219 Assert.assertEquals(MDC.get("RemoteHost"), REMOTE_HOST);
221 InetAddress host = InetAddress.getLocalHost();
222 Assert.assertEquals(MDC.get("ServerIPAddress"), host.getHostAddress());
223 Assert.assertEquals(MDC.get("ServerFQDN"), host.getHostName());
225 count.incrementAndGet();
228 public int getCount() {
233 private static class TestServletResponse implements ServletResponse {
236 public String getCharacterEncoding() {
241 public void setCharacterEncoding(String s) {
246 public String getContentType() {
251 public void setContentType(String s) {
256 public ServletOutputStream getOutputStream() throws IOException {
261 public PrintWriter getWriter() throws IOException {
266 public void setContentLength(int i) {
271 public int getBufferSize() {
276 public void setBufferSize(int i) {
281 public void flushBuffer() throws IOException {
286 public void resetBuffer() {
291 public boolean isCommitted() {
296 public void reset() {
301 public Locale getLocale() {
306 public void setLocale(Locale locale) {