226dae04b71c27799b649a8459ce7b306c37195d
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / RouteServletTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning;
25
26 import javax.persistence.EntityManager;
27 import javax.persistence.EntityManagerFactory;
28 import javax.persistence.Persistence;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.onap.dmaap.datarouter.provisioning.beans.*;
36 import org.powermock.modules.junit4.PowerMockRunner;
37 import javax.servlet.ServletOutputStream;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import static org.hamcrest.Matchers.notNullValue;
42 import static org.mockito.Matchers.*;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
46
47 @RunWith(PowerMockRunner.class)
48 public class RouteServletTest {
49
50     private static EntityManagerFactory emf;
51     private static EntityManager em;
52     private RouteServlet routeServlet;
53
54     @Mock
55     private HttpServletRequest request;
56
57     @Mock
58     private HttpServletResponse response;
59
60     @BeforeClass
61     public static void init() {
62         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
63         em = emf.createEntityManager();
64         System.setProperty(
65                 "org.onap.dmaap.datarouter.provserver.properties",
66                 "src/test/resources/h2Database.properties");
67     }
68
69     @AfterClass
70     public static void tearDownClass() {
71         em.clear();
72         em.close();
73         emf.close();
74     }
75
76     @Before
77     public void setUp() throws Exception {
78         routeServlet = new RouteServlet();
79     }
80
81     @Test
82     public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
83             throws Exception {
84         when(request.getRemoteAddr()).thenReturn("stub_addr");
85         routeServlet.doDelete(request, response);
86         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
87     }
88
89     @Test
90     public void Given_Request_Is_HTTP_DELETE_And_Ingress_Route_Does_Not_Exist_In_Path_Then_Route_Does_Not_Exist_Is_Returned()
91             throws Exception {
92         when(request.getPathInfo()).thenReturn("/ingress/3/internal/route/");
93         routeServlet.doDelete(request, response);
94         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
95     }
96
97     @Test
98     public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_FeedID_Then_Feed_Not_Found_Is_Returned()
99             throws Exception {
100         when(request.getPathInfo()).thenReturn("/ingress/feedID/internal/route/");
101         routeServlet.doDelete(request, response);
102         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
103     }
104
105     @Test
106     public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Sequence_Number_Then_Invalid_Sequence_Is_Returned()
107             throws Exception {
108         when(request.getPathInfo()).thenReturn("/ingress/feedID/");
109         routeServlet.doDelete(request, response);
110         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
111     }
112
113     @Test
114     public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Number_Of_Arguments() throws Exception {
115         when(request.getPathInfo()).thenReturn("/ingress/");
116         routeServlet.doDelete(request, response);
117         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
118     }
119
120     @Test
121     public void Given_Request_Is_HTTP_DELETE_And_Egress_Route_Does_Not_Exist_In_Path() throws Exception {
122         when(request.getPathInfo()).thenReturn("/egress/3");
123         routeServlet.doDelete(request, response);
124         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
125     }
126
127     @Test
128     public void Given_Request_Is_HTTP_DELETE_And_Egress_Path_Contains_Invalid_SubID() throws Exception {
129         when(request.getPathInfo()).thenReturn("/egress/subID");
130         routeServlet.doDelete(request, response);
131         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
132     }
133
134     @Test
135     public void Given_Request_Is_HTTP_DELETE_And_Egress_Path_Contains_Invalid_Number_Of_Arguments() throws Exception {
136         when(request.getPathInfo()).thenReturn("/egress/");
137         routeServlet.doDelete(request, response);
138         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
139     }
140
141     @Test
142     public void Given_Request_Is_HTTP_DELETE_And_Network_Path_Contains_Invalid_Number_Of_Arguments() throws Exception {
143         when(request.getPathInfo()).thenReturn("/network/");
144         routeServlet.doDelete(request, response);
145         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
146     }
147
148     @Test
149     public void Given_Request_Is_HTTP_DELETE_And_Deletable_Is_Null_Then_Bad_Url_Is_Returned() throws Exception {
150         when(request.getPathInfo()).thenReturn("/route/");
151         routeServlet.doDelete(request, response);
152         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
153     }
154
155     @Test
156     public void Given_Request_Is_HTTP_DELETE_And_Fails() throws Exception {
157         when(request.getPathInfo()).thenReturn("/network/node01/node02");
158         RouteServlet routeServlet = new RouteServlet() {
159             protected boolean isAuthorizedForInternal(HttpServletRequest req) {
160                 return true;
161             }
162
163             @Override
164             protected boolean doDelete(Deleteable bean) {
165                 return false;
166             }
167         };
168         routeServlet.doDelete(request, response);
169         verify(response)
170                 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
171     }
172
173     @Test
174     public void Given_Request_Is_HTTP_GET_And_Is_Not_Authorized() throws Exception {
175         when(request.getRemoteAddr()).thenReturn("stub_addr");
176         routeServlet.doGet(request, response);
177         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
178     }
179
180     @Test
181     public void Given_Request_Is_HTTP_GET_And_Path_Does_Not_Start_With_Valid_Route() throws Exception {
182         when(request.getPathInfo()).thenReturn("/route/");
183         routeServlet.doGet(request, response);
184         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
185     }
186
187
188     @Test
189     public void Given_Request_Is_HTTP_GET_And_Path_Equals_Ingress_And_Get_Succeeds() throws Exception {
190         when(request.getPathInfo()).thenReturn("/ingress/");
191         ServletOutputStream outStream = mock(ServletOutputStream.class);
192         when(response.getOutputStream()).thenReturn(outStream);
193         routeServlet.doGet(request, response);
194         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
195     }
196
197     @Test
198     public void Given_Request_Is_HTTP_GET_And_Path_Equals_Egress_And_Get_Succeeds() throws Exception {
199         when(request.getPathInfo()).thenReturn("/egress/");
200         ServletOutputStream outStream = mock(ServletOutputStream.class);
201         when(response.getOutputStream()).thenReturn(outStream);
202         routeServlet.doGet(request, response);
203         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
204     }
205
206     @Test
207     public void Given_Request_Is_HTTP_GET_And_Ingress_Path_Equals_Network_And_Get_Succeeds() throws Exception {
208         when(request.getPathInfo()).thenReturn("/network/");
209         ServletOutputStream outStream = mock(ServletOutputStream.class);
210         when(response.getOutputStream()).thenReturn(outStream);
211         routeServlet.doGet(request, response);
212         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
213     }
214
215     @Test
216     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Authorized() throws Exception {
217         when(request.getRemoteAddr()).thenReturn("stub_addr");
218         routeServlet.doPut(request, response);
219         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
220     }
221
222     @Test
223     public void Given_Request_Is_HTTP_PUT_And_Contains_Bad_URL() throws Exception {
224         routeServlet.doPut(request, response);
225         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
226     }
227
228
229     @Test
230     public void Given_Request_Is_HTTP_POST_And_Is_Not_Authorized() throws Exception {
231         when(request.getRemoteAddr()).thenReturn("stub_addr");
232         routeServlet.doPost(request, response);
233         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
234     }
235
236     @Test
237     public void Given_Request_Is_HTTP_POST_And_Ingress_Path_Starts_With_Ingress_And_Contains_Invalid_Arguments()
238             throws Exception {
239         when(request.getPathInfo()).thenReturn("/ingress/");
240         when(request.getParameter("feed")).thenReturn("3");
241         when(request.getParameter("user")).thenReturn(null);
242         when(request.getParameter("subnet")).thenReturn(null);
243         when(request.getParameter("nodepatt")).thenReturn(null);
244         when(request.getParameter("seq")).thenReturn(null);
245         routeServlet.doPost(request, response);
246         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
247     }
248
249     @Test
250     public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_EgressRoute_Already_Exists()
251             throws Exception {
252         when(request.getPathInfo()).thenReturn("/egress/");
253         when(request.getParameter("sub")).thenReturn("1");
254         routeServlet.doPost(request, response);
255         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
256     }
257
258     @Test
259     public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_Contains_Invalid_Arguments()
260             throws Exception {
261         when(request.getPathInfo()).thenReturn("/egress/");
262         when(request.getParameter("sub")).thenReturn("3");
263         routeServlet.doPost(request, response);
264         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
265     }
266
267     @Test
268     public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Network_And_Is_Missing_Arguments() throws Exception {
269         when(request.getPathInfo()).thenReturn("/network/");
270         routeServlet.doPost(request, response);
271         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
272     }
273
274     @Test
275     public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Network_And_Route_Already_Exists() throws Exception {
276         when(request.getPathInfo()).thenReturn("/network/");
277         when(request.getParameter("from")).thenReturn("stub_from");
278         when(request.getParameter("to")).thenReturn("stub_to");
279         when(request.getParameter("via")).thenReturn("stub_via");
280         routeServlet.doPost(request, response);
281         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
282     }
283
284     @Test
285     public void Given_Request_Is_HTTP_POST_And_Path_URL_Is_Null() throws Exception {
286         when(request.getPathInfo()).thenReturn("/route/");
287         when(request.getParameter("from")).thenReturn("stub_from");
288         when(request.getParameter("to")).thenReturn("stub_to");
289         when(request.getParameter("via")).thenReturn("stub_via");
290         routeServlet.doPost(request, response);
291         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
292     }
293
294     @Test
295     public void Given_Request_Is_HTTP_POST_And_Fails() throws Exception {
296         when(request.getPathInfo()).thenReturn("/network/");
297         when(request.getParameter("from")).thenReturn("node01");
298         when(request.getParameter("to")).thenReturn("node02");
299         when(request.getParameter("via")).thenReturn("node03");
300         RouteServlet routeServlet = new RouteServlet() {
301             protected boolean isAuthorizedForInternal(HttpServletRequest req) {
302                 return true;
303             }
304
305             @Override
306             protected boolean doInsert(Insertable bean) {
307                 return false;
308             }
309         };
310
311         routeServlet.doPost(request, response);
312         verify(response)
313                 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
314     }
315 }