ce3648d1607009aacd5ecc41e2a1f119660d8045
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / runtimembean / RuntimeBeanRegistratorImplTest.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.impl.runtimembean;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
13 import static org.junit.internal.matchers.StringContains.containsString;
14
15 import java.lang.management.ManagementFactory;
16 import java.util.Map;
17
18 import javax.management.InstanceNotFoundException;
19 import javax.management.ObjectName;
20
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;
31
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.collect.Maps;
34
35 public class RuntimeBeanRegistratorImplTest extends
36         AbstractLockedPlatformMBeanServerTest {
37     static final String module1 = "module1";
38     static final String INSTANCE_NAME = "instanceName";
39     String additionalKey = "key";
40     String additionalValue = "value";
41     Map<String, String> additionalProperties = ImmutableMap.of(additionalKey,
42             additionalValue);
43
44     private BaseJMXRegistrator baseJMXRegistrator;
45     private RootRuntimeBeanRegistratorImpl tested;
46     private final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(
47             module1, INSTANCE_NAME);
48
49     @Before
50     public void setUp() {
51         baseJMXRegistrator = new BaseJMXRegistrator(
52                 ManagementFactory.getPlatformMBeanServer());
53         tested = baseJMXRegistrator
54                 .createRuntimeBeanRegistrator(moduleIdentifier);
55     }
56
57     @After
58     public void tearDown() {
59         tested.close();
60         assertEquals(0, baseJMXRegistrator.getRegisteredObjectNames().size());
61     }
62
63     protected void checkExists(ObjectName on) throws Exception {
64         platformMBeanServer.getMBeanInfo(on);
65     }
66
67     protected void checkNotExists(ObjectName on) throws Exception {
68         try {
69             platformMBeanServer.getMBeanInfo(on);
70             fail();
71         } catch (InstanceNotFoundException e) {
72
73         }
74     }
75
76     @Test
77     public void testRegisterMBeanWithoutAdditionalProperties() throws Exception {
78         createRoot();
79     }
80
81     private HierarchicalRuntimeBeanRegistrationImpl createRoot()
82             throws Exception {
83         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = tested
84                 .registerRoot(new TestingRuntimeBean());
85
86         ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(module1,
87                 INSTANCE_NAME, Maps.<String, String> newHashMap());
88
89         assertEquals(expectedON1, rootRegistration.getObjectName());
90         checkExists(rootRegistration.getObjectName());
91         return rootRegistration;
92     }
93
94     @Test
95     public void testRegisterMBeanWithAdditionalProperties() throws Exception {
96         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
97         createAdditional(rootRegistration);
98     }
99
100     private HierarchicalRuntimeBeanRegistration createAdditional(
101             HierarchicalRuntimeBeanRegistrationImpl rootRegistration)
102             throws Exception {
103
104         HierarchicalRuntimeBeanRegistrationImpl registration = rootRegistration
105                 .register(additionalKey, additionalValue, new TestingRuntimeBean());
106
107         ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(module1,
108                 INSTANCE_NAME, additionalProperties);
109
110         assertEquals(expectedON1, registration.getObjectName());
111         checkExists(registration.getObjectName());
112         return registration;
113     }
114
115     @Test
116     public void testCloseRegistration() throws Exception {
117         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
118         rootRegistration.close();
119         checkNotExists(rootRegistration.getObjectName());
120     }
121
122     @Test
123     public void testCloseRegistrator() throws Exception {
124         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
125         HierarchicalRuntimeBeanRegistration childRegistration = createAdditional(rootRegistration);
126         tested.close();
127         checkNotExists(rootRegistration.getObjectName());
128         checkNotExists(childRegistration.getObjectName());
129     }
130
131     @Test(expected = IllegalArgumentException.class)
132     public void testRegistration_overrideType() throws Exception {
133         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
134         rootRegistration.register("type", "xxx", new TestingRuntimeBean());
135     }
136
137     @Test
138     public void testRegistrationException() throws Exception {
139         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
140         try {
141             createRoot();
142             fail();
143         } catch (IllegalStateException e) {
144             assertThat(e.getMessage(), containsString(rootRegistration
145                     .getObjectName().toString()));
146             assertThat(e.getMessage(),
147                     containsString("Could not register runtime bean"));
148             assertThat(e.getMessage(),
149                     containsString(moduleIdentifier.toString()));
150         }
151     }
152
153     @Test
154     public void testIgnoringExceptionInClose() throws Exception {
155         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
156         platformMBeanServer.unregisterMBean(rootRegistration.getObjectName());
157         rootRegistration.close();
158     }
159
160 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.