diff options
Diffstat (limited to 'datarouter-node/src/test')
5 files changed, 149 insertions, 15 deletions
diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DRNodeCadiFilterTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DRNodeCadiFilterTest.java new file mode 100644 index 00000000..f6737b1e --- /dev/null +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DRNodeCadiFilterTest.java @@ -0,0 +1,121 @@ +/**-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dmaap.datarouter.node;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.onap.aaf.cadi.PropAccess;
+import org.onap.aaf.cadi.filter.CadiFilter;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.api.support.membermodification.MemberMatcher;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+import static org.mockito.Mockito.*;
+
+@SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
+@PrepareForTest({CadiFilter.class})
+@RunWith(PowerMockRunner.class)
+public class DRNodeCadiFilterTest
+{
+
+ @Mock
+ private PropAccess access;
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Mock
+ private HttpServletResponse response;
+
+ @Mock
+ private FilterChain chain;
+
+ private DRNodeCadiFilter cadiFilter;
+
+
+ @Before
+ public void setUp() throws ServletException {
+ cadiFilter = new DRNodeCadiFilter(false, access);
+ }
+
+ @Test
+ public void Given_doFilter_Called_And_Method_Is_GET_And_AAF_DB_Instance_Is_NULL_Then_Chain_doFilter_Called() throws Exception {
+ PowerMockito.mockStatic(NodeConfigManager.class);
+ NodeConfigManager config = mock(NodeConfigManager.class);
+
+ PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
+ PowerMockito.when(config.getAafInstance("/other/5")).thenReturn("legacy");
+ when(request.getPathInfo()).thenReturn("/publish/5");
+ when(request.getMethod()).thenReturn("GET");
+ cadiFilter.doFilter(request,response,chain);
+ verify(chain, times(1)).doFilter(request, response);
+ }
+
+ @Test
+ public void Given_doFilter_Called_And_Method_Is_GET_And_Path_Includes_Internal_Then_Chain_doFilter_Called() throws Exception {
+ PowerMockito.mockStatic(NodeConfigManager.class);
+ NodeConfigManager config = mock(NodeConfigManager.class);
+
+ PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
+ PowerMockito.when(config.getAafInstance("/other/5")).thenReturn("legacy");
+ when(request.getPathInfo()).thenReturn("/internal/5");
+ when(request.getMethod()).thenReturn("GET");
+ cadiFilter.doFilter(request,response,chain);
+ verify(chain, times(1)).doFilter(request, response);
+ }
+
+ @Test
+ public void Given_doFilter_Called_And_Method_Is_GET_And_AAF_DB_Is_Not_Null_Then_Super_doFilter_Called() throws Exception {
+ PowerMockito.mockStatic(NodeConfigManager.class);
+ NodeConfigManager config = mock(NodeConfigManager.class);
+
+ PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
+ PowerMockito.when(config.getAafInstance("5")).thenReturn("EXISTS");
+ when(request.getPathInfo()).thenReturn("/publish/5/fileId");
+ when(request.getMethod()).thenReturn("GET");
+ PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));
+ cadiFilter.doFilter(request,response,chain);
+ verify(chain, times(0)).doFilter(request, response);
+ }
+
+ @Test
+ public void Given_getFileid_Called_And_SendError_Fails_Then_Throw_IOException_And_Call_chain_doFilter() throws Exception {
+ PowerMockito.mockStatic(NodeConfigManager.class);
+ NodeConfigManager config = mock(NodeConfigManager.class);
+
+ PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);
+ when(request.getPathInfo()).thenReturn("/publish/5");
+ when(request.getMethod()).thenReturn("DELETE");
+ doThrow(new IOException()).when(response).sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid request URI. Expecting <feed-publishing-url>/<fileid>. Possible missing fileid.");
+ cadiFilter.doFilter(request,response,chain);
+ verify(chain, times(1)).doFilter(request, response);
+ }
+}
diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryQueueTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryQueueTest.java index 97904a5e..9a3d82e5 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryQueueTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryQueueTest.java @@ -22,6 +22,7 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; + import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Before; import org.junit.Test; @@ -29,12 +30,10 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.powermock.modules.junit4.PowerMockRunner; -import static org.junit.Assert.*; import java.io.File; - - -import static org.mockito.Mockito.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) public class DeliveryQueueTest { @@ -55,7 +54,7 @@ public class DeliveryQueueTest { } @Test - public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() throws Exception { + public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() { when(destInfo.getSpool()).thenReturn("tmp"); File file = new File("tmp"); assertTrue(file.exists()); @@ -63,14 +62,14 @@ public class DeliveryQueueTest { } @Test - public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception{ + public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception { FieldUtils.writeField(deliveryQueue,"failed",true,true); FieldUtils.writeField(deliveryQueue,"resumetime",System.currentTimeMillis()*2,true); assertNull(deliveryQueue.peekNext()); } @Test - public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception{ + public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception { prepareFiles(); when(destInfo.getSpool()).thenReturn(dirPath); deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo); @@ -81,19 +80,19 @@ public class DeliveryQueueTest { } @Test - public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() throws Exception{ + public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() { long rc = deliveryQueue.cancelTask("123.node.datarouternew.com"); assertEquals(0, rc); } - private void prepareFiles() throws Exception{ + private void prepareFiles() throws Exception { createFolder(dirPath); createFile(FileName1, dirPath); String[] files = new String[2]; files[0] = dirPath + FileName1; } - private void createFolder(String dirName) throws Exception{ + private void createFolder(String dirName) throws Exception { String dirPath = dirName; File newDirectory = new File(dirPath); @@ -101,13 +100,13 @@ public class DeliveryQueueTest { if (isCreated) { System.out.println("1. Successfully created directories, path: " + newDirectory.getCanonicalPath()); } else if (newDirectory.exists()) { - System.out.printf("1. Directory path already exist, path: " + newDirectory.getCanonicalPath()); + System.out.print("1. Directory path already exist, path: " + newDirectory.getCanonicalPath()); } else { System.out.println("1. Unable to create directory"); } } - private void createFile( String file, String dir) throws Exception{ + private void createFile(String file, String dir) throws Exception { String FileName = file; String dirPath = dir; 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 4ca907f7..efa43e11 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, false, false); + DestInfo destInfo = new DestInfo("node.datarouternew.com", "spool/s/0/1", "1", "logs/", "/subs/1", "user1", "Basic dXNlcjE6cGFzc3dvcmQx", false, true, false, false, 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 4b614d56..7dddd67a 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 @@ -193,6 +193,7 @@ public class NodeConfigTest { endpointAddrs.put("172.0.0.1"); auth.put("endpoint_addrs", endpointAddrs); feed.put("authorization", auth); + feed.put("aaf_instance", "legacy"); feeds.put(feed); provData.put("feeds", feeds); } @@ -211,6 +212,7 @@ public class NodeConfigTest { delivery.put("use100", true); subscription.put("delivery", delivery); subscription.put("privilegedSubscriber", false); + subscription.put("follow_redirect", false); subscription.put("decompress", 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 065565d3..99e34c6f 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 @@ -59,7 +59,9 @@ public class NodeServletTest { @Mock private HttpServletResponse response; - ListAppender<ILoggingEvent> listAppender; + private ListAppender<ILoggingEvent> listAppender; + + private NodeConfigManager config = mock(NodeConfigManager.class); @Before public void setUp() throws Exception { @@ -216,6 +218,17 @@ public class NodeServletTest { } @Test + public void Given_Request_Is_HTTP_PUT_On_Publish_On_AAF_Feed_And_Cadi_Enabled_And_No_Permissions_Then_Forbidden_Response_Is_Generated() throws Exception { + when(config.getCadiEnabeld()).thenReturn(true); + when(config.getAafInstance("1")).thenReturn("*"); + when(request.getPathInfo()).thenReturn("/publish/1/fileName"); + setHeadersForValidRequest(true); + nodeServlet.doPut(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); + } + + @Test public void Given_Request_Is_HTTP_DELETE_On_Publish_With_Meta_Data_Malformed_Then_Bad_Request_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/publish/1/fileName"); setHeadersForValidRequest(false); @@ -286,7 +299,6 @@ public class NodeServletTest { } private void setUpConfig() throws IllegalAccessException { - NodeConfigManager config = mock(NodeConfigManager.class); PowerMockito.mockStatic(NodeConfigManager.class); when(config.isShutdown()).thenReturn(false); when(config.isConfigured()).thenReturn(true); |