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