c1416cfa17a36ef28aaf9b5f2351e0897c085aa5
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / openecomp / appc / listener / TestAbstractListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.listener;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.Properties;
31 import java.util.concurrent.ThreadPoolExecutor;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.openecomp.appc.listener.AbstractListener;
36 import org.openecomp.appc.listener.ListenerProperties;
37
38 public class TestAbstractListener {
39
40     private class DummyListener extends AbstractListener {
41         public DummyListener(ListenerProperties props) {
42             super(props);
43         }
44
45         public boolean getRun() {
46             return run.get();
47         }
48
49         public ThreadPoolExecutor getExecutor() {
50             return executor;
51         }
52     }
53
54     private DummyListener listener;
55     private ListenerProperties props;
56
57     @Before
58     public void setup() throws Exception {
59         Properties regularProps = new Properties();
60         regularProps.load(getClass().getResourceAsStream("/org/openecomp/appc/default.properties"));
61         props = new ListenerProperties("", regularProps);
62         listener = new DummyListener(props);
63     }
64
65     @Test
66     public void testRun() {
67         Thread t = new Thread(listener);
68         t.run();
69         assertFalse(t.isAlive()); // Should die immediately
70     }
71
72     @Test
73     public void testStop() {
74         listener.stop();
75         assertFalse(listener.getRun());
76         assertTrue(listener.getExecutor().isShutdown());
77     }
78
79     @Test
80     public void testStopNow() {
81         listener.stopNow();
82         assertFalse(listener.getRun());
83         assertTrue(listener.getExecutor().isShutdown());
84     }
85
86     @Test
87     public void testBenchmark() {
88         String out = listener.getBenchmark();
89         assertNotNull(out);
90         assertTrue(out.contains(listener.getListenerId()));
91     }
92
93     @Test
94     public void testListenerId() {
95         assertEquals(props.getPrefix(), listener.getListenerId());
96         listener.setListenerId("newId");
97         assertEquals("newId", listener.getListenerId());
98     }
99 }