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