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