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;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
22 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntryTest;
23 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
24 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
27 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
28 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate;
29 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralClassTemplate;
30 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralInterfaceTemplate;
31 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory;
32 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method;
34 public class ModuleMXBeanEntryPluginTest extends ModuleMXBeanEntryTest {
37 public void testThreadsJavaPlugin() {
38 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
39 {// check threadfactory-naming
40 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
41 .get(THREADFACTORY_NAMING_MXB_NAME);
42 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
44 assertThat(runtimeBeans.size(), is(4));
45 // first one should be root
47 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
48 THREADFACTORY_NAMING_MXB_NAME);
49 assertThat(rootRB.isRoot(), is(true));
50 assertThat(rootRB.getAttributes().size(), is(1));
51 JavaAttribute attribute = (JavaAttribute) rootRB
52 .getAttributes().iterator().next();
53 assertThat(attribute.getAttributeYangName(),
54 is("created-sessions"));
55 assertThat(rootRB.getYangName(),
56 is(THREADFACTORY_NAMING_MXB_NAME));
57 Map<String, FtlTemplate> ftlMap = TemplateFactory
58 .getTOAndMXInterfaceFtlFiles(rootRB);
59 assertThat(ftlMap.size(), is(1));
60 GeneralInterfaceTemplate rootGeneratorInterface = (GeneralInterfaceTemplate) ftlMap
61 .get("NamingThreadFactoryRuntimeMXBean.java");
62 assertNotNull(rootGeneratorInterface);
63 assertThat(rootGeneratorInterface.getPackageName(),
65 assertThat(rootGeneratorInterface.getFullyQualifiedName(),
66 is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
68 rootGeneratorInterface.getTypeDeclaration()
71 .asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
73 assertThat(rootGeneratorInterface.getMethods().size(), is(1));
74 Method getCreatedSessions = findFirstMethodByName(
75 rootGeneratorInterface.getMethods(),
76 "getCreatedSessions");
77 assertThat(getCreatedSessions.getName(),
78 is("getCreatedSessions"));
79 assertThat(getCreatedSessions.getParameters().isEmpty(),
81 assertThat(getCreatedSessions.getReturnType(),
82 is(Long.class.getName()));
86 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
87 .get(THREADFACTORY_NAMING_MXB_NAME);
88 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
90 assertThat(runtimeBeans.size(), is(4));
93 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
95 assertNotNull(streamRB);
96 assertFalse(streamRB.getKeyYangName().isPresent());
97 assertFalse(streamRB.getKeyJavaName().isPresent());
98 Map<String, AttributeIfc> attributeMap = streamRB
99 .getYangPropertiesToTypesMap();
100 assertEquals(4, attributeMap.size());
101 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
102 assertNotNull(toAttr);
103 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
105 assertNotNull(timestampAttr);
106 JavaAttribute stateAttr = (JavaAttribute) attributeMap
108 assertNotNull(stateAttr);
109 ListAttribute innerStreamList = (ListAttribute) attributeMap
110 .get("inner-stream-list");
111 assertNotNull(innerStreamList);
113 Map<String, FtlTemplate> ftlMap = TemplateFactory
114 .getTOAndMXInterfaceFtlFiles(streamRB);
115 assertThat(ftlMap.size(), is(3));
116 GeneralInterfaceTemplate streamGeneralInterface = (GeneralInterfaceTemplate) ftlMap
117 .get("ThreadStreamRuntimeMXBean.java");
118 assertThat(streamGeneralInterface.getMethods().size(), is(4));
119 Method getPeer = findFirstMethodByName(
120 streamGeneralInterface.getMethods(), "getPeer");
121 assertNotNull(getPeer);
122 assertThat(getPeer.getReturnType(), is(PACKAGE_NAME + ".Peer"));
125 GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap
127 assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
128 assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(),
130 assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME
132 assertThat(peerTO.getMethods().size(), is(5));
133 Method getPort = findFirstMethodByName(peerTO.getMethods(),
135 assertNotNull(getPort);
136 Method setPort = findFirstMethodByName(peerTO.getMethods(),
138 assertNotNull(setPort);
139 Method getCoreSize = findFirstMethodByName(peerTO.getMethods(),
141 Method setCoreSize = findFirstMethodByName(peerTO.getMethods(),
143 assertNotNull(setCoreSize);
144 assertNotNull(getCoreSize);
150 private Method findFirstMethodByName(List<? extends Method> methods,
152 for (Method ms : methods) {
153 if (name.equals(ms.getName())) {
157 throw new IllegalArgumentException("Method with given name not found");