b07e6bc76744b3ca0841d9861780d3aa900cdeb2
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / Bug5151Test.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 junit.framework.TestCase.assertTrue;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Maps;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.net.URISyntaxException;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Scanner;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
25 import org.opendaylight.mdsal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 /**
30  * Bug5151 involves adding <code>{@literal @}return</code> annotations to accessor methods.
31  */
32 public class Bug5151Test extends BaseCompilationTest {
33
34     private static final String BUG_ID = "bug5151";
35
36     @Test
37     public void test() throws Exception {
38         final File sourcesOutputDir = CompilationTestUtils.generatorOutput(BUG_ID);
39         final File compiledOutputDir = CompilationTestUtils.compilerOutput(BUG_ID);
40         generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID, sourcesOutputDir);
41
42         // Test if sources are compilable
43         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
44
45         final Map<String, File> generatedFiles = getFiles(sourcesOutputDir);
46         assertEquals(3, generatedFiles.size());
47
48         final File fooContainerFile = generatedFiles.get("FooContainer.java");
49         assertNotNull(fooContainerFile);
50         assertTrue(findInFile(fooContainerFile,
51                 "@return <code>java.lang.String</code> <code>fooInContainer</code>, "
52                         + "or <code>null</code> if not present"));
53
54         final File fooDataFile = generatedFiles.get("FooData.java");
55         assertNotNull(fooDataFile);
56         assertTrue(findInFile(fooDataFile, "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present"));
57
58         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
59     }
60
61     private static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
62         final Scanner scanner = new Scanner(file);
63         while (scanner.hasNextLine()) {
64             final String nextLine = scanner.nextLine();
65             if (nextLine.contains(searchText)) {
66                 return true;
67             }
68         }
69         return false;
70     }
71
72     private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
73             throws IOException, URISyntaxException {
74         final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
75         final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
76         final List<Type> types = bindingGenerator.generateTypes(context);
77         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
78         generator.generateToFile(sourcesOutputDir);
79     }
80
81     private static Map<String, File> getFiles(final File path) {
82         return getFiles(path, Maps.newHashMap());
83     }
84
85     private static Map<String, File> getFiles(final File path, final Map<String, File> files) {
86         final File [] dirFiles = path.listFiles();
87         for (File file : dirFiles) {
88             if (file.isDirectory()) {
89                 return getFiles(file, files);
90             }
91
92             files.put(file.getName(), file);
93         }
94         return files;
95     }
96 }