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.ValidationException;
15 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
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.JMX;
24 import javax.management.ObjectName;
25 import java.util.Collections;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32 import static org.mockito.Mockito.doNothing;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.reset;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.verifyNoMoreInteractions;
37 import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
39 public class ShutdownTest extends AbstractConfigTest {
40 private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
42 private Bundle mockedSysBundle;
45 public void setUp() throws Exception {
46 MockitoAnnotations.initMocks(this);
47 ModuleFactoriesResolver factoriesResolver = new HardcodedModuleFactoriesResolver(mockedContext, factory);
48 super.initConfigTransactionManagerImpl(factoriesResolver);
49 doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
50 mockedContext.getBundle(0);
51 doNothing().when(mockedSysBundle).stop();
55 public void testSingleton_invalidName() throws Exception {
56 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
58 transaction.createModule(NAME, "foo");
60 } catch (IllegalArgumentException e) {
61 assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
66 public void testWithoutSecret() throws Exception {
67 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
68 transaction.createModule(NAME, NAME);
71 ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
72 ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
74 runtime.shutdown("foo", null);
76 } catch (IllegalArgumentException e) {
77 assertEquals("Invalid secret", e.getMessage());
79 runtime.shutdown("", null);
85 public void testWithSecret() throws Exception {
86 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
87 ObjectName on = transaction.createModule(NAME, NAME);
88 ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
89 String secret = "secret";
90 proxy.setSecret(secret);
92 shutdownViaRuntimeJMX(secret);
95 transaction = configRegistryClient.createTransaction();
96 on = transaction.lookupConfigBean(NAME, NAME);
97 proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
99 rethrowCause(proxy).getOldSecret();
101 } catch (UnsupportedOperationException e) {
104 rethrowCause(proxy).getSecret();
106 } catch (UnsupportedOperationException e) {
108 // set secret to nothing
109 String newSecret = "newSecret";
110 proxy.setSecret(newSecret);
112 transaction.commit();
113 fail("Old secret not provided - should fail validation");
114 } catch (ValidationException e) {
115 Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = e.getFailedValidations();
116 assertTrue(failedValidations.containsKey(NAME));
117 ExceptionMessageWithStackTrace exceptionMessageWithStackTrace = failedValidations.get(NAME).get(NAME);
118 assertNotNull(exceptionMessageWithStackTrace);
119 assertEquals("OldSecret Invalid old secret", exceptionMessageWithStackTrace.getMessage());
122 proxy.setOldSecret(secret);
123 transaction.commit();
124 shutdownViaRuntimeJMX(newSecret);
127 private void shutdownViaRuntimeJMX(String secret) throws Exception {
129 ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
130 ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
132 runtime.shutdown("", null);
134 } catch (IllegalArgumentException e) {
135 assertEquals("Invalid secret", e.getMessage());
137 runtime.shutdown(secret, null);
142 private void assertStopped() throws Exception {
143 Thread.sleep(2000); // happens on another thread
144 verify(mockedSysBundle).stop();
145 verifyNoMoreInteractions(mockedSysBundle);
146 reset(mockedSysBundle);
147 doNothing().when(mockedSysBundle).stop();