2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.interceptor;
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Matchers.anyString;
41 import static org.mockito.Mockito.when;
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.HashSet;
46 import java.util.List;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import javax.servlet.http.HttpSession;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.mockito.InjectMocks;
56 import org.mockito.Mock;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalapp.portal.controller.BasicAuthenticationController;
59 import org.onap.portalapp.portal.domain.EPApp;
60 import org.onap.portalapp.portal.service.ExternalAccessRolesService;
61 import org.onap.portalsdk.core.controller.FusionBaseController;
62 import org.springframework.web.method.HandlerMethod;
64 public class PortalResourceInterceptorTest {
67 PortalResourceInterceptor portalResourceInterceptor;
69 HttpServletRequest request;
71 HttpServletResponse response;
73 HandlerMethod handler;
77 FusionBaseController fusionBaseController;
79 BasicAuthenticationController basicAuthenticationController;
81 private ExternalAccessRolesService externalAccessRolesService;
83 PrintWriter printWriter;
87 MockitoAnnotations.initMocks(this);
92 public void testPreHandle()throws Exception {
93 Set<String> data=new HashSet<>();
95 when(request.getRequestURI()).thenReturn("test/portalApi/test");
97 when(session.getAttribute(anyString())).thenReturn(data);
98 when(request.getSession()).thenReturn(session);
99 when(fusionBaseController.isAccessible()).thenReturn(true);
100 when(handler.getBean()).thenReturn(fusionBaseController);
103 boolean result= portalResourceInterceptor.preHandle(request, response, handler);
104 assertEquals(true, result);
110 public void testPreHandlePass()throws Exception {
111 Set<String> data=new HashSet<>();
112 data.add("test/test");
113 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
115 when(session.getAttribute(anyString())).thenReturn(data);
116 when(request.getSession()).thenReturn(session);
117 when(fusionBaseController.isAccessible()).thenReturn(false);
118 when(handler.getBean()).thenReturn(fusionBaseController);
121 boolean result= portalResourceInterceptor.preHandle(request, response, handler);
123 assertEquals(true, result);
130 public void testPreHandleAuth()throws Exception {
131 Set<String> data=new HashSet<>();
132 data.add("test/test");
133 List<EPApp> apps=new ArrayList<>();
134 EPApp app=new EPApp();
135 app.setUsername("test");
137 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
139 when(request.getHeader("Authorization")).thenReturn("Basictest");
140 when(request.getHeader("uebkey")).thenReturn("test");
141 when(request.getSession()).thenReturn(session);
142 when(fusionBaseController.isAccessible()).thenReturn(false);
143 when(handler.getBean()).thenReturn(basicAuthenticationController);
144 when( externalAccessRolesService.getApp("test")).thenReturn(apps);
145 when(response.getWriter()).thenReturn(printWriter);
147 boolean result= portalResourceInterceptor.preHandle(request, response, handler);
149 assertEquals(false, result);