From c50374709585766e887f349a139de0a6595c1ca1 Mon Sep 17 00:00:00 2001 From: econwar Date: Thu, 14 Feb 2019 09:37:44 +0000 Subject: Add optional API for PM Mapper Added new field to Subscriber class to keep files after published Added new Delete endpoint so that file can then be deleted Change-Id: Id72da67689a7ceda8ddd4997cd6349b981cb1cdb Issue-ID: DMAAP-981 Signed-off-by: econwar --- .../onap/dmaap/datarouter/node/DeliveryTest.java | 2 +- .../onap/dmaap/datarouter/node/NodeConfigTest.java | 1 + .../dmaap/datarouter/node/NodeServletTest.java | 116 +++++++++++++++++++-- 3 files changed, 112 insertions(+), 7 deletions(-) (limited to 'datarouter-node/src/test/java/org') diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTest.java index 8a7460e8..ae8cd2cd 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTest.java @@ -97,7 +97,7 @@ public class DeliveryTest { private DestInfo[] createDestInfoObjects() { DestInfo[] destInfos = new DestInfo[1]; - DestInfo destInfo = new DestInfo("node.datarouternew.com", "spool/s/0/1", "1", "logs/", "/subs/1", "user1", "Basic dXNlcjE6cGFzc3dvcmQx", false, true); + DestInfo destInfo = new DestInfo("node.datarouternew.com", "spool/s/0/1", "1", "logs/", "/subs/1", "user1", "Basic dXNlcjE6cGFzc3dvcmQx", false, true, false); destInfos[0] = destInfo; return destInfos; } diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java index 18108172..5092141a 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java @@ -210,6 +210,7 @@ public class NodeConfigTest { delivery.put("password", "password1"); delivery.put("use100", true); subscription.put("delivery", delivery); + subscription.put("privilegedSubscriber", false); subscriptions.put(subscription); provData.put("subscriptions", subscriptions); } diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java index 2a659aaf..065565d3 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java @@ -37,6 +37,8 @@ import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; import java.util.*; import static org.hamcrest.Matchers.notNullValue; @@ -49,6 +51,7 @@ import static org.mockito.Mockito.*; public class NodeServletTest { private NodeServlet nodeServlet; + private Delivery delivery; @Mock private HttpServletRequest request; @@ -59,18 +62,26 @@ public class NodeServletTest { ListAppender listAppender; @Before - public void setUp() throws Exception{ + public void setUp() throws Exception { listAppender = setTestLogger(); - nodeServlet = new NodeServlet(); setBehalfHeader("Stub_Value"); when(request.getPathInfo()).thenReturn("2"); when(request.isSecure()).thenReturn(true); + createFilesAndDirectories(); setUpConfig(); setUpNodeMainDelivery(); + delivery = mock(Delivery.class); + when(delivery.markTaskSuccess("spool/s/0/1", "dmaap-dr-node.1234567")).thenReturn(true); + nodeServlet = new NodeServlet(delivery); when(request.getHeader("Authorization")).thenReturn("User1"); when(request.getHeader("X-DMAAP-DR-PUBLISH-ID")).thenReturn("User1"); } + @AfterClass + public static void tearDown() { + deleteCreatedDirectories(); + } + @Test public void Given_Request_Is_HTTP_GET_And_Config_Is_Down_Then_Service_Unavailable_Response_Is_Generated() throws Exception { setNodeConfigManagerIsConfiguredToReturnFalse(); @@ -80,7 +91,7 @@ public class NodeServletTest { } @Test - public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_Internal_FetchProv_Then_No_Content_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_Internal_FetchProv_Then_No_Content_Response_Is_Generated() { when(request.getPathInfo()).thenReturn("/internal/fetchProv"); nodeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); @@ -88,7 +99,7 @@ public class NodeServletTest { } @Test - public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_ResetSubscription_Then_No_Content_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_Endpoint_Is_ResetSubscription_Then_No_Content_Response_Is_Generated() { when(request.getPathInfo()).thenReturn("/internal/resetSubscription/1"); nodeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); @@ -213,6 +224,48 @@ public class NodeServletTest { verifyEnteringExitCalled(listAppender); } + @Test + public void Given_Request_Is_HTTP_DELETE_File_With_Invalid_Endpoint_Then_Not_Found_Response_Is_Generated() throws Exception { + when(request.getPathInfo()).thenReturn("/delete/1"); + nodeServlet.doDelete(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_File_And_Is_Not_Privileged_Subscription_Then_Not_Found_Response_Is_Generated() throws Exception { + when(request.getPathInfo()).thenReturn("/delete/1/dmaap-dr-node.1234567"); + setUpConfigToReturnUnprivilegedSubscriber(); + nodeServlet.doDelete(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED)); + verifyEnteringExitCalled(listAppender); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_File_And_Subscription_Does_Not_Exist_Then_Not_Found_Response_Is_Generated() throws Exception { + when(request.getPathInfo()).thenReturn("/delete/1/dmaap-dr-node.1234567"); + setUpConfigToReturnNullOnIsDeletePermitted(); + nodeServlet.doDelete(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND)); + verifyEnteringExitCalled(listAppender); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_File_Then_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/delete/1/dmaap-dr-node.1234567"); + createFilesAndDirectories(); + nodeServlet.doDelete(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_File_And_File_Does_Not_Exist_Then_Not_Found_Response_Is_Generated() throws IOException { + when(request.getPathInfo()).thenReturn("/delete/1/nonExistingFile"); + nodeServlet.doDelete(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); + } private void setBehalfHeader(String headerValue) { when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn(headerValue); @@ -232,21 +285,45 @@ public class NodeServletTest { assertEquals(3, listAppender.list.size()); } - private void setUpConfig() throws IllegalAccessException{ + private void setUpConfig() throws IllegalAccessException { NodeConfigManager config = mock(NodeConfigManager.class); PowerMockito.mockStatic(NodeConfigManager.class); when(config.isShutdown()).thenReturn(false); when(config.isConfigured()).thenReturn(true); - when(config.getSpoolDir()).thenReturn("spool/dir"); + when(config.getSpoolDir()).thenReturn("spool/f"); + when(config.getSpoolBase()).thenReturn("spool"); when(config.getLogDir()).thenReturn("log/dir"); when(config.getPublishId()).thenReturn("User1"); when(config.isAnotherNode(anyString(), anyString())).thenReturn(true); when(config.getEventLogInterval()).thenReturn("40"); + when(config.isDeletePermitted("1")).thenReturn(true); + when(config.getAllDests()).thenReturn(new DestInfo[0]); FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true); FieldUtils.writeDeclaredStaticField(NodeMain.class, "nodeConfigManager", config, true); PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config); } + private void setUpConfigToReturnUnprivilegedSubscriber() throws IllegalAccessException { + NodeConfigManager config = mock(NodeConfigManager.class); + PowerMockito.mockStatic(NodeConfigManager.class); + when(config.isShutdown()).thenReturn(false); + when(config.isConfigured()).thenReturn(true); + when(config.isDeletePermitted("1")).thenReturn(false); + FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true); + FieldUtils.writeDeclaredStaticField(NodeMain.class, "nodeConfigManager", config, true); + PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config); + } + + private void setUpConfigToReturnNullOnIsDeletePermitted() throws IllegalAccessException { + NodeConfigManager config = mock(NodeConfigManager.class); + PowerMockito.mockStatic(NodeConfigManager.class); + when(config.isShutdown()).thenReturn(false); + when(config.isConfigured()).thenReturn(true); + when(config.isDeletePermitted("1")).thenThrow(new NullPointerException()); + FieldUtils.writeDeclaredStaticField(NodeServlet.class, "config", config, true); + FieldUtils.writeDeclaredStaticField(NodeMain.class, "nodeConfigManager", config, true); + PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config); + } private void setUpNodeMainDelivery() throws IllegalAccessException{ Delivery delivery = mock(Delivery.class); @@ -313,4 +390,31 @@ public class NodeServletTest { when(request.getHeaders("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn(behalfHeader); when(request.getHeaders("X-DMAAP-DR-META")).thenReturn(metaDataHeader); } + + private void createFilesAndDirectories() throws IOException { + File nodeDir = new File("spool/n/172.0.0.1"); + File spoolDir = new File("spool/s/0/1"); + File dataFile = new File("spool/s/0/1/dmaap-dr-node.1234567"); + File metaDataFile = new File("spool/s/0/1/dmaap-dr-node.1234567.M"); + nodeDir.mkdirs(); + spoolDir.mkdirs(); + dataFile.createNewFile(); + metaDataFile.createNewFile(); + } + + private static void deleteCreatedDirectories() { + File spoolDir = new File("spool"); + delete(spoolDir); + } + + private static void delete(File file) { + if (file.isDirectory()) { + for (File f: file.listFiles()) { + delete(f); + } + } + if (!file.delete()) { + System.out.println("Failed to delete: " + file); + } + } } -- cgit 1.2.3-korg