Update junit Assert imports
[controller.git] / opendaylight / config / netty-timer-config / src / test / java / org / opendaylight / controller / config / yang / netty / timer / HashedWheelTimerModuleTest.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.netty.timer;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12 import javax.management.InstanceAlreadyExistsException;
13 import javax.management.InstanceNotFoundException;
14 import javax.management.ObjectName;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.config.api.ConflictingVersionException;
18 import org.opendaylight.controller.config.api.ValidationException;
19 import org.opendaylight.controller.config.api.jmx.CommitStatus;
20 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
21 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
22 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
23 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
24 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
25
26 public class HashedWheelTimerModuleTest extends AbstractConfigTest {
27
28     private HashedWheelTimerModuleFactory factory;
29     private NamingThreadFactoryModuleFactory threadFactory;
30     private final String instanceName = "hashed-wheel-timer1";
31
32     @Before
33     public void setUp() {
34         factory = new HashedWheelTimerModuleFactory();
35         threadFactory = new NamingThreadFactoryModuleFactory();
36         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, factory, threadFactory));
37     }
38
39     public void testValidationExceptionTickDuration() throws InstanceAlreadyExistsException {
40         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
41         try {
42             createInstance(transaction, instanceName, 0L, 10, true);
43             transaction.validateConfig();
44             fail();
45         } catch (ValidationException e) {
46             assertTrue(e.getMessage().contains("TickDuration value must be greater than 0"));
47         }
48     }
49
50     public void testValidationExceptionTicksPerWheel() throws InstanceAlreadyExistsException {
51         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
52         try {
53             createInstance(transaction, instanceName, 500L, 0, true);
54             transaction.validateConfig();
55             fail();
56         } catch (ValidationException e) {
57             assertTrue(e.getMessage().contains("TicksPerWheel value must be greater than 0"));
58         }
59     }
60
61     @Test
62     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
63             ConflictingVersionException {
64         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
65
66         createInstance(transaction, instanceName, 500L, 10, true);
67         createInstance(transaction, instanceName + 1, null, null, false);
68         createInstance(transaction, instanceName + 2, 500L, 10, false);
69         createInstance(transaction, instanceName + 3, 500L, null, false);
70         transaction.validateConfig();
71         CommitStatus status = transaction.commit();
72
73         assertBeanCount(4, factory.getImplementationName());
74         assertStatus(status, 5, 0, 0);
75     }
76
77     @Test
78     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
79             ValidationException {
80
81         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
82         createInstance(transaction, instanceName, 500L, 10, true);
83
84         transaction.commit();
85
86         transaction = configRegistryClient.createTransaction();
87         assertBeanCount(1, factory.getImplementationName());
88         CommitStatus status = transaction.commit();
89
90         assertBeanCount(1, factory.getImplementationName());
91         assertStatus(status, 0, 0, 2);
92     }
93
94     @Test
95     public void testReconfigure() throws InstanceAlreadyExistsException, ConflictingVersionException,
96             ValidationException, InstanceNotFoundException {
97
98         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
99         createInstance(transaction, instanceName, 500L, 10, true);
100         transaction.commit();
101
102         transaction = configRegistryClient.createTransaction();
103         assertBeanCount(1, factory.getImplementationName());
104         HashedWheelTimerModuleMXBean mxBean = transaction.newMBeanProxy(
105                 transaction.lookupConfigBean(factory.getImplementationName(), instanceName),
106                 HashedWheelTimerModuleMXBean.class);
107         mxBean.setTicksPerWheel(20);
108         CommitStatus status = transaction.commit();
109
110         assertBeanCount(1, factory.getImplementationName());
111         assertStatus(status, 0, 1, 1);
112     }
113
114     private ObjectName createInstance(final ConfigTransactionJMXClient transaction, final String instanceName,
115             final Long tickDuration, final Integer ticksPerWheel, final boolean hasThreadfactory)
116             throws InstanceAlreadyExistsException {
117         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
118         HashedWheelTimerModuleMXBean mxBean = transaction
119                 .newMBeanProxy(nameCreated, HashedWheelTimerModuleMXBean.class);
120         mxBean.setTickDuration(tickDuration);
121         mxBean.setTicksPerWheel(ticksPerWheel);
122         if (hasThreadfactory) {
123             mxBean.setThreadFactory(createThreadfactoryInstance(transaction, "thread-factory1", "th"));
124         }
125         return nameCreated;
126     }
127
128     private ObjectName createThreadfactoryInstance(final ConfigTransactionJMXClient transaction, final String instanceName,
129             final String namePrefix) throws InstanceAlreadyExistsException {
130         ObjectName nameCreated = transaction.createModule(threadFactory.getImplementationName(), instanceName);
131         NamingThreadFactoryModuleMXBean mxBean = transaction.newMBeanProxy(nameCreated,
132                 NamingThreadFactoryModuleMXBean.class);
133         mxBean.setNamePrefix(namePrefix);
134         return nameCreated;
135     }
136
137 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.