Moving all files to root directory
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / openecomp / appc / listener / TestAbstractListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.listener;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Properties;
30 import java.util.concurrent.ThreadPoolExecutor;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.openecomp.appc.listener.AbstractListener;
35 import org.openecomp.appc.listener.ListenerProperties;
36
37 public class TestAbstractListener {
38
39     private class DummyListener extends AbstractListener {
40         public DummyListener(ListenerProperties props) {
41             super(props);
42         }
43
44         public boolean getRun() {
45             return run.get();
46         }
47
48         public ThreadPoolExecutor getExecutor() {
49             return executor;
50         }
51     }
52
53     private DummyListener listener;
54     private ListenerProperties props;
55
56     @Before
57     public void setup() throws Exception {
58         Properties regularProps = new Properties();
59         regularProps.load(getClass().getResourceAsStream("/org/openecomp/appc/default.properties"));
60         props = new ListenerProperties("", regularProps);
61         listener = new DummyListener(props);
62     }
63
64     @Test
65     public void testRun() {
66         Thread t = new Thread(listener);
67         t.run();
68         assertFalse(t.isAlive()); // Should die immediately
69     }
70
71     @Test
72     public void testStop() {
73         listener.stop();
74         assertFalse(listener.getRun());
75         assertTrue(listener.getExecutor().isShutdown());
76     }
77
78     @Test
79     public void testStopNow() {
80         listener.stopNow();
81         assertFalse(listener.getRun());
82         assertTrue(listener.getExecutor().isShutdown());
83     }
84
85     @Test
86     public void testBenchmark() {
87         String out = listener.getBenchmark();
88         assertNotNull(out);
89         assertTrue(out.contains(listener.getListenerId()));
90     }
91
92     @Test
93     public void testListenerId() {
94         assertEquals(props.getPrefix(), listener.getListenerId());
95         listener.setListenerId("newId");
96         assertEquals("newId", listener.getListenerId());
97     }
98 }