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.ConflictingVersionException;
15 import org.opendaylight.controller.config.api.ValidationException;
16 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
17 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
18 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
19 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
20 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
21 import org.osgi.framework.Bundle;
23 import javax.management.InstanceNotFoundException;
24 import javax.management.JMX;
25 import javax.management.ObjectName;
26 import java.util.Collections;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.fail;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.verify;
33 import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
35 public class ShutdownTest extends AbstractConfigTest {
36 private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
38 private Bundle mockedSysBundle, mockedConfigManager;
42 public void setUp() throws Exception {
43 MockitoAnnotations.initMocks(this);
44 ModuleFactoriesResolver factoriesResolver = new HardcodedModuleFactoriesResolver(mockedContext, factory);
45 super.initConfigTransactionManagerImpl(factoriesResolver);
46 doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
47 mockedContext.getBundle(0);
48 doNothing().when(mockedSysBundle).stop();
49 doNothing().when(mockedConfigManager).stop();
50 doReturn(mockedContext).when(mockedSysBundle).getBundleContext();
51 doReturn(new Bundle[]{mockedSysBundle, mockedConfigManager}).when(mockedContext).getBundles();
52 doReturn("system bundle").when(mockedSysBundle).getSymbolicName();
53 doReturn(StopSystemBundleThread.CONFIG_MANAGER_SYMBOLIC_NAME).when(mockedConfigManager).getSymbolicName();
56 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
57 // initialize default instance
62 public void testSingleton_invalidName() throws Exception {
63 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
65 transaction.createModule(NAME, "foo");
67 } catch (IllegalArgumentException e) {
68 assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
72 private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
75 public void testWithoutSecret() throws Exception {
78 ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
80 runtime.shutdown("foo", 60000L, null);
82 } catch (IllegalArgumentException e) {
83 assertEquals("Invalid secret", e.getMessage());
85 runtime.shutdown("", 60000L, null);
91 public void testWithSecret() throws Exception {
92 String secret = "secret";
94 shutdownViaRuntimeJMX(secret);
97 private void setSecret(String secret) throws InstanceNotFoundException, ValidationException, ConflictingVersionException {
98 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
99 ObjectName on = transaction.lookupConfigBean(NAME, NAME);
100 ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
101 proxy.setSecret(secret);
102 transaction.commit();
106 public void testWrongSecret() throws Exception {
109 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
110 runtime.shutdown("foo", 60000L, null);
112 } catch (IllegalArgumentException e) {
113 assertEquals("Invalid secret", e.getMessage());
117 private void shutdownViaRuntimeJMX(String secret) throws Exception {
119 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
121 runtime.shutdown("", 60000L, null);
123 } catch (IllegalArgumentException e) {
124 assertEquals("Invalid secret", e.getMessage());
126 runtime.shutdown(secret, 60000L, null);
130 private void assertStopped() throws Exception {
131 Thread.sleep(3000); // happens on another thread
132 verify(mockedConfigManager).stop();
133 verify(mockedSysBundle).stop();