2 * ============LICENSE_START=======================================================
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.listener.impl;
30 import static org.junit.Assert.assertTrue;
32 import java.util.HashSet;
34 import java.util.Properties;
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;
50 public class TestController {
52 private ListenerProperties listenerProperties;
53 private Set<ListenerProperties> properties = Mockito.spy(new HashSet<>());
54 private EELFLogger log = Mockito.spy(EELFManager.getInstance().getLogger(ControllerImpl.class));
57 public ExpectedException expectedEx = ExpectedException.none();
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());
68 public void testListeners() {
69 listenerProperties = Mockito.mock(ListenerProperties.class);
70 properties.add(listenerProperties);
71 assertTrue(new ControllerImpl(properties).getListeners() instanceof Map);
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();
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();