Merge "Fix warnings in config-util"
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / ConfigRegistryClientsTest.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.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;
18 import java.util.Map;
19 import java.util.Set;
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;
28
29 public class ConfigRegistryClientsTest {
30
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;
37
38     @Before
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<>();
46     }
47
48     @After
49     public void cleanUp() throws Exception {
50         if (testingRegistryON != null) {
51             mbs.unregisterMBean(testingRegistryON);
52         }
53     }
54
55     @Test
56     public void testCreateTransaction() throws Exception{
57         jmxTransactionClient = jmxRegistryClient.createTransaction();
58         assertNotNull(jmxTransactionClient);
59     }
60
61     @Test
62     public void testGetConfigTransactionClient2() throws Exception{
63         jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient("transactionName");
64         assertNotNull(jmxTransactionClient);
65     }
66
67     @Test
68     public void testGetConfigTransactionClient() throws Exception{
69         jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient(testingRegistryON);
70         assertNotNull(jmxTransactionClient);
71     }
72
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));
78         } else {
79             throw new AssertionError("brm msg");
80         }
81     }
82
83     @Test
84     public void testBeginConfig() throws Exception{
85         Assert.assertNotNull(jmxRegistryClient.beginConfig());
86     }
87
88     @Test
89     public void testCommitConfig() throws Exception{
90         assertNull(jmxRegistryClient.commitConfig(testingRegistryON));
91     }
92
93     @Test
94     public void testGetOpenConfigs() throws Exception{
95         assertNull(jmxRegistryClient.getOpenConfigs());
96     }
97
98     @Test(expected = RuntimeException.class)
99     public void testGetVersion() throws Exception{
100         assertEquals(3, jmxRegistryClient.getVersion());
101     }
102
103     @Test
104     public void testGetAvailableModuleNames() throws Exception{
105         assertNull(jmxRegistryClient.getAvailableModuleNames());
106     }
107
108     @Test
109     public void testIsHealthy() throws Exception{
110         assertEquals(false, jmxRegistryClient.isHealthy());
111     }
112
113     @Test
114     public void testLookupConfigBeans3() throws Exception{
115         Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans();
116         assertEquals(3, son.size());
117     }
118
119     @Test
120     public void testLookupConfigBeans2() throws Exception{
121         Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1);
122         assertEquals(2, son.size());
123     }
124
125     @Test
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);
130     }
131
132     @Test
133     public void testLookupConfigBean() throws Exception{
134         ObjectName on = jmxRegistryClient.lookupConfigBean(TestingConfigRegistry.moduleName1, null);
135         assertEquals(TestingConfigRegistry.conf3, on);
136     }
137
138     @Test
139     public void testLookupRuntimeBeans() throws Exception {
140         Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
141         assertEquals(Sets.newHashSet(TestingConfigRegistry.run2, TestingConfigRegistry.run1, TestingConfigRegistry.run3), jmxLookup);
142     }
143
144     private Set<ObjectName> lookupRuntimeBeans(final ConfigRegistryClient client)
145             throws Exception {
146         Set<ObjectName> beans = client.lookupRuntimeBeans();
147         for (ObjectName on : beans) {
148             assertEquals("RuntimeBean", on.getKeyProperty("type"));
149         }
150         assertEquals(3, beans.size());
151         return beans;
152     }
153
154     @Test
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);
162
163         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
164                 jmxRegistryClient, TestingConfigRegistry.moduleName2,
165                 TestingConfigRegistry.instName2);
166         assertEquals(1, jmxLookup.size());
167         assertEquals(Sets.newHashSet(TestingConfigRegistry.run3), jmxLookup);
168
169         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
170                 jmxRegistryClient, TestingConfigRegistry.moduleName1,
171                 TestingConfigRegistry.instName2);
172         assertEquals(0, jmxLookup.size());
173         assertEquals(Sets.newHashSet(), jmxLookup);
174     }
175
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"));
182         }
183         return beans;
184     }
185
186     @Test
187     public void testCheckConfigBeanExists() throws Exception{
188         jmxRegistryClient.checkConfigBeanExists(testingRegistryON);
189         assertEquals(true, TestingConfigRegistry.checkBool);
190     }
191
192     @Test
193     public void testLookupConfigBeanByServiceInterfaceName() throws Exception{
194         ObjectName on = clientLookupConfigBeanByServiceInterfaceName();
195         assertEquals(TestingConfigRegistry.conf1, on);
196     }
197
198     private ObjectName clientLookupConfigBeanByServiceInterfaceName(){
199         return jmxRegistryClient.lookupConfigBeanByServiceInterfaceName("qnameA", "refA");
200     }
201
202     @Test
203     public void testGetServiceMapping() throws Exception{
204         assertNull(jmxRegistryClient.getServiceMapping());
205     }
206
207     @Test
208     public void testLookupServiceReferencesByServiceInterfaceName() throws Exception{
209         map.put("conf2", TestingConfigRegistry.conf2);
210         assertEquals(map, jmxRegistryClient.lookupServiceReferencesByServiceInterfaceName("qnameB"));
211     }
212
213     @Test
214     public void testLookupServiceInterfaceNames() throws Exception{
215         assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName1));
216         assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName2));
217     }
218
219     private Set<String> clientLookupServiceInterfaceNames(final ObjectName client) throws InstanceNotFoundException{
220         return jmxRegistryClient.lookupServiceInterfaceNames(client);
221     }
222
223     @Test
224     public void testGetServiceInterfaceName() throws Exception{
225         assertNull(jmxRegistryClient.getServiceInterfaceName(null, null));
226     }
227
228     @Test(expected = RuntimeException.class)
229     public void testInvokeMethod() throws Exception{
230         assertNull(jmxRegistryClient.invokeMethod(testingRegistryON, "name", null, null));
231     }
232
233     @Test(expected = RuntimeException.class)
234     public void testGetAttributeCurrentValue() throws Exception{
235         assertNull(jmxRegistryClient.getAttributeCurrentValue(testingRegistryON, "attrName"));
236     }
237
238     @Test
239     public void testGetAvailableModuleFactoryQNames() throws Exception{
240         for(String str : jmxRegistryClient.getAvailableModuleFactoryQNames()){
241             if(str != TestingConfigRegistry.moduleName1){
242                 assertEquals(TestingConfigRegistry.moduleName2, str);
243             }
244             else{
245                 assertEquals(TestingConfigRegistry.moduleName1, str);
246             }
247         }
248     }
249
250     @Test
251     public void testGetServiceReference() throws Exception{
252         Assert.assertNotNull(jmxRegistryClient.getServiceReference(null, null));
253     }
254
255     @Test(expected = UnsupportedOperationException.class)
256     public void testcheckServiceReferenceExists() throws Exception{
257         jmxRegistryClient.checkServiceReferenceExists(testingRegistryON);
258     }
259 }