Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[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 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;
16
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;
33
34 public class ShutdownTest extends AbstractConfigTest {
35     private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
36     @Mock
37     private Bundle mockedSysBundle;
38
39
40     @Before
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();
51
52
53
54         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
55         // initialize default instance
56         transaction.commit();
57     }
58
59     @Test
60     public void testSingleton_invalidName() throws Exception {
61         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
62         try {
63             transaction.createModule(NAME, "foo");
64             fail();
65         } catch (IllegalArgumentException e) {
66             assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
67         }
68     }
69
70     private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
71
72     @Test
73     public void testWithoutSecret() throws Exception {
74         // test JMX rpc
75
76         ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
77         try {
78             runtime.shutdown("foo", 60000L, null);
79             fail();
80         } catch (IllegalArgumentException e) {
81             assertEquals("Invalid secret", e.getMessage());
82         }
83         runtime.shutdown("", 60000L, null);
84         assertStopped();
85     }
86
87
88     @Test
89     public void testWithSecret() throws Exception {
90         String secret = "secret";
91         setSecret(secret);
92         shutdownViaRuntimeJMX(secret);
93     }
94
95     private void setSecret(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();
101     }
102
103     @Test
104     public void testWrongSecret() throws Exception {
105         setSecret("secret");
106         try {
107             ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
108             runtime.shutdown("foo", 60000L, null);
109             fail();
110         } catch (IllegalArgumentException e) {
111             assertEquals("Invalid secret", e.getMessage());
112         }
113     }
114
115     private void shutdownViaRuntimeJMX(String secret) throws Exception {
116         // test JMX rpc
117         ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
118         try {
119             runtime.shutdown("", 60000L, null);
120             fail();
121         } catch (IllegalArgumentException e) {
122             assertEquals("Invalid secret", e.getMessage());
123         }
124         runtime.shutdown(secret, 60000L, null);
125         assertStopped();
126     }
127
128     private void assertStopped() throws Exception {
129         Thread.sleep(3000); // happens on another thread
130         verify(mockedSysBundle).stop();
131     }
132 }