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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.matchers.JUnitMatchers.hasItem;
16 import java.lang.management.ManagementFactory;
17 import java.util.HashMap;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.config.api.ConfigRegistry;
31 import com.google.common.collect.Sets;
33 public class ConfigRegistryClientsTest {
35 private TestingConfigRegistry testingRegistry;
36 private ObjectName testingRegistryON;
37 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
38 private ConfigRegistryClient jmxRegistryClient;
39 private ConfigTransactionClient jmxTransactionClient;
40 private Map<String, ObjectName> map;
43 public void setUp() throws Exception {
44 testingRegistry = new TestingConfigRegistry();
45 testingRegistryON = ConfigRegistry.OBJECT_NAME;
46 mbs.registerMBean(testingRegistry, testingRegistryON);
47 jmxRegistryClient = new ConfigRegistryJMXClient(
48 ManagementFactory.getPlatformMBeanServer());
49 map = new HashMap<>();
53 public void cleanUp() throws Exception {
54 if (testingRegistryON != null) {
55 mbs.unregisterMBean(testingRegistryON);
60 public void testCreateTransaction() throws Exception{
61 jmxTransactionClient = jmxRegistryClient.createTransaction();
62 assertNotNull(jmxTransactionClient);
66 public void testGetConfigTransactionClient2() throws Exception{
67 jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient("transactionName");
68 assertNotNull(jmxTransactionClient);
72 public void testGetConfigTransactionClient() throws Exception{
73 jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient(testingRegistryON);
74 assertNotNull(jmxTransactionClient);
77 @Test(expected = IllegalArgumentException.class)
78 public void testNewMXBeanProxy() throws Exception{
79 if (jmxRegistryClient instanceof ConfigRegistryJMXClient) {
80 ConfigRegistryJMXClient client = (ConfigRegistryJMXClient) jmxRegistryClient;
81 assertNull(client.newMXBeanProxy(testingRegistryON, String.class));
83 throw new AssertionError("brm msg");
88 public void testBeginConfig() throws Exception{
89 Assert.assertNotNull(jmxRegistryClient.beginConfig());
93 public void testCommitConfig() throws Exception{
94 assertNull(jmxRegistryClient.commitConfig(testingRegistryON));
98 public void testGetOpenConfigs() throws Exception{
99 assertNull(jmxRegistryClient.getOpenConfigs());
102 @Test(expected = RuntimeException.class)
103 public void testGetVersion() throws Exception{
104 assertEquals(3, jmxRegistryClient.getVersion());
108 public void testGetAvailableModuleNames() throws Exception{
109 assertNull(jmxRegistryClient.getAvailableModuleNames());
113 public void testIsHealthy() throws Exception{
114 assertEquals(false, jmxRegistryClient.isHealthy());
118 public void testLookupConfigBeans3() throws Exception{
119 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans();
120 assertEquals(3, son.size());
124 public void testLookupConfigBeans2() throws Exception{
125 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1);
126 assertEquals(2, son.size());
130 public void testLookupConfigBeans() throws Exception{
131 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1);
132 Set<ObjectName> on = Sets.newHashSet(TestingConfigRegistry.conf2);
133 assertEquals(on, son);
137 public void testLookupConfigBean() throws Exception{
138 ObjectName on = jmxRegistryClient.lookupConfigBean(TestingConfigRegistry.moduleName1, null);
139 assertEquals(TestingConfigRegistry.conf3, on);
143 public void testLookupRuntimeBeans() throws Exception {
144 Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
145 assertEquals(Sets.newHashSet(TestingConfigRegistry.run2, TestingConfigRegistry.run1, TestingConfigRegistry.run3), jmxLookup);
148 private Set<ObjectName> lookupRuntimeBeans(ConfigRegistryClient client)
150 Set<ObjectName> beans = client.lookupRuntimeBeans();
151 for (ObjectName on : beans) {
152 assertEquals("RuntimeBean", on.getKeyProperty("type"));
154 assertEquals(3, beans.size());
159 public void testLookupRuntimeBeansWithIfcNameAndInstanceName()
160 throws InstanceNotFoundException {
161 Set<ObjectName> jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
162 jmxRegistryClient, TestingConfigRegistry.moduleName1,
163 TestingConfigRegistry.instName1);
164 assertEquals(1, jmxLookup.size());
165 assertEquals(Sets.newHashSet(TestingConfigRegistry.run2), jmxLookup);
167 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
168 jmxRegistryClient, TestingConfigRegistry.moduleName2,
169 TestingConfigRegistry.instName2);
170 assertEquals(1, jmxLookup.size());
171 assertEquals(Sets.newHashSet(TestingConfigRegistry.run3), jmxLookup);
173 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
174 jmxRegistryClient, TestingConfigRegistry.moduleName1,
175 TestingConfigRegistry.instName2);
176 assertEquals(0, jmxLookup.size());
177 assertEquals(Sets.newHashSet(), jmxLookup);
180 private Set<ObjectName> clientLookupRuntimeBeansWithModuleAndInstance(
181 ConfigRegistryClient client, String moduleName, String instanceName) {
182 Set<ObjectName> beans = client.lookupRuntimeBeans(moduleName, instanceName);
183 if (beans.size() > 0) {
184 assertEquals("RuntimeBean",
185 beans.iterator().next().getKeyProperty("type"));
191 public void testCheckConfigBeanExists() throws Exception{
192 jmxRegistryClient.checkConfigBeanExists(testingRegistryON);
193 assertEquals(true, TestingConfigRegistry.checkBool);
197 public void testLookupConfigBeanByServiceInterfaceName() throws Exception{
198 ObjectName on = clientLookupConfigBeanByServiceInterfaceName();
199 assertEquals(TestingConfigRegistry.conf1, on);
202 private ObjectName clientLookupConfigBeanByServiceInterfaceName(){
203 return jmxRegistryClient.lookupConfigBeanByServiceInterfaceName("qnameA", "refA");
207 public void testGetServiceMapping() throws Exception{
208 assertNull(jmxRegistryClient.getServiceMapping());
212 public void testLookupServiceReferencesByServiceInterfaceName() throws Exception{
213 map.put("conf2", TestingConfigRegistry.conf2);
214 assertEquals(map, jmxRegistryClient.lookupServiceReferencesByServiceInterfaceName("qnameB"));
218 public void testLookupServiceInterfaceNames() throws Exception{
219 assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName1));
220 assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName2));
223 private Set<String> clientLookupServiceInterfaceNames(ObjectName client) throws InstanceNotFoundException{
224 return jmxRegistryClient.lookupServiceInterfaceNames(client);
228 public void testGetServiceInterfaceName() throws Exception{
229 assertNull(jmxRegistryClient.getServiceInterfaceName(null, null));
232 @Test(expected = RuntimeException.class)
233 public void testInvokeMethod() throws Exception{
234 assertNull(jmxRegistryClient.invokeMethod(testingRegistryON, "name", null, null));
237 @Test(expected = RuntimeException.class)
238 public void testGetAttributeCurrentValue() throws Exception{
239 assertNull(jmxRegistryClient.getAttributeCurrentValue(testingRegistryON, "attrName"));
243 public void testGetAvailableModuleFactoryQNames() throws Exception{
244 for(String str : jmxRegistryClient.getAvailableModuleFactoryQNames()){
245 if(str != TestingConfigRegistry.moduleName1){
246 assertEquals(TestingConfigRegistry.moduleName2, str);
249 assertEquals(TestingConfigRegistry.moduleName1, str);
255 public void testGetServiceReference() throws Exception{
256 Assert.assertNotNull(jmxRegistryClient.getServiceReference(null, null));
259 @Test(expected = UnsupportedOperationException.class)
260 public void testcheckServiceReferenceExists() throws Exception{
261 jmxRegistryClient.checkServiceReferenceExists(testingRegistryON);