Merge "Bug-915: Adding static document generation. Currently the API Explorer can...
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / ConfigTransactionClientsTest.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.assertNull;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
14
15 import com.google.common.collect.Sets;
16 import java.lang.management.ManagementFactory;
17 import java.util.Collections;
18 import java.util.Map;
19 import java.util.Set;
20 import javax.management.Attribute;
21 import javax.management.MBeanException;
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.Ignore;
28 import org.junit.Test;
29 import org.opendaylight.controller.config.api.ValidationException;
30 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
31 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
32
33 public class ConfigTransactionClientsTest {
34     private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
35     private TestingConfigTransactionController transactionController;
36     private ObjectName transactionControllerON;
37     private ConfigTransactionClient jmxTransactionClient;
38     Attribute attr;
39
40
41     @Before
42     public void setUp() throws Exception {
43         transactionController = new TestingConfigTransactionController();
44         transactionControllerON = new ObjectName(ObjectNameUtil.ON_DOMAIN + ":"
45                 + ObjectNameUtil.TYPE_KEY + "=TransactionController");
46         mbs.registerMBean(transactionController, transactionControllerON);
47         jmxTransactionClient = new ConfigTransactionJMXClient(null,
48                 transactionControllerON,
49                 ManagementFactory.getPlatformMBeanServer());
50     }
51
52     @After
53     public void cleanUp() throws Exception {
54         if (transactionControllerON != null) {
55             mbs.unregisterMBean(transactionControllerON);
56         }
57     }
58
59     @Test
60     public void testLookupConfigBeans() throws Exception {
61         Set<ObjectName> jmxLookup = testClientLookupConfigBeans(jmxTransactionClient);
62         assertEquals(Sets.newHashSet(transactionController.conf1,
63                 transactionController.conf2, transactionController.conf3),
64                 jmxLookup);
65     }
66
67     private Set<ObjectName> testClientLookupConfigBeans(
68             ConfigTransactionClient client) {
69         Set<ObjectName> beans = client.lookupConfigBeans();
70         for (ObjectName on : beans) {
71             assertEquals("Module", on.getKeyProperty("type"));
72         }
73         assertEquals(3, beans.size());
74         return beans;
75     }
76
77     @Test
78     public void testGetObjectName() throws Exception {
79         testClientGetObjectName(jmxTransactionClient);
80         assertEquals(testClientGetObjectName(jmxTransactionClient), true);
81     }
82
83     private boolean testClientGetObjectName(ConfigTransactionClient client) {
84         return transactionControllerON.equals(client.getObjectName());
85     }
86
87     @Test
88     public void testGetAvailableModuleNames() throws Exception {
89         Set<String> jmxMN = testClientGetAvailableModuleNames(jmxTransactionClient);
90         assertNull(jmxMN);
91     }
92
93     private Set<String> testClientGetAvailableModuleNames(
94             ConfigTransactionClient client) {
95         return client.getAvailableModuleNames();
96     }
97
98     @Test
99     public void testGetTransactionName() throws Exception {
100         String jmxTN = testClientGetTransactionName(jmxTransactionClient);
101         assertEquals("transactionName", jmxTN);
102     }
103
104     private String testClientGetTransactionName(ConfigTransactionClient client) {
105         return client.getTransactionName();
106     }
107
108     @Ignore
109     public void testGetVersion() throws Exception {
110         long jmxVersion = jmxTransactionClient.getVersion();
111         assertNull(jmxVersion);
112     }
113
114     @Ignore
115     public void testGetParentVersion() throws Exception {
116         long jmxParentVersion = jmxTransactionClient.getParentVersion();
117         assertNull(jmxParentVersion);
118     }
119
120     @Test
121     public void testValidateConfig() throws Exception {
122         jmxTransactionClient.validateConfig();
123     }
124
125     @Test
126     public void testAbortConfig() throws Exception {
127         jmxTransactionClient.abortConfig();
128     }
129
130     @Test
131     public void testDestroyModule2() throws Exception {
132         jmxTransactionClient.destroyModule("moduleB", "instB");
133         assertNull(transactionController.conf4);
134     }
135
136     @Test
137     public void testDestroyModule() throws Exception {
138         ObjectName on = testClientCreateModule(jmxTransactionClient);
139         jmxTransactionClient.destroyModule(on);
140     }
141
142     @Test
143     public void testCreateModule() throws Exception {
144         ObjectName on = testClientCreateModule(jmxTransactionClient);
145         Assert.assertNotNull(on);
146     }
147
148     private ObjectName testClientCreateModule(ConfigTransactionClient client)
149             throws Exception {
150         return client.createModule("testModuleName", "testInstanceName");
151     }
152
153     @Ignore
154     public void testAssertVersion() {
155         jmxTransactionClient.assertVersion((int)jmxTransactionClient.getParentVersion(),
156             (int)jmxTransactionClient.getVersion());
157     }
158
159     @Test(expected = NullPointerException.class)
160     public void testCommit() throws Exception {
161         jmxTransactionClient.commit();
162     }
163
164     @Test
165     public void testLookupConfigBeans2() throws Exception {
166         Set<ObjectName> jmxLookup = testClientLookupConfigBeans2(
167                 jmxTransactionClient, "moduleB");
168         assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
169     }
170
171     private Set<ObjectName> testClientLookupConfigBeans2(
172             ConfigTransactionClient client, String moduleName) {
173         Set<ObjectName> beans = client.lookupConfigBeans(moduleName);
174         assertEquals(1, beans.size());
175         return beans;
176     }
177
178     @Test
179     public void testLookupConfigBean() throws Exception {
180         Set<ObjectName> jmxLookup = testClientLookupConfigBean(
181                 jmxTransactionClient, "moduleB", "instB");
182         assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
183     }
184
185     private Set<ObjectName> testClientLookupConfigBean(
186             ConfigTransactionClient client, String moduleName,
187             String instanceName) {
188         Set<ObjectName> beans = client.lookupConfigBeans(moduleName,
189                 instanceName);
190         assertEquals(1, beans.size());
191         return beans;
192     }
193
194     @Test
195     public void testLookupConfigBeans3() throws Exception {
196         Set<ObjectName> jmxLookup = testClientLookupConfigBeans3(
197                 jmxTransactionClient, "moduleB", "instB");
198         assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
199     }
200
201     private Set<ObjectName> testClientLookupConfigBeans3(
202             ConfigTransactionClient client, String moduleName,
203             String instanceName) {
204         Set<ObjectName> beans = client.lookupConfigBeans(moduleName,
205                 instanceName);
206         assertEquals(1, beans.size());
207         return beans;
208     }
209
210     @Test
211     public void testCheckConfigBeanExists() throws Exception {
212         jmxTransactionClient.checkConfigBeanExists(transactionControllerON);
213         assertEquals("configBeanExists", transactionController.check);
214     }
215
216     @Test
217     public void testSaveServiceReference() throws Exception {
218         assertEquals(transactionControllerON, jmxTransactionClient.saveServiceReference("serviceInterfaceName", "refName", transactionControllerON));
219     }
220
221     @Test
222     public void testRemoveServiceReference() throws Exception {
223         jmxTransactionClient.removeServiceReference("serviceInterface", "refName");
224         assertEquals("refName", transactionController.check);
225     }
226
227     @Test
228     public void testRemoveAllServiceReferences() throws Exception {
229         jmxTransactionClient.removeAllServiceReferences();
230         assertNull(transactionController.check);
231     }
232
233     @Test
234     public void testLookupConfigBeanByServiceInterfaceName() throws Exception {
235         assertEquals(transactionController.conf3, jmxTransactionClient.lookupConfigBeanByServiceInterfaceName("serviceInterface", "refName"));
236     }
237
238     @Test
239     public void testGetServiceMapping() throws Exception {
240         Assert.assertNotNull(jmxTransactionClient.getServiceMapping());
241     }
242
243     @Test
244     public void testLookupServiceReferencesByServiceInterfaceName() throws Exception {
245         Assert.assertNotNull(jmxTransactionClient.lookupServiceReferencesByServiceInterfaceName("serviceInterfaceQName"));
246     }
247
248     @Test
249     public void testLookupServiceInterfaceNames() throws Exception {
250         assertEquals(Sets.newHashSet("setA"), jmxTransactionClient.lookupServiceInterfaceNames(transactionControllerON));
251     }
252
253     @Test
254     public void testGetServiceInterfaceName() throws Exception {
255         assertEquals("namespace" + "localName", jmxTransactionClient.getServiceInterfaceName("namespace", "localName"));
256     }
257
258     @Test
259     public void removeServiceReferences() throws Exception {
260         assertEquals(true, jmxTransactionClient.removeServiceReferences(transactionControllerON));
261     }
262
263     @Test
264     public void testGetServiceReference() throws Exception {
265         assertEquals(transactionController.conf3, jmxTransactionClient.getServiceReference("serviceInterfaceQName", "refName"));
266     }
267
268     @Test
269     public void testCheckServiceReferenceExists() throws Exception {
270         jmxTransactionClient.checkServiceReferenceExists(transactionControllerON);
271         assertEquals("referenceExist", transactionController.check);
272     }
273
274     @Test(expected = RuntimeException.class)
275     public void testValidateBean() throws Exception {
276         jmxTransactionClient.validateBean(transactionControllerON);
277     }
278
279     @Test(expected = ValidationException.class)
280     public void testValidateBean2() throws Exception {
281         MBeanServer mbsLocal = mock(MBeanServer.class);
282         MBeanException mBeanException = new MBeanException(new ValidationException(
283                 Collections.<String, Map<String, ExceptionMessageWithStackTrace>>emptyMap()));
284         doThrow(mBeanException).when(mbsLocal).invoke(transactionControllerON, "validate", null, null);
285
286         ConfigTransactionJMXClient jmxTransactionClientFake = new ConfigTransactionJMXClient(null,
287                 transactionControllerON,
288                 mbsLocal);
289         jmxTransactionClientFake.validateBean(transactionControllerON);
290     }
291
292     @Test(expected = RuntimeException.class)
293     public void testValidateBean3() throws Exception {
294         MBeanServer mbsLocal = mock(MBeanServer.class);
295         MBeanException mBeanException = new MBeanException(new RuntimeException());
296         doThrow(mBeanException).when(mbsLocal).invoke(transactionControllerON, "validate", null, null);
297         ConfigTransactionJMXClient jmxTransactionClientFake = new ConfigTransactionJMXClient(null,
298                 transactionControllerON,
299                 mbsLocal);
300         jmxTransactionClientFake.validateBean(transactionControllerON);
301     }
302
303     @Test(expected = IllegalArgumentException.class)
304     public void testSetAttribute() throws Exception {
305         attr = null;
306         jmxTransactionClient.setAttribute(transactionControllerON, "attrName", attr);
307     }
308
309     @Test(expected = IllegalArgumentException.class)
310     public void testGetAttribute() throws Exception {
311         attr = jmxTransactionClient.getAttribute(transactionController.conf3, "attrName");
312         assertNull(attr);
313     }
314
315     @Test
316     public void testGetAvailableModuleFactoryQNames() throws Exception {
317         Assert.assertNotNull(jmxTransactionClient.getAvailableModuleFactoryQNames());
318     }
319 }