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