Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / test / RuntimeBeanTest.java
1 /*
2  * Copyright (c) 2013, 2017 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
34  * .scheduledthreadpool.TestingScheduledThreadPoolModuleFactory#NAME}
35  * ,<br>
36  * {@link org.opendaylight.controller.config.manager
37  * .testingservices.threadpool.TestingFixedThreadPoolModuleFactory#NAME}
38  * <br>
39  * <br>
40  * It also exports 2 runtime beans, one default and one with additional
41  * properties of {'a':'b'}.
42  */
43 public class RuntimeBeanTest extends AbstractScheduledTest {
44
45     ObjectName ifc1runtimeON1 = ObjectNameUtil.createRuntimeBeanName(TestingScheduledThreadPoolModuleFactory.NAME,
46             SCHEDULED1, Maps.<String, String>newHashMap());
47     // additional runtime bean
48     ObjectName ifc1runtimeON2 = ObjectNameUtil.createRuntimeBeanName(TestingScheduledThreadPoolModuleFactory.NAME,
49             SCHEDULED1, ImmutableMap.of("a", "b"));
50
51     List<ObjectName> allObjectNames = Lists.newArrayList(ifc1runtimeON1, ifc1runtimeON2);
52
53     private ObjectName createScheduled()
54             throws InstanceAlreadyExistsException, ConflictingVersionException, ValidationException {
55         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
56
57         // create using TestingThreadPoolIfc:
58         ObjectName createdConfigBean = transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
59                 SCHEDULED1);
60         // commit
61         transaction.commit();
62         return createdConfigBean;
63     }
64
65     @Test
66     public void testCreateScheduled() throws Exception {
67         createScheduled();
68         checkRuntimeBeans();
69     }
70
71     private void checkRuntimeBeans() throws Exception {
72         // check runtime bean - on 2 places
73         for (ObjectName on : allObjectNames) {
74             checkRuntimeBean(on);
75         }
76     }
77
78     private static void checkRuntimeBean(final ObjectName on) throws Exception {
79         assertEquals(0, platformMBeanServer.getAttribute(on, "ActualNumberOfThreads"));
80     }
81
82     private static void checkRuntimeBeanDoesNotExist(final ObjectName on) throws Exception {
83         try {
84             checkRuntimeBean(on);
85             fail();
86         } catch (final InstanceNotFoundException e) {
87             // No-op
88         }
89     }
90
91     @Test
92     public void testLookup() throws Exception {
93         createScheduled();
94         assertEquals(Sets.newHashSet(ifc1runtimeON1, ifc1runtimeON2), configRegistryClient.lookupRuntimeBeans());
95     }
96
97     @Test
98     public void testReuse() throws Exception {
99         ObjectName createdConfigBean = createScheduled();
100         // empty transaction
101         CommitStatus commitInfo = configRegistryClient.createTransaction().commit();
102
103         // check that it was reused
104         ObjectName readableConfigBean = ObjectNameUtil.withoutTransactionName(createdConfigBean);
105         List<ObjectName> newInstances = Collections.<ObjectName>emptyList();
106         List<ObjectName> reusedInstances = Lists.newArrayList(readableConfigBean);
107         List<ObjectName> recreatedInstaces = Collections.<ObjectName>emptyList();
108         assertEquals(new CommitStatus(newInstances, reusedInstances, recreatedInstaces), commitInfo);
109         checkRuntimeBeans();
110     }
111
112     @Test
113     public void testRecreate() throws Exception {
114         ObjectName createdConfigBean = createScheduled();
115         // empty transaction
116         ConfigTransactionJMXClient configTransaction = configRegistryClient.createTransaction();
117         ObjectName scheduledWritableON = configTransaction
118                 .lookupConfigBean(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
119         TestingScheduledThreadPoolConfigBeanMXBean scheduledWritableProxy = configTransaction
120                 .newMXBeanProxy(scheduledWritableON, TestingScheduledThreadPoolConfigBeanMXBean.class);
121         scheduledWritableProxy.setRecreate(true);
122         CommitStatus commitInfo = configTransaction.commit();
123         // check that it was recreated
124         ObjectName readableConfigBean = ObjectNameUtil.withoutTransactionName(createdConfigBean);
125         List<ObjectName> newInstances = Collections.<ObjectName>emptyList();
126         List<ObjectName> reusedInstances = Collections.<ObjectName>emptyList();
127         List<ObjectName> recreatedInstaces = Lists.newArrayList(readableConfigBean);
128         assertEquals(new CommitStatus(newInstances, reusedInstances, recreatedInstaces), commitInfo);
129         checkRuntimeBeans();
130     }
131
132     @Test
133     public void testDestroy_shouldUnregisterRuntimeBeans() throws Exception {
134         ObjectName createdConfigBean = createScheduled();
135         ConfigTransactionJMXClient configTransaction = configRegistryClient.createTransaction();
136         configTransaction.destroyModule(
137                 ObjectNameUtil.createTransactionModuleON(configTransaction.getTransactionName(), createdConfigBean));
138         configTransaction.commit();
139         for (ObjectName on : allObjectNames) {
140             checkRuntimeBeanDoesNotExist(on);
141         }
142     }
143 }