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

©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.