added test case to TestController.java
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / impl / TestController.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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Modifications Copyright (C) 2019 IBM
12  * =============================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  * 
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  * 
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  * 
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.listener.impl;
29
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.HashSet;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.Set;
36 import java.util.concurrent.ThreadPoolExecutor;
37 import java.util.concurrent.TimeUnit;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mockito;
42 import org.onap.appc.listener.Controller;
43 import org.onap.appc.listener.Listener;
44 import org.onap.appc.listener.ListenerProperties;
45 import org.onap.appc.listener.demo.impl.ListenerImpl;
46 import org.powermock.reflect.Whitebox;
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFManager;
49
50 public class TestController {
51
52     private ListenerProperties listenerProperties;
53     private Set<ListenerProperties> properties = Mockito.spy(new HashSet<>());
54     private EELFLogger log = Mockito.spy(EELFManager.getInstance().getLogger(ControllerImpl.class));
55
56     @Rule
57     public ExpectedException expectedEx = ExpectedException.none();
58
59     @Test
60     public void testExceptionConstructor() {
61         listenerProperties = Mockito.mock(ListenerProperties.class);
62         properties.add(listenerProperties);
63         new ControllerImpl(properties);
64         Mockito.verify(properties).remove(Mockito.any());
65     }
66     
67     @Test
68     public void testListeners() {
69         listenerProperties = Mockito.mock(ListenerProperties.class);
70         properties.add(listenerProperties);
71         assertTrue(new ControllerImpl(properties).getListeners() instanceof Map);
72         
73     }
74
75     @Test
76     public void testStartException() throws NoSuchMethodException, SecurityException {
77         Properties props = new Properties();
78         props.put("TEST", "TEST");
79         listenerProperties = Mockito.spy(new ListenerProperties("TEST", props));
80         listenerProperties.setListenerClass(Listener.class);
81         properties.add(listenerProperties);
82         ControllerImpl controllerImpl = new ControllerImpl(properties);
83         controllerImpl.start();
84         Mockito.verify(listenerProperties, Mockito.times(2)).getListenerClass();
85     }
86
87     @Test
88     public void testStopException() throws NoSuchMethodException, SecurityException, InterruptedException {
89         Properties props = new Properties();
90         props.put("TEST", "TEST");
91         listenerProperties = Mockito.spy(new ListenerProperties("TEST", props));
92         listenerProperties.setListenerClass(Listener.class);
93         properties.add(listenerProperties);
94         ControllerImpl controllerImpl = new ControllerImpl(properties);
95         //controllerImpl.start();
96         Map<String, Listener> map = Whitebox.getInternalState(controllerImpl, "listeners");
97         map.put("TEST", new ListenerImpl(listenerProperties));
98         ThreadPoolExecutor executor = Mockito.mock(ThreadPoolExecutor.class);
99         Mockito.when(executor.awaitTermination(300, TimeUnit.SECONDS)).thenReturn(false);
100         Whitebox.setInternalState(controllerImpl, "executor", executor);
101         controllerImpl.stop(false);
102         Mockito.verify(executor).shutdown();
103     }
104 }