Fix checkstyle warnings in yang-jmx-generator.
[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
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;
22 import java.util.Map;
23 import javax.management.openmbean.SimpleType;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
27 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
33
34 public class RuntimeBeanEntryTest extends AbstractYangTest {
35
36     public static final String PACKAGE_NAME = "packages.sis";
37     public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
38     public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean";
39     public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread";
40     public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread";
41     public static final String SLEEP_RPC_NAME = "sleep";
42     public static final String SLEEP_RPC_OUTPUT = "ThreadState";
43     public static final String SLEEP_RPC_INPUT_NAME = "millis";
44     public static final String SLEEP_RPC_INPUT_TYPE = "Long";
45     private static final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
46
47     @Test
48     public void createRuntimeBean() {
49         ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class);
50         doReturn(new HashSet<LeafSchemaNode>()).when(caseNode).getChildNodes();
51         doReturn(new ArrayList<UnknownSchemaNode>()).when(caseNode)
52                 .getUnknownSchemaNodes();
53         Map<String, RuntimeBeanEntry> runtimeBeans = RuntimeBeanEntry
54                 .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new
55                         TypeProviderImpl(context)), "test", jmxImplModule);
56         assertEquals(1, runtimeBeans.size());
57         RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean");
58         assertTrue(runtimeMXBean.isRoot());
59         assertEquals("test-name", runtimeMXBean.getYangName());
60     }
61
62     @Test
63     public void runtimeBeanRPCTest() {
64         // create service interfaces
65         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
66                 .create(threadsModule, "packages.sis",identitiesToSIs);
67         assertNotNull(modulesToSIEs);
68
69         // create MXBeans map
70         Map<String, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry.create(
71                 threadsJavaModule, modulesToSIEs, context,
72                 new TypeProviderWrapper(new TypeProviderImpl(context)),
73                 PACKAGE_NAME);
74         assertFalse(namesToMBEs.isEmpty());
75
76         // get threadfactory-naming bean
77         ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs
78                 .get(THREADFACTORY_NAMING_MXB_NAME);
79         assertNotNull(threadfactoryNamingMXBean);
80
81         // get runtime beans
82         Collection<RuntimeBeanEntry> runtimeBeanEntries = threadfactoryNamingMXBean
83                 .getRuntimeBeans();
84         assertFalse(runtimeBeanEntries.isEmpty());
85
86         // get root runtime bean
87         RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName(
88                 runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean");
89         assertNotNull(threadfactoryRuntimeBeanEntry);
90         assertTrue(threadfactoryRuntimeBeanEntry.isRoot());
91
92         // get thread runtime bean
93         RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName(
94                 runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME);
95         assertNotNull(runtimeBeanEntry);
96
97         // test thread runtime bean properties
98         assertEquals(THREAD_RUNTIME_BEAN_JAVA_PREFIX, runtimeBeanEntry.getJavaNamePrefix());
99         assertEquals(PACKAGE_NAME, runtimeBeanEntry.getPackageName());
100         assertEquals(PACKAGE_NAME + "." + THREAD_RUNTIME_BEAN_JAVA_NAME,
101             runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry
102                 .getJavaNameOfRuntimeMXBean()));
103         assertEquals(THREAD_RUNTIME_BEAN_YANG_NAME, runtimeBeanEntry.getYangName());
104
105         // get thread runtime bean rpcs
106         List<RuntimeBeanEntry.Rpc> rpcs = new ArrayList<RuntimeBeanEntry.Rpc>(
107                 runtimeBeanEntry.getRpcs());
108         assertEquals(2, rpcs.size());
109
110         // get sleep rpc and test it
111         RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME);
112         assertNotNull(rpc);
113         assertEquals(SLEEP_RPC_NAME, rpc.getYangName());
114
115         assertTrue(((JavaAttribute)rpc.getReturnType()).getType().getFullyQualifiedName().endsWith(SLEEP_RPC_OUTPUT));
116
117         // get sleep rpc input attribute and test it
118         List<JavaAttribute> attributes = rpc.getParameters();
119         assertEquals(1, attributes.size());
120         JavaAttribute attribute = attributes.get(0);
121         assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getAttributeYangName());
122         assertEquals(SLEEP_RPC_INPUT_TYPE, attribute.getType().getName());
123         assertEquals(SLEEP_RPC_INPUT_NAME, attribute.getLowerCaseCammelCase());
124         assertEquals("Millis", attribute.getUpperCaseCammelCase());
125         assertNull(attribute.getNullableDefault());
126         assertNull(attribute.getNullableDescription());
127         assertTrue(attribute.getOpenType() instanceof SimpleType);
128     }
129
130     private RuntimeBeanEntry getRuntimeBeanEntryByJavaName(
131             final Collection<RuntimeBeanEntry> runtimeBeanEntries,
132             final 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, final 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 }