Refactor shutdown-impl: add parameter to RPC, remove secret validation and masking.
[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.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;
20
21 import javax.management.JMX;
22 import javax.management.ObjectName;
23 import java.util.Collections;
24
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;
33
34 public class ShutdownTest extends AbstractConfigTest {
35     private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
36     @Mock
37     private Bundle mockedSysBundle;
38
39     @Before
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
49         transaction.commit();
50     }
51
52     @Test
53     public void testSingleton_invalidName() throws Exception {
54         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
55         try {
56             transaction.createModule(NAME, "foo");
57             fail();
58         } catch (IllegalArgumentException e) {
59             assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
60         }
61     }
62
63     private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
64
65     @Test
66     public void testWithoutSecret() throws Exception {
67         // test JMX rpc
68
69         ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
70         try {
71             runtime.shutdown("foo", 60000L, null);
72             fail();
73         } catch (IllegalArgumentException e) {
74             assertEquals("Invalid secret", e.getMessage());
75         }
76         runtime.shutdown("", 60000L, null);
77         assertStopped();
78     }
79
80
81     @Test
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);
88         transaction.commit();
89         shutdownViaRuntimeJMX(secret);
90         try {
91             ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
92             runtime.shutdown("foo", 60000L, null);
93             fail();
94         } catch (IllegalArgumentException e) {
95             assertEquals("Invalid secret", e.getMessage());
96         }
97     }
98
99     private void shutdownViaRuntimeJMX(String secret) throws Exception {
100         // test JMX rpc
101         ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
102         try {
103             runtime.shutdown("", 60000L, null);
104             fail();
105         } catch (IllegalArgumentException e) {
106             assertEquals("Invalid secret", e.getMessage());
107         }
108         runtime.shutdown(secret, 60000L, null);
109         assertStopped();
110     }
111
112
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();
119     }
120 }