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.runtimembean;
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.fail;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Maps;
17 import java.lang.management.ManagementFactory;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.ObjectName;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
26 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
27 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
28 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
29 import org.opendaylight.controller.config.manager.impl.jmx.HierarchicalRuntimeBeanRegistrationImpl;
30 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
32 public class RuntimeBeanRegistratorImplTest extends AbstractLockedPlatformMBeanServerTest {
33 private static final String MODULE1 = "module1";
34 private static final String INSTANCE_NAME = "instanceName";
35 String additionalKey = "key";
36 String additionalValue = "value";
37 Map<String, String> additionalProperties = ImmutableMap.of(additionalKey, additionalValue);
39 private BaseJMXRegistrator baseJMXRegistrator;
40 private RootRuntimeBeanRegistratorImpl tested;
41 private final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(MODULE1, INSTANCE_NAME);
45 baseJMXRegistrator = new BaseJMXRegistrator(ManagementFactory.getPlatformMBeanServer());
46 tested = baseJMXRegistrator.createRuntimeBeanRegistrator(moduleIdentifier);
50 public void tearDown() {
52 assertEquals(0, baseJMXRegistrator.getRegisteredObjectNames().size());
55 protected void checkExists(final ObjectName on) throws Exception {
56 platformMBeanServer.getMBeanInfo(on);
59 protected void checkNotExists(final ObjectName on) throws Exception {
61 platformMBeanServer.getMBeanInfo(on);
63 } catch (final InstanceNotFoundException e) {
64 // FIXME: should it be empty?
69 public void testRegisterMBeanWithoutAdditionalProperties() throws Exception {
73 private HierarchicalRuntimeBeanRegistrationImpl createRoot() throws Exception {
74 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = tested.registerRoot(new TestingRuntimeBean());
76 ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(MODULE1, INSTANCE_NAME,
77 Maps.<String, String>newHashMap());
79 assertEquals(expectedON1, rootRegistration.getObjectName());
80 checkExists(rootRegistration.getObjectName());
81 return rootRegistration;
85 public void testRegisterMBeanWithAdditionalProperties() throws Exception {
86 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
87 createAdditional(rootRegistration);
90 private HierarchicalRuntimeBeanRegistration createAdditional(
91 final HierarchicalRuntimeBeanRegistrationImpl rootRegistration) throws Exception {
93 HierarchicalRuntimeBeanRegistrationImpl registration = rootRegistration.register(additionalKey, additionalValue,
94 new TestingRuntimeBean());
96 ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(MODULE1, INSTANCE_NAME, additionalProperties);
98 assertEquals(expectedON1, registration.getObjectName());
99 checkExists(registration.getObjectName());
104 public void testCloseRegistration() throws Exception {
105 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
106 rootRegistration.close();
107 checkNotExists(rootRegistration.getObjectName());
111 public void testCloseRegistrator() throws Exception {
112 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
113 HierarchicalRuntimeBeanRegistration childRegistration = createAdditional(rootRegistration);
115 checkNotExists(rootRegistration.getObjectName());
116 checkNotExists(childRegistration.getObjectName());
119 @Test(expected = IllegalArgumentException.class)
120 public void testRegistration_overrideType() throws Exception {
121 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
122 rootRegistration.register("type", "xxx", new TestingRuntimeBean());
126 public void testRegistrationException() throws Exception {
127 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
131 } catch (final IllegalStateException e) {
132 assertThat(e.getMessage(), containsString(rootRegistration.getObjectName().toString()));
133 assertThat(e.getMessage(), containsString("Could not register runtime bean"));
134 assertThat(e.getMessage(), containsString(moduleIdentifier.toString()));
139 public void testIgnoringExceptionInClose() throws Exception {
140 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
141 platformMBeanServer.unregisterMBean(rootRegistration.getObjectName());
142 rootRegistration.close();