1 /*******************************************************************************
2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2019 AT&T Intellectual Property
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
21 *******************************************************************************/
22 package org.onap.music.eelf.logging;
24 import static org.mockito.Mockito.doNothing;
25 import java.io.IOException;
26 import java.util.Enumeration;
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.music.main.MusicUtil;
38 public class MusicLoggingServletFilterTest {
39 MusicLoggingServletFilter filter;
42 public void setup() throws ServletException {
43 filter = new MusicLoggingServletFilter();
47 public void testDoFilter() throws IOException, ServletException {
48 FilterChain chain = Mockito.mock(FilterChain.class);
49 Enumeration<String> headerNames = Mockito.mock(Enumeration.class);
50 HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
51 HttpServletResponse httpResponse = Mockito.mock(HttpServletResponse.class);
52 Mockito.when(headerNames.hasMoreElements()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
53 Mockito.when(headerNames.nextElement()).thenReturn("element1").thenReturn("element2").thenReturn("element3");
54 Mockito.when(httpRequest.getHeader(Mockito.anyString())).thenReturn("key1").thenReturn("key2").thenReturn("key3");
55 Mockito.when(httpRequest.getHeaderNames()).thenReturn(headerNames);
56 MusicUtil.setTransIdRequired(false);
57 MusicUtil.setConversationIdRequired(false);
58 MusicUtil.setMessageIdRequired(false);
59 MusicUtil.setClientIdRequired(false);
60 doNothing().when(chain).doFilter(Mockito.any(), Mockito.any());
61 filter.doFilter(httpRequest, httpResponse, chain);