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

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