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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
17 import java.util.Collections;
18 import javax.management.InstanceNotFoundException;
19 import javax.management.JMX;
20 import javax.management.ObjectName;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.config.api.ConflictingVersionException;
26 import org.opendaylight.controller.config.api.ValidationException;
27 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
28 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
29 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
30 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
31 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
32 import org.osgi.framework.Bundle;
34 public class ShutdownTest extends AbstractConfigTest {
35 private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
37 private Bundle mockedSysBundle;
41 public void setUp() throws Exception {
42 MockitoAnnotations.initMocks(this);
43 ModuleFactoriesResolver factoriesResolver = new HardcodedModuleFactoriesResolver(mockedContext, factory);
44 super.initConfigTransactionManagerImpl(factoriesResolver);
45 doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
46 mockedContext.getBundle(0);
47 doNothing().when(mockedSysBundle).stop();
48 doReturn(mockedContext).when(mockedSysBundle).getBundleContext();
49 doReturn(new Bundle[]{mockedSysBundle}).when(mockedContext).getBundles();
50 doReturn("system bundle").when(mockedSysBundle).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 (final 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 (final 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(final String secret) throws InstanceNotFoundException, ValidationException, ConflictingVersionException {
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 (final IllegalArgumentException e) {
111 assertEquals("Invalid secret", e.getMessage());
115 private void shutdownViaRuntimeJMX(final String secret) throws Exception {
117 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
119 runtime.shutdown("", 60000L, null);
121 } catch (final 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(mockedSysBundle).stop();