BUG-625: migrate InventoryAndReadAdapter
[controller.git] / opendaylight / config / threadpool-config-impl / src / test / java / org / opendaylight / controller / config / threadpool / fixed / FixedThreadPoolConfigBeanTest.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.fixed;
9
10 import static org.junit.Assert.assertThat;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.junit.matchers.JUnitMatchers.containsString;
14
15 import java.lang.management.ManagementFactory;
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.fixed.FixedThreadPoolModuleFactory;
30 import org.opendaylight.controller.config.yang.threadpool.impl.fixed.FixedThreadPoolModuleMXBean;
31
32 public class FixedThreadPoolConfigBeanTest extends AbstractConfigTest {
33
34     private FixedThreadPoolModuleFactory factory;
35     private final String nameInstance = "fixedInstance";
36
37     @Before
38     public void setUp() {
39         factory = new FixedThreadPoolModuleFactory();
40         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,factory,
41                 new NamingThreadFactoryModuleFactory()));
42     }
43
44     @Test
45     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
46             ConflictingVersionException {
47         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
48         createFixed(transaction, nameInstance, 2);
49
50         transaction.validateConfig();
51         CommitStatus status = transaction.commit();
52
53         assertBeanCount(1, factory.getImplementationName());
54         assertStatus(status, 2, 0, 0);
55     }
56
57     @Test
58     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
59             ValidationException {
60         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
61         createFixed(transaction, nameInstance, 4);
62
63         transaction.validateConfig();
64         transaction.commit();
65
66         assertBeanCount(1, factory.getImplementationName());
67
68         transaction = configRegistryClient.createTransaction();
69         CommitStatus status = transaction.commit();
70
71         assertBeanCount(1, factory.getImplementationName());
72         assertStatus(status, 0, 0, 2);
73     }
74
75     @Test
76     public void testNegative() throws ConflictingVersionException, ValidationException, InstanceAlreadyExistsException {
77         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
78
79         createFixed(transaction, nameInstance, 5);
80         transaction.commit();
81
82         transaction = configRegistryClient.createTransaction();
83         try {
84             createFixed(transaction, nameInstance, 0);
85             fail();
86         } catch (InstanceAlreadyExistsException e) {
87             assertThat(
88                     e.getMessage(),
89                     containsString("There is an instance registered with name ModuleIdentifier{factoryName='threadpool-fixed', instanceName='fixedInstance'}"));
90         }
91     }
92
93     @Test
94     public void testDestroy() throws InstanceAlreadyExistsException, ValidationException, ConflictingVersionException,
95             InstanceNotFoundException {
96
97         int numberOfThreads = 100;
98         int threadCount1 = ManagementFactory.getThreadMXBean().getThreadCount();
99         assertTrue("Expected less than " + numberOfThreads + " threads, got " + threadCount1, threadCount1 < numberOfThreads);
100         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
101         createFixed(transaction, nameInstance, numberOfThreads);
102         transaction.commit();
103         int threadCount2 = ManagementFactory.getThreadMXBean().getThreadCount();
104         assertTrue("Expected more than " + numberOfThreads + " threads, got " + threadCount2, threadCount2 > numberOfThreads);
105
106         transaction = configRegistryClient.createTransaction();
107         transaction.destroyModule(factory.getImplementationName(), nameInstance);
108         CommitStatus status = transaction.commit();
109
110         assertBeanCount(0, factory.getImplementationName());
111         assertStatus(status, 0, 0, 1);
112         int threadCount3 = ManagementFactory.getThreadMXBean().getThreadCount();
113         assertTrue("Expected less than " + numberOfThreads + " threads, got " + threadCount3, threadCount3 < numberOfThreads);
114     }
115
116     @Test
117     public void testValidationException() throws InstanceAlreadyExistsException {
118         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
119         createFixed(transaction, nameInstance, -1);
120         try {
121             transaction.validateConfig();
122             fail();
123         } catch (ValidationException e) {
124             assertThat(e.getMessage(), containsString("MaxThreadCount must be greater than zero"));
125         }
126     }
127
128     private ObjectName createFixed(ConfigTransactionJMXClient transaction, String name, int numberOfThreads)
129             throws InstanceAlreadyExistsException {
130         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), name);
131         FixedThreadPoolModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, FixedThreadPoolModuleMXBean.class);
132         mxBean.setMaxThreadCount(numberOfThreads);
133
134         ObjectName threadFactoryON = transaction.createModule(NamingThreadFactoryModuleFactory.NAME, "naming");
135         NamingThreadFactoryModuleMXBean namingThreadFactoryModuleMXBean = transaction.newMXBeanProxy(threadFactoryON,
136                 NamingThreadFactoryModuleMXBean.class);
137         namingThreadFactoryModuleMXBean.setNamePrefix("prefix");
138
139         mxBean.setThreadFactory(threadFactoryON);
140
141         return nameCreated;
142     }
143
144 }