a21bcd416d0f2edf21e8af89705024fb8be24dbb
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / TestAbstractListener.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
32 import java.util.Properties;
33 import java.util.concurrent.ThreadPoolExecutor;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.appc.listener.AbstractListener;
38 import org.onap.appc.listener.ListenerProperties;
39
40 public class TestAbstractListener {
41
42     private class DummyListener extends AbstractListener {
43         public DummyListener(ListenerProperties props) {
44             super(props);
45         }
46
47         public boolean getRun() {
48             return run.get();
49         }
50
51         public ThreadPoolExecutor getExecutor() {
52             return executor;
53         }
54     }
55
56     private DummyListener listener;
57     private ListenerProperties props;
58
59     @Before
60     public void setup() throws Exception {
61         Properties regularProps = new Properties();
62         regularProps.load(getClass().getResourceAsStream("/org/onap/appc/default.properties"));
63         props = new ListenerProperties("", regularProps);
64         listener = new DummyListener(props);
65     }
66
67     @Test
68     public void testRun() {
69         Thread t = new Thread(listener);
70         t.run();
71         assertFalse(t.isAlive()); // Should die immediately
72     }
73
74     @Test
75     public void testStop() {
76         listener.stop();
77         assertFalse(listener.getRun());
78         assertTrue(listener.getExecutor().isShutdown());
79     }
80
81     @Test
82     public void testStopNow() {
83         listener.stopNow();
84         assertFalse(listener.getRun());
85         assertTrue(listener.getExecutor().isShutdown());
86     }
87
88     @Test
89     public void testBenchmark() {
90         String out = listener.getBenchmark();
91         assertNotNull(out);
92         assertTrue(out.contains(listener.getListenerId()));
93     }
94
95     @Test
96     public void testListenerId() {
97         assertEquals(props.getPrefix(), listener.getListenerId());
98         listener.setListenerId("newId");
99         assertEquals("newId", listener.getListenerId());
100     }
101 }