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