From 5a55b790e8afa3131fd5f894e5d1b1e036dc4cd1 Mon Sep 17 00:00:00 2001 From: econwar Date: Mon, 24 Jun 2019 11:03:58 +0000 Subject: [PATCH] Add test cases for 65% branch coverage Format the Test classes to adhere to ONAP Java style Change-Id: I9efad3caa532c4db73bb20327d0356b3af5ed2b1 Issue-ID: DMAAP-1195 Signed-off-by: econwar --- datarouter-node/pom.xml | 5 + .../org/onap/dmaap/datarouter/node/Delivery.java | 8 +- .../org/onap/dmaap/datarouter/node/LogManager.java | 12 +- .../org/onap/dmaap/datarouter/node/StatusLog.java | 2 +- .../datarouter/node/DRNodeCadiFilterTest.java | 47 +++-- .../dmaap/datarouter/node/DeliveryQueueTest.java | 172 +++++++++++++++-- .../dmaap/datarouter/node/DeliveryTaskTest.java | 71 +++++++ .../onap/dmaap/datarouter/node/DeliveryTest.java | 162 +++++++++------- .../onap/dmaap/datarouter/node/DestInfoTest.java | 77 ++++++++ .../onap/dmaap/datarouter/node/LogManagerTest.java | 109 +++++++++++ .../onap/dmaap/datarouter/node/NodeConfigTest.java | 203 ++++++++++----------- .../onap/dmaap/datarouter/node/NodeUtilsTest.java | 22 +-- .../onap/dmaap/datarouter/node/ProvDataTest.java | 153 ++++++++-------- .../onap/dmaap/datarouter/node/StatusLogTest.java | 6 +- 14 files changed, 754 insertions(+), 295 deletions(-) create mode 100644 datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java create mode 100644 datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DestInfoTest.java create mode 100644 datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/LogManagerTest.java diff --git a/datarouter-node/pom.xml b/datarouter-node/pom.xml index 06aa3fcf..a4de6f76 100755 --- a/datarouter-node/pom.xml +++ b/datarouter-node/pom.xml @@ -128,6 +128,11 @@ org.apache.commons commons-lang3 + + org.awaitility + awaitility + 3.1.6 + diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/Delivery.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/Delivery.java index df73c1e9..e4013240 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/Delivery.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/Delivery.java @@ -28,7 +28,7 @@ import com.att.eelf.configuration.EELFManager; import java.io.File; import java.util.ArrayList; import java.util.Arrays; -import java.util.Hashtable; +import java.util.HashMap; import java.util.Objects; /** @@ -50,7 +50,7 @@ public class Delivery { private int threads; private int curthreads; private NodeConfigManager config; - private Hashtable dqs = new Hashtable<>(); + private HashMap dqs = new HashMap<>(); private DeliveryQueue[] queues = new DeliveryQueue[0]; private int qpos = 0; private long nextcheck; @@ -178,7 +178,7 @@ public class Delivery { DestInfo[] alldis = config.getAllDests(); DeliveryQueue[] nqs = new DeliveryQueue[alldis.length]; qpos = 0; - Hashtable ndqs = new Hashtable<>(); + HashMap ndqs = new HashMap<>(); for (DestInfo di : alldis) { String spl = di.getSpool(); DeliveryQueue dq = dqs.get(spl); @@ -266,7 +266,7 @@ public class Delivery { return false; } - private static class DelItem implements Comparable { + static class DelItem implements Comparable { private String pubid; private String spool; diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java index 3277408c..903a0c5f 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java @@ -56,6 +56,8 @@ public class LogManager extends TimerTask { private String uploaddir; private String logdir; + private static final String EXCEPTION = "Exception"; + /** * Construct a log manager * @@ -68,7 +70,7 @@ public class LogManager extends TimerTask { isnodelog = Pattern.compile("node\\.log\\.\\d{8}").matcher(""); iseventlog = Pattern.compile("events-\\d{12}\\.log").matcher(""); } catch (Exception e) { - logger.error("Exception", e); + logger.error(EXCEPTION, e); } logdir = config.getLogDir(); uploaddir = logdir + "/.spool"; @@ -87,9 +89,12 @@ public class LogManager extends TimerTask { worker.poke(); } - private class Uploader extends Thread implements DeliveryQueueHelper { + public Uploader getWorker() { + return worker; + } + + class Uploader extends Thread implements DeliveryQueueHelper { - private static final String EXCEPTION = "Exception"; private static final String META = "/.meta"; private EELFLogger logger = EELFManager.getInstance().getLogger(Uploader.class); private DeliveryQueue dq; @@ -166,6 +171,7 @@ public class LogManager extends TimerTask { notify(); } + @Override public void run() { while (true) { scan(); diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/StatusLog.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/StatusLog.java index e6165588..2d02fa66 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/StatusLog.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/StatusLog.java @@ -53,7 +53,7 @@ public class StatusLog { private long nexttime; private OutputStream os; private long intvl; - private NodeConfigManager config = NodeConfigManager.getInstance(); + private static NodeConfigManager config = NodeConfigManager.getInstance(); private StatusLog() { } 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 index f6737b1e..bb367186 100644 --- 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 @@ -1,4 +1,4 @@ -/**- +/* * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * ================================================================================ @@ -20,6 +20,17 @@ package org.onap.dmaap.datarouter.node; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,19 +43,10 @@ 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 -{ +public class DRNodeCadiFilterTest { @Mock private PropAccess access; @@ -67,7 +69,8 @@ public class DRNodeCadiFilterTest } @Test - public void Given_doFilter_Called_And_Method_Is_GET_And_AAF_DB_Instance_Is_NULL_Then_Chain_doFilter_Called() throws Exception { + 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); @@ -75,12 +78,13 @@ public class DRNodeCadiFilterTest PowerMockito.when(config.getAafInstance("/other/5")).thenReturn("legacy"); when(request.getPathInfo()).thenReturn("/publish/5"); when(request.getMethod()).thenReturn("GET"); - cadiFilter.doFilter(request,response,chain); + 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 { + 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); @@ -88,12 +92,13 @@ public class DRNodeCadiFilterTest PowerMockito.when(config.getAafInstance("/other/5")).thenReturn("legacy"); when(request.getPathInfo()).thenReturn("/internal/5"); when(request.getMethod()).thenReturn("GET"); - cadiFilter.doFilter(request,response,chain); + 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 { + 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); @@ -102,20 +107,22 @@ public class DRNodeCadiFilterTest when(request.getPathInfo()).thenReturn("/publish/5/fileId"); when(request.getMethod()).thenReturn("GET"); PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class)); - cadiFilter.doFilter(request,response,chain); + 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 { + 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 /. Possible missing fileid."); - cadiFilter.doFilter(request,response,chain); + doThrow(new IOException()).when(response).sendError(HttpServletResponse.SC_NOT_FOUND, + "Invalid request URI. Expecting /. 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 fa868b26..6a5f219b 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 @@ -23,34 +23,48 @@ package org.onap.dmaap.datarouter.node; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.util.Hashtable; +import java.util.Vector; import org.apache.commons.lang3.reflect.FieldUtils; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import java.io.File; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.when; - @RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager") public class DeliveryQueueTest { + @Mock + DeliveryQueueHelper deliveryQueueHelper; private DeliveryQueue deliveryQueue; @Mock private DestInfo destInfo; - @Mock - DeliveryQueueHelper deliveryQueueHelper; - private String dirPath = "/tmp/dir001/"; private String fileName = "10000000000004.fileName.M"; @Before - public void setUp() { + public void setUp() throws IllegalAccessException { when(destInfo.getSpool()).thenReturn(dirPath); + when(destInfo.isPrivilegedSubscriber()).thenReturn(true); deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo); + NodeConfigManager configManager = mockNodeConfigManager(); + FieldUtils.writeDeclaredStaticField(StatusLog.class, "config", configManager, true); } @Test @@ -61,8 +75,8 @@ public class DeliveryQueueTest { @Test 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); + FieldUtils.writeField(deliveryQueue, "failed", true, true); + FieldUtils.writeField(deliveryQueue, "resumetime", System.currentTimeMillis() * 2, true); assertNull(deliveryQueue.peekNext()); } @@ -77,12 +91,148 @@ public class DeliveryQueueTest { deleteFile(dirPath); } + @Test + public void Given_Task_In_Todo_Is_Already_Cleaned_GetNext_Returns_Null() throws Exception { + when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L); + deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo); + Vector tasks = new Vector<>(); + DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"); + task.clean(); + tasks.add(task); + FieldUtils.writeField(deliveryQueue, "todo", tasks, true); + DeliveryTask nt = deliveryQueue.getNext(); + assertNull(nt); + } + + @Test + public void Given_Task_In_Todo_Has_Resume_Time_In_Future_GetNext_Returns_Null() throws Exception { + when(destInfo.isPrivilegedSubscriber()).thenReturn(true); + when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L); + deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo); + Vector tasks = new Vector<>(); + DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"); + long timeInFuture = 2558366240223L; + task.setResumeTime(timeInFuture); + tasks.add(task); + FieldUtils.writeField(deliveryQueue, "todo", tasks, true); + DeliveryTask nt = deliveryQueue.getNext(); + assertNull(nt); + } + + @Test + public void Given_Task_In_Todo_Is_Expired_GetNext_Returns_Null() throws Exception { + when(destInfo.isPrivilegedSubscriber()).thenReturn(true); + when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L); + deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo); + Vector tasks = new Vector<>(); + DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"); + long timeInPast = 1058366240223L; + task.setResumeTime(timeInPast); + tasks.add(task); + FieldUtils.writeField(deliveryQueue, "todo", tasks, true); + DeliveryTask nt = deliveryQueue.getNext(); + assertNull(nt); + } + @Test public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() { long rc = deliveryQueue.cancelTask("123.node.datarouternew.com"); assertEquals(0, rc); } + @Test + public void Given_Delivery_Task_Is_Working_Cancel_Task_Returns_Zero() throws IllegalAccessException { + Hashtable tasks = new Hashtable<>(); + tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com")); + FieldUtils.writeField(deliveryQueue, "working", tasks, true); + long rc = deliveryQueue.cancelTask("123.node.datarouternew.com"); + assertEquals(0, rc); + } + + @Test + public void Given_Delivery_Task_In_Todo_Cancel_Task_Returns_Zero() throws IllegalAccessException { + Vector tasks = new Vector<>(); + tasks.add(new DeliveryTask(deliveryQueue, "123.node.datarouternew.com")); + FieldUtils.writeField(deliveryQueue, "todo", tasks, true); + long rc = deliveryQueue.cancelTask("123.node.datarouternew.com"); + assertEquals(0, rc); + } + + @Test + public void Given_Ok_Status_And_Privileged_Subscriber_Then_Set_Resume_Time_Is_Called_On_DeliveryTask() { + DeliveryTask deliveryTask = mockDeliveryTask(); + deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery"); + verify(deliveryTask, times(1)).setResumeTime(anyLong()); + cleanUpLogging(); + } + + @Test + public void Given_Ok_Status_And_Not_Privileged_Subscriber_Then_Clean_Is_Called_On_DeliveryTask() { + DeliveryTask deliveryTask = mockDeliveryTask(); + when(destInfo.isPrivilegedSubscriber()).thenReturn(false); + deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo); + deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery"); + verify(deliveryTask, times(1)).clean(); + cleanUpLogging(); + } + + @Test + public void Given_Not_Ok_Status_Then_Clean_Is_Called_On_DeliveryTask() { + DeliveryTask deliveryTask = mockDeliveryTask(); + deliveryQueue.reportStatus(deliveryTask, 400, "123456789.dmaap-dr-node", "delivery"); + verify(deliveryTask, times(1)).clean(); + cleanUpLogging(); + } + + @Test + public void Given_Task_In_Working_MarkTaskSuccess_Returns_True() throws IllegalAccessException { + Hashtable tasks = new Hashtable<>(); + tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com")); + FieldUtils.writeField(deliveryQueue, "working", tasks, true); + assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com")); + } + + @Test + public void Given_Task_In_Retry_MarkTaskSuccess_Returns_True() throws IllegalAccessException { + Hashtable tasks = new Hashtable<>(); + tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com")); + FieldUtils.writeField(deliveryQueue, "retry", tasks, true); + assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com")); + } + + @Test + public void Given_Task_Does_Not_Exist_MarkTaskSuccess_Returns_False() { + assertFalse(deliveryQueue.markTaskSuccess("false.pubId.com")); + } + + private void cleanUpLogging() { + final File currentDir = new File(System.getProperty("user.dir")); + final File[] files = currentDir.listFiles((file, name) -> name.matches("null.*")); + if (files != null) { + for (final File file : files) { + file.delete(); + } + } + } + + @NotNull + private DeliveryTask mockDeliveryTask() { + DeliveryTask deliveryTask = mock(DeliveryTask.class); + when(deliveryTask.getPublishId()).thenReturn("123456789.dmaap-dr-node"); + when(deliveryTask.getFeedId()).thenReturn("1"); + when(deliveryTask.getSubId()).thenReturn("1"); + when(deliveryTask.getURL()).thenReturn("http://subcriber.com:7070/delivery"); + when(deliveryTask.getCType()).thenReturn("application/json"); + when(deliveryTask.getLength()).thenReturn(486L); + return deliveryTask; + } + + private NodeConfigManager mockNodeConfigManager() { + NodeConfigManager config = mock(NodeConfigManager.class); + PowerMockito.when(config.getEventLogInterval()).thenReturn("30000"); + return config; + } + private void prepareFiles() throws Exception { createFolder(dirPath); createFile(fileName, dirPath); diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java new file mode 100644 index 00000000..3d17e3e3 --- /dev/null +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java @@ -0,0 +1,71 @@ +/* + * ============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 static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mock; + +public class DeliveryTaskTest { + + @Mock + private DeliveryQueue deliveryQueue; + + @Test + public void Validate_Delivery_Task_Equals() { + DestInfo destInfo = getDestInfo(); + deliveryQueue = mockDelvieryQueue(destInfo); + DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile"); + DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile"); + Assert.assertEquals(task, task2); + Assert.assertEquals(task.hashCode(), task2.hashCode()); + Assert.assertEquals(task.toString(), task2.toString()); + Assert.assertEquals(0, task.compareTo(task2)); + } + + @Test + public void Validate_Delivery_Tasks_Not_Equal() { + DestInfo destInfo = getDestInfo(); + deliveryQueue = mockDelvieryQueue(destInfo); + DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node"); + DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile"); + Assert.assertNotEquals(task, task2); + Assert.assertNotEquals(0, task.compareTo(task2)); + } + + private DestInfo getDestInfo() { + return new DestInfoBuilder().setName("n:" + "dmaap-dr-node") + .setSpool(System.getProperty("user.dir") + "/src/test/resources") + .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish") + .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true) + .setPrivilegedSubscriber(false).setFollowRedirects(false).setDecompress(false).createDestInfo(); + } + + private DeliveryQueue mockDelvieryQueue(DestInfo destInfo) { + DeliveryQueue mockedDeliveryQueue = mock(DeliveryQueue.class); + when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo); + return mockedDeliveryQueue; + } + +} 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 c21bdecc..18d9d56c 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 @@ -22,86 +22,124 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Hashtable; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.onap.dmaap.datarouter.node.Delivery.DelItem; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import java.io.File; -import java.io.IOException; -import java.util.Hashtable; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager") public class DeliveryTest { - @Mock - private DeliveryQueue deliveryQueue; + @Mock + private DeliveryQueue deliveryQueue; + @Mock + private NodeConfigManager config; + private File nDir = new File("tmp/n"); + private File newNDir = new File("tmp/n/0"); + private File newNFile = new File("tmp/n/0/testN.txt"); + private File sDir = new File("tmp/s"); + private File newSDir = new File("tmp/s/0/1"); + private File newSpoolFile = new File("tmp/s/0/1/123456789.dmaap-dr-node"); + private File spoolFileMeta = new File("tmp/s/0/1/123456789.dmaap-dr-node.M"); + + @Before + public void setUp() throws IOException { + nDir.mkdirs(); + sDir.mkdirs(); + newNDir.mkdirs(); + newNFile.createNewFile(); + newSDir.mkdirs(); + newSpoolFile.createNewFile(); + spoolFileMeta.createNewFile(); + config = mockNodeConfigManager(); + } + + @Test + public void Validate_Reset_Queue_Calls_Reset_Queue_On_Delivery_Queue_Object() throws IllegalAccessException { + Delivery delivery = new Delivery(config); + HashMap dqs = new HashMap<>(); + dqs.put("tmp/s/0/1", deliveryQueue); + FieldUtils.writeDeclaredField(delivery, "dqs", dqs, true); + delivery.resetQueue("tmp/s/0/1"); + verify(deliveryQueue, times(1)).resetQueue(); + } - private File nDir = new File("tmp/n"); - private File sDir = new File("tmp/s"); + @Test + public void Validate_Mark_Success_Calls_Mark_Success_On_Delivery_Queue_Object() throws IllegalAccessException { + Delivery delivery = new Delivery(config); + HashMap dqs = new HashMap<>(); + dqs.put("tmp/s/0/1", deliveryQueue); + FieldUtils.writeDeclaredField(delivery, "dqs", dqs, true); + delivery.markTaskSuccess("tmp/s/0/1", "123456789.dmaap-dr-node"); + verify(deliveryQueue, times(1)).markTaskSuccess("123456789.dmaap-dr-node"); + } - @Before - public void setUp() throws IOException { - nDir.mkdirs(); - sDir.mkdirs(); - File newNDir = new File("tmp/n/0"); - newNDir.mkdirs(); - File newNFile = new File("tmp/n/0/testN.txt"); - newNFile.createNewFile(); - File newSDir = new File("tmp/s/0/1"); - newSDir.mkdirs(); - File newSpoolFile = new File("tmp/s/0/1/testSpool.txt"); - newSpoolFile.createNewFile(); - } + @Test + public void Validate_DelItem_With_Equal_Spool_And_PubId_Are_Equal() { + DelItem delItem1 = new DelItem("123456789.dmaap-dr-node", "tmp/s/0/1"); + DelItem delItem2 = new DelItem("123456789.dmaap-dr-node", "tmp/s/0/1"); + Assert.assertEquals(delItem1, delItem2); + Assert.assertEquals(0, delItem1.compareTo(delItem2)); + } - @Test - public void Validate_Reset_Queue_Calls_Reset_Queue_On_Delivery_Queue_Object() throws IllegalAccessException { - NodeConfigManager config = mockNodeConfigManager(); - Delivery delivery = new Delivery(config); - Hashtable dqs = new Hashtable<>(); - dqs.put("spool/s/0/1", deliveryQueue); - FieldUtils.writeDeclaredField(delivery, "dqs", dqs, true); - delivery.resetQueue("spool/s/0/1"); - verify(deliveryQueue, times(1)).resetQueue(); - } + @Test + public void Validate_DelItem_With_Unequal_Spool_And_PubId_Are_Not_Equal() { + DelItem delItem1 = new DelItem("123456789.dmaap-dr-node", "tmp/s/0/1"); + DelItem delItem2 = new DelItem("000000000.dmaap-dr-node", "tmp/s/0/2"); + Assert.assertNotEquals(delItem1, delItem2); + Assert.assertNotEquals(0, delItem1.compareTo(delItem2)); + } - @After - public void tearDown() { - nDir.delete(); - sDir.delete(); - File tmpDir = new File("tmp"); - tmpDir.delete(); - } + @After + public void tearDown() { + newSpoolFile.delete(); + spoolFileMeta.delete(); + newNFile.delete(); + newNDir.delete(); + newSDir.delete(); + new File("tmp/s/0").delete(); + nDir.delete(); + sDir.delete(); + File tmpDir = new File("tmp"); + tmpDir.delete(); + } - private NodeConfigManager mockNodeConfigManager() { - PowerMockito.mockStatic(NodeConfigManager.class); - NodeConfigManager config = mock(NodeConfigManager.class); - PowerMockito.when(config.isConfigured()).thenReturn(true); - PowerMockito.when(config.getAllDests()).thenReturn(createDestInfoObjects()); - PowerMockito.when(config.getFreeDiskStart()).thenReturn(0.49); - PowerMockito.when(config.getFreeDiskStop()).thenReturn(0.5); - PowerMockito.when(config.getDeliveryThreads()).thenReturn(0); - PowerMockito.when(config.getSpoolBase()).thenReturn("tmp"); - return config; - } + private NodeConfigManager mockNodeConfigManager() { + NodeConfigManager config = mock(NodeConfigManager.class); + PowerMockito.when(config.isConfigured()).thenReturn(true); + PowerMockito.when(config.getAllDests()).thenReturn(createDestInfoObjects()); + PowerMockito.when(config.getFreeDiskStart()).thenReturn(0.9); + PowerMockito.when(config.getFreeDiskStop()).thenReturn(0.2); + PowerMockito.when(config.getDeliveryThreads()).thenReturn(0); + PowerMockito.when(config.getSpoolBase()).thenReturn("tmp"); + return config; + } - private DestInfo[] createDestInfoObjects() { - DestInfo[] destInfos = new DestInfo[1]; - DestInfo destInfo = new DestInfoBuilder().setName("node.datarouternew.com").setSpool("spool/s/0/1").setSubid("1") - .setLogdata("logs/").setUrl("/subs/1").setAuthuser("user1").setAuthentication("Basic dXNlcjE6cGFzc3dvcmQx") - .setMetaonly(false).setUse100(true).setPrivilegedSubscriber(false).setFollowRedirects(false) - .setDecompress(false).createDestInfo(); - destInfos[0] = destInfo; - return destInfos; - } + private DestInfo[] createDestInfoObjects() { + DestInfo[] destInfos = new DestInfo[1]; + DestInfo destInfo = new DestInfoBuilder().setName("node.datarouternew.com").setSpool("tmp/s/0/1") + .setSubid("1") + .setLogdata("logs/").setUrl("/subs/1").setAuthuser("user1") + .setAuthentication("Basic dXNlcjE6cGFzc3dvcmQx") + .setMetaonly(false).setUse100(true).setPrivilegedSubscriber(false).setFollowRedirects(false) + .setDecompress(false).createDestInfo(); + destInfos[0] = destInfo; + return destInfos; + } } diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DestInfoTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DestInfoTest.java new file mode 100644 index 00000000..ed629bf2 --- /dev/null +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DestInfoTest.java @@ -0,0 +1,77 @@ +/* + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class DestInfoTest { + + private DestInfo destInfo; + + @Before + public void setUp() { + destInfo = getDestInfo("/src/test/resources"); + } + + @Test + public void Validate_Getters_And_Setters() { + assertEquals("n:dmaap-dr-node", destInfo.getName()); + assertEquals("/src/test/resources", destInfo.getSpool()); + assertEquals("1", destInfo.getSubId()); + assertEquals("n2n-dmaap-dr-node", destInfo.getLogData()); + assertEquals("https://dmaap-dr-node:8443/internal/publish", destInfo.getURL()); + assertEquals("dmaap-dr-node", destInfo.getAuthUser()); + assertEquals("Auth", destInfo.getAuth()); + assertFalse(destInfo.isMetaDataOnly()); + assertTrue(destInfo.isUsing100()); + assertFalse(destInfo.isPrivilegedSubscriber()); + assertFalse(destInfo.isFollowRedirects()); + assertFalse(destInfo.isDecompress()); + } + + @Test + public void Validate_DestInfo_Objects_Are_Equal() { + DestInfo destInfo2 = getDestInfo("/src/test/resources"); + assertEquals(destInfo, destInfo2); + assertEquals(destInfo.hashCode(), destInfo2.hashCode()); + } + + @Test + public void Validate_DestInfo_Objects_Are_Not_Equal() { + DestInfo destInfo2 = getDestInfo("notEqual"); + assertNotEquals(destInfo, destInfo2); + assertNotEquals(destInfo.hashCode(), destInfo2.hashCode()); + } + + private DestInfo getDestInfo(String spool) { + return new DestInfoBuilder().setName("n:" + "dmaap-dr-node").setSpool(spool) + .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish") + .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true) + .setPrivilegedSubscriber(false).setFollowRedirects(false).setDecompress(false).createDestInfo(); + } + +} diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/LogManagerTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/LogManagerTest.java new file mode 100644 index 00000000..b95fb365 --- /dev/null +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/LogManagerTest.java @@ -0,0 +1,109 @@ +/* + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.Timer; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.onap.dmaap.datarouter.node.LogManager.Uploader; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.NodeConfigManager"}) +public class LogManagerTest { + + @Mock + private NodeConfigManager config; + + private LogManager logManager; + + @Before + public void setUp() throws IllegalAccessException { + mockNodeConfigManager(); + FieldUtils.writeDeclaredStaticField(StatusLog.class, "config", config, true); + logManager = new LogManager(config); + } + + @After + public void tearDown() { + File spoolDir = new File(System.getProperty("user.dir") + "/src/test/resources/.spool"); + for (File file : spoolDir.listFiles()) { + if (file.exists()) { + file.delete(); + } + } + spoolDir.delete(); + } + + @Test + public void Verify_LogManager_Attempts_To_Deliver_Log_Files_To_Prov() { + logManager.run(); + try { + Thread.sleep(1000); + } catch (Exception e) { + System.out.println("Exception caught: " + e.getMessage()); + } + File file = new File(System.getProperty("user.dir") + "/src/test/resources/.spool/.lastqueued"); + assertTrue(file.isFile()); + } + + @Test + public void Validate_Uploader_Getters() { + Uploader worker = logManager.getWorker(); + assertEquals(10000L, worker.getInitFailureTimer()); + assertEquals(600000L, worker.getWaitForFileProcessFailureTimer()); + assertEquals(2.0, worker.getFailureBackoff(), 0.0); + assertEquals(150000L, worker.getMaxFailureTimer()); + assertEquals(604800000L, worker.getExpirationTimer()); + assertEquals(10000, worker.getFairFileLimit()); + assertEquals(86400000, worker.getFairTimeLimit()); + assertEquals("https://dmaap-dr-prov:8443/internal/logs", + worker.getDestURL(new DestInfoBuilder().createDestInfo(), "String")); + assertFalse(worker.handleRedirection(new DestInfoBuilder().createDestInfo(), "", "")); + assertFalse(worker.isFollowRedirects()); + assertNull(worker.getFeedId("")); + } + + private void mockNodeConfigManager() { + PowerMockito.when(config.getLogDir()).thenReturn(System.getProperty("user.dir") + "/src/test/resources"); + PowerMockito.when(config.getTimer()).thenReturn(new Timer("Node Configuration Timer", true)); + PowerMockito.when(config.getEventLogPrefix()) + .thenReturn(System.getProperty("user.dir") + "/src/test/resources/events"); + PowerMockito.when(config.getEventLogSuffix()).thenReturn(".log"); + PowerMockito.when(config.getLogRetention()).thenReturn(94608000000L); + PowerMockito.when(config.getEventLogInterval()).thenReturn("30s"); + PowerMockito.when(config.getPublishId()).thenReturn("123456789.dmaap-dr-node"); + PowerMockito.when(config.getEventLogUrl()).thenReturn("https://dmaap-dr-prov:8443/internal/logs"); + } + +} 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 5e357373..79719243 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 @@ -22,6 +22,9 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Assert; @@ -31,10 +34,6 @@ import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; - @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.ProvData", "org.onap.dmaap.datarouter.node.NodeUtils"}) @@ -43,11 +42,107 @@ public class NodeConfigTest { private static NodeConfig nodeConfig; @BeforeClass - public static void setUp() throws IOException{ + public static void setUp() throws IOException { ProvData provData = setUpProvData(); nodeConfig = new NodeConfig(provData, "Name", "spool/dir", 80, "Key"); } + private static ProvData setUpProvData() throws IOException { + JSONObject provData = new JSONObject(); + createValidFeed(provData); + createValidSubscription(provData); + createValidParameters(provData); + createValidIngressValues(provData); + createValidEgressValues(provData); + createValidRoutingValues(provData); + Reader reader = new StringReader(provData.toString()); + return new ProvData(reader); + } + + private static void createValidFeed(JSONObject provData) { + JSONArray feeds = new JSONArray(); + JSONObject feed = new JSONObject(); + JSONObject auth = new JSONObject(); + JSONArray endpointIds = new JSONArray(); + JSONArray endpointAddrs = new JSONArray(); + JSONObject endpointId = new JSONObject(); + feed.put("feedid", "1"); + feed.put("name", "Feed1"); + feed.put("version", "m1.0"); + feed.put("suspend", false); + feed.put("deleted", false); + endpointId.put("id", "user1"); + endpointId.put("password", "password1"); + endpointIds.put(endpointId); + auth.put("endpoint_ids", endpointIds); + 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); + } + + private static void createValidSubscription(JSONObject provData) { + JSONArray subscriptions = new JSONArray(); + JSONObject subscription = new JSONObject(); + JSONObject delivery = new JSONObject(); + subscription.put("subid", "1"); + subscription.put("feedid", "1"); + subscription.put("suspend", false); + subscription.put("metadataOnly", false); + delivery.put("url", "https://172.0.0.2"); + delivery.put("user", "user1"); + delivery.put("password", "password1"); + 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); + } + + private static void createValidParameters(JSONObject provData) { + JSONObject parameters = new JSONObject(); + JSONArray nodes = new JSONArray(); + parameters.put("PROV_NAME", "prov.datarouternew.com"); + parameters.put("DELIVERY_INIT_RETRY_INTERVAL", "10"); + parameters.put("DELIVERY_MAX_AGE", "86400"); + parameters.put("PROV_DOMAIN", ""); + nodes.put("172.0.0.4"); + parameters.put("NODES", nodes); + provData.put("parameters", parameters); + } + + private static void createValidIngressValues(JSONObject provData) { + JSONArray ingresses = new JSONArray(); + JSONObject ingress = new JSONObject(); + ingress.put("feedid", "1"); + ingress.put("subnet", ""); + ingress.put("user", ""); + ingress.put("node", "172.0.0.4"); + ingresses.put(ingress); + provData.put("ingress", ingresses); + } + + private static void createValidEgressValues(JSONObject provData) { + JSONObject egress = new JSONObject(); + egress.put("subid", "1"); + egress.put("nodeid", "172.0.0.4"); + provData.put("egress", egress); + } + + private static void createValidRoutingValues(JSONObject provData) { + JSONArray routings = new JSONArray(); + JSONObject routing = new JSONObject(); + routing.put("from", "prov.datarouternew.com"); + routing.put("to", "172.0.0.4"); + routing.put("via", "172.100.0.1"); + routings.put(routing); + provData.put("routing", routings); + } + @Test public void Given_Feed_Does_Not_Exist_Then_Is_Publish_Permitted_Returns_Not_Null() { String permitted = nodeConfig.isPublishPermitted("2", "user", "0.0.0.0"); @@ -73,7 +168,7 @@ public class NodeConfigTest { } @Test - public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id(){ + public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id() { String feedId = nodeConfig.getFeedId("1"); Assert.assertEquals("1", feedId); } @@ -164,100 +259,4 @@ public class NodeConfigTest { String auth = nodeConfig.getMyAuth(); Assert.assertEquals("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9", auth); } - - private static ProvData setUpProvData() throws IOException { - JSONObject provData = new JSONObject(); - createValidFeed(provData); - createValidSubscription(provData); - createValidParameters(provData); - createValidIngressValues(provData); - createValidEgressValues(provData); - createValidRoutingValues(provData); - Reader reader = new StringReader(provData.toString()); - return new ProvData(reader); - } - - private static void createValidFeed(JSONObject provData) { - JSONArray feeds = new JSONArray(); - JSONObject feed = new JSONObject(); - JSONObject auth = new JSONObject(); - JSONArray endpointIds = new JSONArray(); - JSONArray endpointAddrs = new JSONArray(); - JSONObject endpointId = new JSONObject(); - feed.put("feedid", "1"); - feed.put("name", "Feed1"); - feed.put("version", "m1.0"); - feed.put("suspend", false); - feed.put("deleted", false); - endpointId.put("id", "user1"); - endpointId.put("password", "password1"); - endpointIds.put(endpointId); - auth.put("endpoint_ids", endpointIds); - 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); - } - - private static void createValidSubscription(JSONObject provData) { - JSONArray subscriptions = new JSONArray(); - JSONObject subscription = new JSONObject(); - JSONObject delivery = new JSONObject(); - subscription.put("subid", "1"); - subscription.put("feedid", "1"); - subscription.put("suspend", false); - subscription.put("metadataOnly", false); - delivery.put("url", "https://172.0.0.2"); - delivery.put("user", "user1"); - delivery.put("password", "password1"); - 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); - } - - private static void createValidParameters(JSONObject provData) { - JSONObject parameters = new JSONObject(); - JSONArray nodes = new JSONArray(); - parameters.put("PROV_NAME", "prov.datarouternew.com"); - parameters.put("DELIVERY_INIT_RETRY_INTERVAL", "10"); - parameters.put("DELIVERY_MAX_AGE", "86400"); - parameters.put("PROV_DOMAIN", ""); - nodes.put("172.0.0.4"); - parameters.put("NODES", nodes); - provData.put("parameters", parameters); - } - - private static void createValidIngressValues(JSONObject provData) { - JSONArray ingresses = new JSONArray(); - JSONObject ingress = new JSONObject(); - ingress.put("feedid", "1"); - ingress.put("subnet", ""); - ingress.put("user", ""); - ingress.put("node", "172.0.0.4"); - ingresses.put(ingress); - provData.put("ingress", ingresses); - } - - private static void createValidEgressValues(JSONObject provData) { - JSONObject egress = new JSONObject(); - egress.put("subid", "1"); - egress.put("nodeid", "172.0.0.4"); - provData.put("egress", egress); - } - - private static void createValidRoutingValues(JSONObject provData) { - JSONArray routings = new JSONArray(); - JSONObject routing = new JSONObject(); - routing.put("from", "prov.datarouternew.com"); - routing.put("to", "172.0.0.4"); - routing.put("via", "172.100.0.1"); - routings.put(routing); - provData.put("routing", routings); - } } diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java index 27fcd1c4..88e57432 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java @@ -22,6 +22,15 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; +import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; +import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +import java.io.IOException; +import java.net.InetAddress; +import java.util.UUID; +import javax.servlet.http.HttpServletRequest; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,20 +40,9 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFo import org.powermock.modules.junit4.PowerMockRunner; import org.slf4j.MDC; -import javax.servlet.http.HttpServletRequest; - -import java.io.IOException; -import java.net.InetAddress; -import java.util.UUID; - -import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; -import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeUtils") -@PrepareForTest({ UUID.class, InetAddress.class }) +@PrepareForTest({UUID.class, InetAddress.class}) public class NodeUtilsTest { @Mock diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/ProvDataTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/ProvDataTest.java index 662f2cc0..1fd79d9a 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/ProvDataTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/ProvDataTest.java @@ -20,17 +20,16 @@ package org.onap.dmaap.datarouter.node; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.modules.junit4.PowerMockRunner; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; import java.io.ByteArrayInputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) public class ProvDataTest { @@ -40,77 +39,77 @@ public class ProvDataTest { public void Validate_Values_Are_Set_Correctly_Through_ProvData_Constuctor() throws Exception { String InternalProvData = "{" + - "\"ingress\":[{" + - "\"feedid\":1," + - "\"subnet\":\"\"," + - "\"user\":\"\"," + - "\"node\":\"node\"" + - "}]," + - "\"routing\":[{" + - "\"from\":\"172.10.10.10\"," + - "\"to\":\"172.10.10.12\"," + - "\"via\":\"172.10.10.11\"" + - "}]," + - "\"subscriptions\":[{" + - "\"subid\":1," + - "\"suspend\":false," + - "\"delivery\":{" + - "\"use100\":true," + - "\"password\":\"PASSWORD\"," + - "\"user\":\"LOGIN\"," + - "\"url\":\"http://172.18.0.2:7070\"" + - "}," + - "\"last_mod\":1553608460000," + - "\"subscriber\":\"PMMAPER\"," + - "\"feedid\":1," + - "\"decompress\":false," + - "\"groupid\":1," + - "\"metadataOnly\":false," + - "\"follow_redirect\":false," + - "\"links\":{" + - "\"feed\":\"https://dmaap-dr-prov/feed/1\"" + - ",\"log\":\"https://dmaap-dr-prov/sublog/1\"" + - ",\"self\":\"https://dmaap-dr-prov/subs/1\"" + - "}," + - "\"created_date\":1553608460000," + - "\"privilegedSubscriber\":false" + - "}]," + - "\"feeds\":[{" + - "\"suspend\":false," + - "\"groupid\":0," + - "\"description\":\"Default feed\"," + - "\"version\":\"m1.0\"," + - "\"authorization\":{" + - "\"endpoint_addrs\":[\"172.10.10.20\"]," + - "\"classification\":\"unclassified\"," + - "\"endpoint_ids\":[{" + - "\"password\":\"password\"," + - "\"id\":\"user\"" + - "}]" + - "}," + - "\"last_mod\":1553608454000," + - "\"deleted\":false," + - "\"feedid\":1," + - "\"name\":\"CSIT_Test2\"" + - ",\"business_description\":\"Default Feed\"" + - ",\"publisher\":\"dradmin\"" + - ",\"links\":{" + - "\"subscribe\":\"https://dmaap-dr-prov/subscribe/1\"," + - "\"log\":\"https://dmaap-dr-prov/feedlog/1\"," + - "\"publish\":\"https://dmaap-dr-prov/publish/1\"," + - "\"self\":\"https://dmaap-dr-prov/feed/1\"" + - "}," + - "\"created_date\":1553608454000" + - "}]," + - "\"groups\":[]," + - "\"parameters\":{" + - "\"NODES\":[\"dmaap-dr-node\"]," + - "\"PROV_DOMAIN\":\"\"" + - "}," + - "\"egress\":{" + - "\"1\":1" + - "}" + - "}" ; + "\"ingress\":[{" + + "\"feedid\":1," + + "\"subnet\":\"\"," + + "\"user\":\"\"," + + "\"node\":\"node\"" + + "}]," + + "\"routing\":[{" + + "\"from\":\"172.10.10.10\"," + + "\"to\":\"172.10.10.12\"," + + "\"via\":\"172.10.10.11\"" + + "}]," + + "\"subscriptions\":[{" + + "\"subid\":1," + + "\"suspend\":false," + + "\"delivery\":{" + + "\"use100\":true," + + "\"password\":\"PASSWORD\"," + + "\"user\":\"LOGIN\"," + + "\"url\":\"http://172.18.0.2:7070\"" + + "}," + + "\"last_mod\":1553608460000," + + "\"subscriber\":\"PMMAPER\"," + + "\"feedid\":1," + + "\"decompress\":false," + + "\"groupid\":1," + + "\"metadataOnly\":false," + + "\"follow_redirect\":false," + + "\"links\":{" + + "\"feed\":\"https://dmaap-dr-prov/feed/1\"" + + ",\"log\":\"https://dmaap-dr-prov/sublog/1\"" + + ",\"self\":\"https://dmaap-dr-prov/subs/1\"" + + "}," + + "\"created_date\":1553608460000," + + "\"privilegedSubscriber\":false" + + "}]," + + "\"feeds\":[{" + + "\"suspend\":false," + + "\"groupid\":0," + + "\"description\":\"Default feed\"," + + "\"version\":\"m1.0\"," + + "\"authorization\":{" + + "\"endpoint_addrs\":[\"172.10.10.20\"]," + + "\"classification\":\"unclassified\"," + + "\"endpoint_ids\":[{" + + "\"password\":\"password\"," + + "\"id\":\"user\"" + + "}]" + + "}," + + "\"last_mod\":1553608454000," + + "\"deleted\":false," + + "\"feedid\":1," + + "\"name\":\"CSIT_Test2\"" + + ",\"business_description\":\"Default Feed\"" + + ",\"publisher\":\"dradmin\"" + + ",\"links\":{" + + "\"subscribe\":\"https://dmaap-dr-prov/subscribe/1\"," + + "\"log\":\"https://dmaap-dr-prov/feedlog/1\"," + + "\"publish\":\"https://dmaap-dr-prov/publish/1\"," + + "\"self\":\"https://dmaap-dr-prov/feed/1\"" + + "}," + + "\"created_date\":1553608454000" + + "}]," + + "\"groups\":[]," + + "\"parameters\":{" + + "\"NODES\":[\"dmaap-dr-node\"]," + + "\"PROV_DOMAIN\":\"\"" + + "}," + + "\"egress\":{" + + "\"1\":1" + + "}" + + "}"; Reader r = new InputStreamReader(new ByteArrayInputStream(InternalProvData.getBytes(StandardCharsets.UTF_8))); ProvData pd = new ProvData(r); diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/StatusLogTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/StatusLogTest.java index e60f576c..d3793069 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/StatusLogTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/StatusLogTest.java @@ -22,6 +22,9 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -31,9 +34,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager") @PrepareForTest(StatusLog.class) -- 2.16.6