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.JMX;
22 import javax.management.ObjectName;
23 import java.util.Collections;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.reset;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyNoMoreInteractions;
32 import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
34 public class ShutdownTest extends AbstractConfigTest {
35 private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
37 private Bundle mockedSysBundle;
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 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
48 // initialize default instance
53 public void testSingleton_invalidName() throws Exception {
54 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
56 transaction.createModule(NAME, "foo");
58 } catch (IllegalArgumentException e) {
59 assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
63 private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
66 public void testWithoutSecret() throws Exception {
69 ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
71 runtime.shutdown("foo", 60000L, null);
73 } catch (IllegalArgumentException e) {
74 assertEquals("Invalid secret", e.getMessage());
76 runtime.shutdown("", 60000L, null);
82 public void testWithSecret() throws Exception {
83 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
84 ObjectName on = transaction.lookupConfigBean(NAME, NAME);
85 ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
86 String secret = "secret";
87 proxy.setSecret(secret);
89 shutdownViaRuntimeJMX(secret);
91 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
92 runtime.shutdown("foo", 60000L, null);
94 } catch (IllegalArgumentException e) {
95 assertEquals("Invalid secret", e.getMessage());
99 private void shutdownViaRuntimeJMX(String secret) throws Exception {
101 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
103 runtime.shutdown("", 60000L, null);
105 } catch (IllegalArgumentException e) {
106 assertEquals("Invalid secret", e.getMessage());
108 runtime.shutdown(secret, 60000L, null);
113 private void assertStopped() throws Exception {
114 Thread.sleep(2000); // happens on another thread
115 verify(mockedSysBundle).stop();
116 verifyNoMoreInteractions(mockedSysBundle);
117 reset(mockedSysBundle);
118 doNothing().when(mockedSysBundle).stop();