X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FInternalServletTest.java;h=1601e63daa96903b605decb5825ad8e7263f71de;hb=8552e04175c7091d0d748e0c0f4b14a0d9321b40;hp=f8342449bf4c12ebc6e81cc7a06a89368521ad6a;hpb=82882d01690cff6f78ea0a606ed6a68f9a65aa4d;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java old mode 100755 new mode 100644 index f8342449..1601e63d --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java @@ -22,53 +22,49 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning; -import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import java.io.File; import java.net.InetAddress; -import java.util.HashMap; -import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.reflect.FieldUtils; +import org.junit.AfterClass; +import org.junit.Assert; 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.authz.AuthorizationResponse; -import org.onap.dmaap.datarouter.authz.Authorizer; import org.onap.dmaap.datarouter.provisioning.beans.Deleteable; -import org.onap.dmaap.datarouter.provisioning.beans.Feed; import org.onap.dmaap.datarouter.provisioning.beans.Insertable; import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; -import org.onap.dmaap.datarouter.provisioning.beans.NodeClass; import org.onap.dmaap.datarouter.provisioning.beans.Parameters; -import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; -import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader; -import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet; +import org.onap.dmaap.datarouter.provisioning.utils.Poker; import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(LogRecord.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", - "org.onap.dmaap.datarouter.provisioning.beans.Parameters", - "org.onap.dmaap.datarouter.provisioning.beans.NodeClass", - "org.onap.dmaap.datarouter.provisioning.beans.Subscription", - "org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader"}) +@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"}) public class InternalServletTest extends DrServletTestBase { - + private static EntityManagerFactory emf; + private static EntityManager em; private InternalServlet internalServlet; @Mock @@ -77,12 +73,29 @@ public class InternalServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + ListAppender listAppender; + + @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(); - internalServlet = new InternalServlet(); - setAuthoriserToReturnRequestIsAuthorized(); - setUpValidAuthorisedRequest(); + listAppender = setTestLogger(InternalServlet.class); + internalServlet = new InternalServlet(); + setUpValidAuthorisedRequest(); } @Test @@ -91,7 +104,8 @@ public class InternalServletTest extends DrServletTestBase { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); internalServlet.doGet(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString()); + verifyEnteringExitCalled(listAppender); } @Test @@ -105,7 +119,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/halt"); when(request.isSecure()).thenReturn(false); when(request.getRemoteAddr()).thenReturn("127.0.0.1"); @@ -114,16 +128,17 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/fetchProv"); when(request.isSecure()).thenReturn(false); internalServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test - public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/prov"); when(request.getQueryString()).thenReturn(null); setPokerToNotCreateTimers(); @@ -134,7 +149,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/logs/"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); @@ -143,18 +158,23 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/logs/TestFile"); internalServlet.doGet(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_NO_CONTENT), anyString()); } @Test - public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_And_File_Exists_Then_Request_Returns_Ok() + throws Exception { + when(request.getPathInfo()).thenReturn("/logs/testFile.txt"); + File testDir = new File("unit-test-logs"); + File testFile = new File("unit-test-logs/testFile.txt"); + testDir.mkdirs(); + testFile.createNewFile(); + testFile.deleteOnExit(); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -162,13 +182,18 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Then_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/api/DELIVERY_MAX_RETRY_INTERVAL"); + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/drlogs/"); - PowerMockito.mockStatic(LogfileLoader.class); - LogfileLoader logfileLoader = mock(LogfileLoader.class); - when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet()); - PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -176,47 +201,47 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() + public void Given_Request_Is_HTTP_GET_With_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/incorrect/"); internalServlet.doGet(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); } @Test public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); internalServlet.doPut(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString()); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); internalServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), - argThat(notNullValue(String.class))); + anyString()); } @Test @@ -225,43 +250,53 @@ public class InternalServletTest extends DrServletTestBase { when(request.getPathInfo()).thenReturn("/incorrect"); internalServlet.doPut(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); } @Test public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); internalServlet.doDelete(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString()); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_With_LogRollInterval_Api_In_Endpoint_Request_Succeeds() { + when(request.getPathInfo()).thenReturn("/api/LOGROLL_INTERVAL"); + internalServlet.doDelete(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + Parameters p1 = Parameters.getParameter("NODES"); + Assert.assertEquals("{\"keyname\":\"NODES\",\"value\":\"dmaap-dr-node\"}", p1.asJSONObject().toString()); + Assert.assertEquals("PARAM: keyname=NODES, value=dmaap-dr-node", p1.toString()); } @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); internalServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), - argThat(notNullValue(String.class))); + anyString()); } @Test @@ -270,7 +305,7 @@ public class InternalServletTest extends DrServletTestBase { when(request.getPathInfo()).thenReturn("/incorrect"); internalServlet.doDelete(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); } @Test @@ -279,38 +314,36 @@ public class InternalServletTest extends DrServletTestBase { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); internalServlet.doPost(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString()); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(true); + when(request.getPathInfo()).thenReturn("/api/key"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(true); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); internalServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), - argThat(notNullValue(String.class))); + anyString()); } @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); when(request.getPathInfo()).thenReturn("/logs/"); internalServlet.doPost(request, response); @@ -318,8 +351,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Encoding")).thenReturn("not-supported"); when(request.getPathInfo()).thenReturn("/logs/"); internalServlet.doPost(request, response); @@ -327,8 +359,21 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() + public void Given_Request_Is_HTTP_POST_To_Logs_Then_Request_Succeeds() throws Exception { + when(request.getHeader("Content-Encoding")).thenReturn("gzip"); + when(request.getPathInfo()).thenReturn("/logs/"); + ServletInputStream inStream = mock(ServletInputStream.class); + when(request.getInputStream()).thenReturn(inStream); + File testDir = new File("unit-test-logs/spool"); + testDir.mkdirs(); + testDir.deleteOnExit(); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_CREATED)); + } + + @Test + public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); when(request.getPathInfo()).thenReturn("/drlogs/"); internalServlet.doPost(request, response); @@ -346,21 +391,20 @@ public class InternalServletTest extends DrServletTestBase { verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } + @Test + public void Given_Request_Is_HTTP_POST_To_Api_And_Request_Succeeds() { + when(request.getPathInfo()).thenReturn("/api/NEW_PARAM?val=blah"); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + @Test public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/incorrect/"); internalServlet.doPost(request, response); verify(response) - .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); - } - - private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException { - AuthorizationResponse authResponse = mock(AuthorizationResponse.class); - Authorizer authorizer = mock(Authorizer.class); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true); - when(authorizer.decide(request)).thenReturn(authResponse); - when(authResponse.isAuthorized()).thenReturn(true); + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); } private void setUpValidAuthorisedRequest() throws Exception { @@ -393,16 +437,6 @@ public class InternalServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true); } - private void setParametersToNotContactDb(boolean isPost) { - PowerMockito.mockStatic(Parameters.class); - Parameters parameters = mock(Parameters.class); - if (isPost) { - PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(null); - } else { - PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(parameters); - } - } - private InternalServlet internalServerSuccess() { InternalServlet internalServlet = new InternalServlet() { @@ -438,13 +472,4 @@ public class InternalServletTest extends DrServletTestBase { }; return internalServlet; } - - private void mockProvisioningParametersChanged() throws IllegalAccessException { - PowerMockito.mockStatic(Feed.class); - PowerMockito.mockStatic(Subscription.class); - PowerMockito.when(Feed.countActiveFeeds()).thenReturn(0); - PowerMockito.when(Subscription.countActiveSubscriptions()).thenReturn(0); - Map map = new HashMap<>(); - FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true); - } }