Trigger a GC once initial configuration has been pushed
[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 org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
17 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
18 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPImpl;
19 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
20 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
21 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
22 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
23 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
24
25 import javax.management.ObjectName;
26 import java.util.Map;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertThat;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32 import static org.junit.internal.matchers.StringContains.containsString;
33
34 public class DependentWiringTest extends AbstractParallelAPSPTest {
35     private final String fixed1 = "fixed1";
36     private final String apsp1 = "apsp-parallel";
37
38     @Before
39     public void setUp() {
40         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,
41                 new TestingFixedThreadPoolModuleFactory(),
42                 new TestingParallelAPSPModuleFactory()));
43     }
44
45     @After
46     public void tearDown() {
47         TestingFixedThreadPool.cleanUp();
48     }
49
50     @Override
51     protected String getThreadPoolImplementationName() {
52         return TestingFixedThreadPoolModuleFactory.NAME;
53     }
54
55     @Test
56     public void testDependencies() throws Exception {
57         ObjectName apspON;
58         {
59             ConfigTransactionJMXClient transaction = configRegistryClient
60                     .createTransaction();
61             // create fixed1
62             ObjectName threadPoolTransactionON = createFixed1(transaction,
63                     TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS);
64             // create apsp-parallel
65             ObjectName apspNameTransactionON = createParallelAPSP(transaction,
66                     threadPoolTransactionON);
67             TestingParallelAPSPConfigMXBean parallelAPSPConfigProxy = transaction
68                     .newMXBeanProxy(apspNameTransactionON, TestingParallelAPSPConfigMXBean.class);
69             parallelAPSPConfigProxy.setSomeParam("");// trigger validation
70                                                      // failure
71             try {
72                 transaction.validateConfig();
73                 fail();
74             } catch (ValidationException e) {
75                 for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
76                         .getFailedValidations().entrySet()) {
77                     for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
78                             .getValue().entrySet()) {
79                         assertThat(
80                                 entry.getValue().getMessage(),
81                                 containsString("Parameter 'SomeParam' is blank"));
82                     }
83                 }
84             }
85
86             // try committing (validation fails)
87             try {
88                 transaction.commit();
89                 fail();
90             } catch (ValidationException e) {
91                 for (Map.Entry<String, Map<String, ExceptionMessageWithStackTrace>> exception : e
92                         .getFailedValidations().entrySet()) {
93                     for (Map.Entry<String, ExceptionMessageWithStackTrace> entry : exception
94                             .getValue().entrySet()) {
95                         String err = entry.getValue().getMessage();
96                         assertTrue("Unexpected error message: " + err,
97                                 err.contains("Parameter 'SomeParam' is blank"));
98                     }
99                 }
100             }
101
102             parallelAPSPConfigProxy.setSomeParam("abc");// fix validation
103                                                         // failure
104             transaction.commit();
105             apspON = ObjectNameUtil
106                     .withoutTransactionName(apspNameTransactionON);
107         }
108
109         // test reported apsp number of threads
110         TestingParallelAPSPConfigMXBean parallelAPSPRuntimeProxy = configRegistryClient
111                 .newMBeanProxy(apspON, TestingParallelAPSPConfigMXBean.class);
112         assertEquals(
113                 (Integer) TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS,
114                 parallelAPSPRuntimeProxy.getMaxNumberOfThreads());
115
116         // next transaction - recreate new thread pool
117         int newNumberOfThreads = TestingParallelAPSPImpl.MINIMAL_NUMBER_OF_THREADS * 2;
118         {
119             // start new transaction
120             ConfigTransactionJMXClient transaction = configRegistryClient
121                     .createTransaction();
122             ObjectName threadPoolNames_newTx = transaction.lookupConfigBean(
123                     getThreadPoolImplementationName(), fixed1);
124             TestingFixedThreadPoolConfigMXBean fixedConfigTransactionProxy = transaction
125                     .newMXBeanProxy(threadPoolNames_newTx, TestingFixedThreadPoolConfigMXBean.class);
126             fixedConfigTransactionProxy.setThreadCount(newNumberOfThreads);
127
128             transaction.commit();
129         }
130         // new reference should be copied to apsp-parallel
131         assertEquals((Integer) newNumberOfThreads,
132                 parallelAPSPRuntimeProxy.getMaxNumberOfThreads());
133
134     }
135 }