Fixed missing uses of ${artifactId} in urn
[controller.git] / opendaylight / config / threadpool-config-impl / src / test / java / org / opendaylight / controller / config / threadpool / naming / NamingThreadPoolFactoryConfigBeanTest.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.naming;
9
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;
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.ThreadFactoryServiceInterface;
28 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
29 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
30
31 public class NamingThreadPoolFactoryConfigBeanTest extends AbstractConfigTest {
32
33     private NamingThreadFactoryModuleFactory factory;
34     private final String instanceName = "named";
35
36     @Before
37     public void setUp() {
38
39         factory = new NamingThreadFactoryModuleFactory();
40         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, factory));
41     }
42
43     @Test
44     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
45             ConflictingVersionException {
46         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
47
48         createNamed(transaction, instanceName, "prefixes");
49         transaction.validateConfig();
50         CommitStatus status = transaction.commit();
51
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());
56     }
57
58     @Test
59     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
60             ValidationException {
61
62         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
63         createNamed(transaction, instanceName, "prefixes");
64
65         transaction.commit();
66
67         assertEquals(1, configRegistry.lookupConfigBeans(factory.getImplementationName()).size());
68
69         transaction = configRegistryClient.createTransaction();
70         CommitStatus status = transaction.commit();
71
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());
76
77     }
78
79     @Test
80     public void testInstanceAlreadyExistsException() throws ConflictingVersionException, ValidationException,
81             InstanceAlreadyExistsException {
82         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
83
84         createNamed(transaction, instanceName, "prefixes");
85         transaction.commit();
86
87         transaction = configRegistryClient.createTransaction();
88         try {
89             createNamed(transaction, instanceName, "prefixes1");
90             fail();
91         } catch (InstanceAlreadyExistsException e) {
92             assertThat(
93                     e.getMessage(),
94                     containsString("There is an instance registered with name ModuleIdentifier{factoryName='threadfactory-naming', instanceName='named'}"));
95         }
96     }
97
98     @Test
99     public void testValidationException() throws InstanceAlreadyExistsException {
100         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
101
102         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
103         transaction.newMXBeanProxy(nameCreated, ThreadFactoryServiceInterface.class);
104         try {
105             transaction.validateConfig();
106             fail();
107         } catch (ValidationException e) {
108             assertTrue(e.getFailedValidations().containsKey(factory.getImplementationName()));
109             assertEquals(1, e.getFailedValidations().get(factory.getImplementationName()).keySet().size());
110         }
111     }
112
113     @Test
114     public void testReconfigurationInstance() throws InstanceAlreadyExistsException, ValidationException,
115             ConflictingVersionException, InstanceNotFoundException {
116         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
117         createNamed(transaction, instanceName, "pref");
118
119         transaction.commit();
120
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");
126
127         CommitStatus status = transaction.commit();
128
129         assertBeanCount(1, factory.getImplementationName());
130         assertStatus(status, 0, 1, 0);
131     }
132
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);
139         return nameCreated;
140     }
141
142 }