Add service reference binding to config registry.
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / LookupTest.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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import java.lang.management.ManagementFactory;
14 import java.lang.reflect.Method;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
20 import javax.management.InstanceNotFoundException;
21 import javax.management.MBeanServer;
22 import javax.management.ObjectName;
23
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.ConfigRegistry;
28 import org.opendaylight.controller.config.api.LookupRegistry;
29 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
30 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
31 import org.opendaylight.controller.config.util.jolokia.ConfigRegistryJolokiaClient;
32 import org.opendaylight.controller.config.util.jolokia.ConfigTransactionJolokiaClient;
33
34 import com.google.common.collect.ImmutableMap;
35 import com.google.common.collect.Sets;
36
37 public class LookupTest {
38
39     private String jolokiaURL;
40     private TestingConfigRegistry testingRegistry;
41     private ObjectName testingRegistryON;
42     private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
43     private ConfigRegistryClient jmxRegistryClient, jolokiaRegistryClient;
44     private ConfigTransactionControllerMXBean testingTransactionController;
45     private ObjectName testingTransactionControllerON;
46     private ConfigTransactionClient jmxTransactionClient,
47             jolokiaTransactionClient;
48
49     Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
50
51     @Before
52     public void setUp() throws Exception {
53         jolokiaURL = JolokiaHelper.startTestingJolokia();
54         testingRegistry = new TestingConfigRegistry();
55         testingRegistryON = ConfigRegistry.OBJECT_NAME;
56         mbs.registerMBean(testingRegistry, testingRegistryON);
57         jmxRegistryClient = new ConfigRegistryJMXClient(
58                 ManagementFactory.getPlatformMBeanServer());
59         jolokiaRegistryClient = new ConfigRegistryJolokiaClient(jolokiaURL);
60
61         testingTransactionController = new TestingConfigTransactionController();
62         testingTransactionControllerON = new ObjectName(
63                 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
64                         + "=TransactionController");
65         mbs.registerMBean(testingTransactionController,
66                 testingTransactionControllerON);
67
68         jmxTransactionClient = new ConfigTransactionJMXClient(null,
69                 testingTransactionControllerON,
70                 ManagementFactory.getPlatformMBeanServer());
71         jolokiaTransactionClient = new ConfigTransactionJolokiaClient(
72                 jolokiaURL, testingTransactionControllerON, null);
73         HashSet<ConfigRegistryClient> registryClients = Sets.newHashSet(jmxRegistryClient, jolokiaRegistryClient);
74         HashSet<ConfigTransactionClient> configTransactionClients = Sets.newHashSet(jmxTransactionClient, jolokiaTransactionClient);
75         lookupProvidersToClients = ImmutableMap.of((LookupRegistry) testingRegistry, registryClients,                testingTransactionController, configTransactionClients);
76     }
77
78     @After
79     public void cleanUp() throws Exception {
80         JolokiaHelper.stopJolokia();
81         mbs.unregisterMBean(testingRegistryON);
82         mbs.unregisterMBean(testingTransactionControllerON);
83     }
84
85     @Test
86     public void testLookupConfigBeans() throws Exception {
87         Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
88         Object[] args = new Object[0];
89         test(method, args);
90     }
91
92     @Test
93     public void testLookupConfigBeans1() throws Exception {
94         Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
95                 String.class);
96         Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
97         test(method, args);
98     }
99
100     @Test
101     public void testLookupConfigBeans2() throws Exception {
102         Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
103                 String.class, String.class);
104         Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
105                 TestingConfigRegistry.instName1 };
106         test(method, args);
107     }
108
109     @Test
110     public void testLookupConfigBean() throws Exception {
111         Method method = LookupRegistry.class.getMethod("lookupConfigBean",
112                 String.class, String.class);
113         Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
114                 TestingConfigRegistry.instName1 };
115         test(method, args);
116     }
117
118     private void test(Method method, Object[] args) throws Exception {
119         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
120                 .entrySet()) {
121             Object expected = method.invoke(entry.getKey(), args);
122             for (LookupRegistry client : entry.getValue()) {
123                 Object actual = method.invoke(client, args);
124                 assertEquals("Error while comparing " + entry.getKey()
125                         + " with client " + client, expected, actual);
126             }
127         }
128     }
129
130     @Test
131     public void testException() {
132         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
133                 .entrySet()) {
134             for (LookupRegistry client : entry.getValue()) {
135                 try {
136                     client.lookupConfigBean(
137                             InstanceNotFoundException.class.getSimpleName(), "");
138                     fail(client.toString());
139                 } catch (InstanceNotFoundException e) {
140
141                 }
142             }
143         }
144     }
145 }