[DMaaP DR] JKD 11 migration
[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 static org.mockito.ArgumentMatchers.anyString;\r
24 import static org.mockito.ArgumentMatchers.eq;\r
25 import static org.mockito.Mockito.times;\r
26 import static org.mockito.Mockito.verify;\r
27 import static org.mockito.Mockito.when;\r
28 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;\r
29 \r
30 import javax.persistence.EntityManager;\r
31 import javax.persistence.EntityManagerFactory;\r
32 import javax.persistence.Persistence;\r
33 import javax.servlet.FilterChain;\r
34 import javax.servlet.http.HttpServletRequest;\r
35 import javax.servlet.http.HttpServletResponse;\r
36 import org.junit.Before;\r
37 import org.junit.BeforeClass;\r
38 import org.junit.Test;\r
39 import org.junit.runner.RunWith;\r
40 import org.mockito.Mock;\r
41 import org.onap.aaf.cadi.PropAccess;\r
42 import org.onap.aaf.cadi.filter.CadiFilter;\r
43 import org.onap.dmaap.datarouter.provisioning.BaseServlet;\r
44 import org.powermock.api.mockito.PowerMockito;\r
45 import org.powermock.api.support.membermodification.MemberMatcher;\r
46 import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
47 import org.powermock.core.classloader.annotations.PrepareForTest;\r
48 import org.powermock.modules.junit4.PowerMockRunner;\r
49 \r
50 @RunWith(PowerMockRunner.class)\r
51 @PrepareForTest({CadiFilter.class})\r
52 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})\r
53 public class DRProvCadiFilterTest {\r
54 \r
55     @Mock\r
56     private PropAccess access;\r
57 \r
58     @Mock\r
59     private HttpServletRequest request;\r
60 \r
61     @Mock\r
62     private HttpServletResponse response;\r
63 \r
64     @Mock\r
65     private FilterChain chain;\r
66 \r
67     private DRProvCadiFilter cadiFilter;\r
68 \r
69 \r
70     private static EntityManagerFactory emf;\r
71     private static EntityManager em;\r
72 \r
73 \r
74     @BeforeClass\r
75     public static void init() {\r
76         emf = Persistence.createEntityManagerFactory("dr-unit-tests");\r
77         em = emf.createEntityManager();\r
78         System.setProperty(\r
79                 "org.onap.dmaap.datarouter.provserver.properties",\r
80                 "src/test/resources/h2Database.properties");\r
81     }\r
82 \r
83     @Before\r
84     public void setUp() throws Exception {\r
85         cadiFilter = new DRProvCadiFilter(false, access);\r
86     }\r
87 \r
88     @Test\r
89     public void Given_doFilter_Called_And_Path_Contains_subs_And_SubId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned() throws Exception{\r
90         setRequestMocking("PUT", "subs");\r
91 \r
92         cadiFilter.doFilter(request, response, chain);\r
93         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());\r
94     }\r
95 \r
96     @Test\r
97     public void Given_doFilter_called_And_Path_Contains_subs_And_Is_AAF_Subscriber_then_call_Super_doFilter() throws Exception{\r
98         setRequestMocking("PUT", "subs");\r
99         when(request.getPathInfo()).thenReturn("/2");\r
100         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
101         cadiFilter.doFilter(request, response, chain);\r
102         verify(chain, times(0)).doFilter(request, response);\r
103     }\r
104 \r
105     @Test\r
106     public void Given_doFilter_called_And_Path_Contains_subs_And_Is_Not_AAF_Subscriber_then_call_chain_doFilter() throws Exception{\r
107         setRequestMocking("PUT", "subs");\r
108         when(request.getPathInfo()).thenReturn("/5");\r
109 \r
110         cadiFilter.doFilter(request, response, chain);\r
111         verify(chain, times(1)).doFilter(request, response);\r
112     }\r
113 \r
114     @Test\r
115     public void Given_doFilter_called_And_FeedId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned () throws Exception{\r
116         setRequestMocking("PUT", "feeds");\r
117 \r
118         cadiFilter.doFilter(request, response, chain);\r
119         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());\r
120     }\r
121 \r
122     @Test\r
123     public void Given_doFilter_called_And_FeedId_Is_Correctly_Set_And_Is_AAF_Feed_Then_Call_Super_doFilter() throws Exception{\r
124         setRequestMocking("PUT", "feeds");\r
125         when(request.getPathInfo()).thenReturn("/2");\r
126         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
127         cadiFilter.doFilter(request, response, chain);\r
128         verify(chain, times(0)).doFilter(request, response);\r
129     }\r
130 \r
131     @Test\r
132     public void Given_doFilter_called_And_FeedId_Is_Correctly_Set_And_Is_Not_AAF_Feed_then_call_chain_doFilter() throws Exception{\r
133         setRequestMocking("PUT", "feeds");\r
134         when(request.getPathInfo()).thenReturn("/1");\r
135 \r
136         cadiFilter.doFilter(request, response, chain);\r
137         verify(chain, times(1)).doFilter(request, response);\r
138     }\r
139 \r
140     @Test\r
141     public void Given_doFilter_called_With_Get_Then_call_chain_doFilter() throws Exception{\r
142         setRequestMocking("GET", "feeds");\r
143         when(request.getPathInfo()).thenReturn("/5");\r
144 \r
145         cadiFilter.doFilter(request, response, chain);\r
146         verify(chain, times(1)).doFilter(request, response);\r
147     }\r
148 \r
149 \r
150     @Test\r
151     public void Given_doFilter_called_With_POST_Then_call_chain_doFilter() throws Exception{\r
152         setRequestMocking("POST", "subscribe");\r
153 \r
154         cadiFilter.doFilter(request, response, chain);\r
155         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());\r
156 \r
157     }\r
158 \r
159     @Test\r
160     public void Given_doFilter_called_With_POST_And_FeedId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned() throws Exception{\r
161         setRequestMocking("POST", "subscribe");\r
162 \r
163         cadiFilter.doFilter(request, response, chain);\r
164         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());\r
165 \r
166     }\r
167 \r
168     @Test\r
169     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Is_NULL_Then_Bad_Request_Response_Returned() throws Exception{\r
170         setRequestMocking("POST", "subscribe");\r
171         when(request.getPathInfo()).thenReturn("/2");\r
172 \r
173         cadiFilter.doFilter(request, response, chain);\r
174         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());\r
175 \r
176     }\r
177 \r
178     @Test\r
179     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Equals_True_Then_Call_Chain_doFilter() throws Exception{\r
180         setRequestMocking("POST", "subscribe");\r
181         when(request.getPathInfo()).thenReturn("/2");\r
182         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("true");\r
183 \r
184         cadiFilter.doFilter(request, response, chain);\r
185         verify(chain, times(1)).doFilter(request, response);\r
186 \r
187     }\r
188 \r
189     @Test\r
190     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Equals_False_Then_Call_Super_doFilter() throws Exception{\r
191         setRequestMocking("POST", "subscribe");\r
192         when(request.getPathInfo()).thenReturn("/2");\r
193         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");\r
194         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
195 \r
196         cadiFilter.doFilter(request, response, chain);\r
197         verify(chain, times(0)).doFilter(request, response);\r
198 \r
199     }\r
200 \r
201     @Test\r
202     public void Given_doFilter_called_With_POST_And_Is_Not_AAF_Exclude_AAF_Equals_Then_Call_Chain_doFilter() throws Exception{\r
203         setRequestMocking("POST", "subscribe");\r
204         when(request.getPathInfo()).thenReturn("/5");\r
205         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");\r
206 \r
207         cadiFilter.doFilter(request, response, chain);\r
208         verify(chain, times(1)).doFilter(request, response);\r
209 \r
210     }\r
211 \r
212     @Test\r
213     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
214         setRequestMocking("POST", "other");\r
215         when(request.getPathInfo()).thenReturn("/5");\r
216 \r
217         cadiFilter.doFilter(request, response, chain);\r
218         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());\r
219 \r
220     }\r
221 \r
222     @Test\r
223     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Equals_True_Then_Call_Chain_doFilter() throws Exception{\r
224         setRequestMocking("POST", "other");\r
225         when(request.getPathInfo()).thenReturn("/5");\r
226         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("true");\r
227 \r
228         cadiFilter.doFilter(request, response, chain);\r
229         verify(chain, times(1)).doFilter(request, response);\r
230 \r
231     }\r
232 \r
233     @Test\r
234     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Equals_False_Then_Call_Super_doFilter() throws Exception{\r
235         setRequestMocking("POST", "other");\r
236         when(request.getPathInfo()).thenReturn("/5");\r
237         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");\r
238         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
239 \r
240         cadiFilter.doFilter(request, response, chain);\r
241         verify(chain, times(0)).doFilter(request, response);\r
242 \r
243     }\r
244 \r
245     @Test\r
246     public void Given_doFilter_Called_And_Path_Contains_subs_And_getSubId_Throws_NumberFormatException_then_Not_Found_response_returned() throws Exception{\r
247             setRequestMocking("PUT", "subs");\r
248             when(request.getPathInfo()).thenReturn("5/");\r
249             cadiFilter.doFilter(request, response, chain);\r
250             verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());\r
251 \r
252     }\r
253 \r
254     @Test\r
255     public void Given_doFilter_called_And_FeedId_Throws_Set_Then_Not_Found_Response_Returned () throws Exception{\r
256         setRequestMocking("PUT", "feeds");\r
257         when(request.getPathInfo()).thenReturn("//5");\r
258         cadiFilter.doFilter(request, response, chain);\r
259         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());\r
260     }\r
261 \r
262     private void setRequestMocking(String method, String servletPath)\r
263     {\r
264         when(request.getRemoteAddr()).thenReturn(null);\r
265         when(request.getHeader(BEHALF_HEADER)).thenReturn(null);\r
266         when(request.getAttribute(BaseServlet.CERT_ATTRIBUTE)).thenReturn(null);\r
267         when(request.getMethod()).thenReturn(method);\r
268         when(request.getServletPath()).thenReturn(servletPath);\r
269     }\r
270 \r
271     }\r