X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fshutdown-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fshutdown%2Fimpl%2FShutdownTest.java;h=5dd856a605b82d0f6a80502f4e89158e7e14fcd8;hp=5887e98f30daa4816da2bfb1e1215a6385ebc783;hb=1417bcd892b1dc9a4d68c9562d91eee8320de38f;hpb=765f74bd08b61de6896411eb1a9d8377a981ec82 diff --git a/opendaylight/config/shutdown-impl/src/test/java/org/opendaylight/controller/config/yang/shutdown/impl/ShutdownTest.java b/opendaylight/config/shutdown-impl/src/test/java/org/opendaylight/controller/config/yang/shutdown/impl/ShutdownTest.java index 5887e98f30..5dd856a605 100644 --- a/opendaylight/config/shutdown-impl/src/test/java/org/opendaylight/controller/config/yang/shutdown/impl/ShutdownTest.java +++ b/opendaylight/config/shutdown-impl/src/test/java/org/opendaylight/controller/config/yang/shutdown/impl/ShutdownTest.java @@ -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(); } }