2 * Copyright (c) 2013 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;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.Maps;
17 import java.lang.management.ManagementFactory;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.ObjectName;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.config.api.ModuleIdentifier;
27 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
28 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
29 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
30 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
31 import org.opendaylight.controller.config.manager.impl.jmx.HierarchicalRuntimeBeanRegistrationImpl;
32 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
34 public class RuntimeBeanRegistratorImplTest extends
35 AbstractLockedPlatformMBeanServerTest {
36 static final String module1 = "module1";
37 static final String INSTANCE_NAME = "instanceName";
38 String additionalKey = "key";
39 String additionalValue = "value";
40 Map<String, String> additionalProperties = ImmutableMap.of(additionalKey,
43 private BaseJMXRegistrator baseJMXRegistrator;
44 private RootRuntimeBeanRegistratorImpl tested;
45 private final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(
46 module1, INSTANCE_NAME);
50 baseJMXRegistrator = new BaseJMXRegistrator(
51 ManagementFactory.getPlatformMBeanServer());
52 tested = baseJMXRegistrator
53 .createRuntimeBeanRegistrator(moduleIdentifier);
57 public void tearDown() {
59 assertEquals(0, baseJMXRegistrator.getRegisteredObjectNames().size());
62 protected void checkExists(final ObjectName on) throws Exception {
63 platformMBeanServer.getMBeanInfo(on);
66 protected void checkNotExists(final ObjectName on) throws Exception {
68 platformMBeanServer.getMBeanInfo(on);
70 } catch (InstanceNotFoundException e) {
76 public void testRegisterMBeanWithoutAdditionalProperties() throws Exception {
80 private HierarchicalRuntimeBeanRegistrationImpl createRoot()
82 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = tested
83 .registerRoot(new TestingRuntimeBean());
85 ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(module1,
86 INSTANCE_NAME, Maps.<String, String> newHashMap());
88 assertEquals(expectedON1, rootRegistration.getObjectName());
89 checkExists(rootRegistration.getObjectName());
90 return rootRegistration;
94 public void testRegisterMBeanWithAdditionalProperties() throws Exception {
95 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
96 createAdditional(rootRegistration);
99 private HierarchicalRuntimeBeanRegistration createAdditional(
100 final HierarchicalRuntimeBeanRegistrationImpl rootRegistration)
103 HierarchicalRuntimeBeanRegistrationImpl registration = rootRegistration
104 .register(additionalKey, additionalValue, new TestingRuntimeBean());
106 ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(module1,
107 INSTANCE_NAME, additionalProperties);
109 assertEquals(expectedON1, registration.getObjectName());
110 checkExists(registration.getObjectName());
115 public void testCloseRegistration() throws Exception {
116 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
117 rootRegistration.close();
118 checkNotExists(rootRegistration.getObjectName());
122 public void testCloseRegistrator() throws Exception {
123 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
124 HierarchicalRuntimeBeanRegistration childRegistration = createAdditional(rootRegistration);
126 checkNotExists(rootRegistration.getObjectName());
127 checkNotExists(childRegistration.getObjectName());
130 @Test(expected = IllegalArgumentException.class)
131 public void testRegistration_overrideType() throws Exception {
132 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
133 rootRegistration.register("type", "xxx", new TestingRuntimeBean());
137 public void testRegistrationException() throws Exception {
138 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
142 } catch (IllegalStateException e) {
143 assertThat(e.getMessage(), containsString(rootRegistration
144 .getObjectName().toString()));
145 assertThat(e.getMessage(),
146 containsString("Could not register runtime bean"));
147 assertThat(e.getMessage(),
148 containsString(moduleIdentifier.toString()));
153 public void testIgnoringExceptionInClose() throws Exception {
154 HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
155 platformMBeanServer.unregisterMBean(rootRegistration.getObjectName());
156 rootRegistration.close();