Bug-1520 Config subsystem:config-api tests 54/9254/4
authorMaros Marsalek <mmarsale@cisco.com>
Mon, 21 Jul 2014 11:34:13 +0000 (13:34 +0200)
committerMaros Marsalek <mmarsale@cisco.com>
Wed, 13 Aug 2014 14:59:01 +0000 (14:59 +0000)
Change-Id: Iefb8b8ac1a5099c276ab200a3a6337cb8befefe7
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/config/config-api/src/main/java/org/opendaylight/controller/config/api/jmx/ObjectNameUtil.java
opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/JmxAttributeValidationExceptionTest.java [new file with mode: 0644]
opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/ValidationExceptionTest.java [new file with mode: 0644]
opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/jmx/ObjectNameUtilTest.java [new file with mode: 0644]
opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/util/ObjectNameUtilTest.java [deleted file]

index d60e6086176daebd69f58a795ccadd4f24eca4de..abb9f1ae9bf4d448c78bc795a74f0076b68de745 100644 (file)
@@ -35,7 +35,6 @@ public class ObjectNameUtil {
     public static final String SERVICE_QNAME_KEY = "serviceQName";
     public static final String INSTANCE_NAME_KEY = "instanceName";
     public static final String TYPE_KEY = ConfigRegistryConstants.TYPE_KEY;
-    public static final String TYPE_CONFIG_REGISTRY = ConfigRegistryConstants.TYPE_CONFIG_REGISTRY;
     public static final String TYPE_CONFIG_TRANSACTION = "ConfigTransaction";
     public static final String TYPE_MODULE = "Module";
     public static final String TYPE_SERVICE_REFERENCE = "ServiceReference";
@@ -43,6 +42,7 @@ public class ObjectNameUtil {
     public static final String TRANSACTION_NAME_KEY = "TransactionName";
     public static final String REF_NAME_KEY = "RefName";
     private static final String REPLACED_QUOTATION_MARK = "\\?";
+    public static final String ON_WILDCARD = "*";
 
     public static ObjectName createON(String on) {
         try {
@@ -304,12 +304,9 @@ public class ObjectNameUtil {
 
     public static ObjectName createModulePattern(String moduleName,
                                                  String instanceName) {
-        if (moduleName == null) {
-            moduleName = "*";
-        }
-        if (instanceName == null) {
-            instanceName = "*";
-        }
+        moduleName = moduleName == null ? ON_WILDCARD : moduleName;
+        instanceName = instanceName == null ? ON_WILDCARD : instanceName;
+
         // do not return object names containing transaction name
         ObjectName namePattern = ObjectNameUtil
                 .createON(ObjectNameUtil.ON_DOMAIN + ":"
@@ -323,6 +320,10 @@ public class ObjectNameUtil {
 
     public static ObjectName createModulePattern(String ifcName,
                                                  String instanceName, String transactionName) {
+        ifcName = ifcName == null ? ON_WILDCARD : ifcName;
+        instanceName = instanceName == null ? ON_WILDCARD : instanceName;
+        transactionName = transactionName == null ? ON_WILDCARD : transactionName;
+
         return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
                 + "=" + ifcName + "," + ObjectNameUtil.INSTANCE_NAME_KEY + "="
@@ -332,6 +333,9 @@ public class ObjectNameUtil {
 
     public static ObjectName createRuntimeBeanPattern(String moduleName,
                                                       String instanceName) {
+        moduleName = moduleName == null ? ON_WILDCARD : moduleName;
+        instanceName = instanceName == null ? ON_WILDCARD : instanceName;
+
         return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN + ":"
                 + ObjectNameUtil.TYPE_KEY + "="
                 + ObjectNameUtil.TYPE_RUNTIME_BEAN + ","
diff --git a/opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/JmxAttributeValidationExceptionTest.java b/opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/JmxAttributeValidationExceptionTest.java
new file mode 100644 (file)
index 0000000..7a057bb
--- /dev/null
@@ -0,0 +1,51 @@
+package org.opendaylight.controller.config.api;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.Lists;
+import org.junit.Before;
+import org.junit.Test;
+
+public class JmxAttributeValidationExceptionTest {
+
+    private JmxAttribute jmxAttribute = new JmxAttribute("attr1");
+    private JmxAttribute jmxAttribute2 = new JmxAttribute("attr2");
+
+    @Before
+    public void setUp() throws Exception {
+
+    }
+
+    @Test
+    public void testGetAttributeNames() throws Exception {
+
+    }
+
+    @Test
+    public void testCheckNotNull() throws Exception {
+        try {
+            JmxAttributeValidationException.checkNotNull(false, "message", jmxAttribute);
+        } catch (JmxAttributeValidationException e) {
+            assertJmxEx(e, jmxAttribute.getAttributeName() + " " + "message", jmxAttribute);
+        }
+    }
+
+    @Test
+    public void testWrap() throws Exception {
+
+    }
+
+    @Test
+    public void testCheckCondition() throws Exception {
+        try {
+            JmxAttributeValidationException.checkCondition(false, "message", jmxAttribute);
+        } catch (JmxAttributeValidationException e) {
+            assertJmxEx(e, jmxAttribute.getAttributeName() + " " + "message", jmxAttribute);
+        }
+    }
+
+    private void assertJmxEx(JmxAttributeValidationException e, String message, JmxAttribute... attrNames) {
+        assertEquals(message, e.getMessage());
+        assertEquals(Lists.newArrayList(attrNames), e.getAttributeNames());
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/ValidationExceptionTest.java b/opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/ValidationExceptionTest.java
new file mode 100644 (file)
index 0000000..1809e45
--- /dev/null
@@ -0,0 +1,54 @@
+package org.opendaylight.controller.config.api;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.containsString;
+
+import com.google.common.collect.Lists;
+import java.util.Map;
+import org.junit.Test;
+
+public class ValidationExceptionTest {
+
+    private String instance = "instance";
+    private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
+    private String instance2 = "instance2";
+    private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
+    private final String message = "ex message";
+    private final Exception e = new IllegalStateException(message);
+
+    @Test
+    public void testCreateFromCollectedValidationExceptions() throws Exception {
+        ValidationException single = ValidationException.createForSingleException(mi, e);
+        ValidationException single2 = ValidationException.createForSingleException(mi2, e);
+
+        ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
+
+        Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
+        assertEquals(1, failedMap.size());
+        assertTrue(failedMap.containsKey("module"));
+
+        Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
+        assertEquals(2, failedModule.size());
+        assertTrue(failedModule.containsKey(instance));
+        assertEquals(message, failedModule.get(instance).getMessage());
+        assertEquals(message, failedModule.get(instance2).getMessage());
+        assertEquals(failedModule.get(instance), failedModule.get(instance2));
+    }
+
+    @Test
+    public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
+        ValidationException single = ValidationException.createForSingleException(mi, e);
+        ValidationException single2 = ValidationException.createForSingleException(mi, e);
+        try {
+            ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
+        } catch (IllegalArgumentException ex) {
+            // Duplicate exception
+            assertThat(ex.getMessage(), containsString("Cannot merge"));
+            return;
+        }
+        fail("Duplicate exception should have failed");
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/jmx/ObjectNameUtilTest.java b/opendaylight/config/config-api/src/test/java/org/opendaylight/controller/config/api/jmx/ObjectNameUtilTest.java
new file mode 100644 (file)
index 0000000..d3d8469
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.config.api.jmx;
+
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.base.Throwables;
+import com.google.common.collect.Maps;
+import java.util.Map;
+import javax.management.ObjectName;
+import junit.framework.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.config.api.ModuleIdentifier;
+
+public class ObjectNameUtilTest {
+
+    private String moduleName;
+    private String instanceName;
+
+    @Before
+    public void setUp() throws Exception {
+        moduleName = "module";
+        instanceName = "instance";
+    }
+
+    @Test
+    public void testServiceReferenceName() throws Exception {
+        String serviceQName = "(namespace?revision=r)qname";
+        String refName = "refName";
+        String transaction = "transaction";
+
+        ObjectName serviceReferenceON = ObjectNameUtil.createTransactionServiceON(transaction, serviceQName, refName);
+        ObjectNameUtil.checkType(serviceReferenceON, ObjectNameUtil.TYPE_SERVICE_REFERENCE);
+
+        assertFalse(serviceReferenceON.isPattern());
+        assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
+        assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
+        assertEquals(transaction, ObjectNameUtil.getTransactionName(serviceReferenceON));
+        assertEquals(ObjectNameUtil.createReadOnlyServiceON(serviceQName, refName), ObjectNameUtil.withoutTransactionName(serviceReferenceON));
+
+        serviceReferenceON = ObjectNameUtil.createReadOnlyServiceON(serviceQName, refName);
+        assertFalse(serviceReferenceON.isPattern());
+        assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
+        assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
+        assertEquals(null, ObjectNameUtil.getTransactionName(serviceReferenceON));
+    }
+
+    @Test
+    public void testModuleName() throws Exception {
+        String txName = "transaction";
+
+        ObjectName on = ObjectNameUtil.createTransactionModuleON(txName, moduleName, instanceName);
+
+        ObjectNameUtil.checkDomain(on);
+        ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_MODULE);
+
+        assertFalse(on.isPattern());
+        assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
+        assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
+        assertEquals(txName, ObjectNameUtil.getTransactionName(on));
+        assertEquals(4, ObjectNameUtil.getAdditionalProperties(on).size());
+
+        ObjectName withoutTx = ObjectNameUtil.withoutTransactionName(on);
+        assertEquals(ObjectNameUtil.createReadOnlyModuleON(moduleName, instanceName), withoutTx);
+        assertEquals(moduleName, ObjectNameUtil.getFactoryName(withoutTx));
+        assertEquals(instanceName, ObjectNameUtil.getInstanceName(withoutTx));
+        assertEquals(null, ObjectNameUtil.getTransactionName(withoutTx));
+        assertEquals(on, ObjectNameUtil.withTransactionName(withoutTx, txName));
+
+        ObjectName pattern = ObjectNameUtil.createModulePattern(moduleName, null);
+        assertPattern(withoutTx, pattern);
+        pattern = ObjectNameUtil.createModulePattern(moduleName, null, txName);
+        assertPattern(on, pattern);
+    }
+
+    private void assertPattern(ObjectName test, ObjectName pattern) {
+        assertTrue(pattern.isPattern());
+        assertTrue(pattern.apply(test));
+    }
+
+    @Test
+    public void testRuntimeBeanName() throws Exception {
+
+        Map<String, String> properties = Maps.newHashMap();
+        properties.put("p1", "value");
+        properties.put("p2", "value2");
+
+        ObjectName on = ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, properties);
+
+        ObjectNameUtil.checkDomain(on);
+        ObjectNameUtil.checkTypeOneOf(on, ObjectNameUtil.TYPE_RUNTIME_BEAN);
+
+        assertFalse(on.isPattern());
+        assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
+        assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
+        assertEquals(2, ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).size());
+        assertTrue(ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).containsKey("p1"));
+        assertEquals("value", ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).get("p1"));
+        assertTrue(ObjectNameUtil.getAdditionalProperties(on).containsKey("p2"));
+        assertEquals("value2", ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).get("p2"));
+
+        ObjectName pattern = ObjectNameUtil.createRuntimeBeanPattern(null, instanceName);
+        assertPattern(on, pattern);
+    }
+
+    @Test
+    public void testModuleIdentifier() throws Exception {
+        ModuleIdentifier mi = new ModuleIdentifier(moduleName, instanceName);
+        ObjectName on = ObjectNameUtil.createReadOnlyModuleON(mi);
+        assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
+        assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
+
+        assertEquals(mi, ObjectNameUtil.fromON(on, ObjectNameUtil.TYPE_MODULE));
+    }
+
+    @Test
+    public void testChecks() throws Exception {
+        final ObjectName on = ObjectNameUtil.createON("customDomain", ObjectNameUtil.TYPE_KEY, ObjectNameUtil.TYPE_MODULE);
+
+        assertFailure(new Runnable() {
+            @Override
+            public void run() {
+                ObjectNameUtil.checkTypeOneOf(on, ObjectNameUtil.TYPE_RUNTIME_BEAN, ObjectNameUtil.TYPE_CONFIG_TRANSACTION);
+            }
+        }, IllegalArgumentException.class);
+
+        assertFailure(new Runnable() {
+            @Override
+            public void run() {
+                ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_RUNTIME_BEAN);
+            }
+        }, IllegalArgumentException.class);
+
+        assertFailure(new Runnable() {
+            @Override
+            public void run() {
+                ObjectNameUtil.checkDomain(on);
+            }
+        }, IllegalArgumentException.class);
+    }
+
+    private void assertFailure(Runnable test, Class<? extends Exception> ex) {
+        try {
+            test.run();
+        } catch(Exception e) {
+            Assert.assertTrue("Failed with wrong exception: " + Throwables.getStackTraceAsString(e),
+                    e.getClass().isAssignableFrom(ex));
+            return;
+        }
+
+        fail(test + " should have failed on " + ex);
+    }
+}
diff --git a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/util/ObjectNameUtilTest.java b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/util/ObjectNameUtilTest.java
deleted file mode 100644 (file)
index fe32289..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.config.manager.impl.util;
-
-import com.google.common.base.Throwables;
-import com.google.common.collect.Sets;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
-
-import javax.management.ObjectName;
-import java.util.Set;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-
-public class ObjectNameUtilTest extends AbstractLockedPlatformMBeanServerTest {
-    private Set<ObjectName> unregisterONs;
-
-    @Before
-    public void initUnregisterList() {
-        unregisterONs = Sets.newHashSet();
-    }
-
-    @After
-    public void unregisterONs() {
-        Exception lastException = null;
-        for (ObjectName on : unregisterONs) {
-            try {
-                platformMBeanServer.unregisterMBean(on);
-            } catch (Exception e) {
-                lastException = e;
-            }
-        }
-        if (lastException != null) {
-            throw Throwables.propagate(lastException);
-        }
-    }
-
-    @Test
-    public void testQuotation() throws Exception {
-        String serviceQName = "(namespace?revision=r)qname";
-        String refName = "refName";
-        String transaction = "transaction";
-        ObjectName serviceReferenceON = ObjectNameUtil.createTransactionServiceON(transaction, serviceQName, refName);
-        assertFalse(serviceReferenceON.isPattern());
-        assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
-        assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
-        assertEquals(transaction, ObjectNameUtil.getTransactionName(serviceReferenceON));
-
-        serviceReferenceON = ObjectNameUtil.createReadOnlyServiceON(serviceQName, refName);
-        assertFalse(serviceReferenceON.isPattern());
-        assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
-        assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
-        assertEquals(null, ObjectNameUtil.getTransactionName(serviceReferenceON));
-
-    }
-}