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