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