[DMAAP-BC] Fix failing jenkins
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / AAFAuthenticationFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dmaap.dbcapi.resources;
21
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyZeroInteractions;
29
30 import javax.servlet.FilterChain;
31 import javax.servlet.FilterConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.aaf.cadi.filter.CadiFilter;
45 import org.onap.dmaap.dbcapi.util.DmaapConfig;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class AAFAuthenticationFilterTest {
49
50     @Spy
51     private AAFAuthenticationFilter filter;
52     @Mock
53     private FilterConfig filterConfig;
54     @Mock
55     private CadiFilter cadiFilterMock;
56     @Mock
57     private HttpServletRequest servletRequest;
58     @Mock
59     private HttpServletResponse servletResponse;
60     @Mock
61     private FilterChain filterChain;
62     @Mock
63     private DmaapConfig dmaapConfig;
64
65     @Rule
66     public ExpectedException thrown = ExpectedException.none();
67
68     @BeforeClass
69     public static void setUpClass(){
70         System.setProperty("ConfigFile", "src/test/resources/dmaapbc.properties");
71     }
72
73     @Before
74     public void setUp() throws Exception {
75         doReturn(dmaapConfig).when(filter).getConfig();
76     }
77
78     @Test
79     public void init_shouldNotInitializeCADI_whenAafIsNotUsed() throws Exception {
80         //given
81         doReturn("false").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.CADI_AUTHN_FLAG), anyString());
82
83         //when
84         filter.init(filterConfig);
85
86         //then
87         assertFalse(filter.isCadiEnabled());
88         assertNull(filter.getCadiFilter());
89     }
90
91     @Test
92     public void doFilter_shouldSkipCADI_whenAafIsNotUsed() throws Exception {
93         //given
94         doReturn("false").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.CADI_AUTHN_FLAG), anyString());
95         filter.init(filterConfig);
96         filter.setCadiFilter(cadiFilterMock);
97
98         //when
99         filter.doFilter(servletRequest, servletResponse, filterChain);
100
101         //then
102         verify(filterChain).doFilter(servletRequest,servletResponse);
103         verifyZeroInteractions(cadiFilterMock,servletRequest,servletResponse);
104     }
105
106     @Test
107     public void init_shouldFail_whenAafIsUsed_andCadiPropertiesHasNotBeenSet() throws Exception {
108         //given
109         doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.CADI_AUTHN_FLAG), anyString());
110         doReturn("").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
111
112         //then
113         thrown.expect(ServletException.class);
114         thrown.expectMessage("Cannot initialize CADI filter.CADI properties not available.");
115
116         //when
117         filter.init(filterConfig);
118     }
119
120     @Test
121     public void init_shouldFail_whenAafIsUsed_andInvalidCadiPropertiesSet() throws Exception {
122         //given
123         String invalidFilePath = "src/test/resources/notExisting.properties";
124         doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.CADI_AUTHN_FLAG), anyString());
125         doReturn(invalidFilePath).when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
126
127         //then
128         thrown.expect(ServletException.class);
129         thrown.expectMessage("Could not load CADI properties file: "+invalidFilePath);
130
131         //when
132         filter.init(filterConfig);
133     }
134 }