Change mapping of uint{8,16,32,64}
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / CompilationTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.java.api.generator.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Range;
18 import java.io.File;
19 import java.io.IOException;
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23 import java.lang.reflect.Modifier;
24 import java.lang.reflect.ParameterizedType;
25 import java.lang.reflect.WildcardType;
26 import java.math.BigDecimal;
27 import java.net.URISyntaxException;
28 import java.net.URL;
29 import java.net.URLClassLoader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.stream.Collectors;
35 import org.junit.Test;
36 import org.opendaylight.yangtools.yang.binding.ChildOf;
37 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
38 import org.opendaylight.yangtools.yang.common.Empty;
39 import org.opendaylight.yangtools.yang.common.Uint16;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42 import org.opendaylight.yangtools.yang.common.Uint8;
43
44 /**
45  * Test correct code generation.
46  *
47  */
48 public class CompilationTest extends BaseCompilationTest {
49
50     /*
51      * Java 8 allows JaCoCo to hook onto interfaces, as well as
52      * generating a default implementation. We only want to check
53      * abstract methods.
54      */
55     private static Collection<Method> abstractMethods(final Class<?> clazz) {
56         // Filter out
57         return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()),
58             input -> Modifier.isAbstract(input.getModifiers()));
59     }
60
61     @Test
62     public void testListGeneration() throws Exception {
63         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("list-gen");
64         final File compiledOutputDir = CompilationTestUtils.compilerOutput("list-gen");
65         generateTestSources("/compilation/list-gen", sourcesOutputDir);
66
67         // Test if all sources are generated
68         File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
69         final File keyArgs = new File(parent, "KeyArgs.java");
70         final File links = new File(parent, "Links.java");
71         final File linksBuilder = new File(parent, "LinksBuilder.java");
72         final File linksKey = new File(parent, "LinksKey.java");
73         final File testData = new File(parent, "TestData.java");
74         assertTrue(keyArgs.exists());
75         assertTrue(links.exists());
76         assertTrue(linksBuilder.exists());
77         assertTrue(linksKey.exists());
78         assertTrue(testData.exists());
79         CompilationTestUtils.assertFilesCount(parent, 7);
80
81         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
82         final File level = new File(parent, "Level.java");
83         final File linkGroup = new File(parent, "LinkGroup.java");
84         final File node = new File(parent, "Node.java");
85         final File nodeBuilder = new File(parent, "NodeBuilder.java");
86         final File nodeList = new File(parent, "NodeList.java");
87         final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
88         final File nodesType = new File(parent, "NodesType.java");
89         assertTrue(level.exists());
90         assertTrue(linkGroup.exists());
91         assertTrue(node.exists());
92         assertTrue(nodeBuilder.exists());
93         assertTrue(nodeList.exists());
94         assertTrue(nodeListBuilder.exists());
95         assertTrue(nodesType.exists());
96         CompilationTestUtils.assertFilesCount(parent, 7);
97
98         // Test if sources are compilable
99         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
100
101         final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
102         final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG
103             + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
104         final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG
105             + ".urn.opendaylight.test.rev131008.Links", true, loader);
106         final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG
107             + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
108
109         // Test generated 'grouping key-args'
110         assertTrue(keyArgsClass.isInterface());
111         CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
112         CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
113         assertEquals(3, abstractMethods(keyArgsClass).size());
114
115         // Test generated 'list links'
116         assertTrue(linksClass.isInterface());
117         CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
118         // TODO: anyxml
119         assertEquals(6, abstractMethods(linksClass).size());
120
121         // Test list key constructor arguments ordering
122         CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
123         // Test serialVersionUID generation
124         final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
125         suid.setAccessible(true);
126         assertEquals(-8829501012356283881L, suid.getLong(null));
127
128         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
129     }
130
131     @Test
132     public void testAugmentUnderUsesGeneration() throws Exception {
133         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
134         final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
135         generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
136
137         // Test if all sources were generated from 'module foo'
138         File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
139         assertTrue(new File(parent, "Object.java").exists());
140         assertTrue(new File(parent, "ClosedObject.java").exists());
141         assertTrue(new File(parent, "OpenObject.java").exists());
142         assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
143         assertTrue(new File(parent, "PathKeySubobject.java").exists());
144         CompilationTestUtils.assertFilesCount(parent, 10);
145
146         parent = new File(parent, "object");
147         assertTrue(new File(parent, "Nodes.java").exists());
148         assertTrue(new File(parent, "NodesBuilder.java").exists());
149         CompilationTestUtils.assertFilesCount(parent, 2);
150
151         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
152         CompilationTestUtils.assertFilesCount(parent, 1);
153
154         parent = new File(parent, "object");
155         assertTrue(new File(parent, "Link1.java").exists());
156         assertTrue(new File(parent, "Link1Builder.java").exists());
157         CompilationTestUtils.assertFilesCount(parent, 2);
158
159         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
160         CompilationTestUtils.assertFilesCount(parent, 1);
161
162         parent = new File(parent, "object");
163         assertTrue(new File(parent, "Nodes1.java").exists());
164         assertTrue(new File(parent, "Nodes1Builder.java").exists());
165         CompilationTestUtils.assertFilesCount(parent, 3);
166
167         parent = new File(parent, "nodes");
168         assertTrue(new File(parent, "Links.java").exists());
169         assertTrue(new File(parent, "LinksBuilder.java").exists());
170         CompilationTestUtils.assertFilesCount(parent, 2);
171
172         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
173         CompilationTestUtils.assertFilesCount(parent, 1);
174         parent = new File(parent, "route");
175         CompilationTestUtils.assertFilesCount(parent, 1);
176         parent = new File(parent, "object");
177         assertTrue(new File(parent, "Subobjects.java").exists());
178         assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
179         CompilationTestUtils.assertFilesCount(parent, 3);
180
181         parent = new File(parent, "subobjects");
182         CompilationTestUtils.assertFilesCount(parent, 1);
183         parent = new File(parent, "subobject");
184         CompilationTestUtils.assertFilesCount(parent, 1);
185         parent = new File(parent, "type");
186         assertTrue(new File(parent, "PathKey.java").exists());
187         assertTrue(new File(parent, "PathKeyBuilder.java").exists());
188         CompilationTestUtils.assertFilesCount(parent, 3);
189
190         parent = new File(parent, "path");
191         CompilationTestUtils.assertFilesCount(parent, 1);
192         parent = new File(parent, "key");
193         assertTrue(new File(parent, "PathKey.java").exists());
194         assertTrue(new File(parent, "PathKeyBuilder.java").exists());
195         CompilationTestUtils.assertFilesCount(parent, 2);
196
197         // Test if all sources were generated from 'module bar'
198         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
199         assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
200         assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
201         assertTrue(new File(parent, "RouteSubobjects.java").exists());
202         CompilationTestUtils.assertFilesCount(parent, 6);
203
204         parent = new File(parent, "route");
205         CompilationTestUtils.assertFilesCount(parent, 1);
206         parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
207         CompilationTestUtils.assertFilesCount(parent, 1);
208         parent = new File(parent, "explicit");
209         CompilationTestUtils.assertFilesCount(parent, 1);
210         parent = new File(parent, "route");
211         CompilationTestUtils.assertFilesCount(parent, 1);
212
213         parent = new File(parent, "subobjects");
214         CompilationTestUtils.assertFilesCount(parent, 2);
215         assertTrue(new File(parent, "SubobjectType.java").exists());
216
217         parent = new File(parent, "subobject");
218         CompilationTestUtils.assertFilesCount(parent, 1);
219
220         parent = new File(parent, "type");
221         assertTrue(new File(parent, "IpPrefix.java").exists());
222         assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
223         assertTrue(new File(parent, "Label.java").exists());
224         assertTrue(new File(parent, "LabelBuilder.java").exists());
225         CompilationTestUtils.assertFilesCount(parent, 4);
226
227         // Test if sources are compilable
228         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
229
230         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
231     }
232
233     @Test
234     public void testAugmentOfAugmentGeneration() throws Exception {
235         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
236         final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
237         generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
238
239         // Test if all sources were generated from 'module foo'
240         File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
241         final File fooListener = new File(parent, "FooListener.java");
242         File pathAttributes = new File(parent, "PathAttributes.java");
243         final File update = new File(parent, "Update.java");
244         final File updateBuilder = new File(parent, "UpdateBuilder.java");
245         assertTrue(fooListener.exists());
246         assertTrue(pathAttributes.exists());
247         assertTrue(update.exists());
248         assertTrue(updateBuilder.exists());
249         CompilationTestUtils.assertFilesCount(parent, 7);
250
251         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
252         CompilationTestUtils.assertFilesCount(parent, 1);
253         parent = new File(parent, "attributes");
254         CompilationTestUtils.assertFilesCount(parent, 2);
255         final File origin = new File(parent, "Origin.java");
256         final File originBuilder = new File(parent, "OriginBuilder.java");
257         assertTrue(origin.exists());
258         assertTrue(originBuilder.exists());
259
260         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
261         CompilationTestUtils.assertFilesCount(parent, 2);
262         pathAttributes = new File(parent, "PathAttributes.java");
263         final File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
264         assertTrue(pathAttributes.exists());
265         assertTrue(pathAttributesBuilder.exists());
266
267         // Test if all sources were generated from 'module bar'
268         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
269         final File destination = new File(parent, "Destination.java");
270         final File pathAttributes1 = new File(parent, "PathAttributes1.java");
271         final File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
272         assertTrue(destination.exists());
273         assertTrue(pathAttributes1.exists());
274         assertTrue(pathAttributes1Builder.exists());
275         CompilationTestUtils.assertFilesCount(parent, 6);
276
277         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
278         CompilationTestUtils.assertFilesCount(parent, 2);
279         final File destinationType = new File(parent, "DestinationType.java");
280         assertTrue(destinationType.exists());
281
282         parent = new File(parent, "destination");
283         CompilationTestUtils.assertFilesCount(parent, 1);
284         parent = new File(parent, "type");
285         CompilationTestUtils.assertFilesCount(parent, 2);
286         final File destinationIpv4 = new File(parent, "DestinationIp.java");
287         final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
288         assertTrue(destinationIpv4.exists());
289         assertTrue(destinationIpv4Builder.exists());
290
291         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
292         CompilationTestUtils.assertFilesCount(parent, 1);
293         parent = new File(parent, "path");
294         CompilationTestUtils.assertFilesCount(parent, 1);
295         parent = new File(parent, "attributes");
296         final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
297         final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
298         assertTrue(mpUnreachNlri.exists());
299         assertTrue(mpUnreachNlriBuilder.exists());
300         CompilationTestUtils.assertFilesCount(parent, 3);
301
302         parent = new File(parent, "mp");
303         CompilationTestUtils.assertFilesCount(parent, 1);
304         parent = new File(parent, "unreach");
305         CompilationTestUtils.assertFilesCount(parent, 1);
306         parent = new File(parent, "nlri");
307         final File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
308         final File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
309         assertTrue(withdrawnRoutes.exists());
310         assertTrue(withdrawnRoutesBuilder.exists());
311         CompilationTestUtils.assertFilesCount(parent, 2);
312
313         // Test if all sources were generated from 'module baz'
314         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
315         CompilationTestUtils.assertFilesCount(parent, 3);
316         final File linkstateDestination = new File(parent, "LinkstateDestination.java");
317         assertTrue(linkstateDestination.exists());
318
319         parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
320         CompilationTestUtils.assertFilesCount(parent, 1);
321         parent = new File(parent, "path");
322         CompilationTestUtils.assertFilesCount(parent, 1);
323         parent = new File(parent, "attributes");
324         CompilationTestUtils.assertFilesCount(parent, 1);
325         parent = new File(parent, "mp");
326         CompilationTestUtils.assertFilesCount(parent, 1);
327         parent = new File(parent, "unreach");
328         CompilationTestUtils.assertFilesCount(parent, 1);
329         parent = new File(parent, "nlri");
330         CompilationTestUtils.assertFilesCount(parent, 1);
331         parent = new File(parent, "withdrawn");
332         CompilationTestUtils.assertFilesCount(parent, 1);
333         parent = new File(parent, "routes");
334         CompilationTestUtils.assertFilesCount(parent, 1);
335         parent = new File(parent, "destination");
336         CompilationTestUtils.assertFilesCount(parent, 1);
337         parent = new File(parent, "type");
338         final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
339         final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
340         assertTrue(destinationLinkstate.exists());
341         assertTrue(destinationLinkstateBuilder.exists());
342         CompilationTestUtils.assertFilesCount(parent, 3);
343         parent = new File(parent, "destination");
344         CompilationTestUtils.assertFilesCount(parent, 1);
345         parent = new File(parent, "linkstate");
346         final File links = new File(parent, "Links.java");
347         final File linksBuilder = new File(parent, "LinksBuilder.java");
348         assertTrue(links.exists());
349         assertTrue(linksBuilder.exists());
350         CompilationTestUtils.assertFilesCount(parent, 3);
351         parent = new File(parent, "links");
352         final File source = new File(parent, "Source.java");
353         final File sourceBuilder = new File(parent, "SourceBuilder.java");
354         assertTrue(source.exists());
355         assertTrue(sourceBuilder.exists());
356         CompilationTestUtils.assertFilesCount(parent, 3);
357         parent = new File(parent, "source");
358         final File address = new File(parent, "Address.java");
359         final File addressBuilder = new File(parent, "AddressBuilder.java");
360         assertTrue(address.exists());
361         assertTrue(addressBuilder.exists());
362         CompilationTestUtils.assertFilesCount(parent, 2);
363
364         // Test if sources are compilable
365         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
366
367         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
368     }
369
370     @Test
371     public void testLeafReturnTypes() throws Exception {
372         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
373         final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
374         generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
375
376         final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
377         assertTrue(new File(parent, "TestData.java").exists());
378         assertTrue(new File(parent, "Nodes.java").exists());
379         assertTrue(new File(parent, "NodesBuilder.java").exists());
380         assertTrue(new File(parent, "Alg.java").exists());
381         assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
382         CompilationTestUtils.assertFilesCount(parent, 6);
383
384         // Test if sources are compilable
385         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
386
387         final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
388         final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
389         final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
390         final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
391
392         // Test methods return type
393         final byte[] b = new byte[] {};
394         CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
395         CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
396         CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
397         CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
398         CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
399         CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
400         testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
401         testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
402         CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
403         CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
404         CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
405         CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
406         CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
407         CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
408         CompilationTestUtils.assertContainsMethod(nodesClass, Uint8.class, "getIdU8");
409         CompilationTestUtils.assertContainsMethod(nodesClass, Uint16.class, "getIdU16");
410         CompilationTestUtils.assertContainsMethod(nodesClass, Uint32.class, "getIdU32");
411         CompilationTestUtils.assertContainsMethod(nodesClass, Uint64.class, "getIdU64");
412         CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
413
414         final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
415
416         Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
417             b.getClass());
418         final List<Range<Integer>> lengthConstraints = new ArrayList<>();
419         lengthConstraints.add(Range.closed(1, 10));
420         byte[] arg = new byte[] {};
421         String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
422             lengthConstraints);
423         CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
424
425         method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
426             BigDecimal.class);
427         final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
428         rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
429         Object arg1 = new BigDecimal("1.4");
430         expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
431         CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
432
433         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
434     }
435
436     @Test
437     public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
438             ClassNotFoundException {
439         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
440         final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
441         generateTestSources("/compilation/context-reference", sourcesOutputDir);
442
443         // Test if all sources are generated
444         final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
445         CompilationTestUtils.assertFilesCount(fooParent, 4);
446         assertTrue(new File(fooParent, "FooData.java").exists());
447         assertTrue(new File(fooParent, "Nodes.java").exists());
448         assertTrue(new File(fooParent, "NodesBuilder.java").exists());
449
450         final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
451         CompilationTestUtils.assertFilesCount(barParent, 2);
452         assertTrue(new File(barParent, "IdentityClass.java").exists());
453
454         // Test if sources are compilable
455         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
456
457         final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
458         final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
459             + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
460         final Class<?> identityClass = Class
461                 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
462
463         // test identity
464         final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
465             loader);
466         assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
467
468         // Test annotation
469         final Method getId;
470         try {
471             getId = nodesClass.getMethod("getId");
472         } catch (final NoSuchMethodException e) {
473             throw new AssertionError("Method getId() not found", e);
474         }
475
476         assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
477             .map(Annotation::annotationType).collect(Collectors.toSet()));
478         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
479     }
480
481     @Test
482     public void compilationTest() throws Exception {
483         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
484         final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
485         generateTestSources("/yang", sourcesOutputDir);
486
487         // Test if sources are compilable
488         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
489
490         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
491     }
492
493     @Test
494     public void testBug586() throws Exception {
495         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
496         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
497         generateTestSources("/compilation/bug586", sourcesOutputDir);
498
499         // Test if sources are compilable
500         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
501
502         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
503     }
504
505     @Test
506     public void testBug4760() throws Exception {
507         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
508         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
509         generateTestSources("/compilation/bug4760", sourcesOutputDir);
510
511         // Test if sources are compilable
512         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
513
514         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
515     }
516
517     /**
518      * Test handling nested uses-augmentations.
519      */
520     @Test
521     public void testBug1172() throws Exception {
522         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
523         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
524         generateTestSources("/compilation/bug1172", sourcesOutputDir);
525
526         // Test if sources are compilable
527         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
528
529         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
530     }
531
532     @Test
533     public void testBug5461() throws Exception {
534         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
535         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
536         generateTestSources("/compilation/bug5461", sourcesOutputDir);
537
538         // Test if sources are compilable
539         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
540
541         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
542     }
543
544     @Test
545     public void testBug5882() throws Exception {
546         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
547         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
548         generateTestSources("/compilation/bug5882", sourcesOutputDir);
549
550         // Test if sources are compilable
551         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
552
553         final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
554         assertTrue(new File(parent, "FooData.java").exists());
555         assertTrue(new File(parent, "TypedefCurrent.java").exists());
556         assertTrue(new File(parent, "TypedefDeprecated.java").exists());
557
558         try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
559             final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
560             final Class<?> cls = loader.loadClass(pkg + ".FooData");
561             final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
562             final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
563             final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
564             final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
565             final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
566             final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
567             final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
568             final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
569             assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
570             assertTrue(clsTypedefCur.getAnnotations().length == 0);
571             assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
572             assertTrue(clsGroupingCur.getAnnotations().length == 0);
573             assertTrue(clsTypeDef1.getAnnotations().length == 0);
574             assertTrue(clsTypeDef3.getAnnotations().length == 0);
575             assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
576
577             /*methods inside container*/
578             assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
579             assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
580             assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
581             assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
582             assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
583             assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
584
585             /*methods inside module*/
586             assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
587             assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
588             assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
589             assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
590             assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
591             assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
592             assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
593         }
594
595         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
596     }
597
598     /**
599      * Test if class generated for node from grouping implements ChildOf.
600      */
601     @Test
602     public void testBug1377() throws Exception {
603         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
604         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
605
606         generateTestSources("/compilation/bug1377", sourcesOutputDir);
607
608         // Test if sources are compilable
609         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
610
611         final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
612         final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
613                 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
614         final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
615             true, loader);
616
617         // Test generated 'container output-action'
618         assertTrue(outputActionClass.isInterface());
619         CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
620             actionClass.getCanonicalName());
621
622         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
623     }
624
625     @Test
626     public void testMdsal327() throws Exception {
627         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
628         final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
629         generateTestSources("/compilation/mdsal327", sourcesOutputDir);
630         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
631         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
632     }
633
634     @Test
635     public void testMdsal365() throws Exception {
636         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
637         final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
638         generateTestSources("/compilation/mdsal365", sourcesOutputDir);
639         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
640         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
641     }
642
643     @Test
644     public void testMdsal395() throws Exception {
645         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
646         final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
647         generateTestSources("/compilation/mdsal395", sourcesOutputDir);
648         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
649         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
650     }
651
652     @Test
653     public void classNamesColisionTest() throws Exception {
654         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
655         final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
656         generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
657         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
658         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
659     }
660
661     @Test
662     public void innerEnumerationNameCollisionTest() throws Exception {
663         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
664         final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
665         generateTestSources("/compilation/mdsal321", sourcesOutputDir);
666         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
667         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
668     }
669
670     @Test
671     public void twoNestedUnionsTest() throws Exception {
672         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
673         final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
674         generateTestSources("/compilation/mdsal320", sourcesOutputDir);
675         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
676         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
677     }
678
679     @Test
680     public void testMdsal425() throws Exception {
681         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
682         final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
683         generateTestSources("/compilation/mdsal425", sourcesOutputDir);
684         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
685         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
686     }
687
688     private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
689             final String returnTypeStr) throws NoSuchMethodException {
690         Method method = clazz.getMethod(methodName);
691         assertEquals(java.lang.Class.class, method.getReturnType());
692         java.lang.reflect.Type returnType = method.getGenericReturnType();
693         assertTrue(returnType instanceof ParameterizedType);
694         final ParameterizedType pt = (ParameterizedType) returnType;
695         final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
696         assertEquals(1, parameters.length);
697         final java.lang.reflect.Type parameter = parameters[0];
698         assertTrue(parameter instanceof WildcardType);
699         final WildcardType wildcardType = (WildcardType) parameter;
700         assertEquals("? extends " + returnTypeStr, wildcardType.toString());
701     }
702
703     private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
704             final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
705         final Method method = clazz.getMethod(methodName);
706         final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
707             loader);
708         assertEquals(rawReturnType, method.getReturnType());
709         final java.lang.reflect.Type returnType = method.getGenericReturnType();
710         assertTrue(returnType instanceof ParameterizedType);
711         final ParameterizedType pt = (ParameterizedType) returnType;
712         final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
713         assertEquals(1, parameters.length);
714         final java.lang.reflect.Type parameter = parameters[0];
715         assertTrue(parameter instanceof WildcardType);
716         final WildcardType wildcardType = (WildcardType) parameter;
717         assertEquals("?", wildcardType.toString());
718     }
719 }