Merge "BUG-650: Split out CommitCoordinationTask"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / test / RuntimeBeanTest.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.manager.testingservices.scheduledthreadpool.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Sets;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.management.InstanceAlreadyExistsException;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.ObjectName;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.ConflictingVersionException;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.jmx.CommitStatus;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolConfigBeanMXBean;
28 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
30
31 /**
32  * TestingScheduledThreadPool exports 2 interfaces: <br>
33  * {@link org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory#NAME}
34  * ,<br>
35  * {@link org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory#NAME}
36  * <br>
37  * <br>
38  * It also exports 2 runtime beans, one default and one with additional
39  * properties of {'a':'b'}.
40  */
41 public class RuntimeBeanTest extends AbstractScheduledTest {
42
43     ObjectName ifc1runtimeON1 = ObjectNameUtil.createRuntimeBeanName(
44             TestingScheduledThreadPoolModuleFactory.NAME, scheduled1,
45             Maps.<String, String> newHashMap());
46     // additional runtime bean
47     ObjectName ifc1runtimeON2 = ObjectNameUtil.createRuntimeBeanName(
48             TestingScheduledThreadPoolModuleFactory.NAME, scheduled1,
49             ImmutableMap.of("a", "b"));
50
51     List<ObjectName> allObjectNames = Lists.newArrayList(ifc1runtimeON1,
52             ifc1runtimeON2);
53
54     private ObjectName createScheduled() throws InstanceAlreadyExistsException,
55             ConflictingVersionException, ValidationException {
56         ConfigTransactionJMXClient transaction = configRegistryClient
57                 .createTransaction();
58
59         // create using TestingThreadPoolIfc:
60         ObjectName createdConfigBean = transaction.createModule(
61                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
62         // commit
63         transaction.commit();
64         return createdConfigBean;
65     }
66
67     @Test
68     public void testCreateScheduled() throws Exception {
69         createScheduled();
70         checkRuntimeBeans();
71     }
72
73     private void checkRuntimeBeans() throws Exception {
74         // check runtime bean - on 2 places
75         for (ObjectName on : allObjectNames)
76             checkRuntimeBean(on);
77     }
78
79     private void checkRuntimeBean(ObjectName on) throws Exception {
80         assertEquals(0,
81                 platformMBeanServer.getAttribute(on, "ActualNumberOfThreads"));
82     }
83
84     private void checkRuntimeBeanDoesNotExist(ObjectName on) throws Exception {
85         try {
86             checkRuntimeBean(on);
87             fail();
88         } catch (InstanceNotFoundException e) {
89
90         }
91     }
92
93     @Test
94     public void testLookup() throws Exception {
95         createScheduled();
96         assertEquals(Sets.newHashSet(ifc1runtimeON1, ifc1runtimeON2),
97                 configRegistryClient.lookupRuntimeBeans());
98     }
99
100     @Test
101     public void testReuse() throws Exception {
102         ObjectName createdConfigBean = createScheduled();
103         // empty transaction
104         CommitStatus commitInfo = configRegistryClient.createTransaction()
105                 .commit();
106
107         // check that it was reused
108         ObjectName readableConfigBean = ObjectNameUtil
109                 .withoutTransactionName(createdConfigBean);
110         List<ObjectName> newInstances = Collections.<ObjectName> emptyList();
111         List<ObjectName> reusedInstances = Lists
112                 .newArrayList(readableConfigBean);
113         List<ObjectName> recreatedInstaces = Collections
114                 .<ObjectName> emptyList();
115         assertEquals(new CommitStatus(newInstances, reusedInstances,
116                 recreatedInstaces), commitInfo);
117         checkRuntimeBeans();
118     }
119
120     @Test
121     public void testRecreate() throws Exception {
122         ObjectName createdConfigBean = createScheduled();
123         // empty transaction
124         ConfigTransactionJMXClient configTransaction = configRegistryClient
125                 .createTransaction();
126         ObjectName scheduledWritableON = configTransaction.lookupConfigBean(
127                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
128         TestingScheduledThreadPoolConfigBeanMXBean scheduledWritableProxy = configTransaction
129                 .newMXBeanProxy(scheduledWritableON, TestingScheduledThreadPoolConfigBeanMXBean.class);
130         scheduledWritableProxy.setRecreate(true);
131         CommitStatus commitInfo = configTransaction.commit();
132         // check that it was recreated
133         ObjectName readableConfigBean = ObjectNameUtil
134                 .withoutTransactionName(createdConfigBean);
135         List<ObjectName> newInstances = Collections.<ObjectName> emptyList();
136         List<ObjectName> reusedInstances = Collections.<ObjectName> emptyList();
137         List<ObjectName> recreatedInstaces = Lists
138                 .newArrayList(readableConfigBean);
139         assertEquals(new CommitStatus(newInstances, reusedInstances,
140                 recreatedInstaces), commitInfo);
141         checkRuntimeBeans();
142     }
143
144     @Test
145     public void testDestroy_shouldUnregisterRuntimeBeans() throws Exception {
146         ObjectName createdConfigBean = createScheduled();
147         ConfigTransactionJMXClient configTransaction = configRegistryClient
148                 .createTransaction();
149         configTransaction.destroyModule(ObjectNameUtil
150                 .createTransactionModuleON(configTransaction.getTransactionName(), createdConfigBean));
151         configTransaction.commit();
152         for (ObjectName on : allObjectNames)
153             checkRuntimeBeanDoesNotExist(on);
154     }
155
156 }