93a08619875daae0dd7e47108a37555058a80422
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / interceptor / PortalResourceInterceptorTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
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;
42
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Set;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import javax.servlet.http.HttpSession;
52
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;
63
64 public class PortalResourceInterceptorTest {
65         
66         @InjectMocks
67         PortalResourceInterceptor portalResourceInterceptor;
68         @Mock
69         HttpServletRequest request;
70         @Mock
71         HttpServletResponse response;
72         @Mock
73         HandlerMethod handler;
74         @Mock
75         HttpSession session;
76         @Mock
77         FusionBaseController fusionBaseController;
78         @Mock
79         BasicAuthenticationController basicAuthenticationController;
80         @Mock
81         private ExternalAccessRolesService externalAccessRolesService;
82         @Mock
83         PrintWriter printWriter;
84         
85         @Before
86         public void setup() {
87                 MockitoAnnotations.initMocks(this);
88                 
89         }
90         
91         @Test
92         public void testPreHandle()throws Exception {
93                 Set<String> data=new HashSet<>();
94                 data.add("test");
95                 when(request.getRequestURI()).thenReturn("test/portalApi/test");
96                 
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);
101                 
102                 
103         boolean result= portalResourceInterceptor.preHandle(request, response, handler);
104         assertEquals(true, result);
105                 
106                 
107         }
108         
109         @Test
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");
114                 
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);
119                 
120                 
121         boolean result= portalResourceInterceptor.preHandle(request, response, handler);
122         
123         assertEquals(true, result);
124                 
125         }
126         
127         
128
129         @Test
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");
136                 apps.add(app);
137                 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
138                 
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);
146                 
147         boolean result= portalResourceInterceptor.preHandle(request, response, handler);
148         
149         assertEquals(false, result);
150                 
151         }
152         
153         
154
155 }