summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Dudycz <jakub.dudycz@nokia.com>2018-03-15 14:46:35 +0100
committerTakamune Cho <tc012c@att.com>2018-03-15 15:06:06 +0000
commit286ef6e7dc5baea219a29bd5f81ea3eabf96e7b4 (patch)
treeea909b7c915c8a43f899ebc85e0a9add70a31a72
parent1275e425d9064fed66532aa2fddb34023dfd99b6 (diff)
AbstractListener unit tests
Improved code coverage. Change-Id: I131f8bbbc72e0587572baa340d10fb56f6a1c5bc Issue-ID: APPC-747 Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
-rw-r--r--appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/AbstractListener.java6
-rw-r--r--appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/AbstractListenerTest.java (renamed from appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestAbstractListener.java)105
-rw-r--r--appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/AppcEventListenerActivatorTest.java (renamed from appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestAppcDmaapListenerActivator.java)16
-rw-r--r--appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/ListenerPropertiesTest.java (renamed from appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestListenerProperties.java)2
4 files changed, 88 insertions, 41 deletions
diff --git a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/AbstractListener.java b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/AbstractListener.java
index 89c75a357..be19cf3aa 100644
--- a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/AbstractListener.java
+++ b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/AbstractListener.java
@@ -43,18 +43,13 @@ public abstract class AbstractListener implements Listener {
private final EELFLogger LOG = EELFManager.getInstance().getLogger(AbstractListener.class);
protected AtomicBoolean run = new AtomicBoolean(false);
-
protected int QUEUED_MIN = 1;
protected int QUEUED_MAX = 10;
-
protected int THREAD_MIN = 4;
protected int THREAD_MAX = THREAD_MIN; // Fixed thread pool
protected int THREAD_SCALE_DOWN_SEC = 10; // Number of seconds to wait until we remove idle threads
-
protected ThreadPoolExecutor executor;
-
protected EventHandler dmaap;
-
protected ListenerProperties props;
private String listenerId;
@@ -157,6 +152,5 @@ public abstract class AbstractListener implements Listener {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
LOG.error(String.format("A job was rejected. [%s]", r));
}
-
}
}
diff --git a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestAbstractListener.java b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/AbstractListenerTest.java
index a21bcd416..9a408ac42 100644
--- a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestAbstractListener.java
+++ b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/AbstractListenerTest.java
@@ -28,30 +28,24 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.util.Properties;
+import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.onap.appc.listener.AbstractListener;
import org.onap.appc.listener.ListenerProperties;
+import sun.awt.windows.ThemeReader;
-public class TestAbstractListener {
-
- private class DummyListener extends AbstractListener {
- public DummyListener(ListenerProperties props) {
- super(props);
- }
-
- public boolean getRun() {
- return run.get();
- }
-
- public ThreadPoolExecutor getExecutor() {
- return executor;
- }
- }
+public class AbstractListenerTest {
private DummyListener listener;
private ListenerProperties props;
@@ -65,37 +59,96 @@ public class TestAbstractListener {
}
@Test
- public void testRun() {
- Thread t = new Thread(listener);
- t.run();
- assertFalse(t.isAlive()); // Should die immediately
- }
+ public void stop_should_shutdown_executor() {
+
+ EventHandler mockEventHandler = mock(EventHandler.class);
+ listener.setEventHandler(mockEventHandler);
+
+ Thread thread = new Thread(listener);
+ thread.start();
+
+ assertTrue(thread.isAlive());
+ assertTrue(listener.getRun());
+ assertFalse(listener.getExecutor().isShutdown());
+ assertFalse(listener.getExecutor().isTerminated());
- @Test
- public void testStop() {
listener.stop();
+
assertFalse(listener.getRun());
assertTrue(listener.getExecutor().isShutdown());
+ assertTrue(listener.getExecutor().isTerminated());
+
+ verify(mockEventHandler).closeClients();
+
}
@Test
- public void testStopNow() {
+ public void stopNow_should_clear_executors_queue_and_call_stop() throws InterruptedException {
+ EventHandler mockEventHandler = mock(EventHandler.class);
+ listener.setEventHandler(mockEventHandler);
+
+ ThreadPoolExecutor mockExecutor = mock(ThreadPoolExecutor.class);
+ BlockingQueue<Runnable> mockBlockingQueue = mock(BlockingQueue.class);
+ listener.setExecutor(mockExecutor);
+ when(mockExecutor.getQueue()).thenReturn(mockBlockingQueue);
+
+ Thread thread = new Thread(listener);
+ thread.start();
+
+ assertTrue(thread.isAlive());
+ assertTrue(listener.getRun());
+
listener.stopNow();
+
assertFalse(listener.getRun());
- assertTrue(listener.getExecutor().isShutdown());
+ verify(mockExecutor).shutdown();
+ verify(mockExecutor).awaitTermination(anyLong(), any(TimeUnit.class));
+ verify(mockBlockingQueue).clear();
+ verify(mockEventHandler).closeClients();
}
@Test
- public void testBenchmark() {
+ public void getBenchmark_result_should_contain_listenerId() {
String out = listener.getBenchmark();
assertNotNull(out);
assertTrue(out.contains(listener.getListenerId()));
}
@Test
- public void testListenerId() {
+ public void getListenerId_should_return_properties_prefix_by_default() {
assertEquals(props.getPrefix(), listener.getListenerId());
listener.setListenerId("newId");
assertEquals("newId", listener.getListenerId());
}
+
+
+ private class DummyListener extends AbstractListener {
+
+ DummyListener(ListenerProperties props) {
+ super(props);
+ }
+
+ boolean getRun() {
+ return run.get();
+ }
+
+ public ThreadPoolExecutor getExecutor() {
+ return executor;
+ }
+
+ void setEventHandler(EventHandler eventHandler){
+ dmaap = eventHandler;
+ }
+
+ void setExecutor(ThreadPoolExecutor executor){
+ this.executor = executor;
+ }
+
+ @Override
+ public void run() {
+
+ while (run.get()) {
+ }
+ }
+ }
}
diff --git a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestAppcDmaapListenerActivator.java b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/AppcEventListenerActivatorTest.java
index f8cbdb3dc..817c8faee 100644
--- a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestAppcDmaapListenerActivator.java
+++ b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/AppcEventListenerActivatorTest.java
@@ -38,7 +38,7 @@ import static org.mockito.Mockito.doReturn;
import org.onap.appc.listener.AppcEventListenerActivator;
-public class TestAppcDmaapListenerActivator {
+public class AppcEventListenerActivatorTest {
@Test
public void testStartStopDefaultProperties() {
@@ -53,21 +53,21 @@ public class TestAppcDmaapListenerActivator {
}
assertNotNull(appc.getName());
}
-
+
@Test
public void testStartStopEmptyProperties() {
InputStream input = getClass().getResourceAsStream("/org/onap/appc/empty.properties");
Properties props = new Properties();
- try {
- props.load(input);
- } catch (IOException e) {
- e.printStackTrace();
- }
+ try {
+ props.load(input);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
AppcEventListenerActivator appc = new AppcEventListenerActivator();
AppcEventListenerActivator spyAppc = Mockito.spy(appc);
doReturn(props).when(spyAppc).getProperties();
-
+
try {
spyAppc.start(null);
Thread.sleep(1000);
diff --git a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestListenerProperties.java b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/ListenerPropertiesTest.java
index 42c263394..79a18ceaf 100644
--- a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/TestListenerProperties.java
+++ b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/ListenerPropertiesTest.java
@@ -40,7 +40,7 @@ import org.onap.appc.listener.AbstractListener;
import org.onap.appc.listener.ListenerProperties;
import org.onap.appc.listener.ListenerProperties.KEYS;
-public class TestListenerProperties {
+public class ListenerPropertiesTest {
private Properties good, bad, both;
private String prefix;