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