2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.yang.shutdown.impl;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.mockito.Mock;
13 import org.mockito.MockitoAnnotations;
14 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
15 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
16 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
17 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
18 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
19 import org.osgi.framework.Bundle;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.JMX;
23 import javax.management.ObjectName;
24 import java.util.Collections;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.verify;
31 import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
33 public class ShutdownTest extends AbstractConfigTest {
34 private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
36 private Bundle mockedSysBundle, mockedConfigManager;
40 public void setUp() throws Exception {
41 MockitoAnnotations.initMocks(this);
42 ModuleFactoriesResolver factoriesResolver = new HardcodedModuleFactoriesResolver(mockedContext, factory);
43 super.initConfigTransactionManagerImpl(factoriesResolver);
44 doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
45 mockedContext.getBundle(0);
46 doNothing().when(mockedSysBundle).stop();
47 doNothing().when(mockedConfigManager).stop();
48 doReturn(mockedContext).when(mockedSysBundle).getBundleContext();
49 doReturn(new Bundle[]{mockedSysBundle, mockedConfigManager}).when(mockedContext).getBundles();
50 doReturn("system bundle").when(mockedSysBundle).getSymbolicName();
51 doReturn(StopSystemBundleThread.CONFIG_MANAGER_SYMBOLIC_NAME).when(mockedConfigManager).getSymbolicName();
54 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
55 // initialize default instance
60 public void testSingleton_invalidName() throws Exception {
61 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
63 transaction.createModule(NAME, "foo");
65 } catch (IllegalArgumentException e) {
66 assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
70 private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
73 public void testWithoutSecret() throws Exception {
76 ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
78 runtime.shutdown("foo", 60000L, null);
80 } catch (IllegalArgumentException e) {
81 assertEquals("Invalid secret", e.getMessage());
83 runtime.shutdown("", 60000L, null);
89 public void testWithSecret() throws Exception {
90 String secret = "secret";
92 shutdownViaRuntimeJMX(secret);
95 private void setSecret(String secret) throws InstanceNotFoundException {
96 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
97 ObjectName on = transaction.lookupConfigBean(NAME, NAME);
98 ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
99 proxy.setSecret(secret);
100 transaction.commit();
104 public void testWrongSecret() throws Exception {
107 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
108 runtime.shutdown("foo", 60000L, null);
110 } catch (IllegalArgumentException e) {
111 assertEquals("Invalid secret", e.getMessage());
115 private void shutdownViaRuntimeJMX(String secret) throws Exception {
117 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
119 runtime.shutdown("", 60000L, null);
121 } catch (IllegalArgumentException e) {
122 assertEquals("Invalid secret", e.getMessage());
124 runtime.shutdown(secret, 60000L, null);
128 private void assertStopped() throws Exception {
129 Thread.sleep(3000); // happens on another thread
130 verify(mockedConfigManager).stop();
131 verify(mockedSysBundle).stop();