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.manager.testingservices.parallelapsp.test;
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.internal.matchers.StringContains.containsString;
18 import javax.management.ObjectName;
20 import org.json.simple.JSONObject;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
28 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
29 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPImpl;
30 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
31 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
32 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
33 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
34 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
35 import org.opendaylight.controller.config.util.jolokia.ConfigTransactionJolokiaClient;
37 public class DependentWiringTest extends AbstractParallelAPSPTest {
38 private final String fixed1 = "fixed1";
39 private final String apsp1 = "apsp-parallel";
43 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
44 new TestingFixedThreadPoolModuleFactory(),
45 new TestingParallelAPSPModuleFactory()));
49 public void tearDown() {
50 TestingFixedThreadPool.cleanUp();
54 String getThreadPoolImplementationName() {
55 return TestingFixedThreadPoolModuleFactory.NAME;
59 public void testDependencies() throws Exception {
62 ConfigTransactionJMXClient transaction = configRegistryClient
65 ObjectName threadPoolTransactionON = createFixed1(transaction,
66 TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS);
67 // create apsp-parallel
68 ObjectName apspNameTransactionON = createParallelAPSP(transaction,
69 threadPoolTransactionON);
70 TestingParallelAPSPConfigMXBean parallelAPSPConfigProxy = transaction
71 .newMXBeanProxy(apspNameTransactionON, TestingParallelAPSPConfigMXBean.class);
72 parallelAPSPConfigProxy.setSomeParam("");// trigger validation
75 transaction.validateConfig();
77 } catch (ValidationException e) {
78 for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
79 .getFailedValidations().entrySet()) {
80 for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
81 .getValue().entrySet()) {
83 entry.getValue().getMessage(),
84 containsString("Parameter 'SomeParam' is blank"));
89 // try committing (validation fails)
93 } catch (ValidationException e) {
94 for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
95 .getFailedValidations().entrySet()) {
96 for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
97 .getValue().entrySet()) {
98 String err = entry.getValue().getMessage();
99 assertTrue("Unexpected error message: " + err,
100 err.contains("Parameter 'SomeParam' is blank"));
105 parallelAPSPConfigProxy.setSomeParam("abc");// fix validation
107 transaction.commit();
108 apspON = ObjectNameUtil
109 .withoutTransactionName(apspNameTransactionON);
112 // test reported apsp number of threads
113 TestingParallelAPSPConfigMXBean parallelAPSPRuntimeProxy = configRegistryClient
114 .newMBeanProxy(apspON, TestingParallelAPSPConfigMXBean.class);
116 (Integer) TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS,
117 parallelAPSPRuntimeProxy.getMaxNumberOfThreads());
119 // next transaction - recreate new thread pool
120 int newNumberOfThreads = TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS * 2;
122 // start new transaction
123 ConfigTransactionJMXClient transaction = configRegistryClient
124 .createTransaction();
125 ObjectName threadPoolNames_newTx = transaction.lookupConfigBean(
126 getThreadPoolImplementationName(), fixed1);
127 TestingFixedThreadPoolConfigMXBean fixedConfigTransactionProxy = transaction
128 .newMXBeanProxy(threadPoolNames_newTx, TestingFixedThreadPoolConfigMXBean.class);
129 fixedConfigTransactionProxy.setThreadCount(newNumberOfThreads);
131 transaction.commit();
133 // new reference should be copied to apsp-parallel
134 assertEquals((Integer) newNumberOfThreads,
135 parallelAPSPRuntimeProxy.getMaxNumberOfThreads());
140 public void testUsingJolokia() throws Exception {
142 ConfigTransactionJolokiaClient transactionClient = configRegistryJolokiaClient
143 .createTransaction();
145 ObjectName fixed1ON = transactionClient.createModule(
146 getThreadPoolImplementationName(), fixed1);
147 transactionClient.setAttribute(fixed1ON, "ThreadCount",
148 TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS);
150 // apsp-parallel with syntetic attrib
151 String threadPoolString = "ThreadPool";
152 ObjectName apsp1ON = transactionClient.createModule(
153 TestingParallelAPSPModuleFactory.NAME, apsp1);
154 transactionClient.setAttribute(apsp1ON, threadPoolString, fixed1ON);
156 assertEquals(ObjectNameUtil.withoutTransactionName(fixed1ON),
157 transactionClient.getAttributeON(apsp1ON, threadPoolString));
158 transactionClient.setAttribute(apsp1ON, "SomeParam", "ahoj");
161 transactionClient.commit();
163 assertEquals(1, TestingFixedThreadPool.allExecutors.size());
164 // check platform MBeanServer
165 ObjectName apspReadOnlyON = ObjectNameUtil
166 .withoutTransactionName(apsp1ON);
167 JSONObject threadPoolONJson = (JSONObject) configRegistryJolokiaClient
168 .getAttribute(apspReadOnlyON, threadPoolString);
169 ObjectName fixed1ReadOnlyON = ObjectNameUtil
170 .withoutTransactionName(fixed1ON);
171 assertEquals(fixed1ReadOnlyON, ObjectNameUtil.createON(threadPoolONJson
172 .get("objectName").toString()));
173 assertEquals(fixed1ReadOnlyON,
174 configRegistryJolokiaClient.getAttributeON(apspReadOnlyON,