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