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;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
23 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntryTest;
24 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
27 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
28 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
29 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate;
30 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralClassTemplate;
31 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralInterfaceTemplate;
32 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory;
33 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method;
35 public class ModuleMXBeanEntryPluginTest extends ModuleMXBeanEntryTest {
38 public void testThreadsJavaPlugin() {
39 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
40 {// check threadfactory-naming
41 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
42 .get(THREADFACTORY_NAMING_MXB_NAME);
43 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
45 assertThat(runtimeBeans.size(), is(4));
46 // first one should be root
48 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
49 THREADFACTORY_NAMING_MXB_NAME);
50 assertThat(rootRB.isRoot(), is(true));
51 assertThat(rootRB.getAttributes().size(), is(1));
52 JavaAttribute attribute = (JavaAttribute) rootRB
53 .getAttributes().iterator().next();
54 assertThat(attribute.getAttributeYangName(),
55 is("created-sessions"));
56 assertThat(rootRB.getYangName(),
57 is(THREADFACTORY_NAMING_MXB_NAME));
58 Map<String, FtlTemplate> ftlMap = TemplateFactory
59 .getTOAndMXInterfaceFtlFiles(rootRB);
60 assertThat(ftlMap.size(), is(1));
61 GeneralInterfaceTemplate rootGeneratorInterface = (GeneralInterfaceTemplate) ftlMap
62 .get("NamingThreadFactoryRuntimeMXBean.java");
63 assertNotNull(rootGeneratorInterface);
64 assertThat(rootGeneratorInterface.getPackageName(),
66 assertThat(rootGeneratorInterface.getFullyQualifiedName(),
67 is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
69 rootGeneratorInterface.getTypeDeclaration()
72 .asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
74 assertThat(rootGeneratorInterface.getMethods().size(), is(1));
75 Method getCreatedSessions = findFirstMethodByName(
76 rootGeneratorInterface.getMethods(),
77 "getCreatedSessions");
78 assertThat(getCreatedSessions.getName(),
79 is("getCreatedSessions"));
80 assertThat(getCreatedSessions.getParameters().isEmpty(),
82 assertThat(getCreatedSessions.getReturnType(),
83 is(Long.class.getName()));
87 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
88 .get(THREADFACTORY_NAMING_MXB_NAME);
89 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
91 assertThat(runtimeBeans.size(), is(4));
94 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
96 assertNotNull(streamRB);
97 assertFalse(streamRB.getKeyYangName().isPresent());
98 assertFalse(streamRB.getKeyJavaName().isPresent());
99 Map<String, AttributeIfc> attributeMap = streamRB
100 .getYangPropertiesToTypesMap();
101 assertEquals(4, attributeMap.size());
102 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
103 assertNotNull(toAttr);
104 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
106 assertNotNull(timestampAttr);
107 JavaAttribute stateAttr = (JavaAttribute) attributeMap
109 assertNotNull(stateAttr);
110 ListAttribute innerStreamList = (ListAttribute) attributeMap
111 .get("inner-stream-list");
112 assertNotNull(innerStreamList);
114 Map<String, FtlTemplate> ftlMap = TemplateFactory
115 .getTOAndMXInterfaceFtlFiles(streamRB);
116 assertThat(ftlMap.size(), is(3));
117 GeneralInterfaceTemplate streamGeneralInterface = (GeneralInterfaceTemplate) ftlMap
118 .get("ThreadStreamRuntimeMXBean.java");
119 assertThat(streamGeneralInterface.getMethods().size(), is(4));
120 Method getPeer = findFirstMethodByName(
121 streamGeneralInterface.getMethods(), "getPeer");
122 assertNotNull(getPeer);
123 assertThat(getPeer.getReturnType(), is(PACKAGE_NAME + ".Peer"));
126 GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap
128 assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
129 assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(),
131 assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME
133 assertThat(peerTO.getMethods().size(), is(5));
134 Method getPort = findFirstMethodByName(peerTO.getMethods(),
136 assertNotNull(getPort);
137 Method setPort = findFirstMethodByName(peerTO.getMethods(),
139 assertNotNull(setPort);
140 Method getCoreSize = findFirstMethodByName(peerTO.getMethods(),
142 Method setCoreSize = findFirstMethodByName(peerTO.getMethods(),
144 assertNotNull(setCoreSize);
145 assertNotNull(getCoreSize);
151 private Method findFirstMethodByName(List<? extends Method> methods,
153 for (Method ms : methods) {
154 if (name.equals(ms.getName())) {
158 throw new IllegalArgumentException("Method with given name not found");