Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / parallelapsp / test / DependentWiringTest.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.manager.testingservices.parallelapsp.test;
9
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;
15
16 import java.util.Map;
17
18 import javax.management.ObjectName;
19
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;
36
37 public class DependentWiringTest extends AbstractParallelAPSPTest {
38     private final String fixed1 = "fixed1";
39     private final String apsp1 = "apsp-parallel";
40
41     @Before
42     public void setUp() {
43         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
44                 new TestingFixedThreadPoolModuleFactory(),
45                 new TestingParallelAPSPModuleFactory()));
46     }
47
48     @After
49     public void tearDown() {
50         TestingFixedThreadPool.cleanUp();
51     }
52
53     @Override
54     String getThreadPoolImplementationName() {
55         return TestingFixedThreadPoolModuleFactory.NAME;
56     }
57
58     @Test
59     public void testDependencies() throws Exception {
60         ObjectName apspON;
61         {
62             ConfigTransactionJMXClient transaction = configRegistryClient
63                     .createTransaction();
64             // create fixed1
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
73                                                      // failure
74             try {
75                 transaction.validateConfig();
76                 fail();
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()) {
82                         assertThat(
83                                 entry.getValue().getMessage(),
84                                 containsString("Parameter 'SomeParam' is blank"));
85                     }
86                 }
87             }
88
89             // try committing (validation fails)
90             try {
91                 transaction.commit();
92                 fail();
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"));
101                     }
102                 }
103             }
104
105             parallelAPSPConfigProxy.setSomeParam("abc");// fix validation
106                                                         // failure
107             transaction.commit();
108             apspON = ObjectNameUtil
109                     .withoutTransactionName(apspNameTransactionON);
110         }
111
112         // test reported apsp number of threads
113         TestingParallelAPSPConfigMXBean parallelAPSPRuntimeProxy = configRegistryClient
114                 .newMBeanProxy(apspON, TestingParallelAPSPConfigMXBean.class);
115         assertEquals(
116                 (Integer) TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS,
117                 parallelAPSPRuntimeProxy.getMaxNumberOfThreads());
118
119         // next transaction - recreate new thread pool
120         int newNumberOfThreads = TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS * 2;
121         {
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);
130
131             transaction.commit();
132         }
133         // new reference should be copied to apsp-parallel
134         assertEquals((Integer) newNumberOfThreads,
135                 parallelAPSPRuntimeProxy.getMaxNumberOfThreads());
136
137     }
138
139     @Test
140     public void testUsingJolokia() throws Exception {
141
142         ConfigTransactionJolokiaClient transactionClient = configRegistryJolokiaClient
143                 .createTransaction();
144         // fixed1
145         ObjectName fixed1ON = transactionClient.createModule(
146                 getThreadPoolImplementationName(), fixed1);
147         transactionClient.setAttribute(fixed1ON, "ThreadCount",
148                 TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS);
149
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);
155         // check
156         assertEquals(ObjectNameUtil.withoutTransactionName(fixed1ON),
157                 transactionClient.getAttributeON(apsp1ON, threadPoolString));
158         transactionClient.setAttribute(apsp1ON, "SomeParam", "ahoj");
159
160         // commit
161         transactionClient.commit();
162         // check thread pool
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,
175                         threadPoolString));
176
177     }
178 }