Merge "This sanity test is sporatically failing in Jenkins for no good reason. Rever...
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / ConfigRegistryClientsTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.util;
9
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;
15
16 import javax.management.InstanceNotFoundException;
17 import javax.management.MBeanServer;
18 import javax.management.ObjectName;
19 import java.lang.management.ManagementFactory;
20 import java.util.Set;
21
22 import static org.junit.Assert.assertEquals;
23
24 public class ConfigRegistryClientsTest {
25
26     private TestingConfigRegistry testingRegistry;
27     private ObjectName testingRegistryON;
28     private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
29     private ConfigRegistryClient jmxRegistryClient;
30
31     @Before
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());
38     }
39
40     @After
41     public void cleanUp() throws Exception {
42         if (testingRegistryON != null) {
43             mbs.unregisterMBean(testingRegistryON);
44         }
45     }
46
47     @Test
48     public void testLookupRuntimeBeans() throws Exception {
49         Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
50         assertEquals(Sets.newHashSet(testingRegistry.run2, testingRegistry.run1, testingRegistry.run3), jmxLookup);
51     }
52
53     private Set<ObjectName> lookupRuntimeBeans(ConfigRegistryClient client)
54             throws Exception {
55         Set<ObjectName> beans = client.lookupRuntimeBeans();
56         for (ObjectName on : beans) {
57             assertEquals("RuntimeBean", on.getKeyProperty("type"));
58         }
59         assertEquals(3, beans.size());
60         return beans;
61     }
62
63     @Test
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);
71
72         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
73                 jmxRegistryClient, TestingConfigRegistry.moduleName2,
74                 TestingConfigRegistry.instName2);
75         assertEquals(1, jmxLookup.size());
76         assertEquals(Sets.newHashSet(testingRegistry.run3), jmxLookup);
77
78         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
79                 jmxRegistryClient, TestingConfigRegistry.moduleName1,
80                 TestingConfigRegistry.instName2);
81         assertEquals(0, jmxLookup.size());
82         assertEquals(Sets.newHashSet(), jmxLookup);
83     }
84
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"));
91         }
92         return beans;
93     }
94 }