BUG-8: migrate newMBeanProxy() -> newMXBeanProxy()
[controller.git] / opendaylight / commons / protocol-framework / src / test / java / org / opendaylight / controller / config / yang / protocol / framework / NeverReconnectStrategyModuleTest.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.yang.protocol.framework;
9
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.config.api.ConflictingVersionException;
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.api.jmx.CommitStatus;
15 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
16 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
17 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
18 import org.opendaylight.controller.config.yang.netty.eventexecutor.GlobalEventExecutorModuleFactory;
19
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.ObjectName;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 public class NeverReconnectStrategyModuleTest extends AbstractConfigTest {
27
28     private static final String INSTANCE_NAME = "never-reconect-strategy-factory-impl";
29     private static final String FACTORY_NAME = NeverReconnectStrategyFactoryModuleFactory.NAME;
30
31     @Before
32     public void setUp() throws Exception {
33         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,
34                 new NeverReconnectStrategyFactoryModuleFactory(), new GlobalEventExecutorModuleFactory()));
35     }
36
37     @Test
38     public void testValidationExceptionTimeoutNotSet() throws Exception {
39         try {
40             createInstance(null);
41             fail();
42         } catch (ValidationException e) {
43             assertTrue(e.getMessage().contains("Timeout value is not set."));
44         }
45     }
46
47     @Test
48     public void testValidationExceptionTimeoutMinValue() throws Exception {
49         try {
50             createInstance(-1);
51             fail();
52         } catch (ValidationException e) {
53             assertTrue(e.getMessage().contains("is less than 0"));
54         }
55     }
56
57     @Test
58     public void testCreateBean() throws Exception {
59         final CommitStatus status = createInstance();
60         assertBeanCount(1, FACTORY_NAME);
61         assertStatus(status, 2, 0, 0);
62     }
63
64     @Test
65     public void testReusingOldInstance() throws Exception {
66         createInstance();
67         final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
68         assertBeanCount(1, FACTORY_NAME);
69         final CommitStatus status = transaction.commit();
70         assertBeanCount(1, FACTORY_NAME);
71         assertStatus(status, 0, 0, 2);
72     }
73
74     @Test
75     public void testReconfigure() throws Exception {
76         createInstance();
77         final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
78         assertBeanCount(1, FACTORY_NAME);
79         final NeverReconnectStrategyFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(
80                 transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME), NeverReconnectStrategyFactoryModuleMXBean.class);
81         mxBean.setTimeout(200);
82         final CommitStatus status = transaction.commit();
83         assertBeanCount(1, FACTORY_NAME);
84         assertStatus(status, 0, 1, 1);
85     }
86
87     private CommitStatus createInstance() throws Exception {
88         return createInstance(500);
89     }
90
91     private CommitStatus createInstance(final Integer timeout) throws InstanceAlreadyExistsException,
92             ConflictingVersionException, ValidationException {
93         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
94         createInstance(transaction, timeout);
95         return transaction.commit();
96     }
97
98     private static ObjectName createInstance(final ConfigTransactionJMXClient transaction, final Integer timeout)
99             throws InstanceAlreadyExistsException {
100         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
101         final NeverReconnectStrategyFactoryModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated,
102                 NeverReconnectStrategyFactoryModuleMXBean.class);
103         mxBean.setTimeout(timeout);
104         mxBean.setExecutor(GlobalEventExecutorUtil.create(transaction));
105         return nameCreated;
106     }
107
108 }