config-manager: final parameters
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / threadpool / test / SimpleConfigurationTest.java
index 6372cb35c2ae87b17b1930f2c9a2291c4a8c96e9..d837ffcdb481ade6eb41c7182b521c20232c08d5 100644 (file)
@@ -10,25 +10,21 @@ package org.opendaylight.controller.config.manager.testingservices.threadpool.te
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.junit.matchers.JUnitMatchers.containsString;
 
+import com.google.common.collect.Sets;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ThreadPoolExecutor;
-
-import javax.management.DynamicMBean;
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanException;
 import javax.management.ObjectName;
 import javax.management.ReflectionException;
-import javax.management.RuntimeMBeanException;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -37,7 +33,7 @@ import org.opendaylight.controller.config.api.ValidationException;
 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
 import org.opendaylight.controller.config.api.jmx.CommitStatus;
 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.AbstractConfigWithJolokiaTest;
+import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
@@ -45,7 +41,6 @@ import org.opendaylight.controller.config.manager.testingservices.threadpool.Tes
 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
 import org.opendaylight.controller.config.util.ConfigTransactionClient;
 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
-import org.opendaylight.controller.config.util.jolokia.ConfigTransactionJolokiaClient;
 
 /**
  * Tests basic functionality of configuration registry:
@@ -59,8 +54,8 @@ import org.opendaylight.controller.config.util.jolokia.ConfigTransactionJolokiaC
  * Only one bean is being configured - {@link TestingThreadPoolIfc} which has no
  * dependencies.
  */
-public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
-    private final int numberOfThreads = 5;
+public class SimpleConfigurationTest extends AbstractConfigTest {
+    private static final int numberOfThreads = 5;
     private final int numberOfThreads2 = 10;
     private static final String fixed1 = "fixed1";
     private static final List<ObjectName> emptyONs = Collections
@@ -72,7 +67,7 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
 
     @Before
     public void setUp() {
-        super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
+        super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,
                 new TestingFixedThreadPoolModuleFactory()));
     }
 
@@ -100,8 +95,8 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         return fixed1names;
     }
 
-    private ObjectName createFixedThreadPool(
-            ConfigTransactionJMXClient transaction)
+    static ObjectName createFixedThreadPool(
+            final ConfigTransactionJMXClient transaction)
             throws InstanceAlreadyExistsException, InstanceNotFoundException {
         transaction.assertVersion(0, 1);
 
@@ -135,16 +130,10 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         testValidation(transaction);
     }
 
-    @Test
-    public void testValidationUsingJolokiaClient() throws Exception {
-        ConfigTransactionClient transaction = configRegistryJolokiaClient
-                .createTransaction();
-        testValidation(transaction);
-    }
 
-    private void testValidation(ConfigTransactionClient transaction)
+    private static void testValidation(final ConfigTransactionClient transaction)
             throws InstanceAlreadyExistsException, ReflectionException,
