2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.threadpool.naming;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
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.ThreadFactoryServiceInterface;
28 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
29 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
31 public class NamingThreadPoolFactoryConfigBeanTest extends AbstractConfigTest {
33 private NamingThreadFactoryModuleFactory factory;
34 private final String instanceName = "named";
39 factory = new NamingThreadFactoryModuleFactory();
40 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, factory));
44 public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
45 ConflictingVersionException {
46 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
48 createNamed(transaction, instanceName, "prefixes");
49 transaction.validateConfig();
50 CommitStatus status = transaction.commit();
52 assertEquals(1, configRegistry.lookupConfigBeans(factory.getImplementationName()).size());
53 assertEquals(1, status.getNewInstances().size());
54 assertEquals(0, status.getRecreatedInstances().size());
55 assertEquals(0, status.getReusedInstances().size());
59 public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
62 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
63 createNamed(transaction, instanceName, "prefixes");
67 assertEquals(1, configRegistry.lookupConfigBeans(factory.getImplementationName()).size());
69 transaction = configRegistryClient.createTransaction();
70 CommitStatus status = transaction.commit();
72 assertEquals(1, configRegistry.lookupConfigBeans(factory.getImplementationName()).size());
73 assertEquals(0, status.getNewInstances().size());
74 assertEquals(0, status.getRecreatedInstances().size());
75 assertEquals(1, status.getReusedInstances().size());
80 public void testInstanceAlreadyExistsException() throws ConflictingVersionException, ValidationException,
81 InstanceAlreadyExistsException {
82 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
84 createNamed(transaction, instanceName, "prefixes");
87 transaction = configRegistryClient.createTransaction();
89 createNamed(transaction, instanceName, "prefixes1");
91 } catch (InstanceAlreadyExistsException e) {
94 containsString("There is an instance registered with name ModuleIdentifier{factoryName='threadfactory-naming', instanceName='named'}"));
99 public void testValidationException() throws InstanceAlreadyExistsException {
100 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
102 ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
103 transaction.newMXBeanProxy(nameCreated, ThreadFactoryServiceInterface.class);
105 transaction.validateConfig();
107 } catch (ValidationException e) {
108 assertTrue(e.getFailedValidations().containsKey(factory.getImplementationName()));
109 assertEquals(1, e.getFailedValidations().get(factory.getImplementationName()).keySet().size());
114 public void testReconfigurationInstance() throws InstanceAlreadyExistsException, ValidationException,
115 ConflictingVersionException, InstanceNotFoundException {
116 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
117 createNamed(transaction, instanceName, "pref");
119 transaction.commit();
121 transaction = configRegistryClient.createTransaction();
122 ObjectName databaseNew = transaction.lookupConfigBean(factory.getImplementationName(), instanceName);
123 NamingThreadFactoryModuleMXBean proxy = transaction.newMXBeanProxy(databaseNew,
124 NamingThreadFactoryModuleMXBean.class);
125 proxy.setNamePrefix("pref1");
127 CommitStatus status = transaction.commit();
129 assertBeanCount(1, factory.getImplementationName());
130 assertStatus(status, 0, 1, 0);
133 private ObjectName createNamed(ConfigTransactionJMXClient transaction, String instanceName, String prefixes)
134 throws InstanceAlreadyExistsException {
135 ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
136 NamingThreadFactoryModuleMXBean mxBean = transaction.newMBeanProxy(nameCreated,
137 NamingThreadFactoryModuleMXBean.class);
138 mxBean.setNamePrefix(prefixes);