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