572765164c20d7c98dbd52724dac1509486d04b9
[controller.git] / opendaylight / config / config-api / src / test / java / org / opendaylight / controller / config / api / jmx / ObjectNameUtilTest.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.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), ObjectNameUtil.withoutTransactionName(serviceReferenceON));
49
50         serviceReferenceON = ObjectNameUtil.createReadOnlyServiceON(serviceQName, refName);
51         assertFalse(serviceReferenceON.isPattern());
52         assertEquals(serviceQName, ObjectNameUtil.getServiceQName(serviceReferenceON));
53         assertEquals(refName, ObjectNameUtil.getReferenceName(serviceReferenceON));
54         assertEquals(null, ObjectNameUtil.getTransactionName(serviceReferenceON));
55     }
56
57     @Test
58     public void testModuleName() throws Exception {
59         String txName = "transaction";
60
61         ObjectName on = ObjectNameUtil.createTransactionModuleON(txName, moduleName, instanceName);
62
63         ObjectNameUtil.checkDomain(on);
64         ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_MODULE);
65
66         assertFalse(on.isPattern());
67         assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
68         assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
69         assertEquals(txName, ObjectNameUtil.getTransactionName(on));
70         assertEquals(4, ObjectNameUtil.getAdditionalProperties(on).size());
71
72         ObjectName withoutTx = ObjectNameUtil.withoutTransactionName(on);
73         assertEquals(ObjectNameUtil.createReadOnlyModuleON(moduleName, instanceName), withoutTx);
74         assertEquals(moduleName, ObjectNameUtil.getFactoryName(withoutTx));
75         assertEquals(instanceName, ObjectNameUtil.getInstanceName(withoutTx));
76         assertEquals(null, ObjectNameUtil.getTransactionName(withoutTx));
77         assertEquals(on, ObjectNameUtil.withTransactionName(withoutTx, txName));
78
79         ObjectName pattern = ObjectNameUtil.createModulePattern(moduleName, null);
80         assertPattern(withoutTx, pattern);
81         pattern = ObjectNameUtil.createModulePattern(moduleName, null, txName);
82         assertPattern(on, pattern);
83     }
84
85     private void assertPattern(final ObjectName test, final ObjectName pattern) {
86         assertTrue(pattern.isPattern());
87         assertTrue(pattern.apply(test));
88     }
89
90     @Test
91     public void testRuntimeBeanName() throws Exception {
92
93         Map<String, String> properties = Maps.newHashMap();
94         properties.put("p1", "value");
95         properties.put("p2", "value2");
96
97         ObjectName on = ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, properties);
98
99         ObjectNameUtil.checkDomain(on);
100         ObjectNameUtil.checkTypeOneOf(on, ObjectNameUtil.TYPE_RUNTIME_BEAN);
101
102         assertFalse(on.isPattern());
103         assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
104         assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
105         assertEquals(2, ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).size());
106         assertTrue(ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).containsKey("p1"));
107         assertEquals("value", ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).get("p1"));
108         assertTrue(ObjectNameUtil.getAdditionalProperties(on).containsKey("p2"));
109         assertEquals("value2", ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(on).get("p2"));
110
111         ObjectName pattern = ObjectNameUtil.createRuntimeBeanPattern(null, instanceName);
112         assertPattern(on, pattern);
113     }
114
115     @Test
116     public void testModuleIdentifier() throws Exception {
117         ModuleIdentifier mi = new ModuleIdentifier(moduleName, instanceName);
118         ObjectName on = ObjectNameUtil.createReadOnlyModuleON(mi);
119         assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
120         assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
121
122         assertEquals(mi, ObjectNameUtil.fromON(on, ObjectNameUtil.TYPE_MODULE));
123     }
124
125     @Test
126     public void testChecks() throws Exception {
127         final ObjectName on = ObjectNameUtil.createON("customDomain", ObjectNameUtil.TYPE_KEY, ObjectNameUtil.TYPE_MODULE);
128
129         assertFailure(
130                 () -> ObjectNameUtil.checkTypeOneOf(on, ObjectNameUtil.TYPE_RUNTIME_BEAN, ObjectNameUtil.TYPE_CONFIG_TRANSACTION), IllegalArgumentException.class);
131
132         assertFailure(() -> ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_RUNTIME_BEAN), IllegalArgumentException.class);
133
134         assertFailure(() -> ObjectNameUtil.checkDomain(on), IllegalArgumentException.class);
135     }
136
137     private void assertFailure(final Runnable test, final Class<? extends Exception> ex) {
138         try {
139             test.run();
140         } catch(final Exception e) {
141             assertTrue("Failed with wrong exception: " + Throwables.getStackTraceAsString(e),
142                     e.getClass().isAssignableFrom(ex));
143             return;
144         }
145
146         fail(test + " should have failed on " + ex);
147     }
148
149     @Test(expected = IllegalArgumentException.class)
150     public void testCreateON() throws Exception {
151         ObjectNameUtil.createON(">}+!#");
152     }
153
154     @Test(expected = IllegalArgumentException.class)
155     public void testCreateON2() throws Exception {
156         Map<String, String> map = new HashMap<>();
157         ObjectNameUtil.createON(">}+!#", map);
158     }
159 }

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