BUG-4329: switch to javaparser
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ModuleMXBeanEntryPluginTest.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.plugin;
9
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;
18 import java.util.Map;
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;
32
33 public class ModuleMXBeanEntryPluginTest extends ModuleMXBeanEntryTest {
34
35     @Test
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
42                     .getRuntimeBeans();
43             assertThat(runtimeBeans.size(), is(4));
44             // first one should be root
45             {
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(),
63                         is(PACKAGE_NAME));
64                 assertThat(rootGeneratorInterface.getFullyQualifiedName(),
65                         is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
66                 assertThat(
67                         rootGeneratorInterface.getTypeDeclaration()
68                                 .getExtended(),
69                         is(Arrays
70                                 .asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
71
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(),
79                         is(true));
80                 assertThat(getCreatedSessions.getReturnType(),
81                         is(Long.class.getName()));
82             }
83         }
84         {
85             ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
86                     .get(THREADFACTORY_NAMING_MXB_NAME);
87             Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
88                     .getRuntimeBeans();
89             assertThat(runtimeBeans.size(), is(4));
90
91             {
92                 RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans,
93                         "ThreadStream");
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
103                         .get("timestamp");
104                 assertNotNull(timestampAttr);
105                 JavaAttribute stateAttr = (JavaAttribute) attributeMap
106                         .get("state");
107                 assertNotNull(stateAttr);
108                 ListAttribute innerStreamList = (ListAttribute) attributeMap
109                         .get("inner-stream-list");
110                 assertNotNull(innerStreamList);
111
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"));
122
123                 // test TO
124                 GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap
125                         .get("pack2.Peer");
126                 assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
127                 assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(),
128                         is(true));
129                 assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME
130                         + ".Peer"));
131                 assertThat(peerTO.getMethods().size(), is(5 + 2/*hashCode Equals*/));
132
133                 Method getPort = findFirstMethodByName(peerTO.getMethods(), "getPort");
134                 assertNotNull(getPort);
135                 Method setPort = findFirstMethodByName(peerTO.getMethods(),
136                         "setPort");
137                 assertNotNull(setPort);
138                 Method getCoreSize = findFirstMethodByName(peerTO.getMethods(),
139                         "getCoreSize");
140                 Method setCoreSize = findFirstMethodByName(peerTO.getMethods(),
141                         "setCoreSize");
142                 assertNotNull(setCoreSize);
143                 assertNotNull(getCoreSize);
144
145             }
146         }
147     }
148
149     private static Method findFirstMethodByName(final List<? extends Method> methods, final String name) {
150         for (Method ms : methods) {
151             if (name.equals(ms.getName())) {
152                 return ms;
153             }
154         }
155         throw new IllegalArgumentException("Method with given name not found");
156     }
157 }