Reduce verbosity/criticality of inconsistent yangstore messages
[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
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Map;
20
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;
34
35 public class ModuleMXBeanEntryPluginTest extends ModuleMXBeanEntryTest {
36
37     @Test
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
44                     .getRuntimeBeans();
45             assertThat(runtimeBeans.size(), is(4));
46             // first one should be root
47             {
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(),
65                         is(PACKAGE_NAME));
66                 assertThat(rootGeneratorInterface.getFullyQualifiedName(),
67                         is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
68                 assertThat(
69                         rootGeneratorInterface.getTypeDeclaration()
70                                 .getExtended(),
71                         is(Arrays
72                                 .asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
73
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(),
81                         is(true));
82                 assertThat(getCreatedSessions.getReturnType(),
83                         is(Long.class.getName()));
84             }
85         }
86         {
87             ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
88                     .get(THREADFACTORY_NAMING_MXB_NAME);
89             Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
90                     .getRuntimeBeans();
91             assertThat(runtimeBeans.size(), is(4));
92
93             {
94                 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
95                         "stream");
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
105                         .get("timestamp");
106                 assertNotNull(timestampAttr);
107                 JavaAttribute stateAttr = (JavaAttribute) attributeMap
108                         .get("state");
109                 assertNotNull(stateAttr);
110                 ListAttribute innerStreamList = (ListAttribute) attributeMap
111                         .get("inner-stream-list");
112                 assertNotNull(innerStreamList);
113
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"));
124
125                 // test TO
126                 GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap
127                         .get("pack2.Peer");
128                 assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
129                 assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(),
130                         is(true));
131                 assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME
132                         + ".Peer"));
133                 assertThat(peerTO.getMethods().size(), is(4));
134                 Method getPort = findFirstMethodByName(peerTO.getMethods(),
135                         "getPort");
136                 assertNotNull(getPort);
137                 Method setPort = findFirstMethodByName(peerTO.getMethods(),
138                         "setPort");
139                 assertNotNull(setPort);
140                 Method getCoreSize = findFirstMethodByName(peerTO.getMethods(),
141                         "getCoreSize");
142                 Method setCoreSize = findFirstMethodByName(peerTO.getMethods(),
143                         "setCoreSize");
144                 assertNotNull(setCoreSize);
145                 assertNotNull(getCoreSize);
146
147             }
148         }
149     }
150
151     private Method findFirstMethodByName(List<? extends Method> methods,
152             String name) {
153         for (Method ms : methods) {
154             if (name.equals(ms.getName())) {
155                 return ms;
156             }
157         }
158         throw new IllegalArgumentException("Method with given name not found");
159     }
160 }