2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 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====================================================
22 package org.onap.aaf.cadi.filter.test;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.when;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.security.Principal;
35 import javax.servlet.FilterChain;
36 import javax.servlet.FilterConfig;
37 import javax.servlet.ServletContext;
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.aaf.cadi.PropAccess;
47 import org.onap.aaf.cadi.config.Config;
48 import org.onap.aaf.cadi.filter.PathFilter;
50 public class JU_PathFilter {
52 private PropAccess access;
54 @Mock private FilterConfig filterConfigMock;
55 @Mock private ServletContext contextMock;
56 @Mock private HttpServletRequest reqMock;
57 @Mock private HttpServletResponse respMock;
58 @Mock private FilterChain chainMock;
59 @Mock private Principal princMock;
63 MockitoAnnotations.initMocks(this);
64 when(filterConfigMock.getServletContext()).thenReturn(contextMock);
65 when(reqMock.getUserPrincipal()).thenReturn(princMock);
66 when(princMock.getName()).thenReturn("name");
68 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
72 public void test() throws ServletException, IOException {
73 PathFilter pathFilter = new PathFilter(access);
75 pathFilter.init(filterConfigMock);
76 fail("Should've thrown an exception");
77 } catch (ServletException e) {
78 assertThat(e.getMessage(), is("PathFilter - pathfilter_ns is not set"));
81 when(contextMock.getAttribute(Config.PATHFILTER_NS)).thenReturn(5);
82 when(contextMock.getAttribute(Config.PATHFILTER_STACK)).thenReturn(5);
83 when(contextMock.getAttribute(Config.PATHFILTER_URLPATTERN)).thenReturn(5);
84 when(contextMock.getAttribute(Config.PATHFILTER_NOT_AUTHORIZED_MSG)).thenReturn(5);
85 pathFilter.init(filterConfigMock);
87 pathFilter.doFilter(reqMock, respMock, chainMock);
89 when(reqMock.isUserInRole(anyString())).thenReturn(true);
90 pathFilter.doFilter(reqMock, respMock, chainMock);
94 pathFilter = new PathFilter();
95 pathFilter.init(filterConfigMock);
97 pathFilter.doFilter(reqMock, respMock, chainMock);
99 when(reqMock.isUserInRole(anyString())).thenReturn(false);
100 pathFilter.doFilter(reqMock, respMock, chainMock);
102 pathFilter.destroy();