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;
45 import sun.awt.windows.ThemeReader;
47 public class AbstractListenerTest {
49 private DummyListener listener;
50 private ListenerProperties props;
53 public void setup() throws Exception {
54 Properties regularProps = new Properties();
55 regularProps.load(getClass().getResourceAsStream("/org/onap/appc/default.properties"));
56 props = new ListenerProperties("", regularProps);
57 listener = new DummyListener(props);
61 public void stop_should_shutdown_executor() {
63 EventHandler mockEventHandler = mock(EventHandler.class);
64 listener.setEventHandler(mockEventHandler);
66 Thread thread = new Thread(listener);
69 assertTrue(thread.isAlive());
70 assertTrue(listener.getRun());
71 assertFalse(listener.getExecutor().isShutdown());
72 assertFalse(listener.getExecutor().isTerminated());
76 assertFalse(listener.getRun());
77 assertTrue(listener.getExecutor().isShutdown());
78 assertTrue(listener.getExecutor().isTerminated());
80 verify(mockEventHandler).closeClients();
85 public void stopNow_should_clear_executors_queue_and_call_stop() throws InterruptedException {
86 EventHandler mockEventHandler = mock(EventHandler.class);
87 listener.setEventHandler(mockEventHandler);
89 ThreadPoolExecutor mockExecutor = mock(ThreadPoolExecutor.class);
90 BlockingQueue<Runnable> mockBlockingQueue = mock(BlockingQueue.class);
91 listener.setExecutor(mockExecutor);
92 when(mockExecutor.getQueue()).thenReturn(mockBlockingQueue);
94 Thread thread = new Thread(listener);
97 assertTrue(thread.isAlive());
98 assertTrue(listener.getRun());
102 assertFalse(listener.getRun());
103 verify(mockExecutor).shutdown();
104 verify(mockExecutor).awaitTermination(anyLong(), any(TimeUnit.class));
105 verify(mockBlockingQueue).clear();
106 verify(mockEventHandler).closeClients();
110 public void getBenchmark_result_should_contain_listenerId() {
111 String out = listener.getBenchmark();
113 assertTrue(out.contains(listener.getListenerId()));
117 public void getListenerId_should_return_properties_prefix_by_default() {
118 assertEquals(props.getPrefix(), listener.getListenerId());
119 listener.setListenerId("newId");
120 assertEquals("newId", listener.getListenerId());
124 private class DummyListener extends AbstractListener {
126 DummyListener(ListenerProperties props) {
134 public ThreadPoolExecutor getExecutor() {
138 void setEventHandler(EventHandler eventHandler){
139 dmaap = eventHandler;
142 void setExecutor(ThreadPoolExecutor executor){
143 this.executor = executor;