Merge "BUG 1734 - Applying a recovered journal to Shard does not happen during recovery"
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.matchers.JUnitMatchers.hasItem;
15
16 import java.lang.management.ManagementFactory;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
24
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.config.api.ConfigRegistry;
30
31 import com.google.common.collect.Sets;
32
33 public class ConfigRegistryClientsTest {
34
35     private TestingConfigRegistry testingRegistry;
36     private ObjectName testingRegistryON;
37     private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
38     private ConfigRegistryClient jmxRegistryClient;
39     private ConfigTransactionClient jmxTransactionClient;
40     private Map<String, ObjectName> map;
41
42     @Before
43     public void setUp() throws Exception {
44         testingRegistry = new TestingConfigRegistry();
45         testingRegistryON = ConfigRegistry.OBJECT_NAME;
46         mbs.registerMBean(testingRegistry, testingRegistryON);
47         jmxRegistryClient = new ConfigRegistryJMXClient(
48                 ManagementFactory.getPlatformMBeanServer());
49         map = new HashMap<>();
50     }
51
52     @After
53     public void cleanUp() throws Exception {
54         if (testingRegistryON != null) {
55             mbs.unregisterMBean(testingRegistryON);
56         }
57     }
58
59     @Test
60     public void testCreateTransaction() throws Exception{
61         jmxTransactionClient = jmxRegistryClient.createTransaction();
62         assertNotNull(jmxTransactionClient);
63     }
64
65     @Test
66     public void testGetConfigTransactionClient2() throws Exception{
67         jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient("transactionName");
68         assertNotNull(jmxTransactionClient);
69     }
70
71     @Test
72     public void testGetConfigTransactionClient() throws Exception{
73         jmxTransactionClient = jmxRegistryClient.getConfigTransactionClient(testingRegistryON);
74         assertNotNull(jmxTransactionClient);
75     }
76
77     @Test(expected = IllegalArgumentException.class)
78     public void testNewMXBeanProxy() throws Exception{
79         if (jmxRegistryClient instanceof ConfigRegistryJMXClient) {
80             ConfigRegistryJMXClient client = (ConfigRegistryJMXClient) jmxRegistryClient;
81             assertNull(client.newMXBeanProxy(testingRegistryON, String.class));
82         } else {
83             throw new AssertionError("brm msg");
84         }
85     }
86
87     @Test
88     public void testBeginConfig() throws Exception{
89         Assert.assertNotNull(jmxRegistryClient.beginConfig());
90     }
91
92     @Test
93     public void testCommitConfig() throws Exception{
94         assertNull(jmxRegistryClient.commitConfig(testingRegistryON));
95     }
96
97     @Test
98     public void testGetOpenConfigs() throws Exception{
99         assertNull(jmxRegistryClient.getOpenConfigs());
100     }
101
102     @Test(expected = RuntimeException.class)
103     public void testGetVersion() throws Exception{
104         assertEquals(3, jmxRegistryClient.getVersion());
105     }
106
107     @Test
108     public void testGetAvailableModuleNames() throws Exception{
109         assertNull(jmxRegistryClient.getAvailableModuleNames());
110     }
111
112     @Test
113     public void testIsHealthy() throws Exception{
114         assertEquals(false, jmxRegistryClient.isHealthy());
115     }
116
117     @Test
118     public void testLookupConfigBeans3() throws Exception{
119         Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans();
120         assertEquals(3, son.size());
121     }
122
123     @Test
124     public void testLookupConfigBeans2() throws Exception{
125         Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1);
126         assertEquals(2, son.size());
127     }
128
129     @Test
130     public void testLookupConfigBeans() throws Exception{
131         Set<ObjectName> son = jmxRegistryClient.lookupConfigBeans(TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1);
132         Set<ObjectName> on = Sets.newHashSet(TestingConfigRegistry.conf2);
133         assertEquals(on, son);
134     }
135
136     @Test
137     public void testLookupConfigBean() throws Exception{
138         ObjectName on = jmxRegistryClient.lookupConfigBean(TestingConfigRegistry.moduleName1, null);
139         assertEquals(TestingConfigRegistry.conf3, on);
140     }
141
142     @Test
143     public void testLookupRuntimeBeans() throws Exception {
144         Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
145         assertEquals(Sets.newHashSet(TestingConfigRegistry.run2, TestingConfigRegistry.run1, TestingConfigRegistry.run3), jmxLookup);
146     }
147
148     private Set<ObjectName> lookupRuntimeBeans(ConfigRegistryClient client)
149             throws Exception {
150         Set<ObjectName> beans = client.lookupRuntimeBeans();
151         for (ObjectName on : beans) {
152             assertEquals("RuntimeBean", on.getKeyProperty("type"));
153         }
154         assertEquals(3, beans.size());
155         return beans;
156     }
157
158     @Test
159     public void testLookupRuntimeBeansWithIfcNameAndInstanceName()
160             throws InstanceNotFoundException {
161         Set<ObjectName> jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
162                 jmxRegistryClient, TestingConfigRegistry.moduleName1,
163                 TestingConfigRegistry.instName1);
164         assertEquals(1, jmxLookup.size());
165         assertEquals(Sets.newHashSet(TestingConfigRegistry.run2), jmxLookup);
166
167         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
168                 jmxRegistryClient, TestingConfigRegistry.moduleName2,
169                 TestingConfigRegistry.instName2);
170         assertEquals(1, jmxLookup.size());
171         assertEquals(Sets.newHashSet(TestingConfigRegistry.run3), jmxLookup);
172
173         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
174                 jmxRegistryClient, TestingConfigRegistry.moduleName1,
175                 TestingConfigRegistry.instName2);
176         assertEquals(0, jmxLookup.size());
177         assertEquals(Sets.newHashSet(), jmxLookup);
178     }
179
180     private Set<ObjectName> clientLookupRuntimeBeansWithModuleAndInstance(
181             ConfigRegistryClient client, String moduleName, String instanceName) {
182         Set<ObjectName> beans = client.lookupRuntimeBeans(moduleName, instanceName);
183         if (beans.size() > 0) {
184             assertEquals("RuntimeBean",
185                     beans.iterator().next().getKeyProperty("type"));
186         }
187         return beans;
188     }
189
190     @Test
191     public void testCheckConfigBeanExists() throws Exception{
192         jmxRegistryClient.checkConfigBeanExists(testingRegistryON);
193         assertEquals(true, TestingConfigRegistry.checkBool);
194     }
195
196     @Test
197     public void testLookupConfigBeanByServiceInterfaceName() throws Exception{
198         ObjectName on = clientLookupConfigBeanByServiceInterfaceName();
199         assertEquals(TestingConfigRegistry.conf1, on);
200     }
201
202     private ObjectName clientLookupConfigBeanByServiceInterfaceName(){
203         return jmxRegistryClient.lookupConfigBeanByServiceInterfaceName("qnameA", "refA");
204     }
205
206     @Test
207     public void testGetServiceMapping() throws Exception{
208         assertNull(jmxRegistryClient.getServiceMapping());
209     }
210
211     @Test
212     public void testLookupServiceReferencesByServiceInterfaceName() throws Exception{
213         map.put("conf2", TestingConfigRegistry.conf2);
214         assertEquals(map, jmxRegistryClient.lookupServiceReferencesByServiceInterfaceName("qnameB"));
215     }
216
217     @Test
218     public void testLookupServiceInterfaceNames() throws Exception{
219         assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName1));
220         assertThat(clientLookupServiceInterfaceNames(testingRegistryON), hasItem(TestingConfigRegistry.serviceQName2));
221     }
222
223     private Set<String> clientLookupServiceInterfaceNames(ObjectName client) throws InstanceNotFoundException{
224         return jmxRegistryClient.lookupServiceInterfaceNames(client);
225     }
226
227     @Test
228     public void testGetServiceInterfaceName() throws Exception{
229         assertNull(jmxRegistryClient.getServiceInterfaceName(null, null));
230     }
231
232     @Test(expected = RuntimeException.class)
233     public void testInvokeMethod() throws Exception{
234         assertNull(jmxRegistryClient.invokeMethod(testingRegistryON, "name", null, null));
235     }
236
237     @Test(expected = RuntimeException.class)
238     public void testGetAttributeCurrentValue() throws Exception{
239         assertNull(jmxRegistryClient.getAttributeCurrentValue(testingRegistryON, "attrName"));
240     }
241
242     @Test
243     public void testGetAvailableModuleFactoryQNames() throws Exception{
244         for(String str : jmxRegistryClient.getAvailableModuleFactoryQNames()){
245             if(str != TestingConfigRegistry.moduleName1){
246                 assertEquals(TestingConfigRegistry.moduleName2, str);
247             }
248             else{
249                 assertEquals(TestingConfigRegistry.moduleName1, str);
250             }
251         }
252     }
253
254     @Test
255     public void testGetServiceReference() throws Exception{
256         Assert.assertNotNull(jmxRegistryClient.getServiceReference(null, null));
257     }
258
259     @Test(expected = UnsupportedOperationException.class)
260     public void testcheckServiceReferenceExists() throws Exception{
261         jmxRegistryClient.checkServiceReferenceExists(testingRegistryON);
262     }
263 }