2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyLong;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 import java.util.Properties;
37 import java.util.concurrent.BlockingQueue;
38 import java.util.concurrent.ThreadPoolExecutor;
40 import java.util.concurrent.TimeUnit;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.appc.listener.AbstractListener;
44 import org.onap.appc.listener.ListenerProperties;
46 public class AbstractListenerTest {
48 private DummyListener listener;
49 private ListenerProperties props;
52 public void setup() throws Exception {
53 Properties regularProps = new Properties();
54 regularProps.load(getClass().getResourceAsStream("/org/onap/appc/default.properties"));
55 props = new ListenerProperties("", regularProps);
56 listener = new DummyListener(props);
60 public void stop_should_shutdown_executor() {
62 EventHandler mockEventHandler = mock(EventHandler.class);
63 listener.setEventHandler(mockEventHandler);
65 Thread thread = new Thread(listener);
68 assertTrue(thread.isAlive());
69 assertTrue(listener.getRun());
70 assertFalse(listener.getExecutor().isShutdown());
71 assertFalse(listener.getExecutor().isTerminated());
75 assertFalse(listener.getRun());
76 assertTrue(listener.getExecutor().isShutdown());
77 assertTrue(listener.getExecutor().isTerminated());
79 verify(mockEventHandler).closeClients();
84 public void stopNow_should_clear_executors_queue_and_call_stop() throws InterruptedException {
85 EventHandler mockEventHandler = mock(EventHandler.class);
86 listener.setEventHandler(mockEventHandler);
88 ThreadPoolExecutor mockExecutor = mock(ThreadPoolExecutor.class);
89 BlockingQueue<Runnable> mockBlockingQueue = mock(BlockingQueue.class);
90 listener.setExecutor(mockExecutor);
91 when(mockExecutor.getQueue()).thenReturn(mockBlockingQueue);
93 Thread thread = new Thread(listener);
96 assertTrue(thread.isAlive());
97 assertTrue(listener.getRun());
101 assertFalse(listener.getRun());
102 verify(mockExecutor).shutdown();
103 verify(mockExecutor).awaitTermination(anyLong(), any(TimeUnit.class));
104 verify(mockBlockingQueue).clear();
105 verify(mockEventHandler).closeClients();
109 public void getBenchmark_result_should_contain_listenerId() {
110 String out = listener.getBenchmark();
112 assertTrue(out.contains(listener.getListenerId()));
116 public void getListenerId_should_return_properties_prefix_by_default() {
117 assertEquals(props.getPrefix(), listener.getListenerId());
118 listener.setListenerId("newId");
119 assertEquals("newId", listener.getListenerId());
123 private class DummyListener extends AbstractListener {
125 DummyListener(ListenerProperties props) {
133 public ThreadPoolExecutor getExecutor() {
137 void setEventHandler(EventHandler eventHandler){
138 dmaap = eventHandler;
141 void setExecutor(ThreadPoolExecutor executor){
142 this.executor = executor;