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 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;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.List;
22 import javax.management.openmbean.SimpleType;
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;
33 public class RuntimeBeanEntryTest extends AbstractYangTest {
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";
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"));
61 public void runtimeBeanRPCTest() {
62 // create service interfaces
63 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
64 .create(threadsModule, "packages.sis");
65 assertNotNull(modulesToSIEs);
68 Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
69 threadsJavaModule, modulesToSIEs, context,
70 new TypeProviderWrapper(new TypeProviderImpl(context)),
72 assertThat(namesToMBEs.isEmpty(), is(false));
74 // get threadfactory-naming bean
75 ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
76 .get(THREADFACTORY_NAMING_MXB_NAME);
77 assertNotNull(threadfactoryNamingMXBean);
80 Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
82 assertThat(runtimeBeanEntries.isEmpty(), is(false));
84 // get root runtime bean
85 RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
86 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
87 assertNotNull(threadfactoryRuntimeBeanEntry);
88 assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true));
90 // get thread runtime bean
91 RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
92 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
93 assertNotNull(runtimeBeanEntry);
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));
105 // get thread runtime bean rpcs
106 List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
107 runtimeBeanEntry.getRpcs());
108 assertThat(rpcs.size(), is(2));
110 // get sleep rpc and test it
111 RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
113 assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME));
114 assertThat(rpc.getReturnType().endsWith(SLEEP_RPC_OUTPUT), is(true));
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));
129 private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
130 final Collection<RuntimeBeanEntry> runtimeBeanEntries,
132 if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
133 for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
134 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
136 return runtimeBeanEntry;
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)) {