Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / ConfigTransactionControllerImplTest.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.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.Sets;
13 import java.lang.management.ManagementFactory;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17 import javax.management.MBeanServer;
18 import javax.management.MBeanServerFactory;
19 import javax.management.ObjectName;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
24 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
25 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
26 import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator;
27 import org.opendaylight.controller.config.manager.impl.runtimembean.TestingRuntimeBean;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.osgi.framework.BundleContext;
30
31 public class ConfigTransactionControllerImplTest extends AbstractLockedPlatformMBeanServerTest {
32
33     private BaseJMXRegistrator baseJMXRegistrator;
34
35     private ConfigTransactionControllerImpl testedTxController;
36     private MBeanServer transactionsMBeanServer;
37
38     private static final String TRANSACTION_NAME123 = "testTX1";
39     private static final String TRANSACTION_NAME4 = "testTX2";
40
41     private static final String MODULE_NAME124 = "module124";
42     private static final String MODULE_NAME3 = "module3";
43
44     private static final String INSTANCE_NAME134 = "instA";
45     private static final String INSTANCE_NAME2 = "instB";
46
47     private static final ObjectName NAME1 =
48             ObjectNameUtil.createTransactionModuleON(TRANSACTION_NAME123, MODULE_NAME124, INSTANCE_NAME134);
49     private static final ObjectName NAME2 =
50             ObjectNameUtil.createTransactionModuleON(TRANSACTION_NAME123,
51             MODULE_NAME124, INSTANCE_NAME2);
52     private static final ObjectName NAME3 =
53             ObjectNameUtil.createTransactionModuleON(TRANSACTION_NAME123, MODULE_NAME3, INSTANCE_NAME134);
54     private static final ObjectName NAME4 =
55             ObjectNameUtil.createTransactionModuleON(TRANSACTION_NAME4, MODULE_NAME124, INSTANCE_NAME134);
56
57     @Before
58     public void setUp() throws Exception {
59         baseJMXRegistrator = new BaseJMXRegistrator(ManagementFactory.getPlatformMBeanServer());
60         transactionsMBeanServer = MBeanServerFactory.createMBeanServer();
61         Map<String, Map.Entry<ModuleFactory, BundleContext>> currentlyRegisteredFactories = new HashMap<>();
62
63         ConfigTransactionLookupRegistry txLookupRegistry = new ConfigTransactionLookupRegistry(
64                 new TransactionIdentifier(TRANSACTION_NAME123),
65             () -> baseJMXRegistrator.createTransactionJMXRegistrator(TRANSACTION_NAME123),
66             currentlyRegisteredFactories);
67
68         SearchableServiceReferenceWritableRegistry writableRegistry = ServiceReferenceRegistryImpl
69                 .createSRWritableRegistry(ServiceReferenceRegistryImpl.createInitialSRLookupRegistry(),
70                         txLookupRegistry, currentlyRegisteredFactories);
71
72         testedTxController = new ConfigTransactionControllerImpl(txLookupRegistry, 1, null, 1,
73                 currentlyRegisteredFactories, transactionsMBeanServer, ManagementFactory.getPlatformMBeanServer(),
74                 false, writableRegistry);
75         TransactionModuleJMXRegistrator transactionModuleJMXRegistrator123 = testedTxController
76                 .getTxModuleJMXRegistrator();
77         transactionModuleJMXRegistrator123.registerMBean(new TestingRuntimeBean(), NAME1);
78         transactionModuleJMXRegistrator123.registerMBean(new TestingRuntimeBean(), NAME2);
79         transactionModuleJMXRegistrator123.registerMBean(new TestingRuntimeBean(), NAME3);
80         TransactionJMXRegistrator jmxRegistrator4 = baseJMXRegistrator
81                 .createTransactionJMXRegistrator(TRANSACTION_NAME4);
82         jmxRegistrator4.createTransactionModuleJMXRegistrator().registerMBean(new TestingRuntimeBean(), NAME4);
83     }
84
85     @After
86     public void cleanUp() {
87         baseJMXRegistrator.close();
88         MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
89     }
90
91     /**
92      * Tests if lookup method returns all beans with defined transaction name.
93      */
94     @Test
95     public void testLookupConfigBeans() {
96         Set<ObjectName> beans = testedTxController.lookupConfigBeans();
97         assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3), beans);
98     }
99
100     @Test
101     public void testLookupConfigBeansWithModuleName() {
102         Set<ObjectName> beans = testedTxController.lookupConfigBeans(MODULE_NAME124);
103         assertEquals(Sets.newHashSet(NAME1, NAME2), beans);
104     }
105
106     @Test
107     public void lookupConfigBeansWithModuleNameAndImplName() throws Exception {
108         Set<ObjectName> beans = testedTxController.lookupConfigBeans(MODULE_NAME124, INSTANCE_NAME134);
109         assertEquals(Sets.newHashSet(NAME1), beans);
110     }
111 }