shutdown-impl: final parameters
[controller.git] / opendaylight / config / shutdown-impl / src / test / java / org / opendaylight / controller / config / yang / shutdown / impl / ShutdownTest.java
index 5887e98f30daa4816da2bfb1e1215a6385ebc783..5dd856a605b82d0f6a80502f4e89158e7e14fcd8 100644 (file)
@@ -7,10 +7,23 @@
  */
 package org.opendaylight.controller.config.yang.shutdown.impl;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
+
+import java.util.Collections;
+import javax.management.InstanceNotFoundException;
+import javax.management.JMX;
+import javax.management.ObjectName;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.api.ConflictingVersionException;
+import org.opendaylight.controller.config.api.ValidationException;
 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
@@ -18,24 +31,12 @@ import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleF
 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
 import org.osgi.framework.Bundle;
 
-import javax.management.JMX;
-import javax.management.ObjectName;
-import java.util.Collections;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
-
 public class ShutdownTest extends AbstractConfigTest {
     private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
     @Mock
     private Bundle mockedSysBundle;
 
+
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
@@ -44,6 +45,12 @@ public class ShutdownTest extends AbstractConfigTest {
         doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
         mockedContext.getBundle(0);
         doNothing().when(mockedSysBundle).stop();
+        doReturn(mockedContext).when(mockedSysBundle).getBundleContext();
+        doReturn(new Bundle[]{mockedSysBundle}).when(mockedContext).getBundles();
+        doReturn("system bundle").when(mockedSysBundle).getSymbolicName();
+
+
+
         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
         // initialize default instance
         transaction.commit();
@@ -55,7 +62,7 @@ public class ShutdownTest extends AbstractConfigTest {
         try {
             transaction.createModule(NAME, "foo");
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (final IllegalArgumentException e) {
             assertEquals("Singleton enforcement failed. Expected instance name shutdown", e.getMessage());
         }
     }
@@ -70,7 +77,7 @@ public class ShutdownTest extends AbstractConfigTest {
         try {
             runtime.shutdown("foo", 60000L, null);
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (final IllegalArgumentException e) {
             assertEquals("Invalid secret", e.getMessage());
         }
         runtime.shutdown("", 60000L, null);
@@ -80,41 +87,46 @@ public class ShutdownTest extends AbstractConfigTest {
 
     @Test
     public void testWithSecret() throws Exception {
+        String secret = "secret";
+        setSecret(secret);
+        shutdownViaRuntimeJMX(secret);
+    }
+
+    private void setSecret(final String secret) throws InstanceNotFoundException, ValidationException, ConflictingVersionException {
         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
         ObjectName on = transaction.lookupConfigBean(NAME, NAME);
         ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
-        String secret = "secret";
         proxy.setSecret(secret);
         transaction.commit();
-        shutdownViaRuntimeJMX(secret);
+    }
+
+    @Test
+    public void testWrongSecret() throws Exception {
+        setSecret("secret");
         try {
             ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
             runtime.shutdown("foo", 60000L, null);
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (final IllegalArgumentException e) {
             assertEquals("Invalid secret", e.getMessage());
         }
     }
 
-    private void shutdownViaRuntimeJMX(String secret) throws Exception {
+    private void shutdownViaRuntimeJMX(final String secret) throws Exception {
         // test JMX rpc
         ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
         try {
             runtime.shutdown("", 60000L, null);
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (final IllegalArgumentException e) {
             assertEquals("Invalid secret", e.getMessage());
         }
         runtime.shutdown(secret, 60000L, null);
         assertStopped();
     }
 
-
     private void assertStopped() throws Exception {
-        Thread.sleep(2000); // happens on another thread
+        Thread.sleep(3000); // happens on another thread
         verify(mockedSysBundle).stop();
-        verifyNoMoreInteractions(mockedSysBundle);
-        reset(mockedSysBundle);
-        doNothing().when(mockedSysBundle).stop();
     }
 }