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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.doReturn;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
23 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.IdentitySchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
34 public class RuntimeBeanEntryTest extends AbstractYangTest {
36 public static final String PACKAGE_NAME = "packages.sis";
37 public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
38 public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean";
39 public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread";
40 public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread";
41 public static final String SLEEP_RPC_NAME = "sleep";
42 public static final String SLEEP_RPC_OUTPUT = "ThreadState";
43 public static final String SLEEP_RPC_INPUT_NAME = "millis";
44 public static final String SLEEP_RPC_INPUT_TYPE = "Long";
45 private static final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
48 public void createRuntimeBean() {
49 ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
50 doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
51 doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
52 .getUnknownSchemaNodes();
53 Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
54 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
55 TypeProviderImpl(context)), "test", jmxImplModule, context);
56 assertEquals(1, runtimeBeans.size());
57 RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
58 assertTrue(runtimeMXBean.isRoot());
59 assertEquals("test-name", runtimeMXBean.getYangName());
63 public void runtimeBeanRPCTest() {
64 // create service interfaces
65 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
66 .create(threadsModule, "packages.sis",identitiesToSIs);
67 assertNotNull(modulesToSIEs);
70 Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
71 threadsJavaModule, modulesToSIEs, context,
72 new TypeProviderWrapper(new TypeProviderImpl(context)),
74 assertFalse(namesToMBEs.isEmpty());
76 // get threadfactory-naming bean
77 ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
78 .get(THREADFACTORY_NAMING_MXB_NAME);
79 assertNotNull(threadfactoryNamingMXBean);
82 Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
84 assertFalse(runtimeBeanEntries.isEmpty());
86 // get root runtime bean
87 RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
88 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
89 assertNotNull(threadfactoryRuntimeBeanEntry);
90 assertTrue(threadfactoryRuntimeBeanEntry.isRoot());
92 // get thread runtime bean
93 RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
94 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
95 assertNotNull(runtimeBeanEntry);
97 // test thread runtime bean properties
98 assertEquals(THREAD_RUNTIME_BEAN_JAVA_PREFIX, runtimeBeanEntry.getJavaNamePrefix());
99 assertEquals(PACKAGE_NAME, runtimeBeanEntry.getPackageName());
100 assertEquals(PACKAGE_NAME + "." + THREAD_RUNTIME_BEAN_JAVA_NAME,
101 runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
102 .getJavaNameOfRuntimeMXBean()));
103 assertEquals(THREAD_RUNTIME_BEAN_YANG_NAME, runtimeBeanEntry.getYangName());
105 // get thread runtime bean rpcs
106 List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
107 runtimeBeanEntry.getRpcs());
108 assertEquals(2, rpcs.size());
110 // get sleep rpc and test it
111 RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
113 assertEquals(SLEEP_RPC_NAME, rpc.getYangName());
115 assertTrue(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT));
117 // get sleep rpc input attribute and test it
118 List<JavaAttribute> attributes = rpc.getParameters();
119 assertEquals(1, attributes.size());
120 JavaAttribute attribute = attributes.get(0);
121 assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getAttributeYangName());
122 assertEquals(SLEEP_RPC_INPUT_TYPE, attribute.getType().getName());
123 assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getLowerCaseCammelCase());
124 assertEquals("Millis", attribute.getUpperCaseCammelCase());
125 assertNull(attribute.getNullableDefault());
126 assertNull(attribute.getNullableDescription());
127 assertTrue(attribute.getOpenType() instanceof SimpleType);
130 private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
131 final Collection<RuntimeBeanEntry> runtimeBeanEntries,
132 final String javaName) {
133 if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
134 for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
135 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
137 return runtimeBeanEntry;
144 private RuntimeBeanEntry.Rpc getRpcByName(
145 final List<RuntimeBeanEntry.Rpc> rpcs, final String name) {
146 if (rpcs != null && !rpcs.isEmpty()) {
147 for (RuntimeBeanEntry.Rpc rpc : rpcs) {
148 if (rpc.getName().equals(name)) {