97258fb697cbc25211a63645a8f454437b59f29c
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / RuntimeBeanEntryTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator;
9
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;
18
19 import javax.management.openmbean.SimpleType;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25
26 import static junit.framework.Assert.assertNotNull;
27 import static junit.framework.Assert.assertNull;
28 import static org.hamcrest.CoreMatchers.is;
29
30 import static org.junit.Assert.assertThat;
31 import static org.mockito.Mockito.doReturn;
32
33 public class RuntimeBeanEntryTest extends AbstractYangTest {
34
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";
44
45     @Test
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"));
58     }
59
60     @Test
61     public void runtimeBeanRPCTest() {
62         // create service interfaces
63         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
64                 .create(threadsModule, "packages.sis");
65         assertNotNull(modulesToSIEs);
66
67         // create MXBeans map
68         Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
69                 threadsJavaModule, modulesToSIEs, context,
70                 new TypeProviderWrapper(new TypeProviderImpl(context)),
71                 PACKAGE_NAME);
72         assertThat(namesToMBEs.isEmpty(), is(false));
73
74         // get threadfactory-naming bean
75         ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
76                 .get(THREADFACTORY_NAMING_MXB_NAME);
77         assertNotNull(threadfactoryNamingMXBean);
78
79         // get runtime beans
80         Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
81                 .getRuntimeBeans();
82         assertThat(runtimeBeanEntries.isEmpty(), is(false));
83
84         // get root runtime bean
85         RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
86                 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
87         assertNotNull(threadfactoryRuntimeBeanEntry);
88         assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true));
89
90         // get thread runtime bean
91         RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
92                 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
93         assertNotNull(runtimeBeanEntry);
94
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));
104
105         // get thread runtime bean rpcs
106         List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
107                 runtimeBeanEntry.getRpcs());
108         assertThat(rpcs.size(), is(2));
109
110         // get sleep rpc and test it
111         RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
112         assertNotNull(rpc);
113         assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME));
114
115         assertThat(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT),  is(true));
116
117         // get sleep rpc input attribute and test it
118         List<JavaAttribute> attributes = rpc.getParameters();
119         assertThat(attributes.size(), is(1));
120         JavaAttribute attribute = attributes.get(0);
121         assertThat(attribute.getAttributeYangName(), is(SLEEP_RPC_INPUT_NAME));
122         assertThat(attribute.getType().getName(), is(SLEEP_RPC_INPUT_TYPE));
123         assertThat(attribute.getLowerCaseCammelCase(), is(SLEEP_RPC_INPUT_NAME));
124         assertThat(attribute.getUpperCaseCammelCase(), is("Millis"));
125         assertNull(attribute.getNullableDefault());
126         assertNull(attribute.getNullableDescription());
127         assertThat(attribute.getOpenType(), is(SimpleType.class));
128     }
129
130     private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
131             final Collection<RuntimeBeanEntry> runtimeBeanEntries,
132             String javaName) {
133         if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
134             for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
135                 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
136                         javaName)) {
137                     return runtimeBeanEntry;
138                 }
139             }
140         }
141         return null;
142     }
143
144     private RuntimeBeanEntry.Rpc getRpcByName(
145             final List<RuntimeBeanEntry.Rpc> rpcs, String name) {
146         if (rpcs != null && !rpcs.isEmpty()) {
147             for (RuntimeBeanEntry.Rpc rpc : rpcs) {
148                 if (rpc.getName().equals(name)) {
149                     return rpc;
150                 }
151             }
152         }
153         return null;
154     }
155
156 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.