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