1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning;
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;
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;
47 @RunWith(PowerMockRunner.class)
48 public class RouteServletTest {
50 private static EntityManagerFactory emf;
51 private static EntityManager em;
52 private RouteServlet routeServlet;
55 private HttpServletRequest request;
58 private HttpServletResponse response;
61 public static void init() {
62 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
63 em = emf.createEntityManager();
65 "org.onap.dmaap.datarouter.provserver.properties",
66 "src/test/resources/h2Database.properties");
70 public static void tearDownClass() {
77 public void setUp() throws Exception {
78 routeServlet = new RouteServlet();
82 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
84 when(request.getRemoteAddr()).thenReturn("stub_addr");
85 routeServlet.doDelete(request, response);
86 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
90 public void Given_Request_Is_HTTP_DELETE_And_Ingress_Route_Does_Not_Exist_In_Path_Then_Route_Does_Not_Exist_Is_Returned()
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)));
98 public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_FeedID_Then_Feed_Not_Found_Is_Returned()
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)));
106 public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Sequence_Number_Then_Invalid_Sequence_Is_Returned()
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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) {
164 protected boolean doDelete(Deleteable bean) {
168 routeServlet.doDelete(request, response);
170 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
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)));
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)));
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));
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));
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));
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)));
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)));
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)));
237 public void Given_Request_Is_HTTP_POST_And_Ingress_Path_Starts_With_Ingress_And_Contains_Invalid_Arguments()
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)));
250 public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_EgressRoute_Already_Exists()
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)));
259 public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_Contains_Invalid_Arguments()
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)));
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)));
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)));
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)));
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) {
306 protected boolean doInsert(Insertable bean) {
311 routeServlet.doPost(request, response);
313 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));