diff options
author | esobmar <mariusz.sobucki@ericsson.com> | 2018-09-07 16:39:17 +0100 |
---|---|---|
committer | Mariusz Sobucki <mariusz.sobucki@ericsson.com> | 2018-09-12 13:47:38 +0100 |
commit | 8f2e78cc17997beb2cf410a26b6ff3e1d2644b4e (patch) | |
tree | 064460aeb88ccdf539fda32c2fd942c735919289 /datarouter-prov/src/test/java | |
parent | fbbee97870e579bc91556d72ab1d3a96f2a9db94 (diff) |
Unit Tests RouteServletTest
Change-Id: Ic97670397329d02e861569421f01461cd204806e
Signed-off-by: Mariusz Sobucki <mariusz.sobucki@ericsson.com>
Issue-ID: DMAAP-101
Diffstat (limited to 'datarouter-prov/src/test/java')
-rwxr-xr-x | datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java | 249 |
1 files changed, 55 insertions, 194 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java index f1082ba3..226dae04 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java @@ -23,21 +23,21 @@ package org.onap.dmaap.datarouter.provisioning; -import org.apache.commons.lang3.reflect.FieldUtils; -import org.json.JSONObject; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.onap.dmaap.datarouter.provisioning.beans.*; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.SortedSet; -import java.util.TreeSet; + import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Matchers.*; import static org.mockito.Mockito.mock; @@ -45,12 +45,10 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.IngressRoute", - "org.onap.dmaap.datarouter.provisioning.beans.EgressRoute", - "org.onap.dmaap.datarouter.provisioning.beans.NodeClass", - "org.onap.dmaap.datarouter.provisioning.beans.NetworkRoute"}) -public class RouteServletTest extends DrServletTestBase -{ +public class RouteServletTest { + + private static EntityManagerFactory emf; + private static EntityManager em; private RouteServlet routeServlet; @Mock @@ -59,53 +57,55 @@ public class RouteServletTest extends DrServletTestBase @Mock private HttpServletResponse response; + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before public void setUp() throws Exception { - super.setUp(); - setPokerToNotCreateTimersWhenDeleteFeedIsCalled(); - setRouteToReturnValid(); routeServlet = new RouteServlet(); } @Test - public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Authorized() throws Exception { - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); + public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() + throws Exception { + when(request.getRemoteAddr()).thenReturn("stub_addr"); routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_DELETE_And_Ingress_Route_Does_Not_Exist_In_Path() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Ingress_Route_Does_Not_Exist_In_Path_Then_Route_Does_Not_Exist_Is_Returned() + throws Exception { when(request.getPathInfo()).thenReturn("/ingress/3/internal/route/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_FeedID() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_FeedID_Then_Feed_Not_Found_Is_Returned() + throws Exception { when(request.getPathInfo()).thenReturn("/ingress/feedID/internal/route/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Sequence_Number() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Sequence_Number_Then_Invalid_Sequence_Is_Returned() + throws Exception { when(request.getPathInfo()).thenReturn("/ingress/feedID/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -113,11 +113,6 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_DELETE_And_Ingress_Path_Contains_Invalid_Number_Of_Arguments() throws Exception { when(request.getPathInfo()).thenReturn("/ingress/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -125,11 +120,6 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_DELETE_And_Egress_Route_Does_Not_Exist_In_Path() throws Exception { when(request.getPathInfo()).thenReturn("/egress/3"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -137,11 +127,6 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_DELETE_And_Egress_Path_Contains_Invalid_SubID() throws Exception { when(request.getPathInfo()).thenReturn("/egress/subID"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -149,11 +134,6 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_DELETE_And_Egress_Path_Contains_Invalid_Number_Of_Arguments() throws Exception { when(request.getPathInfo()).thenReturn("/egress/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -161,37 +141,20 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_DELETE_And_Network_Path_Contains_Invalid_Number_Of_Arguments() throws Exception { when(request.getPathInfo()).thenReturn("/network/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_DELETE_And_Deletable_Is_Null() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Deletable_Is_Null_Then_Bad_Url_Is_Returned() throws Exception { when(request.getPathInfo()).thenReturn("/route/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - - @Override - protected boolean doDelete(Deleteable bean) { - return true; - } - }; routeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @Test public void Given_Request_Is_HTTP_DELETE_And_Fails() throws Exception { - when(request.getPathInfo()).thenReturn("/network/subID/route"); - PowerMockito.mockStatic(NodeClass.class); - PowerMockito.when(NodeClass.normalizeNodename(anyString())).thenReturn("stub_val"); + when(request.getPathInfo()).thenReturn("/network/node01/node02"); RouteServlet routeServlet = new RouteServlet() { protected boolean isAuthorizedForInternal(HttpServletRequest req) { return true; @@ -203,12 +166,13 @@ public class RouteServletTest extends DrServletTestBase } }; routeServlet.doDelete(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); + verify(response) + .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); } @Test public void Given_Request_Is_HTTP_GET_And_Is_Not_Authorized() throws Exception { - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); + when(request.getRemoteAddr()).thenReturn("stub_addr"); routeServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @@ -216,11 +180,6 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_GET_And_Path_Does_Not_Start_With_Valid_Route() throws Exception { when(request.getPathInfo()).thenReturn("/route/"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -229,14 +188,8 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_GET_And_Path_Equals_Ingress_And_Get_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/ingress/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -244,14 +197,8 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_GET_And_Path_Equals_Egress_And_Get_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/egress/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -259,32 +206,21 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_GET_And_Ingress_Path_Equals_Network_And_Get_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/network/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @Test public void Given_Request_Is_HTTP_PUT_And_Is_Not_Authorized() throws Exception { - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); + when(request.getRemoteAddr()).thenReturn("stub_addr"); routeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test public void Given_Request_Is_HTTP_PUT_And_Contains_Bad_URL() throws Exception { - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -292,55 +228,38 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_POST_And_Is_Not_Authorized() throws Exception { - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); + when(request.getRemoteAddr()).thenReturn("stub_addr"); routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_POST_And_Ingress_Path_Starts_With_Ingress_And_Contains_Invalid_Arguments() throws Exception { + public void Given_Request_Is_HTTP_POST_And_Ingress_Path_Starts_With_Ingress_And_Contains_Invalid_Arguments() + throws Exception { when(request.getPathInfo()).thenReturn("/ingress/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); when(request.getParameter("feed")).thenReturn("3"); when(request.getParameter("user")).thenReturn(null); when(request.getParameter("subnet")).thenReturn(null); when(request.getParameter("nodepatt")).thenReturn(null); when(request.getParameter("seq")).thenReturn(null); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_EgressRoute_Already_Exists() throws Exception { + public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_EgressRoute_Already_Exists() + throws Exception { when(request.getPathInfo()).thenReturn("/egress/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); - when(request.getParameter("sub")).thenReturn("3"); - EgressRoute e = mock(EgressRoute.class); - PowerMockito.when(EgressRoute.getEgressRoute(anyInt())).thenReturn(e); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; + when(request.getParameter("sub")).thenReturn("1"); routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_Contains_Invalid_Arguments() throws Exception { + public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Egress_And_Contains_Invalid_Arguments() + throws Exception { when(request.getPathInfo()).thenReturn("/egress/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); when(request.getParameter("sub")).thenReturn("3"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } @@ -348,12 +267,6 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Network_And_Is_Missing_Arguments() throws Exception { when(request.getPathInfo()).thenReturn("/network/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } @@ -361,20 +274,9 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_POST_And_Path_Starts_With_Network_And_Route_Already_Exists() throws Exception { when(request.getPathInfo()).thenReturn("/network/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); when(request.getParameter("from")).thenReturn("stub_from"); when(request.getParameter("to")).thenReturn("stub_to"); when(request.getParameter("via")).thenReturn("stub_via"); - PowerMockito.mockStatic(NodeClass.class); - PowerMockito.when(NodeClass.normalizeNodename(anyString())).thenReturn("stub_val"); - SortedSet<NetworkRoute> networkSet = new TreeSet(); - networkSet.add(mock(NetworkRoute.class)); - PowerMockito.when(NetworkRoute.getAllNetworkRoutes()).thenReturn(networkSet); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } @@ -382,17 +284,9 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_POST_And_Path_URL_Is_Null() throws Exception { when(request.getPathInfo()).thenReturn("/route/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); when(request.getParameter("from")).thenReturn("stub_from"); when(request.getParameter("to")).thenReturn("stub_to"); when(request.getParameter("via")).thenReturn("stub_via"); - PowerMockito.mockStatic(NodeClass.class); - PowerMockito.when(NodeClass.normalizeNodename(anyString())).thenReturn("stub_val"); - RouteServlet routeServlet = new RouteServlet() { - protected boolean isAuthorizedForInternal(HttpServletRequest req) { - return true; - } - }; routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @@ -400,12 +294,9 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_POST_And_Fails() throws Exception { when(request.getPathInfo()).thenReturn("/network/"); - when(request.getRemoteAddr()).thenReturn("stub_addr"); - when(request.getParameter("from")).thenReturn("stub_from"); - when(request.getParameter("to")).thenReturn("stub_to"); - when(request.getParameter("via")).thenReturn("stub_via"); - PowerMockito.mockStatic(NodeClass.class); - PowerMockito.when(NodeClass.normalizeNodename(anyString())).thenReturn("stub_val"); + when(request.getParameter("from")).thenReturn("node01"); + when(request.getParameter("to")).thenReturn("node02"); + when(request.getParameter("via")).thenReturn("node03"); RouteServlet routeServlet = new RouteServlet() { protected boolean isAuthorizedForInternal(HttpServletRequest req) { return true; @@ -416,39 +307,9 @@ public class RouteServletTest extends DrServletTestBase return false; } }; - routeServlet.doPost(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); - } - private void setRouteToReturnValid() throws IllegalAccessException { - PowerMockito.mockStatic(IngressRoute.class); - PowerMockito.when(IngressRoute.getIngressRoute(anyInt(), anyString(), anyString())).thenReturn(null); - SortedSet<IngressRoute> ingressSet = new TreeSet(); - IngressRoute ingressRoute = mock(IngressRoute.class); - JSONObject joIngress = mock(JSONObject.class); - when(joIngress.toString()).thenReturn("{}"); - when(ingressRoute.asJSONObject()).thenReturn(joIngress); - ingressSet.add(ingressRoute); - PowerMockito.when(IngressRoute.getAllIngressRoutes()).thenReturn(ingressSet); - - PowerMockito.mockStatic(EgressRoute.class); - PowerMockito.when(EgressRoute.getEgressRoute(anyInt())).thenReturn(null); - SortedSet<EgressRoute> egressSet = new TreeSet(); - EgressRoute egressRoute = mock(EgressRoute.class); - JSONObject joEgress = mock(JSONObject.class); - when(joEgress.toString()).thenReturn("{}"); - when(egressRoute.asJSONObject()).thenReturn(joEgress); - egressSet.add(egressRoute); - PowerMockito.when(EgressRoute.getAllEgressRoutes()).thenReturn(egressSet); - - PowerMockito.mockStatic(NetworkRoute.class); - SortedSet<NetworkRoute> networkSet = new TreeSet(); - PowerMockito.when(NetworkRoute.getAllNetworkRoutes()).thenReturn(networkSet); - - } - - private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception { - Poker poker = mock(Poker.class); - FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true); + routeServlet.doPost(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); } } |