2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.Matchers.any;
32 import static org.mockito.Matchers.anyLong;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
37 import java.util.Properties;
38 import java.util.concurrent.BlockingQueue;
39 import java.util.concurrent.ThreadPoolExecutor;
41 import java.util.concurrent.TimeUnit;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.onap.appc.listener.AbstractListener;
45 import org.onap.appc.listener.ListenerProperties;
46 import sun.awt.windows.ThemeReader;
48 public class AbstractListenerTest {
50 private DummyListener listener;
51 private ListenerProperties props;
54 public void setup() throws Exception {
55 Properties regularProps = new Properties();
56 regularProps.load(getClass().getResourceAsStream("/org/onap/appc/default.properties"));
57 props = new ListenerProperties("", regularProps);
58 listener = new DummyListener(props);
62 public void stop_should_shutdown_executor() {
64 EventHandler mockEventHandler = mock(EventHandler.class);
65 listener.setEventHandler(mockEventHandler);
67 Thread thread = new Thread(listener);
70 assertTrue(thread.isAlive());
71 assertTrue(listener.getRun());
72 assertFalse(listener.getExecutor().isShutdown());
73 assertFalse(listener.getExecutor().isTerminated());
77 assertFalse(listener.getRun());
78 assertTrue(listener.getExecutor().isShutdown());
79 assertTrue(listener.getExecutor().isTerminated());
81 verify(mockEventHandler).closeClients();
86 public void stopNow_should_clear_executors_queue_and_call_stop() throws InterruptedException {
87 EventHandler mockEventHandler = mock(EventHandler.class);
88 listener.setEventHandler(mockEventHandler);
90 ThreadPoolExecutor mockExecutor = mock(ThreadPoolExecutor.class);
91 BlockingQueue<Runnable> mockBlockingQueue = mock(BlockingQueue.class);
92 listener.setExecutor(mockExecutor);
93 when(mockExecutor.getQueue()).thenReturn(mockBlockingQueue);
95 Thread thread = new Thread(listener);
98 assertTrue(thread.isAlive());
99 assertTrue(listener.getRun());
103 assertFalse(listener.getRun());
104 verify(mockExecutor).shutdown();
105 verify(mockExecutor).awaitTermination(anyLong(), any(TimeUnit.class));
106 verify(mockBlockingQueue).clear();
107 verify(mockEventHandler).closeClients();
111 public void getBenchmark_result_should_contain_listenerId() {
112 String out = listener.getBenchmark();
114 assertTrue(out.contains(listener.getListenerId()));
118 public void getListenerId_should_return_properties_prefix_by_default() {
119 assertEquals(props.getPrefix(), listener.getListenerId());
120 listener.setListenerId("newId");
121 assertEquals("newId", listener.getListenerId());
125 private class DummyListener extends AbstractListener {
127 DummyListener(ListenerProperties props) {
135 public ThreadPoolExecutor getExecutor() {
139 void setEventHandler(EventHandler eventHandler){
140 dmaap = eventHandler;
143 void setExecutor(ThreadPoolExecutor executor){
144 this.executor = executor;