-            InstanceNotFoundException, MBeanException {
+            InstanceNotFoundException, MBeanException, ConflictingVersionException {
         ObjectName fixed1names = transaction.createModule(
                 TestingFixedThreadPoolModuleFactory.NAME, fixed1);
         // call validate on config bean
@@ -152,8 +141,8 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
             platformMBeanServer.invoke(fixed1names, "validate", new Object[0],
                     new String[0]);
             fail();
-        } catch (RuntimeMBeanException e) {
-            RuntimeException targetException = e.getTargetException();
+        } catch (final MBeanException e) {
+            Exception targetException = e.getTargetException();
             assertNotNull(targetException);
             assertEquals(ValidationException.class, targetException.getClass());
         }
@@ -162,7 +151,7 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         try {
             transaction.validateBean(fixed1names);
             fail();
-        } catch (ValidationException e) {
+        } catch (final ValidationException e) {
             for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
                     .getFailedValidations().entrySet()) {
                 for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
@@ -177,7 +166,7 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         try {
             transaction.validateConfig();
             fail();
-        } catch (ValidationException e) {
+        } catch (final ValidationException e) {
             for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
                     .getFailedValidations().entrySet()) {
                 for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
@@ -190,7 +179,7 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         }
         try {
             transaction.commit();
-        } catch (ValidationException e) {
+        } catch (final ValidationException e) {
             for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
                     .getFailedValidations().entrySet()) {
                 for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
@@ -223,11 +212,9 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         ObjectName fixed1name = firstCommit();
 
         // 2, check that configuration was copied to platform
-        DynamicMBean dynamicMBean = configRegistryClient.newMBeanProxy(
-                ObjectNameUtil.withoutTransactionName(fixed1name),
-                DynamicMBean.class);
-        dynamicMBean.getMBeanInfo();
-        assertEquals(numberOfThreads, dynamicMBean.getAttribute("ThreadCount"));
+        ObjectName on = ObjectNameUtil.withoutTransactionName(fixed1name);
+        platformMBeanServer.getMBeanInfo(on);
+        assertEquals(numberOfThreads, platformMBeanServer.getAttribute(on, "ThreadCount"));
 
         // 3, shutdown fixed1 in new transaction
         assertFalse(TestingFixedThreadPool.allExecutors.get(0).isShutdown());
@@ -256,15 +243,14 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
 
         // 4, check
         assertEquals(2, configRegistryClient.getVersion());
-        assertEquals(1, TestingFixedThreadPool.allExecutors.size());
-        assertTrue(TestingFixedThreadPool.allExecutors.get(0).isShutdown());
+        assertEquals(0, TestingFixedThreadPool.allExecutors.size());
 
         // dynamic config should be removed from platform
         try {
-            dynamicMBean.getMBeanInfo();
+            platformMBeanServer.getMBeanInfo(on);
             fail();
-        } catch (Exception e) {
-            assertTrue(e.getCause() instanceof InstanceNotFoundException);
+        } catch (final Exception e) {
+            assertTrue(e instanceof InstanceNotFoundException);
         }
     }
 
@@ -288,11 +274,11 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         // commit
         transaction.commit();
         // check that first threadpool is closed
-        checkThreadPools(2, numberOfThreads2);
+        checkThreadPools(1, numberOfThreads2);
     }
 
-    private void checkThreadPools(int expectedTotalNumberOfExecutors,
-            int expectedNumberOfThreadsInLastExecutor) {
+    private static void checkThreadPools(final int expectedTotalNumberOfExecutors,
+            final int expectedNumberOfThreadsInLastExecutor) {
         assertEquals(expectedTotalNumberOfExecutors,
                 TestingFixedThreadPool.allExecutors.size());
         for (int i = 0; i < expectedTotalNumberOfExecutors - 1; i++) {
@@ -318,15 +304,14 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         // commit
         CommitStatus commitStatus = transaction.commit();
         // check that new threadpool is created and old one is closed
-        checkThreadPools(2, numberOfThreads);
+        checkThreadPools(1, numberOfThreads);
         CommitStatus expected = new CommitStatus(emptyONs, emptyONs, fixed1List);
         assertEquals(expected, commitStatus);
     }
 
     // return MBeanProxy for 'fixed1' and current transaction
-    private TestingFixedThreadPoolConfigMXBean startReconfiguringFixed1ThreadPool(
-            ConfigTransactionJMXClient transaction)
-            throws InstanceNotFoundException {
+    private static TestingFixedThreadPoolConfigMXBean startReconfiguringFixed1ThreadPool(
+            final ConfigTransactionJMXClient transaction) throws InstanceNotFoundException {
         ObjectName fixed1name = transaction.lookupConfigBean(
                 TestingFixedThreadPoolModuleFactory.NAME, fixed1);
 
@@ -336,26 +321,19 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
     }
 
     @Test
-    public void testAbort() {
+    public void testAbort() throws Exception {
         ConfigTransactionJMXClient transaction = configRegistryClient
                 .createTransaction();
         assertEquals(1, configRegistryClient.getOpenConfigs().size());
 
         transaction.abortConfig();
+        assertEquals(0, configRegistryClient.getOpenConfigs().size());
         try {
-            transaction.createModule(TestingFixedThreadPoolModuleFactory.NAME,
-                    fixed1);
-            fail();
-        } catch (Exception e) {
-            assertTrue(e.getCause() instanceof InstanceNotFoundException);
-        }
-        try {
-            transaction.validateConfig();
+            platformMBeanServer.getMBeanInfo(transaction.getObjectName());
             fail();
-        } catch (Exception e) {
-            assertTrue(e.getCause() instanceof InstanceNotFoundException);
+        }catch(final InstanceNotFoundException e){
+            assertEquals("org.opendaylight.controller:TransactionName=ConfigTransaction-0-1,type=ConfigTransaction", e.getMessage());
         }
-        assertEquals(0, configRegistryClient.getOpenConfigs().size());
     }
 
     @Test
@@ -369,7 +347,7 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         try {
             transaction1.commit();
             fail();
-        } catch (ConflictingVersionException e) {
+        } catch (final ConflictingVersionException e) {
             assertEquals(
                     "Optimistic lock failed. Expected parent version 2, was 0",
                     e.getMessage());
@@ -387,75 +365,20 @@ public class SimpleConfigurationTest extends AbstractConfigWithJolokiaTest {
         try {
             configRegistryClient.commitConfig(transaction1.getObjectName());
             fail();
-        } catch (ConflictingVersionException e) {
-            assertEquals(
-                    "Optimistic lock failed. Expected parent version 2, was 0",
-                    e.getMessage());
-        }
-    }
-
-    @Test
-    public void testOptimisticLock_ConfigTransactionJolokiaClient()
-            throws Exception {
-        ConfigTransactionJolokiaClient transaction1 = configRegistryJolokiaClient
-                .createTransaction();
-        ConfigTransactionJolokiaClient transaction2 = configRegistryJolokiaClient
-                .createTransaction();
-        transaction2.assertVersion(0, 2);
-        transaction2.commit();
-        try {
-            transaction1.commit();
-            fail();
-        } catch (ConflictingVersionException e) {
+        } catch (final ConflictingVersionException e) {
             assertEquals(
                     "Optimistic lock failed. Expected parent version 2, was 0",
                     e.getMessage());
         }
     }
 
-    @Test
-    public void testOptimisticLock_ConfigRegistryJolokiaClient()
-            throws Exception {
-        ConfigTransactionJolokiaClient transaction1 = configRegistryJolokiaClient
-                .createTransaction();
-        ConfigTransactionJolokiaClient transaction2 = configRegistryJolokiaClient
-                .createTransaction();
-        transaction2.assertVersion(0, 2);
-        transaction2.commit();
-        try {
-            configRegistryJolokiaClient.commitConfig(transaction1
-                    .getObjectName());
-            fail();
-        } catch (ConflictingVersionException e) {
-            assertEquals(
-                    "Optimistic lock failed. Expected parent version 2, was 0",
-                    e.getMessage());
-        }
-    }
 
     @Test
-    public void testUsingJolokia() throws Exception {
-        ConfigTransactionJolokiaClient transactionClient = configRegistryJolokiaClient
-                .createTransaction();
-
-        ObjectName name = transactionClient.createModule(
-                TestingFixedThreadPoolModuleFactory.NAME, fixed1);
-
-        try {
-            transactionClient.validateConfig();
-            fail();
-        } catch (ValidationException e) {
-            assertThat(
-                    e.getMessage(),
-                    containsString("Parameter 'threadCount' must be greater than 0"));
-        }
+    public void testQNames() {
+        Set<String> availableModuleFactoryQNames = configRegistryClient.getAvailableModuleFactoryQNames();
+        String expected = "(namespace?revision=2012-12-12)name";
 
-        transactionClient.setAttribute(name, "ThreadCount", numberOfThreads);
-        // commit
-        CommitStatus commitStatus = transactionClient.commit();
-        CommitStatus expected = new CommitStatus(Arrays.asList(ObjectNameUtil
-                .withoutTransactionName(name)), emptyONs, emptyONs);
-        assertEquals(expected, commitStatus);
+        assertEquals(Sets.newHashSet(expected), availableModuleFactoryQNames);
     }
 
 }