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