Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / restAuth / PAPAuthenticationFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 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 package org.onap.policy.pap.xacml.restAuth;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import com.mockrunner.mock.web.MockHttpServletRequest;
27 import com.mockrunner.mock.web.MockHttpServletResponse;
28
29 import java.io.IOException;
30
31 import javax.servlet.FilterChain;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40
41 public class PAPAuthenticationFilterTest {
42
43     private HttpServletRequest request = null;
44     private HttpServletResponse response = null;
45     private String oldProperty;
46     private String systemKey = "xacml.properties";
47     private FilterChain filter;
48     private PAPAuthenticationFilter papFilter;
49
50     @Before
51     public void setUp() {
52         // Set the system property temporarily
53         oldProperty = System.getProperty(systemKey);
54         System.setProperty(systemKey, "xacml.pap.properties");
55
56         request = Mockito.mock(HttpServletRequest.class);
57         response = Mockito.mock(HttpServletResponse.class);
58         filter = Mockito.mock(FilterChain.class);
59         papFilter = new PAPAuthenticationFilter();
60     }
61
62     @Test
63     public void testAuth() throws IOException, ServletException {
64         PAPAuthenticationFilter filter = new PAPAuthenticationFilter();
65         MockHttpServletRequest request = new MockHttpServletRequest();
66         request.setRequestURI("/foo");
67         MockHttpServletResponse response = new MockHttpServletResponse();
68         FilterChain filterChain = null;
69
70         // Negative test the filter
71         filter.doFilter(request, response, filterChain);
72         assertEquals(response.getStatusCode(), 401);
73
74         // Test base methods
75         try {
76             filter.destroy();
77             filter.init(null);
78         } catch (Exception ex) {
79             fail("Not expecting any exceptions.");
80         }
81     }
82
83     @Test
84     public void testDoFilter() {
85         Mockito.when(request.getRequestURI()).thenReturn("/pap/");
86         Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dGVzdHBhcDphbHBoYTEyMw==");
87         callDoFilter();
88         Mockito.when(request.getRequestURI()).thenReturn("/pap/onap/");
89         callDoFilter();
90     }
91
92     public void callDoFilter() {
93         try {
94             papFilter.doFilter(request, response, filter);
95         } catch (Exception e) {
96             assertEquals(NullPointerException.class, e.getClass());
97         }
98     }
99
100     @Test
101     public void testOnPassingInvalidParamters() {
102         Mockito.when(request.getRequestURI()).thenReturn("/pap/");
103         Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dGVzdHBhcDphbHBoYTE789==");
104         try {
105             papFilter.doFilter(request, response, filter);
106             assertEquals(0, response.getStatus());
107         } catch (Exception e) {
108             fail();
109         }
110     }
111
112     @After
113     public void reset() {
114         // Restore the original system property
115         if (oldProperty != null) {
116             System.setProperty(systemKey, oldProperty);
117         } else {
118             System.clearProperty(systemKey);
119         }
120     }
121 }