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;
24 import javax.management.openmbean.SimpleType;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
29 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
32 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
36 public class RuntimeBeanEntryTest extends AbstractYangTest {
38 public static final String PACKAGE_NAME = "packages.sis";
39 public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
40 public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean";
41 public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread";
42 public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread";
43 public static final String SLEEP_RPC_NAME = "sleep";
44 public static final String SLEEP_RPC_OUTPUT = "ThreadState";
45 public static final String SLEEP_RPC_INPUT_NAME = "millis";
46 public static final String SLEEP_RPC_INPUT_TYPE = "Long";
47 private static final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
50 public void createRuntimeBean() {
51 ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
52 doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
53 doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
54 .getUnknownSchemaNodes();
55 Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
56 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
57 TypeProviderImpl(context)), "test", jmxImplModule);
58 assertEquals(1, runtimeBeans.size());
59 RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
60 assertTrue(runtimeMXBean.isRoot());
61 assertEquals("test-name", runtimeMXBean.getYangName());
65 public void runtimeBeanRPCTest() {
66 // create service interfaces
67 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
68 .create(threadsModule, "packages.sis",identitiesToSIs);
69 assertNotNull(modulesToSIEs);
72 Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
73 threadsJavaModule, modulesToSIEs, context,
74 new TypeProviderWrapper(new TypeProviderImpl(context)),
76 assertFalse(namesToMBEs.isEmpty());
78 // get threadfactory-naming bean
79 ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
80 .get(THREADFACTORY_NAMING_MXB_NAME);
81 assertNotNull(threadfactoryNamingMXBean);
84 Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
86 assertFalse(runtimeBeanEntries.isEmpty());
88 // get root runtime bean
89 RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
90 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
91 assertNotNull(threadfactoryRuntimeBeanEntry);
92 assertTrue(threadfactoryRuntimeBeanEntry.isRoot());
94 // get thread runtime bean
95 RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
96 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
97 assertNotNull(runtimeBeanEntry);
99 // test thread runtime bean properties
100 assertEquals(THREAD_RUNTIME_BEAN_JAVA_PREFIX, runtimeBeanEntry.getJavaNamePrefix());
101 assertEquals(PACKAGE_NAME, runtimeBeanEntry.getPackageName());
102 assertEquals(PACKAGE_NAME + "." + THREAD_RUNTIME_BEAN_JAVA_NAME,
103 runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
104 .getJavaNameOfRuntimeMXBean()));
105 assertEquals(THREAD_RUNTIME_BEAN_YANG_NAME, runtimeBeanEntry.getYangName());
107 // get thread runtime bean rpcs
108 List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
109 runtimeBeanEntry.getRpcs());
110 assertEquals(2, rpcs.size());
112 // get sleep rpc and test it
113 RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
115 assertEquals(SLEEP_RPC_NAME, rpc.getYangName());
117 assertTrue(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT));
119 // get sleep rpc input attribute and test it
120 List<JavaAttribute> attributes = rpc.getParameters();
121 assertEquals(1, attributes.size());
122 JavaAttribute attribute = attributes.get(0);
123 assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getAttributeYangName());
124 assertEquals(SLEEP_RPC_INPUT_TYPE, attribute.getType().getName());
125 assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getLowerCaseCammelCase());
126 assertEquals("Millis", attribute.getUpperCaseCammelCase());
127 assertNull(attribute.getNullableDefault());
128 assertNull(attribute.getNullableDescription());
129 assertTrue(attribute.getOpenType() instanceof SimpleType);
132 private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
133 final Collection<RuntimeBeanEntry> runtimeBeanEntries,
134 final String javaName) {
135 if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
136 for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
137 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
139 return runtimeBeanEntry;
146 private RuntimeBeanEntry.Rpc getRpcByName(
147 final List<RuntimeBeanEntry.Rpc> rpcs, final String name) {
148 if (rpcs != null && !rpcs.isEmpty()) {
149 for (RuntimeBeanEntry.Rpc rpc : rpcs) {
150 if (rpc.getName().equals(name)) {