X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2FRuntimeBeanEntryTest.java;fp=opendaylight%2Fconfig%2Fyang-jmx-generator%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2FRuntimeBeanEntryTest.java;h=597cf4ccc9d1cf033cb6ae919662c69ec89008f0;hp=0000000000000000000000000000000000000000;hb=9fb64948564e252018f9b1e13e7cea2c92f991aa;hpb=1742b3894614be478c333a1043ced8ef1bc5dc84 diff --git a/opendaylight/config/yang-jmx-generator/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntryTest.java b/opendaylight/config/yang-jmx-generator/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntryTest.java new file mode 100644 index 0000000000..597cf4ccc9 --- /dev/null +++ b/opendaylight/config/yang-jmx-generator/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntryTest.java @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.config.yangjmxgenerator; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doReturn; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +import javax.management.openmbean.SimpleType; + +import org.junit.Test; +import org.mockito.Mockito; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute; +import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; + +public class RuntimeBeanEntryTest extends AbstractYangTest { + + public static final String PACKAGE_NAME = "packages.sis"; + public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming"; + public static final String THREAD_RUNTIME_BEAN_JAVA_NAME = "ThreadRuntimeMXBean"; + public static final String THREAD_RUNTIME_BEAN_JAVA_PREFIX = "Thread"; + public static final String THREAD_RUNTIME_BEAN_YANG_NAME = "thread"; + public static final String SLEEP_RPC_NAME = "sleep"; + public static final String SLEEP_RPC_OUTPUT = "ThreadState"; + public static final String SLEEP_RPC_INPUT_NAME = "millis"; + public static final String SLEEP_RPC_INPUT_TYPE = "Long"; + + @Test + public void createRuntimeBean() { + ChoiceCaseNode caseNode = Mockito.mock(ChoiceCaseNode.class); + doReturn(new HashSet()).when(caseNode).getChildNodes(); + doReturn(new ArrayList()).when(caseNode) + .getUnknownSchemaNodes(); + Map runtimeBeans = RuntimeBeanEntry + .extractClassNameToRuntimeBeanMap(PACKAGE_NAME, caseNode, "test-name", new TypeProviderWrapper(new + TypeProviderImpl(context)), "test", jmxImplModule); + assertThat(runtimeBeans.size(), is(1)); + RuntimeBeanEntry runtimeMXBean = runtimeBeans.get("testRuntimeMXBean"); + assertThat(runtimeMXBean.isRoot(), is(true)); + assertThat(runtimeMXBean.getYangName(), is("test-name")); + } + + @Test + public void runtimeBeanRPCTest() { + // create service interfaces + Map modulesToSIEs = ServiceInterfaceEntry + .create(threadsModule, "packages.sis"); + assertNotNull(modulesToSIEs); + + // create MXBeans map + Map namesToMBEs = ModuleMXBeanEntry.create( + threadsJavaModule, modulesToSIEs, context, + new TypeProviderWrapper(new TypeProviderImpl(context)), + PACKAGE_NAME); + assertThat(namesToMBEs.isEmpty(), is(false)); + + // get threadfactory-naming bean + ModuleMXBeanEntry threadfactoryNamingMXBean = namesToMBEs + .get(THREADFACTORY_NAMING_MXB_NAME); + assertNotNull(threadfactoryNamingMXBean); + + // get runtime beans + Collection runtimeBeanEntries = threadfactoryNamingMXBean + .getRuntimeBeans(); + assertThat(runtimeBeanEntries.isEmpty(), is(false)); + + // get root runtime bean + RuntimeBeanEntry threadfactoryRuntimeBeanEntry = getRuntimeBeanEntryByJavaName( + runtimeBeanEntries, "NamingThreadFactoryRuntimeMXBean"); + assertNotNull(threadfactoryRuntimeBeanEntry); + assertThat(threadfactoryRuntimeBeanEntry.isRoot(), is(true)); + + // get thread runtime bean + RuntimeBeanEntry runtimeBeanEntry = getRuntimeBeanEntryByJavaName( + runtimeBeanEntries, THREAD_RUNTIME_BEAN_JAVA_NAME); + assertNotNull(runtimeBeanEntry); + + // test thread runtime bean properties + assertThat(runtimeBeanEntry.getJavaNamePrefix(), + is(THREAD_RUNTIME_BEAN_JAVA_PREFIX)); + assertThat(runtimeBeanEntry.getPackageName(), is(PACKAGE_NAME)); + assertThat(runtimeBeanEntry.getFullyQualifiedName(runtimeBeanEntry + .getJavaNameOfRuntimeMXBean()), is(PACKAGE_NAME + "." + + THREAD_RUNTIME_BEAN_JAVA_NAME)); + assertThat(runtimeBeanEntry.getYangName(), + is(THREAD_RUNTIME_BEAN_YANG_NAME)); + + // get thread runtime bean rpcs + List rpcs = new ArrayList( + runtimeBeanEntry.getRpcs()); + assertThat(rpcs.size(), is(2)); + + // get sleep rpc and test it + RuntimeBeanEntry.Rpc rpc = getRpcByName(rpcs, SLEEP_RPC_NAME); + assertNotNull(rpc); + assertThat(rpc.getYangName(), is(SLEEP_RPC_NAME)); + assertThat(rpc.getReturnType().endsWith(SLEEP_RPC_OUTPUT), is(true)); + + // get sleep rpc input attribute and test it + List attributes = rpc.getParameters(); + assertThat(attributes.size(), is(1)); + JavaAttribute attribute = attributes.get(0); + assertThat(attribute.getAttributeYangName(), is(SLEEP_RPC_INPUT_NAME)); + assertThat(attribute.getType().getName(), is(SLEEP_RPC_INPUT_TYPE)); + assertThat(attribute.getLowerCaseCammelCase(), is(SLEEP_RPC_INPUT_NAME)); + assertThat(attribute.getUpperCaseCammelCase(), is("Millis")); + assertNull(attribute.getNullableDefault()); + assertNull(attribute.getNullableDescription()); + assertThat(attribute.getOpenType(), is(SimpleType.class)); + } + + private RuntimeBeanEntry getRuntimeBeanEntryByJavaName( + final Collection runtimeBeanEntries, + String javaName) { + if (runtimeBeanEntries != null && !runtimeBeanEntries.isEmpty()) { + for (RuntimeBeanEntry runtimeBeanEntry : runtimeBeanEntries) { + if (runtimeBeanEntry.getJavaNameOfRuntimeMXBean().equals( + javaName)) { + return runtimeBeanEntry; + } + } + } + return null; + } + + private RuntimeBeanEntry.Rpc getRpcByName( + final List rpcs, String name) { + if (rpcs != null && !rpcs.isEmpty()) { + for (RuntimeBeanEntry.Rpc rpc : rpcs) { + if (rpc.getName().equals(name)) { + return rpc; + } + } + } + return null; + } + +}