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";
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 {
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 + ":"
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 + "="
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 + ","
--- /dev/null
+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
--- /dev/null
+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
--- /dev/null
+/*
+ * 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);
+ }
+}
+++ /dev/null
-/*
- * 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));
-
- }
-}