Merge "Bug 2267: Reuse leaf and leaf set entry builders in NormalizedNodeInputStreamR...
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / LookupTest.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.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Sets;
15 import java.lang.management.ManagementFactory;
16 import java.lang.reflect.Method;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.ConfigRegistry;
28 import org.opendaylight.controller.config.api.LookupRegistry;
29 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
30 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
31
32 public class LookupTest {
33
34     private TestingConfigRegistry testingRegistry;
35     private ObjectName testingRegistryON;
36     private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
37     private ConfigRegistryClient jmxRegistryClient;
38     private ConfigTransactionControllerMXBean testingTransactionController;
39     private ObjectName testingTransactionControllerON;
40     private ConfigTransactionClient jmxTransactionClient;
41
42     Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
43
44     @Before
45     public void setUp() throws Exception {
46         testingRegistry = new TestingConfigRegistry();
47         testingRegistryON = ConfigRegistry.OBJECT_NAME;
48         mbs.registerMBean(testingRegistry, testingRegistryON);
49         jmxRegistryClient = new ConfigRegistryJMXClient(
50                 ManagementFactory.getPlatformMBeanServer());
51
52
53         testingTransactionController = new TestingConfigTransactionController();
54         testingTransactionControllerON = new ObjectName(
55                 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
56                         + "=TransactionController");
57         mbs.registerMBean(testingTransactionController,
58                 testingTransactionControllerON);
59
60         jmxTransactionClient = new ConfigTransactionJMXClient(null,
61                 testingTransactionControllerON,
62                 ManagementFactory.getPlatformMBeanServer());
63
64         HashSet<ConfigRegistryClient> registryClients = Sets.newHashSet(jmxRegistryClient);
65         HashSet<ConfigTransactionClient> configTransactionClients = Sets.newHashSet(jmxTransactionClient);
66         lookupProvidersToClients = ImmutableMap.of((LookupRegistry) testingRegistry, registryClients,
67                 testingTransactionController, configTransactionClients);
68     }
69
70     @After
71     public void cleanUp() throws Exception {
72         mbs.unregisterMBean(testingRegistryON);
73         mbs.unregisterMBean(testingTransactionControllerON);
74     }
75
76     @Test
77     public void testLookupConfigBeans() throws Exception {
78         Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
79         Object[] args = new Object[0];
80         test(method, args);
81     }
82
83     @Test
84     public void testLookupConfigBeans1() throws Exception {
85         Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
86                 String.class);
87         Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
88         test(method, args);
89     }
90
91     @Test
92     public void testLookupConfigBeans2() throws Exception {
93         Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
94                 String.class, String.class);
95         Object[] args = new Object[] { TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1 };
96         test(method, args);
97     }
98
99     @Test
100     public void testLookupConfigBean() throws Exception {
101         Method method = LookupRegistry.class.getMethod("lookupConfigBean",
102                 String.class, String.class);
103         Object[] args = new Object[] { TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1 };
104         test(method, args);
105     }
106
107     private void test(Method method, Object[] args) throws Exception {
108         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
109                 .entrySet()) {
110             Object expected = method.invoke(entry.getKey(), args);
111             for (LookupRegistry client : entry.getValue()) {
112                 Object actual = method.invoke(client, args);
113                 assertEquals("Error while comparing " + entry.getKey()
114                         + " with client " + client, expected, actual);
115             }
116         }
117     }
118
119     @Test
120     public void testException() {
121         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
122                 .entrySet()) {
123             for (LookupRegistry client : entry.getValue()) {
124                 try {
125                     client.lookupConfigBean(
126                             InstanceNotFoundException.class.getSimpleName(), "");
127                     fail(client.toString());
128                 } catch (InstanceNotFoundException e) {
129
130                 }
131             }
132         }
133     }
134 }