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=========================================================
20 package org.onap.policy.pap.xacml.restAuth;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
25 import java.io.IOException;
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
37 import com.mockrunner.mock.web.MockHttpServletRequest;
38 import com.mockrunner.mock.web.MockHttpServletResponse;
40 public class PAPAuthenticationFilterTest {
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;
51 // Set the system property temporarily
52 oldProperty = System.getProperty(systemKey);
53 System.setProperty(systemKey, "xacml.pap.properties");
55 request = Mockito.mock(HttpServletRequest.class);
56 response = Mockito.mock(HttpServletResponse.class);
57 filter = Mockito.mock(FilterChain.class);
58 papFilter = new PAPAuthenticationFilter();
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;
69 // Negative test the filter
70 filter.doFilter(request, response, filterChain);
71 assertEquals(response.getStatusCode(), 401);
78 catch (Exception ex) {
79 fail("Not expecting any exceptions.");
84 public void testDoFilter() {
85 Mockito.when(request.getRequestURI()).thenReturn("/pap/");
86 Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dGVzdHBhcDphbHBoYTEyMw==");
88 Mockito.when(request.getRequestURI()).thenReturn("/pap/onap/");
92 public void callDoFilter(){
94 papFilter.doFilter(request, response, filter);
95 } catch (Exception e) {
96 assertEquals(NullPointerException.class, e.getClass());
101 public void testOnPassingInvalidParamters(){
102 Mockito.when(request.getRequestURI()).thenReturn("/pap/");
103 Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dGVzdHBhcDphbHBoYTE789==");
105 papFilter.doFilter(request, response, filter);
106 assertEquals(0, response.getStatus());
107 } catch (Exception e) {
114 // Restore the original system property
115 if (oldProperty != null) {
116 System.setProperty(systemKey, oldProperty);
118 System.clearProperty(systemKey);