Merge "Fix for Bug 3"
[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.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;
22
23 import javax.management.JMX;
24 import javax.management.ObjectName;
25 import java.util.Collections;
26 import java.util.Map;
27
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;
38
39 public class ShutdownTest extends AbstractConfigTest {
40     private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
41     @Mock
42     private Bundle mockedSysBundle;
43
44     @Before
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();
52     }
53
54     @Test
55     public void testSingleton_invalidName() throws Exception {
56         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
57         try {
58             transaction.createModule(NAME, "foo");
59             fail();
60         } catch (IllegalArgumentException e) {
61             assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
62         }
63     }
64
65     @Test
66     public void testWithoutSecret() throws Exception {
67         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
68         transaction.createModule(NAME, NAME);
69         transaction.commit();
70         // test JMX rpc
71         ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
72         ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
73         try {
74             runtime.shutdown("foo", null);
75             fail();
76         } catch (IllegalArgumentException e) {
77             assertEquals("Invalid secret", e.getMessage());
78         }
79         runtime.shutdown("", null);
80         assertStopped();
81     }
82
83
84     @Test
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);
91         transaction.commit();
92         shutdownViaRuntimeJMX(secret);
93
94         // test old secret
95         transaction = configRegistryClient.createTransaction();
96         on = transaction.lookupConfigBean(NAME, NAME);
97         proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
98         try {
99             rethrowCause(proxy).getOldSecret();
100             fail();
101         } catch (UnsupportedOperationException e) {
102         }
103         try {
104             rethrowCause(proxy).getSecret();
105             fail();
106         } catch (UnsupportedOperationException e) {
107         }
108         // set secret to nothing
109         String newSecret = "newSecret";
110         proxy.setSecret(newSecret);
111         try {
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());
120
121         }
122         proxy.setOldSecret(secret);
123         transaction.commit();
124         shutdownViaRuntimeJMX(newSecret);
125     }
126
127     private void shutdownViaRuntimeJMX(String secret) throws Exception {
128         // test JMX rpc
129         ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
130         ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
131         try {
132             runtime.shutdown("", null);
133             fail();
134         } catch (IllegalArgumentException e) {
135             assertEquals("Invalid secret", e.getMessage());
136         }
137         runtime.shutdown(secret, null);
138         assertStopped();
139     }
140
141
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();
148     }
149 }