61647c45e72ac3290534df0cabbd6da3559c80bb
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / JMXGeneratorTest.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.containsString;
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 static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.opendaylight.controller.config.yangjmxgenerator.PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX;
20 import com.github.javaparser.JavaParser;
21 import com.github.javaparser.ParseException;
22 import com.github.javaparser.ast.CompilationUnit;
23 import com.google.common.collect.Collections2;
24 import com.google.common.collect.ImmutableSet;
25 import com.google.common.io.Files;
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Set;
35 import javax.xml.parsers.DocumentBuilder;
36 import javax.xml.parsers.DocumentBuilderFactory;
37 import org.apache.maven.project.MavenProject;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.opendaylight.controller.config.api.DependencyResolver;
41 import org.opendaylight.controller.config.api.DependencyResolverFactory;
42 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
43 import org.opendaylight.controller.config.spi.AbstractModule;
44 import org.opendaylight.controller.config.spi.Module;
45 import org.opendaylight.controller.config.spi.ModuleFactory;
46 import org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants;
47 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntryTest;
48 import org.osgi.framework.BundleContext;
49 import org.xml.sax.ErrorHandler;
50 import org.xml.sax.InputSource;
51 import org.xml.sax.SAXException;
52 import org.xml.sax.SAXParseException;
53
54 //TODO: refactor
55 public class JMXGeneratorTest extends AbstractGeneratorTest {
56
57     JMXGenerator jmxGenerator;
58
59     protected final HashMap<String, String> map = new HashMap<>();
60     protected File outputBaseDir;
61     File generatedResourcesDir;
62
63     private static final List<String> expectedModuleFileNames = ServiceInterfaceEntryTest
64             .toFileNames("[AbstractAsyncEventBusModule.java, AbstractAsyncEventBusModuleFactory.java, AbstractDynamicThreadPoolModule.java, AbstractDynamicThreadPoolModuleFactory.java, AbstractEventBusModule.java, AbstractEventBusModuleFactory.java, AbstractNamingThreadFactoryModule.java, AbstractNamingThreadFactoryModuleFactory.java, AbstractThreadPoolRegistryImplModule.java, AbstractThreadPoolRegistryImplModuleFactory.java, AsyncEventBusModule.java, AsyncEventBusModuleFactory.java, AsyncEventBusModuleMXBean.java, AsyncEventBusRuntimeMXBean.java, AsyncEventBusRuntimeRegistration.java, AsyncEventBusRuntimeRegistrator.java, DynamicThreadPoolModule.java, DynamicThreadPoolModuleFactory.java, DynamicThreadPoolModuleMXBean.java, DynamicThreadPoolRuntimeMXBean.java, DynamicThreadPoolRuntimeRegistration.java, DynamicThreadPoolRuntimeRegistrator.java, EventBusModule.java, EventBusModuleFactory.java, EventBusModuleMXBean.java, EventRuntimeMXBean.java, EventRuntimeRegistration.java, FromGrouping.java, InnerStreamList.java, NamingThreadFactoryModule.java, NamingThreadFactoryModuleFactory.java, NamingThreadFactoryModuleMXBean.java, NamingThreadFactoryRuntimeMXBean.java, NamingThreadFactoryRuntimeRegistration.java, NamingThreadFactoryRuntimeRegistrator.java, Peer.java, StreamRuntimeMXBean.java, StreamRuntimeRegistration.java, ThreadPoolRegistryImplModule.java, ThreadPoolRegistryImplModuleFactory.java, ThreadPoolRegistryImplModuleMXBean.java, ThreadRuntimeMXBean.java, ThreadRuntimeRegistration.java, ThreadStreamRuntimeMXBean.java, ThreadStreamRuntimeRegistration.java]");
65
66     private static final List<String> expectedBGPNames = ServiceInterfaceEntryTest
67             .toFileNames("[AbstractBgpListenerImplModule.java, " + "AbstractBgpListenerImplModuleFactory.java, " +
68                     "BgpListenerImplModule.java, " + "BgpListenerImplModuleFactory.java, " +
69                     "BgpListenerImplModuleMXBean.java, Peers.java]");
70
71     private static final List<String> expectedNetconfNames = ServiceInterfaceEntryTest
72             .toFileNames("[AbstractNetconfTestImplModule.java, " + "AbstractNetconfTestImplModuleFactory.java, " +
73                     "AbstractTestImplModule.java, " + "AbstractTestImplModuleFactory.java, " +
74                     "AutoCloseableServiceInterface.java, " + "ComplexDtoBInner.java, ComplexList.java, Deep.java, " +
75                     "DtoA.java, DtoA1.java, DtoAInner.java, DtoAInnerInner.java, DtoB.java, DtoC.java," + "NetconfTestImplModule.java, NetconfTestImplModuleFactory.java," + "NetconfTestImplModuleMXBean.java, Peer.java, SimpleList.java, TestImplModule.java, " + "TestImplModuleFactory.java," + " TestImplModuleMXBean.java" + "]");
76     private static final List<String> expectedTestFiles = ServiceInterfaceEntryTest
77             .toFileNames("[AbstractNetconfTestFileImplModule.java, AbstractNetconfTestFileImplModuleFactory.java, " +
78                     "AbstractNetconfTestFiles1ImplModule.java, AbstractNetconfTestFiles1ImplModuleFactory.java, " +
79                     "AbstractTestFileImplModule.java, AbstractTestFileImplModuleFactory.java, " +
80                     "AbstractTestFiles1ImplModule.java, AbstractTestFiles1ImplModuleFactory.java, DtoA.java, " +
81                     "DtoA.java, NetconfTestFileImplModuleMXBean.java, NetconfTestFileImplRuntimeMXBean.java, " +
82                     "NetconfTestFileImplRuntimeRegistration.java, NetconfTestFileImplRuntimeRegistrator.java, " +
83                     "NetconfTestFiles1ImplModule.java, NetconfTestFiles1ImplModuleFactory.java, " +
84                     "NetconfTestFiles1ImplModuleMXBean.java, NetconfTestFiles1ImplRuntimeMXBean.java, " +
85                     "NetconfTestFiles1ImplRuntimeRegistration.java, NetconfTestFiles1ImplRuntimeRegistrator.java, TestFileImplModule.java, TestFileImplModuleFactory.java, TestFileImplModuleMXBean.java, TestFileImplRuntimeMXBean.java, TestFileImplRuntimeRegistration.java, TestFileImplRuntimeRegistrator.java, TestFiles1ImplModule.java, TestFiles1ImplModuleFactory.java, TestFiles1ImplModuleMXBean.java, TestFiles1ImplRuntimeMXBean.java, TestFiles1ImplRuntimeRegistration.java, TestFiles1ImplRuntimeRegistrator.java]");
86     private static final List<String> expectedAllFileNames = ServiceInterfaceEntryTest
87             .toFileNames("[AbstractAsyncEventBusModule.java, AbstractAsyncEventBusModuleFactory.java, AbstractBgpListenerImplModule.java, AbstractBgpListenerImplModuleFactory.java, AbstractDynamicThreadPoolModule.java, AbstractDynamicThreadPoolModuleFactory.java, AbstractEventBusModule.java, AbstractEventBusModuleFactory.java, AbstractNamingThreadFactoryModule.java, AbstractNamingThreadFactoryModuleFactory.java, AbstractNetconfTestFileImplModule.java, AbstractNetconfTestFileImplModuleFactory.java, AbstractNetconfTestFiles1ImplModule.java, AbstractNetconfTestFiles1ImplModuleFactory.java, AbstractNetconfTestImplModule.java, AbstractNetconfTestImplModuleFactory.java, AbstractTestFileImplModule.java, AbstractTestFileImplModuleFactory.java, AbstractTestFiles1ImplModule.java, AbstractTestFiles1ImplModuleFactory.java, AbstractTestImplModule.java, AbstractTestImplModuleFactory.java, AbstractThreadPoolRegistryImplModule.java, AbstractThreadPoolRegistryImplModuleFactory.java, AsyncEventBusModule.java, AsyncEventBusModuleFactory.java, AsyncEventBusModuleMXBean.java, AsyncEventBusRuntimeMXBean.java, AsyncEventBusRuntimeRegistration.java, AsyncEventBusRuntimeRegistrator.java, AutoCloseableServiceInterface.java, BgpListenerImplModule.java, BgpListenerImplModuleFactory.java, BgpListenerImplModuleMXBean.java, BgpListenerImplRuntimeMXBean.java, BgpListenerImplRuntimeRegistration.java, BgpListenerImplRuntimeRegistrator.java, ComplexDtoBInner.java, ComplexList.java, Deep.java, DtoA.java, DtoA.java, DtoA.java, DtoA1.java, DtoAInner.java, DtoAInnerInner.java, DtoB.java, DtoC.java, DynamicThreadPoolModule.java, DynamicThreadPoolModuleFactory.java, DynamicThreadPoolModuleMXBean.java, DynamicThreadPoolRuntimeMXBean.java, DynamicThreadPoolRuntimeRegistration.java, DynamicThreadPoolRuntimeRegistrator.java, EventBusModule.java, EventBusModuleFactory.java, EventBusModuleMXBean.java, EventBusServiceInterface.java, EventRuntimeMXBean.java, EventRuntimeRegistration.java, FromGrouping.java, InnerStreamList.java, NamingThreadFactoryModule.java, NamingThreadFactoryModuleFactory.java, NamingThreadFactoryModuleMXBean.java, NamingThreadFactoryRuntimeMXBean.java, NamingThreadFactoryRuntimeRegistration.java, NamingThreadFactoryRuntimeRegistrator.java, NetconfTestFileImplModule.java, NetconfTestFileImplModuleFactory.java, NetconfTestFileImplModuleMXBean.java, NetconfTestFileImplRuntimeMXBean.java, NetconfTestFileImplRuntimeRegistration.java, NetconfTestFileImplRuntimeRegistrator.java, NetconfTestFiles1ImplModule.java, NetconfTestFiles1ImplModuleFactory.java, NetconfTestFiles1ImplModuleMXBean.java, NetconfTestFiles1ImplRuntimeMXBean.java, NetconfTestFiles1ImplRuntimeRegistration.java, NetconfTestFiles1ImplRuntimeRegistrator.java, NetconfTestImplModule.java, NetconfTestImplModuleFactory.java, NetconfTestImplModuleMXBean.java, NetconfTestImplRuntimeMXBean.java, NetconfTestImplRuntimeRegistration.java, NetconfTestImplRuntimeRegistrator.java, Peer.java, Peer.java, PeersRuntimeMXBean.java, PeersRuntimeRegistration.java, ScheduledThreadPoolServiceInterface.java, SimpleList.java, StreamRuntimeMXBean.java, StreamRuntimeRegistration.java, TestFileImplModule.java, TestFileImplModuleFactory.java, TestFileImplModuleMXBean.java, TestFileImplRuntimeMXBean.java, TestFileImplRuntimeRegistration.java, TestFileImplRuntimeRegistrator.java, TestFiles1ImplModule.java, TestFiles1ImplModuleFactory.java, TestFiles1ImplModuleMXBean.java, TestFiles1ImplRuntimeMXBean.java, TestFiles1ImplRuntimeRegistration.java, TestFiles1ImplRuntimeRegistrator.java, TestImplModule.java, TestImplModuleFactory.java, TestImplModuleMXBean.java, TestImplRuntimeMXBean.java, TestImplRuntimeRegistration.java, TestImplRuntimeRegistrator.java, ThreadFactoryServiceInterface.java, ThreadPoolRegistryImplModule.java, ThreadPoolRegistryImplModuleFactory.java, ThreadPoolRegistryImplModuleMXBean.java, ThreadPoolServiceInterface.java, ThreadRuntimeMXBean.java, ThreadRuntimeRegistration.java, ThreadStreamRuntimeMXBean.java, ThreadStreamRuntimeRegistration.java]");
88     private static final List<String> expectedGenerateMBEsListNames = ServiceInterfaceEntryTest
89             .toFileNames("[AbstractBgpListenerImplModule.java, AbstractBgpListenerImplModuleFactory.java, BgpListenerImplModule.java, BgpListenerImplModuleFactory.java, BgpListenerImplModuleMXBean.java, BgpListenerImplRuntimeMXBean.java, BgpListenerImplRuntimeRegistration.java, BgpListenerImplRuntimeRegistrator.java, PeersRuntimeMXBean.java, PeersRuntimeRegistration.java]");
90
91     @Before
92     public void setUp() {
93         map.put(JMXGenerator.NAMESPACE_TO_PACKAGE_PREFIX + "1",
94                 ConfigConstants.CONFIG_NAMESPACE + JMXGenerator.NAMESPACE_TO_PACKAGE_DIVIDER + EXPECTED_PACKAGE_PREFIX);
95         map.put(JMXGenerator.MODULE_FACTORY_FILE_BOOLEAN, "false");
96         jmxGenerator = new JMXGenerator(new CodeWriter());
97         jmxGenerator.setAdditionalConfig(map);
98         File targetDir = new File(generatorOutputPath, "target");
99         generatedResourcesDir = new File(targetDir, "generated-resources");
100         jmxGenerator.setResourceBaseDir(generatedResourcesDir);
101         MavenProject project = mock(MavenProject.class);
102         doReturn(generatorOutputPath).when(project).getBasedir();
103         jmxGenerator.setMavenProject(project);
104         outputBaseDir = JMXGenerator.concatFolders(targetDir, "generated-sources", "config");
105     }
106
107     @Test
108     public void generateSIsMBsTest() {
109         Collection<File> files = jmxGenerator.generateSources(context,
110                 outputBaseDir, context.getModules());
111         List<String> expectedFileNames = new ArrayList<>();
112         expectedFileNames
113                 .addAll(ServiceInterfaceEntryTest.expectedSIEFileNames);
114         expectedFileNames.addAll(expectedModuleFileNames);
115
116         expectedFileNames.addAll(expectedBGPNames);
117         expectedFileNames.addAll(expectedNetconfNames);
118         expectedFileNames.addAll(expectedTestFiles);
119         Collections.sort(expectedFileNames);
120         // TODO: separate expectedAllFileNames into expectedBGPNames,
121         // expectedNetconfNames
122         assertEquals(expectedAllFileNames, toFileNames(files));
123
124         verifyModuleFactoryFile(false);
125     }
126
127     private void verifyModuleFactoryFile(final boolean shouldBePresent) {
128         File factoryFile = new File(generatedResourcesDir, "META-INF"
129                 + File.separator + "services" + File.separator
130                 + ModuleFactory.class.getName());
131         if (!shouldBePresent) {
132             assertFalse("Factory file should not be generated",
133                     factoryFile.exists());
134         } else {
135             assertTrue("Factory file should be generated", factoryFile.exists());
136         }
137     }
138
139     private static List<String> toFileNames(final Collection<File> files) {
140         List<String> result = new ArrayList<>();
141         for (File f : files) {
142             result.add(f.getName());
143         }
144         Collections.sort(result);
145         return result;
146     }
147
148     @Test
149     public void generateSIEsTest() throws IOException, ParseException {
150         Collection<File> files = jmxGenerator.generateSources(context,
151                 outputBaseDir, Collections.singleton(threadsModule));
152         assertEquals(ServiceInterfaceEntryTest.expectedSIEFileNames, toFileNames(files));
153
154         for (File file : files) {
155             String fileName = file.getName();
156             SieASTVisitor verifier = new SieASTVisitor(EXPECTED_PACKAGE_PREFIX + ".threads", fileName);
157             verifyFile(file, verifier);
158
159             assertThat(verifier.extnds,
160                     containsString("org.opendaylight.controller.config.api.annotations.AbstractServiceInterface"));
161             assertNotNull(verifier.javadoc);
162
163             switch (fileName) {
164             case "ThreadPoolServiceInterface.java":
165                 assertContains(verifier.descriptionAnotValue, "A simple pool of threads able to execute work.");
166                 assertContains(verifier.sieAnnotValue, "threadpool");
167                 assertContains(verifier.sieAnnotOsgiRegistrationType, EXPECTED_PACKAGE_PREFIX + ".threadpool.ThreadPool.class");
168                 break;
169             case "ScheduledThreadPoolServiceInterface.java":
170                 assertContains(verifier.extnds,
171                         EXPECTED_PACKAGE_PREFIX  + ".threads.ThreadPoolServiceInterface");
172                 assertContains(verifier.descriptionAnotValue,
173                         "An extension of the simple pool of threads able to schedule work to be executed at some point in time.");
174                 assertContains(verifier.sieAnnotValue, "scheduled-threadpool");
175                 assertContains(verifier.sieAnnotOsgiRegistrationType,
176                         EXPECTED_PACKAGE_PREFIX + ".threadpool.ScheduledThreadPool.class");
177                 break;
178             case "EventBusServiceInterface.java":
179                 assertContains(verifier.descriptionAnotValue,
180                         "Service representing an event bus. The service acts as message router between event producers and event consumers");
181                 assertContains(verifier.sieAnnotValue, "eventbus");
182                 assertContains(verifier.sieAnnotOsgiRegistrationType, "com.google.common.eventbus.EventBus.class");
183                 break;
184             case "ThreadFactoryServiceInterface.java":
185                 assertContains( verifier.descriptionAnotValue,
186                         "Service representing a ThreadFactory instance. It is directly useful in Java world, where various library pieces need to create threads and you may want to inject a customized thread implementation.");
187                 assertContains(verifier.sieAnnotValue, "threadfactory");
188                 assertContains(verifier.sieAnnotOsgiRegistrationType,
189                         "java.util.concurrent.ThreadFactory.class");
190                 break;
191             case "ScheduledExecutorServiceServiceInterface.java":
192                 assertContains(verifier.sieAnnotOsgiRegistrationType,
193                         "java.util.concurrent.ScheduledExecutorService.class");
194                 break;
195             default:
196                 fail("Unknown generated sie " + fileName);
197             }
198         }
199     }
200
201     @Test
202     public void generateMBEsListTest() throws Exception {
203         // default value for module factory file is true
204         map.put(JMXGenerator.MODULE_FACTORY_FILE_BOOLEAN, "randomValue");
205         jmxGenerator.setAdditionalConfig(map);
206
207         Collection<File> files = jmxGenerator.generateSources(context, outputBaseDir,
208             Collections.singleton(bgpListenerJavaModule));
209
210         assertEquals(expectedGenerateMBEsListNames, toFileNames(files));
211     }
212
213     @Test
214     public void generateMBEsTest() throws Exception {
215         // default value for module factory file is true
216         map.put(JMXGenerator.MODULE_FACTORY_FILE_BOOLEAN, "randomValue");
217         jmxGenerator.setAdditionalConfig(map);
218
219         Collection<File> files = jmxGenerator.generateSources(context, outputBaseDir,
220             Collections.singleton(threadsJavaModule));
221
222         assertEquals(expectedModuleFileNames, toFileNames(files));
223
224         for (File file : files) {
225             final String name = file.getName();
226             if (!name.endsWith("java")) {
227                 continue;
228             }
229
230             MbeASTVisitor visitor = new MbeASTVisitor(EXPECTED_PACKAGE_PREFIX + ".threads.java", name);
231
232             verifyFile(file, visitor);
233
234             switch (name) {
235             case "AbstractDynamicThreadPoolModule.java":
236                 assertAbstractDynamicThreadPoolModule(visitor);
237                 break;
238             case "AsyncEventBusModuleMXBean.java":
239                 assertEquals("Incorrenct number of generated methods", 4, visitor.methods.size());
240                 break;
241             case "AbstractNamingThreadFactoryModuleFactory.java":
242                 assertAbstractNamingThreadFactoryModuleFactory(visitor);
243                 break;
244             case "AsyncEventBusModule.java":
245                 assertContains(visitor.extnds, EXPECTED_PACKAGE_PREFIX + ".threads.java.AbstractAsyncEventBusModule");
246                 visitor.assertFields(0);
247                 assertEquals("Incorrenct number of generated methods", 2, visitor.methods.size());
248                 visitor.assertConstructors(2);
249                 visitor.assertMethodDescriptions(0);
250                 visitor.assertMethodJavadocs(0);
251                 break;
252             case "EventBusModuleFactory.java":
253                 assertContains(visitor.extnds,
254                     EXPECTED_PACKAGE_PREFIX + ".threads.java.AbstractEventBusModuleFactory");
255                 visitor.assertFields(0);
256                 assertEquals("Incorrenct number of generated methods", 0, visitor.methods.size());
257                 visitor.assertConstructors(0);
258                 visitor.assertMethodDescriptions(0);
259                 visitor.assertMethodJavadocs(0);
260                 break;
261             }
262         }
263
264         verifyXmlFiles(Collections2.filter(files, input -> input.getName().endsWith("xml")));
265
266         // verify ModuleFactory file
267         File moduleFactoryFile = JMXGenerator.concatFolders(generatedResourcesDir, "META-INF", "services",
268                 ModuleFactory.class.getName());
269         assertTrue(moduleFactoryFile.exists());
270         Set<String> lines = ImmutableSet.copyOf(Files.readLines(moduleFactoryFile, StandardCharsets.UTF_8));
271         Set<String> expectedLines = ImmutableSet.of(
272                 EXPECTED_PACKAGE_PREFIX + ".threads.java.EventBusModuleFactory",
273                 EXPECTED_PACKAGE_PREFIX + ".threads.java.AsyncEventBusModuleFactory",
274                 EXPECTED_PACKAGE_PREFIX + ".threads.java.DynamicThreadPoolModuleFactory",
275                 EXPECTED_PACKAGE_PREFIX + ".threads.java.NamingThreadFactoryModuleFactory",
276                 EXPECTED_PACKAGE_PREFIX + ".threads.java.ThreadPoolRegistryImplModuleFactory");
277
278         assertEquals(expectedLines, lines);
279     }
280
281     private static void verifyXmlFiles(final Collection<File> xmlFiles) throws Exception {
282         ErrorHandler errorHandler = new ErrorHandler() {
283
284             @Override
285             public void warning(final SAXParseException exception)
286                     throws SAXException {
287                 fail("Generated blueprint xml is not well formed " + exception.getMessage());
288             }
289
290             @Override
291             public void fatalError(final SAXParseException exception)
292                     throws SAXException {
293                 fail("Generated blueprint xml is not well formed " + exception.getMessage());
294             }
295
296             @Override
297             public void error(final SAXParseException exception) throws SAXException {
298                 fail("Generated blueprint xml is not well formed "  + exception.getMessage());
299             }
300         };
301
302         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
303         factory.setValidating(false);
304         factory.setNamespaceAware(true);
305
306         for (File file : xmlFiles) {
307             DocumentBuilder builder = factory.newDocumentBuilder();
308             builder.setErrorHandler(errorHandler);
309             builder.parse(new InputSource(file.getPath()));
310         }
311     }
312
313     private static void assertAbstractNamingThreadFactoryModuleFactory( final MbeASTVisitor visitor) {
314         assertContains(visitor.implmts, "org.opendaylight.controller.config.spi.ModuleFactory");
315
316         visitor.assertFields(2);
317         visitor.assertField("public static final java.lang.String NAME = \"threadfactory-naming\"");
318         visitor.assertField(
319                 "private static final java.util.Set<Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>> serviceIfcs");
320
321         assertFactoryMethods(visitor.methods, 10);
322         visitor.assertMethodDescriptions(0);
323         visitor.assertMethodJavadocs(0);
324     }
325
326     private static void assertFactoryMethods(final Set<String> methods, final int expectedSize) {
327
328         List<ArgumentAssertion> args = new ArrayList<>();
329         ArgumentAssertion oldInstanceArg = new ArgumentAssertion(DynamicMBeanWithInstance.class.getCanonicalName(), "old");
330         ArgumentAssertion instanceNameArg = new ArgumentAssertion(String.class.getSimpleName(), "instanceName");
331         ArgumentAssertion dependencyResolverArg = new ArgumentAssertion(DependencyResolver.class.getCanonicalName(), "dependencyResolver");
332         ArgumentAssertion bundleContextArg = new ArgumentAssertion(BundleContext.class.getCanonicalName(), "bundleContext");
333
334         assertMethodPresent(methods, new MethodAssertion(String.class.getSimpleName(), "getImplementationName"));
335
336         args.add(instanceNameArg);
337         args.add(dependencyResolverArg);
338         args.add(bundleContextArg);
339         assertMethodPresent(methods, new MethodAssertion(Module.class.getCanonicalName(), "createModule", args));
340
341         args.add(2, oldInstanceArg);
342         assertMethodPresent(methods, new MethodAssertion(Module.class.getCanonicalName(), "createModule", args));
343
344         args.clear();
345         args.add(oldInstanceArg);
346         assertMethodPresent(methods, new MethodAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "handleChangedClass", args));
347
348         args.clear();
349         args.add(instanceNameArg);
350         args.add(dependencyResolverArg);
351         args.add(bundleContextArg);
352         assertMethodPresent(methods, new MethodAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "instantiateModule", args));
353
354
355         args.add(2, new ArgumentAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "oldModule"));
356         args.add(3, new ArgumentAssertion(AutoCloseable.class.getCanonicalName(), "oldInstance"));
357         assertMethodPresent(methods, new MethodAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "instantiateModule", args));
358
359         args.clear();
360         args.add(new ArgumentAssertion(DependencyResolverFactory.class.getCanonicalName(), "dependencyResolverFactory"));
361         args.add(bundleContextArg);
362         assertMethodPresent(methods, new MethodAssertion("java.util.Set<org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule>", "getDefaultModules", args));
363
364         args.clear();
365         args.add(new ArgumentAssertion("Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>", "serviceInterface"));
366         assertMethodPresent(methods, new MethodAssertion("boolean", "isModuleImplementingServiceInterface", args));
367
368         assertEquals(expectedSize, methods.size());
369     }
370
371     private static void assertMethodPresent(final Set<String> methods, final MethodAssertion methodAssertion) {
372         assertTrue(String.format("Generated methods did not contain %s, generated methods: %s",
373                 methodAssertion.toString(), methods), methods.contains(methodAssertion.toString()));
374     }
375
376     private static void assertAbstractDynamicThreadPoolModule(final MbeASTVisitor visitor) {
377         assertNotNull(visitor.javadoc);
378         assertContains(visitor.descriptionAnotValue, "threadpool-dynamic description");
379         assertContains(visitor.implmts,
380                 EXPECTED_PACKAGE_PREFIX + ".threads.java.DynamicThreadPoolModuleMXBean",
381                 EXPECTED_PACKAGE_PREFIX + ".threads.ScheduledThreadPoolServiceInterface",
382                 EXPECTED_PACKAGE_PREFIX + ".threads.ThreadPoolServiceInterface");
383         assertContains(visitor.extnds, AbstractModule.class.getCanonicalName());
384         visitor.assertConstructors(2);
385         visitor.assertFields(17);
386         visitor.assertField("private java.lang.Long maximumSize");
387         visitor.assertField("private javax.management.ObjectName threadfactory");
388         visitor.assertField("private java.util.concurrent.ThreadFactory threadfactoryDependency");
389         visitor.assertField("private java.lang.Long keepAlive = java.lang.Long.valueOf(\"10\")");
390         visitor.assertField("private java.lang.Long coreSize");
391         visitor.assertField("private byte[] binary");
392
393         assertEquals(1, visitor.requireIfc.size());
394         String reqIfc = visitor.requireIfc.get("setThreadfactory");
395         assertNotNull("Missing generated setter for threadfactory", reqIfc);
396         assertContains(reqIfc, EXPECTED_PACKAGE_PREFIX + ".threads.ThreadFactoryServiceInterface");
397
398         assertEquals("Incorrenct number of generated methods", 26, visitor.methods.size());
399         visitor.assertMethodDescriptions(3);
400         visitor.assertMethodJavadocs(3);
401         visitor.assertMethodJavadoc("setMaximumSize", "void setMaximumSize(java.lang.Long maximumSize)");
402
403     }
404
405     private static void assertContains(final String source, final String... contained) {
406         for (String string : contained) {
407             assertThat(source, containsString(string));
408         }
409     }
410
411     private static void verifyFile(final File file, final AbstractVerifier verifier) throws ParseException, IOException {
412         final CompilationUnit cu = JavaParser.parse(file);
413         cu.accept(verifier, null);
414         verifier.verify();
415     }
416
417     private static class MethodAssertion extends ArgumentAssertion {
418
419         private final List<ArgumentAssertion> arguments;
420
421
422         MethodAssertion(final String type, final String name, final List<ArgumentAssertion> arguments) {
423             super(type, name);
424             this.arguments = arguments;
425         }
426
427         MethodAssertion(final String type, final String name) {
428             this(type, name, Collections.<ArgumentAssertion>emptyList());
429         }
430
431         @Override
432         public String toString() {
433             final StringBuilder sb = new StringBuilder();
434             sb.append(type).append(' ');
435             sb.append(name).append('(');
436
437             int i = 0;
438             for (ArgumentAssertion argument : arguments) {
439                 sb.append(argument.type).append(' ');
440                 sb.append(argument.name);
441                 if(++i != arguments.size()) {
442                     sb.append(", ");
443                 }
444             }
445             sb.append(')');
446             return sb.toString();
447         }
448     }
449
450     private static class ArgumentAssertion {
451
452         protected final String type, name;
453
454         private ArgumentAssertion(final String type, final String name) {
455             this.type = type;
456             this.name = name;
457         }
458
459         @Override
460         public String toString() {
461             return type + " " + name;
462         }
463     }
464 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.