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