2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.util;
10 import com.google.common.collect.Sets;
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.config.api.ConfigRegistry;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.MBeanServer;
18 import javax.management.ObjectName;
19 import java.lang.management.ManagementFactory;
22 import static org.junit.Assert.assertEquals;
24 public class ConfigRegistryClientsTest {
26 private TestingConfigRegistry testingRegistry;
27 private ObjectName testingRegistryON;
28 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
29 private ConfigRegistryClient jmxRegistryClient;
32 public void setUp() throws Exception {
33 testingRegistry = new TestingConfigRegistry();
34 testingRegistryON = ConfigRegistry.OBJECT_NAME;
35 mbs.registerMBean(testingRegistry, testingRegistryON);
36 jmxRegistryClient = new ConfigRegistryJMXClient(
37 ManagementFactory.getPlatformMBeanServer());
41 public void cleanUp() throws Exception {
42 if (testingRegistryON != null) {
43 mbs.unregisterMBean(testingRegistryON);
48 public void testLookupRuntimeBeans() throws Exception {
49 Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
50 assertEquals(Sets.newHashSet(testingRegistry.run2, testingRegistry.run1, testingRegistry.run3), jmxLookup);
53 private Set<ObjectName> lookupRuntimeBeans(ConfigRegistryClient client)
55 Set<ObjectName> beans = client.lookupRuntimeBeans();
56 for (ObjectName on : beans) {
57 assertEquals("RuntimeBean", on.getKeyProperty("type"));
59 assertEquals(3, beans.size());
64 public void testLookupRuntimeBeansWithIfcNameAndInstanceName()
65 throws InstanceNotFoundException {
66 Set<ObjectName> jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
67 jmxRegistryClient, TestingConfigRegistry.moduleName1,
68 TestingConfigRegistry.instName1);
69 assertEquals(1, jmxLookup.size());
70 assertEquals(Sets.newHashSet(testingRegistry.run2), jmxLookup);
72 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
73 jmxRegistryClient, TestingConfigRegistry.moduleName2,
74 TestingConfigRegistry.instName2);
75 assertEquals(1, jmxLookup.size());
76 assertEquals(Sets.newHashSet(testingRegistry.run3), jmxLookup);
78 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
79 jmxRegistryClient, TestingConfigRegistry.moduleName1,
80 TestingConfigRegistry.instName2);
81 assertEquals(0, jmxLookup.size());
82 assertEquals(Sets.newHashSet(), jmxLookup);
85 private Set<ObjectName> clientLookupRuntimeBeansWithModuleAndInstance(
86 ConfigRegistryClient client, String moduleName, String instanceName) {
87 Set<ObjectName> beans = client.lookupRuntimeBeans(moduleName, instanceName);
88 if (beans.size() > 0) {
89 assertEquals("RuntimeBean",
90 beans.iterator().next().getKeyProperty("type"));