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