6bb51c30ddf49c4961c1b6f8a87d74769f12a7c7
[sdc.git] /
1 /*
2  *
3  *  Copyright © 2017-2018 European Support Limited
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  * /
17  *
18  */
19
20 package org.openecomp.sdc.itempermissions.servlet;
21
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;
38
39 public class PermissionsFilterTest {
40
41     @Mock
42     private PermissionsServices permissionsServicesMock;
43
44     @InjectMocks
45     @Spy
46     private PermissionsFilter permissionsFilter;
47
48     @BeforeMethod
49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51     }
52
53     @Test
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);
58
59         initializeMocking(httpServletRequest, httpServletResponse, filterChain);
60         Mockito.when(httpServletRequest.getPathInfo()).thenReturn("onboardingci/onbrest/onboarding-api/v1.0");
61
62         permissionsFilter.doFilter(httpServletRequest, httpServletResponse, filterChain);
63
64         Mockito.verify(filterChain, Mockito.times(0)).doFilter(Mockito.any(), Mockito.any());
65     }
66
67     @Test
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);
72
73         initializeMocking(httpServletRequest, httpServletResponse, filterChain);
74         Mockito.when(httpServletRequest.getPathInfo()).thenReturn("onboardingci/onbrest/onboarding-api");
75         permissionsFilter.doFilter(httpServletRequest, httpServletResponse, filterChain);
76
77         Mockito.verify(filterChain, Mockito.times(1)).doFilter(Mockito.any(), Mockito.any());
78     }
79
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);
89     }
90 }