3 * Copyright © 2017-2018 European Support Limited
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 package org.openecomp.sdc.itempermissions.servlet;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.ws.rs.HttpMethod;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.Spy;
35 import org.openecomp.sdc.itempermissions.PermissionsServices;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
39 public class PermissionsFilterTest {
42 private PermissionsServices permissionsServicesMock;
46 private PermissionsFilter permissionsFilter;
50 MockitoAnnotations.initMocks(this);
54 public void testDoFilter() throws ServletException, IOException {
55 HttpServletRequest httpServletRequest = Mockito.spy(HttpServletRequest.class);
56 HttpServletResponse httpServletResponse = Mockito.spy(HttpServletResponse.class);
57 FilterChain filterChain = Mockito.mock(FilterChain.class);
59 initializeMocking(httpServletRequest, httpServletResponse, filterChain);
60 Mockito.when(httpServletRequest.getPathInfo()).thenReturn("onboardingci/onbrest/onboarding-api/v1.0");
62 permissionsFilter.doFilter(httpServletRequest, httpServletResponse, filterChain);
64 Mockito.verify(filterChain, Mockito.times(0)).doFilter(Mockito.any(), Mockito.any());
68 public void testDoFilterPass() throws ServletException, IOException {
69 HttpServletRequest httpServletRequest = Mockito.spy(HttpServletRequest.class);
70 HttpServletResponse httpServletResponse = Mockito.spy(HttpServletResponse.class);
71 FilterChain filterChain = Mockito.mock(FilterChain.class);
73 initializeMocking(httpServletRequest, httpServletResponse, filterChain);
74 Mockito.when(httpServletRequest.getPathInfo()).thenReturn("onboardingci/onbrest/onboarding-api");
75 permissionsFilter.doFilter(httpServletRequest, httpServletResponse, filterChain);
77 Mockito.verify(filterChain, Mockito.times(1)).doFilter(Mockito.any(), Mockito.any());
80 private void initializeMocking(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
81 FilterChain filterChain) throws ServletException, IOException {
82 PrintWriter printWriter = new PrintWriter(new ByteArrayOutputStream());
83 Mockito.when(httpServletRequest.getMethod()).thenReturn(HttpMethod.POST);
84 Mockito.when(httpServletRequest.getHeader("USER_ID")).thenReturn("cs0008");
85 Mockito.when(httpServletResponse.getWriter()).thenReturn(printWriter);
86 Mockito.doNothing().when(filterChain).doFilter(Mockito.any(), Mockito.any());
87 Mockito.when(permissionsServicesMock.isAllowed(
88 Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(false);