b570302563b9d9c4e00a4171614beb28df578cd8
[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 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;
20
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;
26 import java.util.Map;
27
28 import static junit.framework.Assert.assertNotNull;
29 import static junit.framework.Assert.assertNull;
30 import static org.hamcrest.CoreMatchers.is;
31
32 import static org.junit.Assert.assertThat;
33 import static org.mockito.Mockito.doReturn;
34
35 public class RuntimeBeanEntryTest extends AbstractYangTest {
36
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<>();
47
48     @Test
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"));
61     }
62
63     @Test
64     public void runtimeBeanRPCTest() {
65         // create service interfaces
66         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
67                 .create(threadsModule, "packages.sis",identitiesToSIs);
68         assertNotNull(modulesToSIEs);
69
70         // create MXBeans map
71         Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
72                 threadsJavaModule, modulesToSIEs, context,
73                 new TypeProviderWrapper(new TypeProviderImpl(context)),
74                 PACKAGE_NAME);
75         assertThat(namesToMBEs.isEmpty(), is(false));
76
77         // get threadfactory-naming bean
78         ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
79                 .get(THREADFACTORY_NAMING_MXB_NAME);
80         assertNotNull(threadfactoryNamingMXBean);
81
82         // get runtime beans
83         Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
84                 .getRuntimeBeans();
85         assertThat(runtimeBeanEntries.isEmpty(), is(false));
86
87         // get root runtime bean
88         RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
89                 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
90         assertNotNull(threadfactoryRuntimeBeanEntry);
91         assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true));
92
93         // get thread runtime bean
94         RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
95                 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
96         assertNotNull(runtimeBeanEntry);
97
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));
107
108         // get thread runtime bean rpcs
109         List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
110                 runtimeBeanEntry.getRpcs());
111         assertThat(rpcs.size(), is(2));
112
113         // get sleep rpc and test it
114         RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
115         assertNotNull(rpc);
116         assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME));
117
118         assertThat(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT),  is(true));
119
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));
131     }
132
133     private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
134             final Collection<RuntimeBeanEntry> runtimeBeanEntries,
135             String javaName) {
136         if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
137             for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
138                 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
139                         javaName)) {
140                     return runtimeBeanEntry;
141                 }
142             }
143         }
144         return null;
145     }
146
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)) {
152                     return rpc;
153                 }
154             }
155         }
156         return null;
157     }
158
159 }

©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.