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 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;
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;
49 @RunWith(PowerMockRunner.class)
50 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
51 public class RouteServletTest {
53 private static EntityManagerFactory emf;
54 private static EntityManager em;
55 private RouteServlet routeServlet;
58 private HttpServletRequest request;
61 private HttpServletResponse response;
64 public static void init() {
65 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
66 em = emf.createEntityManager();
68 "org.onap.dmaap.datarouter.provserver.properties",
69 "src/test/resources/h2Database.properties");
73 public static void tearDownClass() {
80 public void setUp() throws Exception {
81 routeServlet = new RouteServlet();
85 public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
87 when(request.getRemoteAddr()).thenReturn("stub_addr");
88 routeServlet.doDelete(request, response);
89 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
93 public void Given_Request_Is_HTTP_DELETE_And_Ingress_Route_Does_Not_Exist_In_Path_Then_Route_Does_Not_Exist_Is_Returned()
95 when(request.getPathInfo()).thenReturn("/ingress/3/internal/route/");
96 routeServlet.doDelete(request, response);
97 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
101 public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_FeedID_Then_Feed_Not_Found_Is_Returned()
103 when(request.getPathInfo()).thenReturn("/ingress/feedID/internal/route/");
104 routeServlet.doDelete(request, response);
105 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
109 public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Sequence_Number_Then_Invalid_Sequence_Is_Returned()
111 when(request.getPathInfo()).thenReturn("/ingress/feedID/");
112 routeServlet.doDelete(request, response);
113 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
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());
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());
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());
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());
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());
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());
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) {
167 protected boolean doDelete(Deleteable bean) {
171 routeServlet.doDelete(request, response);
173 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), anyString());
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());
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());
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));
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));
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));
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());
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());
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());
240 public void Given_Request_Is_HTTP_POST_And_Ingress_Path_Starts_With_Ingress_And_Contains_Invalid_Arguments()
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());
253 public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_EgressRoute_Already_Exists()
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());
262 public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_Contains_Invalid_Arguments()
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());
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());
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());
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());
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) {
309 protected boolean doInsert(Insertable bean) {
314 routeServlet.doPost(request, response);
316 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), anyString());