Merge "Bug 1073: Implemented Transaction chain on InMemoryDOMDataStore level."
[controller.git] / opendaylight / config / threadpool-config-impl / src / test / java / org / opendaylight / controller / config / threadpool / scheduled / ScheduledThreadPoolConfigBeanTest.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.threadpool.scheduled;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.junit.matchers.JUnitMatchers.containsString;
15
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.ObjectName;
19 import org.junit.Before;
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.manager.impl.AbstractConfigTest;
25 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
26 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
27 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
28 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
29 import org.opendaylight.controller.config.yang.threadpool.impl.scheduled.ScheduledThreadPoolModuleFactory;
30 import org.opendaylight.controller.config.yang.threadpool.impl.scheduled.ScheduledThreadPoolModuleMXBean;
31
32 public class ScheduledThreadPoolConfigBeanTest extends AbstractConfigTest {
33
34     private ScheduledThreadPoolModuleFactory factory;
35     private final String instanceName = "scheduled1";
36
37     @Before
38     public void setUp() {
39
40         factory = new ScheduledThreadPoolModuleFactory();
41         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, factory,
42                 new NamingThreadFactoryModuleFactory()));
43     }
44
45     @Test
46     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
47             ConflictingVersionException {
48         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
49
50         createScheduled(transaction, instanceName, 1);
51         transaction.validateConfig();
52         CommitStatus status = transaction.commit();
53
54         assertBeanCount(1, factory.getImplementationName());
55         assertStatus(status, 2, 0, 0);
56     }
57
58     @Test
59     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
60             ValidationException {
61
62         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
63         createScheduled(transaction, instanceName, 1);
64
65         transaction.commit();
66
67         assertBeanCount(1, factory.getImplementationName());
68
69         transaction = configRegistryClient.createTransaction();
70         CommitStatus status = transaction.commit();
71
72         assertBeanCount(1, factory.getImplementationName());
73         assertStatus(status, 0, 0, 2);
74     }
75
76     @Test
77     public void testReconfigurationInstance() throws InstanceAlreadyExistsException, ValidationException,
78             ConflictingVersionException, InstanceNotFoundException {
79         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
80         createScheduled(transaction, instanceName, 1);
81
82         transaction.commit();
83
84         transaction = configRegistryClient.createTransaction();
85         ObjectName databaseNew = transaction.lookupConfigBean(factory.getImplementationName(), instanceName);
86         ScheduledThreadPoolModuleMXBean proxy = transaction.newMXBeanProxy(databaseNew,
87                 ScheduledThreadPoolModuleMXBean.class);
88         proxy.setMaxThreadCount(99);
89
90         CommitStatus status = transaction.commit();
91
92         assertBeanCount(1, factory.getImplementationName());
93         assertStatus(status, 0, 1, 1);
94     }
95
96     @Test
97     public void testDestroy() throws InstanceAlreadyExistsException, ValidationException, ConflictingVersionException,
98             InstanceNotFoundException {
99         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
100         createScheduled(transaction, instanceName, 1);
101
102         transaction.commit();
103
104         transaction = configRegistryClient.createTransaction();
105         transaction.destroyModule(factory.getImplementationName(), instanceName);
106         CommitStatus status = transaction.commit();
107
108         assertBeanCount(0, factory.getImplementationName());
109         assertStatus(status, 0, 0, 1);
110     }
111
112     @Test
113     public void testInstanceAlreadyExistsException() throws ConflictingVersionException, ValidationException,
114             InstanceAlreadyExistsException {
115         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
116         createScheduled(transaction, instanceName, 1);
117         transaction.commit();
118         transaction = configRegistryClient.createTransaction();
119         try {
120             createScheduled(transaction, instanceName, 2);
121             fail();
122         } catch (InstanceAlreadyExistsException e) {
123             assertThat(
124                     e.getMessage(),
125                     containsString("There is an instance registered with name ModuleIdentifier{factoryName='threadpool-scheduled', instanceName='scheduled1'}"));
126         }
127     }
128
129     @Test
130     public void testValidationException() throws InstanceAlreadyExistsException {
131         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
132         createScheduled(transaction, instanceName, 0);
133
134         try {
135             transaction.validateConfig();
136             fail();
137         } catch (ValidationException e) {
138             assertTrue(e.getFailedValidations().containsKey(factory.getImplementationName()));
139             assertEquals(1, e.getFailedValidations().get(factory.getImplementationName()).keySet().size());
140         }
141     }
142
143     private ObjectName createScheduled(ConfigTransactionJMXClient transaction, String instanceName, int maxThreadCount)
144             throws InstanceAlreadyExistsException {
145         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
146         ScheduledThreadPoolModuleMXBean mxBean = transaction.newMBeanProxy(nameCreated,
147                 ScheduledThreadPoolModuleMXBean.class);
148         mxBean.setMaxThreadCount(maxThreadCount);
149
150         ObjectName threadFactoryON = transaction.createModule(NamingThreadFactoryModuleFactory.NAME, "naming");
151         NamingThreadFactoryModuleMXBean namingThreadFactoryModuleMXBean = transaction.newMXBeanProxy(threadFactoryON,
152                 NamingThreadFactoryModuleMXBean.class);
153         namingThreadFactoryModuleMXBean.setNamePrefix("prefix");
154
155         mxBean.setThreadFactory(threadFactoryON);
156
157         return nameCreated;
158     }
159
160 }