2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator;
10 import org.junit.Test;
11 import org.mockito.Mockito;
12 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
13 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import javax.management.openmbean.SimpleType;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.doReturn;
26 public class RuntimeBeanEntryTest extends AbstractYangTest {
28 public static final String PACKAGE_NAME = "packages.sis";
29 public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
30 public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean";
31 public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread";
32 public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread";
33 public static final String SLEEP_RPC_NAME = "sleep";
34 public static final String SLEEP_RPC_OUTPUT = "ThreadState";
35 public static final String SLEEP_RPC_INPUT_NAME = "millis";
36 public static final String SLEEP_RPC_INPUT_TYPE = "Long";
39 public void createRuntimeBean() {
40 ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
41 doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
42 doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
43 .getUnknownSchemaNodes();
44 Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
45 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
46 TypeProviderImpl(context)), "test", jmxImplModule);
47 assertThat(runtimeBeans.size(), is(1));
48 RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
49 assertThat(runtimeMXBean.isRoot(), is(true));
50 assertThat(runtimeMXBean.getYangName(), is("test-name"));
54 public void runtimeBeanRPCTest() {
55 // create service interfaces
56 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
57 .create(threadsModule, "packages.sis");
58 assertNotNull(modulesToSIEs);
61 Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
62 threadsJavaModule, modulesToSIEs, context,
63 new TypeProviderWrapper(new TypeProviderImpl(context)),
65 assertThat(namesToMBEs.isEmpty(), is(false));
67 // get threadfactory-naming bean
68 ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
69 .get(THREADFACTORY_NAMING_MXB_NAME);
70 assertNotNull(threadfactoryNamingMXBean);
73 Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
75 assertThat(runtimeBeanEntries.isEmpty(), is(false));
77 // get root runtime bean
78 RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
79 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
80 assertNotNull(threadfactoryRuntimeBeanEntry);
81 assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true));
83 // get thread runtime bean
84 RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
85 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
86 assertNotNull(runtimeBeanEntry);
88 // test thread runtime bean properties
89 assertThat(runtimeBeanEntry.getJavaNamePrefix(),
90 is(THREAD_RUNTIME_BEAN_JAVA_PREFIX));
91 assertThat(runtimeBeanEntry.getPackageName(), is(PACKAGE_NAME));
92 assertThat(runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
93 .getJavaNameOfRuntimeMXBean()), is(PACKAGE_NAME + "."
94 + THREAD_RUNTIME_BEAN_JAVA_NAME));
95 assertThat(runtimeBeanEntry.getYangName(),
96 is(THREAD_RUNTIME_BEAN_YANG_NAME));
98 // get thread runtime bean rpcs
99 List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
100 runtimeBeanEntry.getRpcs());
101 assertThat(rpcs.size(), is(2));
103 // get sleep rpc and test it
104 RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
106 assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME));
108 assertThat(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT), is(true));
110 // get sleep rpc input attribute and test it
111 List<JavaAttribute> attributes = rpc.getParameters();
112 assertThat(attributes.size(), is(1));
113 JavaAttribute attribute = attributes.get(0);
114 assertThat(attribute.getAttributeYangName(), is(SLEEP_RPC_INPUT_NAME));
115 assertThat(attribute.getType().getName(), is(SLEEP_RPC_INPUT_TYPE));
116 assertThat(attribute.getLowerCaseCammelCase(), is(SLEEP_RPC_INPUT_NAME));
117 assertThat(attribute.getUpperCaseCammelCase(), is("Millis"));
118 assertNull(attribute.getNullableDefault());
119 assertNull(attribute.getNullableDescription());
120 assertThat(attribute.getOpenType(), is(SimpleType.class));
123 private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
124 final Collection<RuntimeBeanEntry> runtimeBeanEntries,
126 if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
127 for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
128 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
130 return runtimeBeanEntry;
137 private RuntimeBeanEntry.Rpc getRpcByName(
138 final List<RuntimeBeanEntry.Rpc> rpcs, String name) {
139 if (rpcs != null && !rpcs.isEmpty()) {
140 for (RuntimeBeanEntry.Rpc rpc : rpcs) {
141 if (rpc.getName().equals(name)) {