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