2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
10 import static org.hamcrest.CoreMatchers.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThat;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
21 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntryTest;
22 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
24 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
27 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate;
28 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralClassTemplate;
29 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralInterfaceTemplate;
30 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory;
31 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method;
33 public class ModuleMXBeanEntryPluginTest extends ModuleMXBeanEntryTest {
36 public void testThreadsJavaPlugin() {
37 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
38 {// check threadfactory-naming
39 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
40 .get(THREADFACTORY_NAMING_MXB_NAME);
41 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
43 assertThat(runtimeBeans.size(), is(4));
44 // first one should be root
46 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
47 THREADFACTORY_NAMING_MXB_NAME);
48 assertThat(rootRB.isRoot(), is(true));
49 assertThat(rootRB.getAttributes().size(), is(1));
50 JavaAttribute attribute = (JavaAttribute) rootRB
51 .getAttributes().iterator().next();
52 assertThat(attribute.getAttributeYangName(),
53 is("created-sessions"));
54 assertThat(rootRB.getYangName(),
55 is(THREADFACTORY_NAMING_MXB_NAME));
56 Map<String, FtlTemplate> ftlMap = TemplateFactory
57 .getTOAndMXInterfaceFtlFiles(rootRB);
58 assertThat(ftlMap.size(), is(1));
59 GeneralInterfaceTemplate rootGeneratorInterface = (GeneralInterfaceTemplate) ftlMap
60 .get("NamingThreadFactoryRuntimeMXBean.java");
61 assertNotNull(rootGeneratorInterface);
62 assertThat(rootGeneratorInterface.getPackageName(),
64 assertThat(rootGeneratorInterface.getFullyQualifiedName(),
65 is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
67 rootGeneratorInterface.getTypeDeclaration()
70 .asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
72 assertThat(rootGeneratorInterface.getMethods().size(), is(1));
73 Method getCreatedSessions = findFirstMethodByName(
74 rootGeneratorInterface.getMethods(),
75 "getCreatedSessions");
76 assertThat(getCreatedSessions.getName(),
77 is("getCreatedSessions"));
78 assertThat(getCreatedSessions.getParameters().isEmpty(),
80 assertThat(getCreatedSessions.getReturnType(),
81 is(Long.class.getName()));
85 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
86 .get(THREADFACTORY_NAMING_MXB_NAME);
87 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
89 assertThat(runtimeBeans.size(), is(4));
92 RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans,
94 assertNotNull(streamRB);
95 assertFalse(streamRB.getKeyYangName().isPresent());
96 assertFalse(streamRB.getKeyJavaName().isPresent());
97 Map<String, AttributeIfc> attributeMap = streamRB
98 .getYangPropertiesToTypesMap();
99 assertEquals(4, attributeMap.size());
100 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
101 assertNotNull(toAttr);
102 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
104 assertNotNull(timestampAttr);
105 JavaAttribute stateAttr = (JavaAttribute) attributeMap
107 assertNotNull(stateAttr);
108 ListAttribute innerStreamList = (ListAttribute) attributeMap
109 .get("inner-stream-list");
110 assertNotNull(innerStreamList);
112 Map<String, FtlTemplate> ftlMap = TemplateFactory
113 .getTOAndMXInterfaceFtlFiles(streamRB);
114 assertThat(ftlMap.size(), is(3));
115 GeneralInterfaceTemplate streamGeneralInterface = (GeneralInterfaceTemplate) ftlMap
116 .get("ThreadStreamRuntimeMXBean.java");
117 assertThat(streamGeneralInterface.getMethods().size(), is(4));
118 Method getPeer = findFirstMethodByName(
119 streamGeneralInterface.getMethods(), "getPeer");
120 assertNotNull(getPeer);
121 assertThat(getPeer.getReturnType(), is(PACKAGE_NAME + ".Peer"));
124 GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap
126 assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
127 assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(),
129 assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME
131 assertThat(peerTO.getMethods().size(), is(5 + 2/*hashCode Equals*/));
133 Method getPort = findFirstMethodByName(peerTO.getMethods(), "getPort");
134 assertNotNull(getPort);
135 Method setPort = findFirstMethodByName(peerTO.getMethods(),
137 assertNotNull(setPort);
138 Method getCoreSize = findFirstMethodByName(peerTO.getMethods(),
140 Method setCoreSize = findFirstMethodByName(peerTO.getMethods(),
142 assertNotNull(setCoreSize);
143 assertNotNull(getCoreSize);
149 private static Method findFirstMethodByName(final List<? extends Method> methods, final String name) {
150 for (Method ms : methods) {
151 if (name.equals(ms.getName())) {
155 throw new IllegalArgumentException("Method with given name not found");