DR AAF CADI integration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / utils / DRProvCadiFilterTest.java
1 /**-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2019 Nordix Foundation.\r
4  * ================================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  * SPDX-License-Identifier: Apache-2.0\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.dmaap.datarouter.provisioning.utils;\r
22 \r
23 import org.junit.Before;\r
24 import org.junit.BeforeClass;\r
25 import org.junit.Test;\r
26 import org.junit.runner.RunWith;\r
27 import org.mockito.Mock;\r
28 import org.onap.aaf.cadi.PropAccess;\r
29 import org.onap.aaf.cadi.filter.CadiFilter;\r
30 import org.onap.dmaap.datarouter.provisioning.BaseServlet;\r
31 import org.powermock.api.mockito.PowerMockito;\r
32 import org.powermock.api.support.membermodification.MemberMatcher;\r
33 import org.powermock.core.classloader.annotations.PrepareForTest;\r
34 import org.powermock.modules.junit4.PowerMockRunner;\r
35 \r
36 import javax.persistence.EntityManager;\r
37 import javax.persistence.EntityManagerFactory;\r
38 import javax.persistence.Persistence;\r
39 import javax.servlet.FilterChain;\r
40 import javax.servlet.http.HttpServletRequest;\r
41 import javax.servlet.http.HttpServletResponse;\r
42 \r
43 import static org.hamcrest.Matchers.notNullValue;\r
44 import static org.mockito.Matchers.argThat;\r
45 import static org.mockito.Matchers.eq;\r
46 import static org.mockito.Mockito.*;\r
47 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;\r
48 \r
49 @RunWith(PowerMockRunner.class)\r
50 @PrepareForTest({CadiFilter.class})\r
51 public class DRProvCadiFilterTest {\r
52 \r
53     @Mock\r
54     private PropAccess access;\r
55 \r
56     @Mock\r
57     private HttpServletRequest request;\r
58 \r
59     @Mock\r
60     private HttpServletResponse response;\r
61 \r
62     @Mock\r
63     private FilterChain chain;\r
64 \r
65     private DRProvCadiFilter cadiFilter;\r
66 \r
67 \r
68     private static EntityManagerFactory emf;\r
69     private static EntityManager em;\r
70 \r
71 \r
72     @BeforeClass\r
73     public static void init() {\r
74         emf = Persistence.createEntityManagerFactory("dr-unit-tests");\r
75         em = emf.createEntityManager();\r
76         System.setProperty(\r
77                 "org.onap.dmaap.datarouter.provserver.properties",\r
78                 "src/test/resources/h2Database.properties");\r
79     }\r
80 \r
81     @Before\r
82     public void setUp() throws Exception {\r
83         cadiFilter = new DRProvCadiFilter(false, access);\r
84     }\r
85 \r
86     @Test\r
87     public void Given_doFilter_Called_And_Path_Contains_subs_And_SubId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned() throws Exception{\r
88         setRequestMocking("PUT", "subs");\r
89 \r
90         cadiFilter.doFilter(request, response, chain);\r
91         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));\r
92     }\r
93 \r
94     @Test\r
95     public void Given_doFilter_called_And_Path_Contains_subs_And_Is_AAF_Subscriber_then_call_Super_doFilter() throws Exception{\r
96         setRequestMocking("PUT", "subs");\r
97         when(request.getPathInfo()).thenReturn("/2");\r
98         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
99         cadiFilter.doFilter(request, response, chain);\r
100         verify(chain, times(0)).doFilter(request, response);\r
101     }\r
102 \r
103     @Test\r
104     public void Given_doFilter_called_And_Path_Contains_subs_And_Is_Not_AAF_Subscriber_then_call_chain_doFilter() throws Exception{\r
105         setRequestMocking("PUT", "subs");\r
106         when(request.getPathInfo()).thenReturn("/5");\r
107 \r
108         cadiFilter.doFilter(request, response, chain);\r
109         verify(chain, times(1)).doFilter(request, response);\r
110     }\r
111 \r
112     @Test\r
113     public void Given_doFilter_called_And_FeedId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned () throws Exception{\r
114         setRequestMocking("PUT", "feeds");\r
115 \r
116         cadiFilter.doFilter(request, response, chain);\r
117         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));\r
118     }\r
119 \r
120     @Test\r
121     public void Given_doFilter_called_And_FeedId_Is_Correctly_Set_And_Is_AAF_Feed_Then_Call_Super_doFilter() throws Exception{\r
122         setRequestMocking("PUT", "feeds");\r
123         when(request.getPathInfo()).thenReturn("/2");\r
124         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
125         cadiFilter.doFilter(request, response, chain);\r
126         verify(chain, times(0)).doFilter(request, response);\r
127     }\r
128 \r
129     @Test\r
130     public void Given_doFilter_called_And_FeedId_Is_Correctly_Set_And_Is_Not_AAF_Feed_then_call_chain_doFilter() throws Exception{\r
131         setRequestMocking("PUT", "feeds");\r
132         when(request.getPathInfo()).thenReturn("/1");\r
133 \r
134         cadiFilter.doFilter(request, response, chain);\r
135         verify(chain, times(1)).doFilter(request, response);\r
136     }\r
137 \r
138     @Test\r
139     public void Given_doFilter_called_With_Get_Then_call_chain_doFilter() throws Exception{\r
140         setRequestMocking("GET", "feeds");\r
141         when(request.getPathInfo()).thenReturn("/5");\r
142 \r
143         cadiFilter.doFilter(request, response, chain);\r
144         verify(chain, times(1)).doFilter(request, response);\r
145     }\r
146 \r
147 \r
148     @Test\r
149     public void Given_doFilter_called_With_POST_Then_call_chain_doFilter() throws Exception{\r
150         setRequestMocking("POST", "subscribe");\r
151 \r
152         cadiFilter.doFilter(request, response, chain);\r
153         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));\r
154 \r
155     }\r
156 \r
157     @Test\r
158     public void Given_doFilter_called_With_POST_And_FeedId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned() throws Exception{\r
159         setRequestMocking("POST", "subscribe");\r
160 \r
161         cadiFilter.doFilter(request, response, chain);\r
162         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));\r
163 \r
164     }\r
165 \r
166     @Test\r
167     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Is_NULL_Then_Bad_Request_Response_Returned() throws Exception{\r
168         setRequestMocking("POST", "subscribe");\r
169         when(request.getPathInfo()).thenReturn("/2");\r
170 \r
171         cadiFilter.doFilter(request, response, chain);\r
172         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));\r
173 \r
174     }\r
175 \r
176     @Test\r
177     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Equals_True_Then_Call_Chain_doFilter() throws Exception{\r
178         setRequestMocking("POST", "subscribe");\r
179         when(request.getPathInfo()).thenReturn("/2");\r
180         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("true");\r
181 \r
182         cadiFilter.doFilter(request, response, chain);\r
183         verify(chain, times(1)).doFilter(request, response);\r
184 \r
185     }\r
186 \r
187     @Test\r
188     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Equals_False_Then_Call_Super_doFilter() throws Exception{\r
189         setRequestMocking("POST", "subscribe");\r
190         when(request.getPathInfo()).thenReturn("/2");\r
191         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");\r
192         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
193 \r
194         cadiFilter.doFilter(request, response, chain);\r
195         verify(chain, times(0)).doFilter(request, response);\r
196 \r
197     }\r
198 \r
199     @Test\r
200     public void Given_doFilter_called_With_POST_And_Is_Not_AAF_Exclude_AAF_Equals_Then_Call_Chain_doFilter() throws Exception{\r
201         setRequestMocking("POST", "subscribe");\r
202         when(request.getPathInfo()).thenReturn("/5");\r
203         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");\r
204 \r
205         cadiFilter.doFilter(request, response, chain);\r
206         verify(chain, times(1)).doFilter(request, response);\r
207 \r
208     }\r
209 \r
210     @Test\r
211     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Is_NULL_Then_Bad_Request_Response_Returned() throws Exception{\r
212         setRequestMocking("POST", "other");\r
213         when(request.getPathInfo()).thenReturn("/5");\r
214 \r
215         cadiFilter.doFilter(request, response, chain);\r
216         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));\r
217 \r
218     }\r
219 \r
220     @Test\r
221     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Equals_True_Then_Call_Chain_doFilter() throws Exception{\r
222         setRequestMocking("POST", "other");\r
223         when(request.getPathInfo()).thenReturn("/5");\r
224         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("true");\r
225 \r
226         cadiFilter.doFilter(request, response, chain);\r
227         verify(chain, times(1)).doFilter(request, response);\r
228 \r
229     }\r
230 \r
231     @Test\r
232     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Equals_False_Then_Call_Super_doFilter() throws Exception{\r
233         setRequestMocking("POST", "other");\r
234         when(request.getPathInfo()).thenReturn("/5");\r
235         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");\r
236         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
237 \r
238         cadiFilter.doFilter(request, response, chain);\r
239         verify(chain, times(0)).doFilter(request, response);\r
240 \r
241     }\r
242 \r
243     @Test\r
244     public void Given_doFilter_Called_And_Path_Contains_subs_And_getSubId_Throws_NumberFormatException_then_Not_Found_response_returned() throws Exception{\r
245             setRequestMocking("PUT", "subs");\r
246             when(request.getPathInfo()).thenReturn("5/");\r
247             cadiFilter.doFilter(request, response, chain);\r
248             verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));\r
249 \r
250     }\r
251 \r
252     @Test\r
253     public void Given_doFilter_called_And_FeedId_Throws_Set_Then_Not_Found_Response_Returned () throws Exception{\r
254         setRequestMocking("PUT", "feeds");\r
255         when(request.getPathInfo()).thenReturn("//5");\r
256         cadiFilter.doFilter(request, response, chain);\r
257         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));\r
258     }\r
259 \r
260     private void setRequestMocking(String method, String servletPath)\r
261     {\r
262         when(request.getRemoteAddr()).thenReturn(null);\r
263         when(request.getHeader(BEHALF_HEADER)).thenReturn(null);\r
264         when(request.getAttribute(BaseServlet.CERT_ATTRIBUTE)).thenReturn(null);\r
265         when(request.getMethod()).thenReturn(method);\r
266         when(request.getServletPath()).thenReturn(servletPath);\r
267     }\r
268 \r
269     }\r