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
index 86cd6fa812fabb9dcd671afe9baa2bca714554de..5887e98f30daa4816da2bfb1e1215a6385ebc783 100644 (file)
@@ -11,8 +11,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.config.api.ValidationException;
-import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
 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;
@@ -23,11 +21,8 @@ import org.osgi.framework.Bundle;
 import javax.management.JMX;
 import javax.management.ObjectName;
 import java.util.Collections;
-import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
@@ -49,6 +44,9 @@ public class ShutdownTest extends AbstractConfigTest {
         doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
         mockedContext.getBundle(0);
         doNothing().when(mockedSysBundle).stop();
+        ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+        // initialize default instance
+        transaction.commit();
     }
 
     @Test
@@ -62,21 +60,20 @@ public class ShutdownTest extends AbstractConfigTest {
         }
     }
 
+    private static final ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
+
     @Test
     public void testWithoutSecret() throws Exception {
-        ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
-        transaction.createModule(NAME, NAME);
-        transaction.commit();
         // test JMX rpc
-        ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
+
         ShutdownRuntimeMXBean runtime = configRegistryClient.newMXBeanProxy(runtimeON, ShutdownRuntimeMXBean.class);
         try {
-            runtime.shutdown("foo", null);
+            runtime.shutdown("foo", 60000L, null);
             fail();
         } catch (IllegalArgumentException e) {
             assertEquals("Invalid secret", e.getMessage());
         }
-        runtime.shutdown("", null);
+        runtime.shutdown("", 60000L, null);
         assertStopped();
     }
 
@@ -84,57 +81,31 @@ public class ShutdownTest extends AbstractConfigTest {
     @Test
     public void testWithSecret() throws Exception {
         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
-        ObjectName on = transaction.createModule(NAME, NAME);
+        ObjectName on = transaction.lookupConfigBean(NAME, NAME);
         ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
         String secret = "secret";
         proxy.setSecret(secret);
         transaction.commit();
         shutdownViaRuntimeJMX(secret);
-
-        // test old secret
-        transaction = configRegistryClient.createTransaction();
-        on = transaction.lookupConfigBean(NAME, NAME);
-        proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
-        try {
-            rethrowCause(proxy).getOldSecret();
-            fail();
-        } catch (UnsupportedOperationException e) {
-        }
         try {
-            rethrowCause(proxy).getSecret();
+            ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
+            runtime.shutdown("foo", 60000L, null);
             fail();
-        } catch (UnsupportedOperationException e) {
-        }
-        // set secret to nothing
-        String newSecret = "newSecret";
-        proxy.setSecret(newSecret);
-        try {
-            transaction.commit();
-            fail("Old secret not provided - should fail validation");
-        } catch (ValidationException e) {
-            Map<String, Map<String, ExceptionMessageWithStackTrace>> failedValidations = e.getFailedValidations();
-            assertTrue(failedValidations.containsKey(NAME));
-            ExceptionMessageWithStackTrace exceptionMessageWithStackTrace = failedValidations.get(NAME).get(NAME);
-            assertNotNull(exceptionMessageWithStackTrace);
-            assertEquals("OldSecret Invalid old secret", exceptionMessageWithStackTrace.getMessage());
-
+        } catch (IllegalArgumentException e) {
+            assertEquals("Invalid secret", e.getMessage());
         }
-        proxy.setOldSecret(secret);
-        transaction.commit();
-        shutdownViaRuntimeJMX(newSecret);
     }
 
     private void shutdownViaRuntimeJMX(String secret) throws Exception {
         // test JMX rpc
-        ObjectName runtimeON = ObjectNameUtil.createRuntimeBeanName(NAME, NAME, Collections.<String, String>emptyMap());
         ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
         try {
-            runtime.shutdown("", null);
+            runtime.shutdown("", 60000L, null);
             fail();
         } catch (IllegalArgumentException e) {
             assertEquals("Invalid secret", e.getMessage());
         }
-        runtime.shutdown(secret, null);
+        runtime.shutdown(secret, 60000L, null);
         assertStopped();
     }