Modify ONAP PAP REST classes basic checkstyle
[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 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 package org.onap.policy.pap.xacml.restAuth;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24
25 import java.io.IOException;
26
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36
37 import com.mockrunner.mock.web.MockHttpServletRequest;
38 import com.mockrunner.mock.web.MockHttpServletResponse;
39
40 public class PAPAuthenticationFilterTest {
41
42     private HttpServletRequest request = null;
43     private HttpServletResponse response = null;
44     private String oldProperty;
45     private String systemKey = "xacml.properties";
46     private FilterChain filter;
47     private PAPAuthenticationFilter papFilter;
48
49     @Before
50     public void setUp(){
51         // Set the system property temporarily
52         oldProperty = System.getProperty(systemKey);
53         System.setProperty(systemKey, "xacml.pap.properties");
54
55         request = Mockito.mock(HttpServletRequest.class);
56         response = Mockito.mock(HttpServletResponse.class);
57         filter = Mockito.mock(FilterChain.class);
58         papFilter = new PAPAuthenticationFilter();
59     }
60
61     @Test
62     public void testAuth() throws IOException, ServletException {
63         PAPAuthenticationFilter filter = new PAPAuthenticationFilter();
64         MockHttpServletRequest request = new MockHttpServletRequest();
65         request.setRequestURI("/foo");
66         MockHttpServletResponse response = new MockHttpServletResponse();
67         FilterChain filterChain = null;
68
69         // Negative test the filter
70         filter.doFilter(request, response, filterChain);
71         assertEquals(response.getStatusCode(), 401);
72
73         // Test base methods
74         try {
75             filter.destroy();
76             filter.init(null);
77         }
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 }