Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / ConfigRegistryImplLookupTest.java
1 /*
2  * Copyright (c) 2013, 2017 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.manager.impl;
9
10 import static org.hamcrest.core.Is.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13
14 import com.google.common.collect.Sets;
15 import java.lang.management.ManagementFactory;
16 import java.lang.reflect.Field;
17 import java.util.Collections;
18 import java.util.Map;
19 import java.util.Set;
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.ObjectName;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.config.api.ModuleIdentifier;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
28 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
29 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
30 import org.opendaylight.controller.config.manager.impl.runtimembean.TestingRuntimeBean;
31
32 public class ConfigRegistryImplLookupTest extends AbstractLockedPlatformMBeanServerTest {
33
34     private ConfigRegistryImpl configRegistryImpl;
35     private BaseJMXRegistrator baseJMXRegistrator;
36
37     private static final String MODULE_NAMEA = "moduleA";
38     private static final String MODULE_NAMEB = "moduleB";
39
40     private static final String INSTANCE_NAMEA = "instA";
41     private static final String INSTANCE_NAMEB = "instB";
42     private static final String INSTANCE_NAMEC = "instC";
43
44     private static final ObjectName NAME1 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEA, INSTANCE_NAMEA);
45     private static final ObjectName NAME2 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEA, INSTANCE_NAMEB);
46     private static final ObjectName NAME3 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEA, INSTANCE_NAMEC);
47     private static final ObjectName NAME4 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEB, INSTANCE_NAMEA);
48
49     private static final ObjectName NAME5 = ObjectNameUtil.createRuntimeBeanName(MODULE_NAMEA, INSTANCE_NAMEA,
50             Collections.<String, String>emptyMap());
51     private static final ObjectName NAME6 = ObjectNameUtil.createRuntimeBeanName(MODULE_NAMEA, INSTANCE_NAMEB,
52             Collections.<String, String>emptyMap());
53     private static final ObjectName NAME8 = ObjectNameUtil.createRuntimeBeanName(MODULE_NAMEB, INSTANCE_NAMEA,
54             Collections.<String, String>emptyMap());
55
56     private static final ObjectName NAME9 = ObjectNameUtil.createTransactionModuleON("transaction", MODULE_NAMEA,
57             INSTANCE_NAMEA);
58
59     @Before
60     public void setUp() throws Exception {
61         configRegistryImpl = new ConfigRegistryImpl(null, ManagementFactory.getPlatformMBeanServer(), null);
62         Field field = configRegistryImpl.getClass().getDeclaredField("baseJMXRegistrator");
63         field.setAccessible(true);
64         baseJMXRegistrator = (BaseJMXRegistrator) field.get(configRegistryImpl);
65
66         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME1);
67         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME2);
68         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME3);
69         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME4);
70
71         registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME5);
72         registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME6);
73         registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME8);
74
75         baseJMXRegistrator.createTransactionJMXRegistrator("transaction").createTransactionModuleJMXRegistrator()
76                 .registerMBean(new TestingRuntimeBean(), NAME9);
77
78     }
79
80     private static void registerModuleBean(final TestingRuntimeBean testingRuntimeBean,
81             final BaseJMXRegistrator baseJMXRegistrator, final ObjectName objectName)
82             throws InstanceAlreadyExistsException {
83         baseJMXRegistrator.createModuleJMXRegistrator().registerMBean(testingRuntimeBean, objectName);
84     }
85
86     private static void registerRuntimeBean(final RuntimeBean object, final BaseJMXRegistrator baseJMXRegistrator,
87             final ObjectName runtimeON) throws InstanceAlreadyExistsException {
88         String factoryName = ObjectNameUtil.getFactoryName(runtimeON);
89         String instanceName = ObjectNameUtil.getInstanceName(runtimeON);
90         Map<String, String> properties = ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(runtimeON);
91
92         RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = baseJMXRegistrator
93                 .createRuntimeBeanRegistrator(new ModuleIdentifier(factoryName, instanceName));
94
95         assertThat(properties.isEmpty(), is(true));
96
97         runtimeBeanRegistrator.registerRoot(object);
98     }
99
100     @After
101     public void cleanUp() {
102         baseJMXRegistrator.close();
103     }
104
105     @Test
106     public void testLookupConfigBeans() throws Exception {
107         Set<ObjectName> beans = configRegistryImpl.lookupConfigBeans();
108         assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3, NAME4), beans);
109         beans = configRegistryImpl.lookupConfigBeans();
110         assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3, NAME4), beans);
111     }
112
113     @Test
114     public void testLookupConfigBeanWithModuleName() throws Exception {
115         Set<ObjectName> bean = configRegistryImpl.lookupConfigBeans(MODULE_NAMEA);
116         assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3), bean);
117     }
118
119     @Test
120     public void testLookupConfigBeanWithModuleNameAndInstanceName() throws Exception {
121         Set<ObjectName> bean = configRegistryImpl.lookupConfigBeans(MODULE_NAMEA, INSTANCE_NAMEA);
122         assertEquals(Sets.newHashSet(NAME1), bean);
123     }
124
125     @Test
126     public void testLookupRuntimeBeans() throws Exception {
127         Set<ObjectName> beans = configRegistryImpl.lookupRuntimeBeans();
128         assertEquals(Sets.newHashSet(NAME5, NAME6, NAME8), beans);
129         beans = configRegistryImpl.lookupRuntimeBeans(null, null);
130         assertEquals(Sets.newHashSet(NAME5, NAME6, NAME8), beans);
131     }
132
133     @Test
134     public void testLookupRuntimeBeansWithIFcNameAndImplName() throws Exception {
135         Set<ObjectName> beans = configRegistryImpl.lookupRuntimeBeans(MODULE_NAMEA, INSTANCE_NAMEA);
136         assertEquals(Sets.newHashSet(NAME5), beans);
137     }
138 }