Update license header in appc-event-listener files
[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-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
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener;
25
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;
35
36 import java.util.Properties;
37 import java.util.concurrent.BlockingQueue;
38 import java.util.concurrent.ThreadPoolExecutor;
39
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;
46
47 public class AbstractListenerTest {
48
49     private DummyListener listener;
50     private ListenerProperties props;
51
52     @Before
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);
58     }
59
60     @Test
61     public void stop_should_shutdown_executor() {
62
63         EventHandler mockEventHandler = mock(EventHandler.class);
64         listener.setEventHandler(mockEventHandler);
65
66         Thread thread = new Thread(listener);
67         thread.start();
68
69         assertTrue(thread.isAlive());
70         assertTrue(listener.getRun());
71         assertFalse(listener.getExecutor().isShutdown());
72         assertFalse(listener.getExecutor().isTerminated());
73
74         listener.stop();
75
76         assertFalse(listener.getRun());
77         assertTrue(listener.getExecutor().isShutdown());
78         assertTrue(listener.getExecutor().isTerminated());
79
80         verify(mockEventHandler).closeClients();
81
82     }
83
84     @Test
85     public void stopNow_should_clear_executors_queue_and_call_stop() throws InterruptedException {
86         EventHandler mockEventHandler = mock(EventHandler.class);
87         listener.setEventHandler(mockEventHandler);
88
89         ThreadPoolExecutor mockExecutor = mock(ThreadPoolExecutor.class);
90         BlockingQueue<Runnable> mockBlockingQueue = mock(BlockingQueue.class);
91         listener.setExecutor(mockExecutor);
92         when(mockExecutor.getQueue()).thenReturn(mockBlockingQueue);
93
94         Thread thread = new Thread(listener);
95         thread.start();
96
97         assertTrue(thread.isAlive());
98         assertTrue(listener.getRun());
99
100         listener.stopNow();
101
102         assertFalse(listener.getRun());
103         verify(mockExecutor).shutdown();
104         verify(mockExecutor).awaitTermination(anyLong(), any(TimeUnit.class));
105         verify(mockBlockingQueue).clear();
106         verify(mockEventHandler).closeClients();
107     }
108
109     @Test
110     public void getBenchmark_result_should_contain_listenerId() {
111         String out = listener.getBenchmark();
112         assertNotNull(out);
113         assertTrue(out.contains(listener.getListenerId()));
114     }
115
116     @Test
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());
121     }
122
123
124     private class DummyListener extends AbstractListener {
125
126         DummyListener(ListenerProperties props) {
127             super(props);
128         }
129
130         boolean getRun() {
131             return run.get();
132         }
133
134         public ThreadPoolExecutor getExecutor() {
135             return executor;
136         }
137
138         void setEventHandler(EventHandler eventHandler){
139             dmaap = eventHandler;
140         }
141
142         void setExecutor(ThreadPoolExecutor executor){
143             this.executor = executor;
144         }
145
146         @Override
147         public void run() {
148
149             while (run.get()) {
150             }
151         }
152     }
153 }