Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / filter / test / JU_PathFilter.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.filter.test;
23
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;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.security.Principal;
34
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;
41
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;
49
50 public class JU_PathFilter {
51
52     private PropAccess access;
53
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;
60
61     @Before
62     public void setup() {
63         MockitoAnnotations.initMocks(this);
64         when(filterConfigMock.getServletContext()).thenReturn(contextMock);
65         when(reqMock.getUserPrincipal()).thenReturn(princMock);
66         when(princMock.getName()).thenReturn("name");
67
68         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
69     }
70
71     @Test
72     public void test() throws ServletException, IOException {
73         PathFilter pathFilter = new PathFilter(access);
74         try {
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"));
79         }
80
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);
86
87         pathFilter.doFilter(reqMock, respMock, chainMock);
88
89         when(reqMock.isUserInRole(anyString())).thenReturn(true);
90         pathFilter.doFilter(reqMock, respMock, chainMock);
91
92         pathFilter.destroy();
93
94         pathFilter = new PathFilter();
95         pathFilter.init(filterConfigMock);
96
97         pathFilter.doFilter(reqMock, respMock, chainMock);
98
99         when(reqMock.isUserInRole(anyString())).thenReturn(false);
100         pathFilter.doFilter(reqMock, respMock, chainMock);
101
102         pathFilter.destroy();
103     }
104
105 }