Merge "Fix some sonar bugs in config-api and config-manager."
[controller.git] / opendaylight / config / shutdown-impl / src / test / java / org / opendaylight / controller / config / yang / shutdown / impl / ShutdownTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.shutdown.impl;
9
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;
22
23 import javax.management.InstanceNotFoundException;
24 import javax.management.JMX;
25 import javax.management.ObjectName;
26 import java.util.Collections;
27
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;
34
35 public class ShutdownTest extends AbstractConfigTest {
36     private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
37     @Mock
38     private Bundle mockedSysBundle, mockedConfigManager;
39
40
41     @Before
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();
54
55
56         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
57         // initialize default instance
58         transaction.commit();
59     }
60
61     @Test
62     public void testSingleton_invalidName() throws Exception {
63         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
64         try {
65             transaction.createModule(NAME, "foo");
66             fail();
67         } catch (IllegalArgumentException e) {
68             assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
69         }
70     }
71
72     private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
73
74     @Test
75     public void testWithoutSecret() throws Exception {
76         // test JMX rpc
77
78         ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
79         try {
80             runtime.shutdown("foo", 60000L, null);
81             fail();
82         } catch (IllegalArgumentException e) {
83             assertEquals("Invalid secret", e.getMessage());
84         }
85         runtime.shutdown("", 60000L, null);
86         assertStopped();
87     }
88
89
90     @Test
91     public void testWithSecret() throws Exception {
92         String secret = "secret";
93         setSecret(secret);
94         shutdownViaRuntimeJMX(secret);
95     }
96
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();
103     }
104
105     @Test
106     public void testWrongSecret() throws Exception {
107         setSecret("secret");
108         try {
109             ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
110             runtime.shutdown("foo", 60000L, null);
111             fail();
112         } catch (IllegalArgumentException e) {
113             assertEquals("Invalid secret", e.getMessage());
114         }
115     }
116
117     private void shutdownViaRuntimeJMX(String secret) throws Exception {
118         // test JMX rpc
119         ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
120         try {
121             runtime.shutdown("", 60000L, null);
122             fail();
123         } catch (IllegalArgumentException e) {
124             assertEquals("Invalid secret", e.getMessage());
125         }
126         runtime.shutdown(secret, 60000L, null);
127         assertStopped();
128     }
129
130     private void assertStopped() throws Exception {
131         Thread.sleep(3000); // happens on another thread
132         verify(mockedConfigManager).stop();
133         verify(mockedSysBundle).stop();
134     }
135 }