Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / runtimembean / RuntimeBeanRegistratorImplTest.java
1 /*
2  * Copyright (c) 2013, 2017 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 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);
38
39     private BaseJMXRegistrator baseJMXRegistrator;
40     private RootRuntimeBeanRegistratorImpl tested;
41     private final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(MODULE1, INSTANCE_NAME);
42
43     @Before
44     public void setUp() {
45         baseJMXRegistrator = new BaseJMXRegistrator(ManagementFactory.getPlatformMBeanServer());
46         tested = baseJMXRegistrator.createRuntimeBeanRegistrator(moduleIdentifier);
47     }
48
49     @After
50     public void tearDown() {
51         tested.close();
52         assertEquals(0, baseJMXRegistrator.getRegisteredObjectNames().size());
53     }
54
55     protected void checkExists(final ObjectName on) throws Exception {
56         platformMBeanServer.getMBeanInfo(on);
57     }
58
59     protected void checkNotExists(final ObjectName on) throws Exception {
60         try {
61             platformMBeanServer.getMBeanInfo(on);
62             fail();
63         } catch (final InstanceNotFoundException e) {
64             // FIXME: should it be empty?
65         }
66     }
67
68     @Test
69     public void testRegisterMBeanWithoutAdditionalProperties() throws Exception {
70         createRoot();
71     }
72
73     private HierarchicalRuntimeBeanRegistrationImpl createRoot() throws Exception {
74         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = tested.registerRoot(new TestingRuntimeBean());
75
76         ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(MODULE1, INSTANCE_NAME,
77                 Maps.<String, String>newHashMap());
78
79         assertEquals(expectedON1, rootRegistration.getObjectName());
80         checkExists(rootRegistration.getObjectName());
81         return rootRegistration;
82     }
83
84     @Test
85     public void testRegisterMBeanWithAdditionalProperties() throws Exception {
86         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
87         createAdditional(rootRegistration);
88     }
89
90     private HierarchicalRuntimeBeanRegistration createAdditional(
91             final HierarchicalRuntimeBeanRegistrationImpl rootRegistration) throws Exception {
92
93         HierarchicalRuntimeBeanRegistrationImpl registration = rootRegistration.register(additionalKey, additionalValue,
94                 new TestingRuntimeBean());
95
96         ObjectName expectedON1 = ObjectNameUtil.createRuntimeBeanName(MODULE1, INSTANCE_NAME, additionalProperties);
97
98         assertEquals(expectedON1, registration.getObjectName());
99         checkExists(registration.getObjectName());
100         return registration;
101     }
102
103     @Test
104     public void testCloseRegistration() throws Exception {
105         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
106         rootRegistration.close();
107         checkNotExists(rootRegistration.getObjectName());
108     }
109
110     @Test
111     public void testCloseRegistrator() throws Exception {
112         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
113         HierarchicalRuntimeBeanRegistration childRegistration = createAdditional(rootRegistration);
114         tested.close();
115         checkNotExists(rootRegistration.getObjectName());
116         checkNotExists(childRegistration.getObjectName());
117     }
118
119     @Test(expected = IllegalArgumentException.class)
120     public void testRegistration_overrideType() throws Exception {
121         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
122         rootRegistration.register("type", "xxx", new TestingRuntimeBean());
123     }
124
125     @Test
126     public void testRegistrationException() throws Exception {
127         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
128         try {
129             createRoot();
130             fail();
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()));
135         }
136     }
137
138     @Test
139     public void testIgnoringExceptionInClose() throws Exception {
140         HierarchicalRuntimeBeanRegistrationImpl rootRegistration = createRoot();
141         platformMBeanServer.unregisterMBean(rootRegistration.getObjectName());
142         rootRegistration.close();
143     }
144 }