9a408ac42eb263f4f3d56091799d189895295b05
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / AbstractListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.listener;
26
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;
36
37 import java.util.Properties;
38 import java.util.concurrent.BlockingQueue;
39 import java.util.concurrent.ThreadPoolExecutor;
40
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;
47
48 public class AbstractListenerTest {
49
50     private DummyListener listener;
51     private ListenerProperties props;
52
53     @Before
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);
59     }
60
61     @Test
62     public void stop_should_shutdown_executor() {
63
64         EventHandler mockEventHandler = mock(EventHandler.class);
65         listener.setEventHandler(mockEventHandler);
66
67         Thread thread = new Thread(listener);
68         thread.start();
69
70         assertTrue(thread.isAlive());
71         assertTrue(listener.getRun());
72         assertFalse(listener.getExecutor().isShutdown());
73         assertFalse(listener.getExecutor().isTerminated());
74
75         listener.stop();
76
77         assertFalse(listener.getRun());
78         assertTrue(listener.getExecutor().isShutdown());
79         assertTrue(listener.getExecutor().isTerminated());
80
81         verify(mockEventHandler).closeClients();
82
83     }
84
85     @Test
86     public void stopNow_should_clear_executors_queue_and_call_stop() throws InterruptedException {
87         EventHandler mockEventHandler = mock(EventHandler.class);
88         listener.setEventHandler(mockEventHandler);
89
90         ThreadPoolExecutor mockExecutor = mock(ThreadPoolExecutor.class);
91         BlockingQueue<Runnable> mockBlockingQueue = mock(BlockingQueue.class);
92         listener.setExecutor(mockExecutor);
93         when(mockExecutor.getQueue()).thenReturn(mockBlockingQueue);
94
95         Thread thread = new Thread(listener);
96         thread.start();
97
98         assertTrue(thread.isAlive());
99         assertTrue(listener.getRun());
100
101         listener.stopNow();
102
103         assertFalse(listener.getRun());
104         verify(mockExecutor).shutdown();
105         verify(mockExecutor).awaitTermination(anyLong(), any(TimeUnit.class));
106         verify(mockBlockingQueue).clear();
107         verify(mockEventHandler).closeClients();
108     }
109
110     @Test
111     public void getBenchmark_result_should_contain_listenerId() {
112         String out = listener.getBenchmark();
113         assertNotNull(out);
114         assertTrue(out.contains(listener.getListenerId()));
115     }
116
117     @Test
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());
122     }
123
124
125     private class DummyListener extends AbstractListener {
126
127         DummyListener(ListenerProperties props) {
128             super(props);
129         }
130
131         boolean getRun() {
132             return run.get();
133         }
134
135         public ThreadPoolExecutor getExecutor() {
136             return executor;
137         }
138
139         void setEventHandler(EventHandler eventHandler){
140             dmaap = eventHandler;
141         }
142
143         void setExecutor(ThreadPoolExecutor executor){
144             this.executor = executor;
145         }
146
147         @Override
148         public void run() {
149
150             while (run.get()) {
151             }
152         }
153     }
154 }