a06d2caa271d626c4c791f230cdb54aa5d61b5e2
[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.*;
21
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.doReturn;
25
26 public class RuntimeBeanEntryTest extends AbstractYangTest {
27
28     public static final String PACKAGE_NAME = "packages.sis";
29     public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
30     public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean";
31     public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread";
32     public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread";
33     public static final String SLEEP_RPC_NAME = "sleep";
34     public static final String SLEEP_RPC_OUTPUT = "ThreadState";
35     public static final String SLEEP_RPC_INPUT_NAME = "millis";
36     public static final String SLEEP_RPC_INPUT_TYPE = "Long";
37
38     @Test
39     public void createRuntimeBean() {
40         ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
41         doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
42         doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
43                 .getUnknownSchemaNodes();
44         Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
45                 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
46                         TypeProviderImpl(context)), "test", jmxImplModule);
47         assertThat(runtimeBeans.size(), is(1));
48         RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
49         assertThat(runtimeMXBean.isRoot(), is(true));
50         assertThat(runtimeMXBean.getYangName(), is("test-name"));
51     }
52
53     @Test
54     public void runtimeBeanRPCTest() {
55         // create service interfaces
56         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
57                 .create(threadsModule, "packages.sis");
58         assertNotNull(modulesToSIEs);
59
60         // create MXBeans map
61         Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
62                 threadsJavaModule, modulesToSIEs, context,
63                 new TypeProviderWrapper(new TypeProviderImpl(context)),
64                 PACKAGE_NAME);
65         assertThat(namesToMBEs.isEmpty(), is(false));
66
67         // get threadfactory-naming bean
68         ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
69                 .get(THREADFACTORY_NAMING_MXB_NAME);
70         assertNotNull(threadfactoryNamingMXBean);
71
72         // get runtime beans
73         Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
74                 .getRuntimeBeans();
75         assertThat(runtimeBeanEntries.isEmpty(), is(false));
76
77         // get root runtime bean
78         RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
79                 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
80         assertNotNull(threadfactoryRuntimeBeanEntry);
81         assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true));
82
83         // get thread runtime bean
84         RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
85                 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
86         assertNotNull(runtimeBeanEntry);
87
88         // test thread runtime bean properties
89         assertThat(runtimeBeanEntry.getJavaNamePrefix(),
90                 is(THREAD_RUNTIME_BEAN_JAVA_PREFIX));
91         assertThat(runtimeBeanEntry.getPackageName(), is(PACKAGE_NAME));
92         assertThat(runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
93                 .getJavaNameOfRuntimeMXBean()), is(PACKAGE_NAME + "."
94                 + THREAD_RUNTIME_BEAN_JAVA_NAME));
95         assertThat(runtimeBeanEntry.getYangName(),
96                 is(THREAD_RUNTIME_BEAN_YANG_NAME));
97
98         // get thread runtime bean rpcs
99         List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
100                 runtimeBeanEntry.getRpcs());
101         assertThat(rpcs.size(), is(2));
102
103         // get sleep rpc and test it
104         RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
105         assertNotNull(rpc);
106         assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME));
107
108         assertThat(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT),  is(true));
109
110         // get sleep rpc input attribute and test it
111         List<JavaAttribute> attributes = rpc.getParameters();
112         assertThat(attributes.size(), is(1));
113         JavaAttribute attribute = attributes.get(0);
114         assertThat(attribute.getAttributeYangName(), is(SLEEP_RPC_INPUT_NAME));
115         assertThat(attribute.getType().getName(), is(SLEEP_RPC_INPUT_TYPE));
116         assertThat(attribute.getLowerCaseCammelCase(), is(SLEEP_RPC_INPUT_NAME));
117         assertThat(attribute.getUpperCaseCammelCase(), is("Millis"));
118         assertNull(attribute.getNullableDefault());
119         assertNull(attribute.getNullableDescription());
120         assertThat(attribute.getOpenType(), is(SimpleType.class));
121     }
122
123     private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
124             final Collection<RuntimeBeanEntry> runtimeBeanEntries,
125             String javaName) {
126         if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) {
127             for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
128                 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
129                         javaName)) {
130                     return runtimeBeanEntry;
131                 }
132             }
133         }
134         return null;
135     }
136
137     private RuntimeBeanEntry.Rpc getRpcByName(
138             final List<RuntimeBeanEntry.Rpc> rpcs, String name) {
139         if (rpcs != null && !rpcs.isEmpty()) {
140             for (RuntimeBeanEntry.Rpc rpc : rpcs) {
141                 if (rpc.getName().equals(name)) {
142                     return rpc;
143                 }
144             }
145         }
146         return null;
147     }
148
149 }