629b1542ba6e7f2843c1f73c5618c71137e662ae
[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 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;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import javax.management.openmbean.SimpleType;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
26 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
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     private static final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
45
46     @Test
47     public void createRuntimeBean() {
48         final ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
49         doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
50         doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
51                 .getUnknownSchemaNodes();
52         final Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
53                 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
54                         TypeProviderImpl(this.context)), "test", this.jmxImplModule, this.context);
55         assertEquals(1, runtimeBeans.size());
56         final RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
57         assertTrue(runtimeMXBean.isRoot());
58         assertEquals("test-name", runtimeMXBean.getYangName());
59     }
60
61     @Test
62     public void runtimeBeanRPCTest() {
63         // create service interfaces
64         final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
65                 .create(this.threadsModule, "packages.sis",identitiesToSIs);
66         assertNotNull(modulesToSIEs);
67
68         // create MXBeans map
69         final Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
70                 this.threadsJavaModule, modulesToSIEs, this.context,
71                 new TypeProviderWrapper(new TypeProviderImpl(this.context)),
72                 PACKAGE_NAME);
73         assertFalse(namesToMBEs.isEmpty());
74
75         // get threadfactory-naming bean
76         final ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
77                 .get(THREADFACTORY_NAMING_MXB_NAME);
78         assertNotNull(threadfactoryNamingMXBean);
79
80         // get runtime beans
81         final Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
82                 .getRuntimeBeans();
83         assertFalse(runtimeBeanEntries.isEmpty());
84
85         // get root runtime bean
86         final RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
87                 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
88         assertNotNull(threadfactoryRuntimeBeanEntry);
89         assertTrue(threadfactoryRuntimeBeanEntry.isRoot());
90
91         // get thread runtime bean
92         final RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
93                 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
94         assertNotNull(runtimeBeanEntry);
95
96         // test thread runtime bean properties
97         assertEquals(THREAD_RUNTIME_BEAN_JAVA_PREFIX, runtimeBeanEntry.getJavaNamePrefix());
98         assertEquals(PACKAGE_NAME, runtimeBeanEntry.getPackageName());
99         assertEquals(PACKAGE_NAME + "." + THREAD_RUNTIME_BEAN_JAVA_NAME,
100             runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
101                 .getJavaNameOfRuntimeMXBean()));
102         assertEquals(THREAD_RUNTIME_BEAN_YANG_NAME, runtimeBeanEntry.getYangName());
103
104         // get thread runtime bean rpcs
105         final List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<>(
106                 runtimeBeanEntry.getRpcs());
107         assertEquals(2, rpcs.size());
108
109         // get sleep rpc and test it
110         final RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
111         assertNotNull(rpc);
112         assertEquals(SLEEP_RPC_NAME, rpc.getYangName());
113
114         assertTrue(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT));
115
116         // get sleep rpc input attribute and test it
117         final List<JavaAttribute> attributes = rpc.getParameters();
118         assertEquals(1, attributes.size());
119         final JavaAttribute attribute = attributes.get(0);
120         assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getAttributeYangName());
121         assertEquals(SLEEP_RPC_INPUT_TYPE, attribute.getType().getName());
122         assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getLowerCaseCammelCase());
123         assertEquals("Millis", attribute.getUpperCaseCammelCase());
124         assertNull(attribute.getNullableDefault());
125         assertNull(attribute.getNullableDescription());
126         assertTrue(attribute.getOpenType() instanceof SimpleType);
127     }
128
129     private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
130             final Collection<RuntimeBeanEntry> runtimeBeanEntries,
131             final String javaName) {
132         if ((runtimeBeanEntries != null) && !runtimeBeanEntries.isEmpty()) {
133             for (final RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) {
134                 if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals(
135                         javaName)) {
136                     return runtimeBeanEntry;
137                 }
138             }
139         }
140         return null;
141     }
142
143     private RuntimeBeanEntry.Rpc getRpcByName(
144             final List<RuntimeBeanEntry.Rpc> rpcs, final String name) {
145         if ((rpcs != null) && !rpcs.isEmpty()) {
146             for (final RuntimeBeanEntry.Rpc rpc : rpcs) {
147                 if (rpc.getName().equals(name)) {
148                     return rpc;
149                 }
150             }
151         }
152         return null;
153     }
154
155 }

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