Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / ConfigRegistryClientsTest.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.hamcrest.core.Is.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.Assert.fail;
14 import static org.junit.matchers.JUnitMatchers.containsString;
15
16 import java.lang.management.ManagementFactory;
17 import java.util.Set;
18
19 import javax.management.InstanceNotFoundException;
20 import javax.management.MBeanServer;
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.ConfigRegistry;
27 import org.opendaylight.controller.config.api.ValidationException;
28 import org.opendaylight.controller.config.util.jolokia.ConfigRegistryJolokiaClient;
29
30 public class ConfigRegistryClientsTest {
31
32     private String jolokiaURL;
33
34     private TestingConfigRegistry testingRegistry;
35     private ObjectName testingRegistryON;
36     private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
37     private ConfigRegistryClient jmxRegistryClient, jolokiaRegistryClient;
38
39     @Before
40     public void setUp() throws Exception {
41         jolokiaURL = JolokiaHelper.startTestingJolokia();
42         testingRegistry = new TestingConfigRegistry();
43         testingRegistryON = ConfigRegistry.OBJECT_NAME;
44         mbs.registerMBean(testingRegistry, testingRegistryON);
45         jmxRegistryClient = new ConfigRegistryJMXClient(
46                 ManagementFactory.getPlatformMBeanServer());
47         jolokiaRegistryClient = new ConfigRegistryJolokiaClient(jolokiaURL);
48     }
49
50     @After
51     public void cleanUp() throws Exception {
52         JolokiaHelper.stopJolokia();
53         if (testingRegistryON != null) {
54             mbs.unregisterMBean(testingRegistryON);
55         }
56     }
57
58     @Test
59     public void testLookupRuntimeBeans() throws Exception {
60         Set<ObjectName> jmxLookup = lookupRuntimeBeans(jmxRegistryClient);
61         Set<ObjectName> jolokiaLookup = lookupRuntimeBeans(jolokiaRegistryClient);
62         assertEquals(jmxLookup, jolokiaLookup);
63     }
64
65     private Set<ObjectName> lookupRuntimeBeans(ConfigRegistryClient client)
66             throws Exception {
67         Set<ObjectName> beans = client.lookupRuntimeBeans();
68         for (ObjectName on : beans) {
69             assertEquals("RuntimeBean", on.getKeyProperty("type"));
70         }
71         assertEquals(3, beans.size());
72         return beans;
73     }
74
75     @Test
76     public void testLookupRuntimeBeansWithIfcNameAndInstanceName()
77             throws InstanceNotFoundException {
78         Set<ObjectName> jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
79                 jmxRegistryClient, TestingConfigRegistry.moduleName1,
80                 TestingConfigRegistry.instName1);
81         assertEquals(1, jmxLookup.size());
82         Set<ObjectName> jolokiaLookup = clientLookupRuntimeBeansWithModuleAndInstance(
83                 jolokiaRegistryClient, TestingConfigRegistry.moduleName1,
84                 TestingConfigRegistry.instName1);
85         assertEquals(jmxLookup, jolokiaLookup);
86
87         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
88                 jmxRegistryClient, TestingConfigRegistry.moduleName2,
89                 TestingConfigRegistry.instName2);
90         assertEquals(1, jmxLookup.size());
91         jolokiaLookup = clientLookupRuntimeBeansWithModuleAndInstance(
92                 jolokiaRegistryClient, TestingConfigRegistry.moduleName2,
93                 TestingConfigRegistry.instName2);
94         assertEquals(jmxLookup, jolokiaLookup);
95
96         jmxLookup = clientLookupRuntimeBeansWithModuleAndInstance(
97                 jmxRegistryClient, TestingConfigRegistry.moduleName1,
98                 TestingConfigRegistry.instName2);
99         assertEquals(0, jmxLookup.size());
100         jolokiaLookup = clientLookupRuntimeBeansWithModuleAndInstance(
101                 jolokiaRegistryClient, TestingConfigRegistry.moduleName1,
102                 TestingConfigRegistry.instName2);
103         assertEquals(jmxLookup, jolokiaLookup);
104     }
105
106     private Set<ObjectName> clientLookupRuntimeBeansWithModuleAndInstance(
107             ConfigRegistryClient client, String moduleName, String instanceName) {
108         Set<ObjectName> beans = client.lookupRuntimeBeans(moduleName, instanceName);
109         if (beans.size() > 0) {
110             assertEquals("RuntimeBean",
111                     beans.iterator().next().getKeyProperty("type"));
112         }
113         return beans;
114     }
115
116     @Test
117     public void testValidationExceptionDeserialization() {
118         try {
119             jolokiaRegistryClient.commitConfig(null);
120             fail();
121         } catch (ValidationException e) {
122             String moduleName = "moduleName", instanceName = "instanceName";
123             assertThat(e.getFailedValidations().containsKey(moduleName),
124                     is(true));
125             assertThat(e.getFailedValidations().size(), is(1));
126             assertThat(e.getFailedValidations().get(moduleName).size(), is(1));
127             assertThat(
128                     e.getFailedValidations().get(moduleName)
129                             .containsKey(instanceName), is(true));
130             assertThat(
131                     e.getFailedValidations().get(moduleName).get(instanceName)
132                             .getMessage(), is("message"));
133             assertThat(
134                     e.getFailedValidations().get(moduleName).get(instanceName)
135                             .getTrace(),
136                     containsString("org.opendaylight.controller.config.util.TestingConfigRegistry.commitConfig"));
137         }
138     }
139
140 }