Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-api / src / test / java / org / opendaylight / controller / config / api / jmx / ObjectNameUtilTest.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.api.jmx;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import com.google.common.base.Throwables;
16 import com.google.common.collect.Maps;
17 import java.util.HashMap;
18 import java.util.Map;
19 import javax.management.ObjectName;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.api.ModuleIdentifier;
23
24 public class ObjectNameUtilTest {
25
26     private String moduleName;
27     private String instanceName;
28
29     @Before
30     public void setUp() throws Exception {
31         moduleName = "module";
32         instanceName = "instance";
33     }
34
35     @Test
36     public void testServiceReferenceName() throws Exception {
37         String serviceQName = "(namespace?revision=r)qname";
38         String refName = "refName";
39         String transaction = "transaction";
40
41         ObjectName serviceReferenceON = ObjectNameUtil.createTransactionServiceON(transaction, serviceQName, refName);
42         ObjectNameUtil.checkType(serviceReferenceON, ObjectNameUtil.TYPE_SERVICE_REFERENCE);
43
44         assertFalse(serviceReferenceON.isPattern());
45         assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
46         assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
47         assertEquals(transaction, ObjectNameUtil.getTransactionName(serviceReferenceON));
48         assertEquals(ObjectNameUtil.createReadOnlyServiceON(serviceQName, refName),
49                 ObjectNameUtil.withoutTransactionName(serviceReferenceON));
50
51         serviceReferenceON = ObjectNameUtil.createReadOnlyServiceON(serviceQName, refName);
52         assertFalse(serviceReferenceON.isPattern());
53         assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
54         assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
55         assertEquals(null, ObjectNameUtil.getTransactionName(serviceReferenceON));
56     }
57
58     @Test
59     public void testModuleName() throws Exception {
60         String txName = "transaction";
61
62         ObjectName on = ObjectNameUtil.createTransactionModuleON(txName, moduleName, instanceName);
63
64         ObjectNameUtil.checkDomain(on);
65         ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_MODULE);
66
67         assertFalse(on.isPattern());
68         assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
69         assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
70         assertEquals(txName, ObjectNameUtil.getTransactionName(on));
71         assertEquals(4, ObjectNameUtil.getAdditionalProperties(on).size());
72
73         ObjectName withoutTx = ObjectNameUtil.withoutTransactionName(on);
74         assertEquals(ObjectNameUtil.createReadOnlyModuleON(moduleName, instanceName), withoutTx);
75         assertEquals(moduleName, ObjectNameUtil.getFactoryName(withoutTx));
76         assertEquals(instanceName, ObjectNameUtil.getInstanceName(withoutTx));
77         assertEquals(null, ObjectNameUtil.getTransactionName(withoutTx));
78         assertEquals(on, ObjectNameUtil.withTransactionName(withoutTx, txName));
79
80         ObjectName pattern = ObjectNameUtil.createModulePattern(moduleName, null);
81         assertPattern(withoutTx, pattern);
82         pattern = ObjectNameUtil.createModulePattern(moduleName, null, txName);
83         assertPattern(on, pattern);
84     }
85
86     private void assertPattern(final ObjectName test, final ObjectName pattern) {
87         assertTrue(pattern.isPattern());
88         assertTrue(pattern.apply(test));
89     }
90
91     @Test
92     public void testRuntimeBeanName() throws Exception {
93
94         Map<String, String> properties = Maps.newHashMap();
95         properties.put("p1", "value");
96         properties.put("p2", "value2");
97
98         ObjectName on = ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, properties);
99
100         ObjectNameUtil.checkDomain(on);
101         ObjectNameUtil.checkTypeOneOf(on, ObjectNameUtil.TYPE_RUNTIME_BEAN);
102
103         assertFalse(on.isPattern());
104         assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
105         assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
106         assertEquals(2, ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).size());
107         assertTrue(ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).containsKey("p1"));
108         assertEquals("value", ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).get("p1"));
109         assertTrue(ObjectNameUtil.getAdditionalProperties(on).containsKey("p2"));
110         assertEquals("value2", ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).get("p2"));
111
112         ObjectName pattern = ObjectNameUtil.createRuntimeBeanPattern(null, instanceName);
113         assertPattern(on, pattern);
114     }
115
116     @Test
117     public void testModuleIdentifier() throws Exception {
118         ModuleIdentifier mi = new ModuleIdentifier(moduleName, instanceName);
119         ObjectName on = ObjectNameUtil.createReadOnlyModuleON(mi);
120         assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
121         assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
122
123         assertEquals(mi, ObjectNameUtil.fromON(on, ObjectNameUtil.TYPE_MODULE));
124     }
125
126     @Test
127     public void testChecks() throws Exception {
128         final ObjectName on = ObjectNameUtil.createON("customDomain", ObjectNameUtil.TYPE_KEY,
129                 ObjectNameUtil.TYPE_MODULE);
130
131         assertFailure(() -> ObjectNameUtil.checkTypeOneOf(on, ObjectNameUtil.TYPE_RUNTIME_BEAN,
132                 ObjectNameUtil.TYPE_CONFIG_TRANSACTION), IllegalArgumentException.class);
133
134         assertFailure(() -> ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_RUNTIME_BEAN),
135                 IllegalArgumentException.class);
136
137         assertFailure(() -> ObjectNameUtil.checkDomain(on), IllegalArgumentException.class);
138     }
139
140     @SuppressWarnings("IllegalCatch")
141     private void assertFailure(final Runnable test, final Class<? extends Exception> ex) {
142         try {
143             test.run();
144         } catch (final Exception e) {
145             assertTrue("Failed with wrong exception: " + Throwables.getStackTraceAsString(e),
146                     e.getClass().isAssignableFrom(ex));
147             return;
148         }
149
150         fail(test + " should have failed on " + ex);
151     }
152
153     @Test(expected = IllegalArgumentException.class)
154     public void testCreateON() throws Exception {
155         ObjectNameUtil.createON(">}+!#");
156     }
157
158     @Test(expected = IllegalArgumentException.class)
159     public void testCreateON2() throws Exception {
160         Map<String, String> map = new HashMap<>();
161         ObjectNameUtil.createON(">}+!#", map);
162     }
163 }

©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.