Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / RuntimeBeanEntryTest.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.yangjmxgenerator;
9
10 import static org.hamcrest.CoreMatchers.is;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThat;
14 import static org.mockito.Mockito.doReturn;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.management.openmbean.SimpleType;
23
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
27 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
32
33 public class RuntimeBeanEntryTest extends AbstractYangTest {
34
35     public static final String PACKAGE_NAME = "packages.sis";
36     public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
37     public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean";
38     public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread";
39     public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread";
40     public static final String SLEEP_RPC_NAME = "sleep";
41     public static final String SLEEP_RPC_OUTPUT = "ThreadState";
42     public static final String SLEEP_RPC_INPUT_NAME = "millis";
43     public static final String SLEEP_RPC_INPUT_TYPE = "Long";
44
45     @Test
46     public void createRuntimeBean() {
47         ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
48         doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
49         doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
50                 .getUnknownSchemaNodes();
51         Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
52                 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
53                         TypeProviderImpl(context)), "test", jmxImplModule);
54         assertThat(runtimeBeans.size(), is(1));
55         RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
56         assertThat(runtimeMXBean.isRoot(), is(true));
57         assertThat(runtimeMXBean.getYangName(), is("test-name"));
58     }
59
60     @Test
61     public void runtimeBeanRPCTest() {
62         // create service interfaces
63         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
64                 .create(threadsModule, "packages.sis");
65         assertNotNull(modulesToSIEs);
66
67         // create MXBeans map
68         Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
69                 threadsJavaModule, modulesToSIEs, context,
70                 new TypeProviderWrapper(new TypeProviderImpl(context)),
71                 PACKAGE_NAME);
72         assertThat(namesToMBEs.isEmpty(), is(false));
73
74         // get threadfactory-naming bean
75         ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
76                 .get(THREADFACTORY_NAMING_MXB_NAME);
77         assertNotNull(threadfactoryNamingMXBean);
78
79         // get runtime beans
80         Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
81                 .getRuntimeBeans();
82         assertThat(runtimeBeanEntries.isEmpty(), is(false));
83
84         // get root runtime bean
85         RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
86                 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
87         assertNotNull(threadfactoryRuntimeBeanEntry);
88         assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true));
89
90         // get thread runtime bean
91         RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
92                 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
93         assertNotNull(runtimeBeanEntry);
94
95         // test thread runtime bean properties
96         assertThat(runtimeBeanEntry.getJavaNamePrefix(),
97                 is(THREAD_RUNTIME_BEAN_JAVA_PREFIX));
98         assertThat(runtimeBeanEntry.getPackageName(), is(PACKAGE_NAME));
99         assertThat(runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
100                 .getJavaNameOfRuntimeMXBean()), is(PACKAGE_NAME + "."
101                 + THREAD_RUNTIME_BEAN_JAVA_NAME));
102         assertThat(runtimeBeanEntry.getYangName(),
103                 is(THREAD_RUNTIME_BEAN_YANG_NAME));
104
105         // get thread runtime bean rpcs
106         List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
107                 runtimeBeanEntry.getRpcs());
108         assertThat(rpcs.size(), is(2));
109
110         // get sleep rpc and test it
111         RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
112         assertNotNull(rpc);
113         assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME));
114         assertThat(rpc.getReturnType().endsWith(SLEEP_RPC_OUTPUT), is(true));
115
116         // get sleep rpc input attribute and test it
117         List<JavaAttribute> attributes = rpc.getParameters();
118         assertThat(attributes.size(), is(1));
119         JavaAttribute attribute = attributes.get(0);
120         assertThat(attribute.getAttributeYangName(), is(SLEEP_RPC_INPUT_NAME));
121         assertThat(attribute.getType().getName(), is(SLEEP_RPC_INPUT_TYPE));
122         assertThat(attribute.getLowerCaseCammelCase(), is(SLEEP_RPC_INPUT_NAME));
123         assertThat(attribute.getUpperCaseCammelCase(), is("Millis"));
124         assertNull(attribute.getNullableDefault());
125         assertNull(attribute.getNullableDescription());
126         assertThat(attribute.getOpenType(), is(SimpleType.class));
127     }
128
129     private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
130             final Collection<RuntimeBeanEntry> runtimeBeanEntries,
131             String javaName) {
132         if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
133             for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
134                 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
135                         javaName)) {
136                     return runtimeBeanEntry;
137                 }
138             }
139         }
140         return null;
141     }
142
143     private RuntimeBeanEntry.Rpc getRpcByName(
144             final List<RuntimeBeanEntry.Rpc> rpcs, String name) {
145         if (rpcs != null && !rpcs.isEmpty()) {
146             for (RuntimeBeanEntry.Rpc rpc : rpcs) {
147                 if (rpc.getName().equals(name)) {
148                     return rpc;
149                 }
150             }
151         }
152         return null;
153     }
154
155 }