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.hamcrest.CoreMatchers.hasItem;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThat;
15 import com.google.common.collect.Sets;
16 import java.lang.management.ManagementFactory;
17 import java.util.HashMap;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.MBeanServer;
22 import javax.management.ObjectName;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.ConfigRegistry;
29 public class ConfigRegistryClientsTest {
31 private TestingConfigRegistry testingRegistry;
32 private ObjectName testingRegistryON;
33 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
34 private ConfigRegistryClient jmxRegistryClient;
35 private ConfigTransactionClient jmxTransactionClient;
36 private Map<String, ObjectName> map;
39 public void setUp() throws Exception {
40 testingRegistry = new TestingConfigRegistry();
41 testingRegistryON = ConfigRegistry.OBJECT_NAME;
42 mbs.registerMBean(testingRegistry, testingRegistryON);
43 jmxRegistryClient = new ConfigRegistryJMXClient(
44 ManagementFactory.getPlatformMBeanServer());
45 map = new HashMap<>();
49 public void cleanUp() throws Exception {
50 if (testingRegistryON != null) {
51 mbs.unregisterMBean(testingRegistryON);
56 public void testCreateTransaction() throws Exception{
57 jmxTransactionClient = jmxRegistryClient.createTransaction();
58 assertNotNull(jmxTransactionClient);
62 public void testGetConfigTransactionClient2() throws Exception{
63 jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient("transactionName");
64 assertNotNull(jmxTransactionClient);
68 public void testGetConfigTransactionClient() throws Exception{
69 jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient(testingRegistryON);
70 assertNotNull(jmxTransactionClient);
73 @Test(expected = IllegalArgumentException.class)
74 public void testNewMXBeanProxy() throws Exception{
75 if (jmxRegistryClient instanceof ConfigRegistryJMXClient) {
76 ConfigRegistryJMXClient client = (ConfigRegistryJMXClient) jmxRegistryClient;
77 assertNull(client.newMXBeanProxy(testingRegistryON, String.class));
79 throw new AssertionError("brm msg");
84 public void testBeginConfig() throws Exception{
85 Assert.assertNotNull(jmxRegistryClient.beginConfig());
89 public void testCommitConfig() throws Exception{
90 assertNull(jmxRegistryClient.commitConfig(testingRegistryON));
94 public void testGetOpenConfigs() throws Exception{
95 assertNull(jmxRegistryClient.getOpenConfigs());
98 @Test(expected = RuntimeException.class)
99 public void testGetVersion() throws Exception{
100 assertEquals(3, jmxRegistryClient.getVersion());
104 public void testGetAvailableModuleNames() throws Exception{
105 assertNull(jmxRegistryClient.getAvailableModuleNames());
109 public void testIsHealthy() throws Exception{
110 assertEquals(false, jmxRegistryClient.isHealthy());
114 public void testLookupConfigBeans3() throws Exception{
115 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans();
116 assertEquals(3, son.size());
120 public void testLookupConfigBeans2() throws Exception{
121 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1);
122 assertEquals(2, son.size());
126 public void testLookupConfigBeans() throws Exception{
127 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1);
128 Set<ObjectName> on = Sets.newHashSet(TestingConfigRegistry.conf2);
129 assertEquals(on, son);
133 public void testLookupConfigBean() throws Exception{
134 ObjectName on = jmxRegistryClient.lookupConfigBean(TestingConfigRegistry.moduleName1, null);
135 assertEquals(TestingConfigRegistry.conf3, on);
139 public void testLookupRuntimeBeans() throws Exception {
140 Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
141 assertEquals(Sets.newHashSet(TestingConfigRegistry.run2, TestingConfigRegistry.run1, TestingConfigRegistry.run3), jmxLookup);
144 private Set<ObjectName> lookupRuntimeBeans(final ConfigRegistryClient client)
146 Set<ObjectName> beans = client.lookupRuntimeBeans();
147 for (ObjectName on : beans) {
148 assertEquals("RuntimeBean", on.getKeyProperty("type"));
150 assertEquals(3, beans.size());
155 public void testLookupRuntimeBeansWithIfcNameAndInstanceName()
156 throws InstanceNotFoundException {
157 Set<ObjectName> jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
158 jmxRegistryClient, TestingConfigRegistry.moduleName1,
159 TestingConfigRegistry.instName1);
160 assertEquals(1, jmxLookup.size());
161 assertEquals(Sets.newHashSet(TestingConfigRegistry.run2), jmxLookup);
163 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
164 jmxRegistryClient, TestingConfigRegistry.moduleName2,
165 TestingConfigRegistry.instName2);
166 assertEquals(1, jmxLookup.size());
167 assertEquals(Sets.newHashSet(TestingConfigRegistry.run3), jmxLookup);
169 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
170 jmxRegistryClient, TestingConfigRegistry.moduleName1,
171 TestingConfigRegistry.instName2);
172 assertEquals(0, jmxLookup.size());
173 assertEquals(Sets.newHashSet(), jmxLookup);
176 private Set<ObjectName> clientLookupRuntimeBeansWithModuleAndInstance(
177 final ConfigRegistryClient client, final String moduleName, final String instanceName) {
178 Set<ObjectName> beans = client.lookupRuntimeBeans(moduleName, instanceName);
179 if (beans.size() > 0) {
180 assertEquals("RuntimeBean",
181 beans.iterator().next().getKeyProperty("type"));
187 public void testCheckConfigBeanExists() throws Exception{
188 jmxRegistryClient.checkConfigBeanExists(testingRegistryON);
189 assertEquals(true, TestingConfigRegistry.checkBool);
193 public void testLookupConfigBeanByServiceInterfaceName() throws Exception{
194 ObjectName on = clientLookupConfigBeanByServiceInterfaceName();
195 assertEquals(TestingConfigRegistry.conf1, on);
198 private ObjectName clientLookupConfigBeanByServiceInterfaceName(){
199 return jmxRegistryClient.lookupConfigBeanByServiceInterfaceName("qnameA", "refA");
203 public void testGetServiceMapping() throws Exception{
204 assertNull(jmxRegistryClient.getServiceMapping());
208 public void testLookupServiceReferencesByServiceInterfaceName() throws Exception{
209 map.put("conf2", TestingConfigRegistry.conf2);
210 assertEquals(map, jmxRegistryClient.lookupServiceReferencesByServiceInterfaceName("qnameB"));
214 public void testLookupServiceInterfaceNames() throws Exception{
215 assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName1));
216 assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName2));
219 private Set<String> clientLookupServiceInterfaceNames(final ObjectName client) throws InstanceNotFoundException{
220 return jmxRegistryClient.lookupServiceInterfaceNames(client);
224 public void testGetServiceInterfaceName() throws Exception{
225 assertNull(jmxRegistryClient.getServiceInterfaceName(null, null));
228 @Test(expected = RuntimeException.class)
229 public void testInvokeMethod() throws Exception{
230 assertNull(jmxRegistryClient.invokeMethod(testingRegistryON, "name", null, null));
233 @Test(expected = RuntimeException.class)
234 public void testGetAttributeCurrentValue() throws Exception{
235 assertNull(jmxRegistryClient.getAttributeCurrentValue(testingRegistryON, "attrName"));
239 public void testGetAvailableModuleFactoryQNames() throws Exception{
240 for(String str : jmxRegistryClient.getAvailableModuleFactoryQNames()){
241 if(str != TestingConfigRegistry.moduleName1){
242 assertEquals(TestingConfigRegistry.moduleName2, str);
245 assertEquals(TestingConfigRegistry.moduleName1, str);
251 public void testGetServiceReference() throws Exception{
252 Assert.assertNotNull(jmxRegistryClient.getServiceReference(null, null));
255 @Test(expected = UnsupportedOperationException.class)
256 public void testcheckServiceReferenceExists() throws Exception{
257 jmxRegistryClient.checkServiceReferenceExists(testingRegistryON);