2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl;
10 import static org.junit.Assert.assertEquals;
12 import com.google.common.collect.Sets;
13 import java.lang.management.ManagementFactory;
14 import java.util.HashMap;
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;
31 public class ConfigTransactionControllerImplTest extends AbstractLockedPlatformMBeanServerTest {
33 private BaseJMXRegistrator baseJMXRegistrator;
35 private ConfigTransactionControllerImpl testedTxController;
36 private MBeanServer transactionsMBeanServer;
38 private static final String TRANSACTION_NAME123 = "testTX1";
39 private static final String TRANSACTION_NAME4 = "testTX2";
41 private static final String MODULE_NAME124 = "module124";
42 private static final String MODULE_NAME3 = "module3";
44 private static final String INSTANCE_NAME134 = "instA";
45 private static final String INSTANCE_NAME2 = "instB";
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);
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<>();
63 ConfigTransactionLookupRegistry txLookupRegistry = new ConfigTransactionLookupRegistry(
64 new TransactionIdentifier(TRANSACTION_NAME123),
65 () -> baseJMXRegistrator.createTransactionJMXRegistrator(TRANSACTION_NAME123),
66 currentlyRegisteredFactories);
68 SearchableServiceReferenceWritableRegistry writableRegistry = ServiceReferenceRegistryImpl
69 .createSRWritableRegistry(ServiceReferenceRegistryImpl.createInitialSRLookupRegistry(),
70 txLookupRegistry, currentlyRegisteredFactories);
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);
86 public void cleanUp() {
87 baseJMXRegistrator.close();
88 MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
92 * Tests if lookup method returns all beans with defined transaction name.
95 public void testLookupConfigBeans() {
96 Set<ObjectName> beans = testedTxController.lookupConfigBeans();
97 assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3), beans);
101 public void testLookupConfigBeansWithModuleName() {
102 Set<ObjectName> beans = testedTxController.lookupConfigBeans(MODULE_NAME124);
103 assertEquals(Sets.newHashSet(NAME1, NAME2), beans);
107 public void lookupConfigBeansWithModuleNameAndImplName() throws Exception {
108 Set<ObjectName> beans = testedTxController.lookupConfigBeans(MODULE_NAME124, INSTANCE_NAME134);
109 assertEquals(Sets.newHashSet(NAME1), beans);