From 5fc125c5159ae84e211ff2d8b03cd43475493142 Mon Sep 17 00:00:00 2001 From: econwar Date: Thu, 30 Aug 2018 06:53:01 +0000 Subject: Add Tests to InternalServletTest Change-Id: I836844b6b1f8779c5fb631fcf6593b798a09b599 Signed-off-by: Conor Ward Issue-ID: DMAAP-101 --- .../datarouter/provisioning/DrServletTestBase.java | 4 +-- .../provisioning/InternalServletTest.java | 41 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) mode change 100755 => 100644 datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java (limited to 'datarouter-prov/src') diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java index 414fc185..c7f639ed 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java @@ -38,8 +38,8 @@ public class DrServletTestBase { public void setUp() throws Exception { Properties props = new Properties(); props.setProperty("org.onap.dmaap.datarouter.provserver.isaddressauthenabled", "false"); - props.setProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir", "datarouter-prov/unit-test-logs"); - props.setProperty("org.onap.dmaap.datarouter.provserver.spooldir", "resources/spooldir"); + props.setProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir", "unit-test-logs"); + props.setProperty("org.onap.dmaap.datarouter.provserver.spooldir", "unit-test-logs/spool"); props.setProperty("org.onap.dmaap.datarouter.provserver.https.relaxation", "false"); FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true); 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 97900d4d..5f6b7ae3 --- 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 @@ -31,6 +31,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; +import java.io.File; import java.net.InetAddress; import java.util.HashMap; import java.util.Map; @@ -153,6 +154,19 @@ public class InternalServletTest extends DrServletTestBase { .sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class))); } + @Test + 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 testFile = new File("unit-test-logs/testFile.txt"); + testFile.createNewFile(); + testFile.deleteOnExit(); + 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_Api_In_Endpoint_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/api/Key"); @@ -167,10 +181,7 @@ public class InternalServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_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); + mockLogfileLoader(); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -331,6 +342,21 @@ public class InternalServletTest extends DrServletTestBase { verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); } + @Test + 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(); + mockLogfileLoader(); + 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() throws Exception { @@ -452,4 +478,11 @@ public class InternalServletTest extends DrServletTestBase { Map map = new HashMap<>(); FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true); } + + private void mockLogfileLoader() { + PowerMockito.mockStatic(LogfileLoader.class); + LogfileLoader logfileLoader = mock(LogfileLoader.class); + when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet()); + PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader); + } } -- cgit 1.2.3-korg