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;
16 import com.google.common.collect.Sets;
17 import java.lang.management.ManagementFactory;
18 import java.util.HashMap;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.config.api.ConfigRegistry;
30 public class ConfigRegistryClientsTest {
32 private TestingConfigRegistry testingRegistry;
33 private ObjectName testingRegistryON;
34 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
35 private ConfigRegistryClient jmxRegistryClient;
36 private ConfigTransactionClient jmxTransactionClient;
37 private Map<String, ObjectName> map;
40 public void setUp() throws Exception {
41 testingRegistry = new TestingConfigRegistry();
42 testingRegistryON = ConfigRegistry.OBJECT_NAME;
43 mbs.registerMBean(testingRegistry, testingRegistryON);
44 jmxRegistryClient = new ConfigRegistryJMXClient(
45 ManagementFactory.getPlatformMBeanServer());
46 map = new HashMap<>();
50 public void cleanUp() throws Exception {
51 if (testingRegistryON != null) {
52 mbs.unregisterMBean(testingRegistryON);
57 public void testCreateTransaction() throws Exception{
58 jmxTransactionClient = jmxRegistryClient.createTransaction();
59 assertNotNull(jmxTransactionClient);
63 public void testGetConfigTransactionClient2() throws Exception{
64 jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient("transactionName");
65 assertNotNull(jmxTransactionClient);
69 public void testGetConfigTransactionClient() throws Exception{
70 jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient(testingRegistryON);
71 assertNotNull(jmxTransactionClient);
74 @Test(expected = IllegalArgumentException.class)
75 public void testNewMXBeanProxy() throws Exception{
76 if (jmxRegistryClient instanceof ConfigRegistryJMXClient) {
77 ConfigRegistryJMXClient client = (ConfigRegistryJMXClient) jmxRegistryClient;
78 assertNull(client.newMXBeanProxy(testingRegistryON, String.class));
80 throw new AssertionError("brm msg");
85 public void testBeginConfig() throws Exception{
86 Assert.assertNotNull(jmxRegistryClient.beginConfig());
90 public void testCommitConfig() throws Exception{
91 assertNull(jmxRegistryClient.commitConfig(testingRegistryON));
95 public void testGetOpenConfigs() throws Exception{
96 assertNull(jmxRegistryClient.getOpenConfigs());
99 @Test(expected = RuntimeException.class)
100 public void testGetVersion() throws Exception{
101 assertEquals(3, jmxRegistryClient.getVersion());
105 public void testGetAvailableModuleNames() throws Exception{
106 assertNull(jmxRegistryClient.getAvailableModuleNames());
110 public void testIsHealthy() throws Exception{
111 assertEquals(false, jmxRegistryClient.isHealthy());
115 public void testLookupConfigBeans3() throws Exception{
116 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans();
117 assertEquals(3, son.size());
121 public void testLookupConfigBeans2() throws Exception{
122 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1);
123 assertEquals(2, son.size());
127 public void testLookupConfigBeans() throws Exception{
128 Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1);
129 Set<ObjectName> on = Sets.newHashSet(TestingConfigRegistry.conf2);
130 assertEquals(on, son);
134 public void testLookupConfigBean() throws Exception{
135 ObjectName on = jmxRegistryClient.lookupConfigBean(TestingConfigRegistry.moduleName1, null);
136 assertEquals(TestingConfigRegistry.conf3, on);
140 public void testLookupRuntimeBeans() throws Exception {
141 Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
142 assertEquals(Sets.newHashSet(TestingConfigRegistry.run2, TestingConfigRegistry.run1, TestingConfigRegistry.run3), jmxLookup);
145 private Set<ObjectName> lookupRuntimeBeans(final ConfigRegistryClient client)
147 Set<ObjectName> beans = client.lookupRuntimeBeans();
148 for (ObjectName on : beans) {
149 assertEquals("RuntimeBean", on.getKeyProperty("type"));
151 assertEquals(3, beans.size());
156 public void testLookupRuntimeBeansWithIfcNameAndInstanceName()
157 throws InstanceNotFoundException {
158 Set<ObjectName> jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
159 jmxRegistryClient, TestingConfigRegistry.moduleName1,
160 TestingConfigRegistry.instName1);
161 assertEquals(1, jmxLookup.size());
162 assertEquals(Sets.newHashSet(TestingConfigRegistry.run2), jmxLookup);
164 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
165 jmxRegistryClient, TestingConfigRegistry.moduleName2,
166 TestingConfigRegistry.instName2);
167 assertEquals(1, jmxLookup.size());
168 assertEquals(Sets.newHashSet(TestingConfigRegistry.run3), jmxLookup);
170 jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
171 jmxRegistryClient, TestingConfigRegistry.moduleName1,
172 TestingConfigRegistry.instName2);
173 assertEquals(0, jmxLookup.size());
174 assertEquals(Sets.newHashSet(), jmxLookup);
177 private Set<ObjectName> clientLookupRuntimeBeansWithModuleAndInstance(
178 final ConfigRegistryClient client, final String moduleName, final String instanceName) {
179 Set<ObjectName> beans = client.lookupRuntimeBeans(moduleName, instanceName);
180 if (beans.size() > 0) {
181 assertEquals("RuntimeBean",
182 beans.iterator().next().getKeyProperty("type"));
188 public void testCheckConfigBeanExists() throws Exception{
189 jmxRegistryClient.checkConfigBeanExists(testingRegistryON);
190 assertEquals(true, TestingConfigRegistry.checkBool);
194 public void testLookupConfigBeanByServiceInterfaceName() throws Exception{
195 ObjectName on = clientLookupConfigBeanByServiceInterfaceName();
196 assertEquals(TestingConfigRegistry.conf1, on);
199 private ObjectName clientLookupConfigBeanByServiceInterfaceName(){
200 return jmxRegistryClient.lookupConfigBeanByServiceInterfaceName("qnameA", "refA");
204 public void testGetServiceMapping() throws Exception{
205 assertNull(jmxRegistryClient.getServiceMapping());
209 public void testLookupServiceReferencesByServiceInterfaceName() throws Exception{
210 map.put("conf2", TestingConfigRegistry.conf2);
211 assertEquals(map, jmxRegistryClient.lookupServiceReferencesByServiceInterfaceName("qnameB"));
215 public void testLookupServiceInterfaceNames() throws Exception{
216 assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName1));
217 assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName2));
220 private Set<String> clientLookupServiceInterfaceNames(final ObjectName client) throws InstanceNotFoundException{
221 return jmxRegistryClient.lookupServiceInterfaceNames(client);
225 public void testGetServiceInterfaceName() throws Exception{
226 assertNull(jmxRegistryClient.getServiceInterfaceName(null, null));
229 @Test(expected = RuntimeException.class)
230 public void testInvokeMethod() throws Exception{
231 assertNull(jmxRegistryClient.invokeMethod(testingRegistryON, "name", null, null));
234 @Test(expected = RuntimeException.class)
235 public void testGetAttributeCurrentValue() throws Exception{
236 assertNull(jmxRegistryClient.getAttributeCurrentValue(testingRegistryON, "attrName"));
240 public void testGetAvailableModuleFactoryQNames() throws Exception{
241 for(String str : jmxRegistryClient.getAvailableModuleFactoryQNames()){
242 if(str != TestingConfigRegistry.moduleName1){
243 assertEquals(TestingConfigRegistry.moduleName2, str);
246 assertEquals(TestingConfigRegistry.moduleName1, str);
252 public void testGetServiceReference() throws Exception{
253 Assert.assertNotNull(jmxRegistryClient.getServiceReference(null, null));
256 @Test(expected = UnsupportedOperationException.class)
257 public void testcheckServiceReferenceExists() throws Exception{
258 jmxRegistryClient.checkServiceReferenceExists(testingRegistryON);