cc3e303fabe710a8946ce99b9f09b1af56c7bbbb
[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.util.List;
19 import java.util.Map;
20 import java.util.Scanner;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 /**
28  * Bug5151 involves adding <code>{@literal @}return</code> annotations to accessor methods.
29  */
30 public class Bug5151Test extends BaseCompilationTest {
31
32     private static final String BUG_ID = "bug5151";
33
34     @Test
35     public void test() throws Exception {
36         final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + BUG_ID);
37         assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
38         final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + BUG_ID);
39         assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
40
41         generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID, sourcesOutputDir);
42
43         // Test if sources are compilable
44         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
45
46         final Map<String, File> generatedFiles = getFiles(sourcesOutputDir);
47         assertEquals(3, generatedFiles.size());
48
49         final File fooContainerFile = generatedFiles.get("FooContainer.java");
50         assertNotNull(fooContainerFile);
51         assertTrue(findInFile(fooContainerFile,
52                 "@return <code>java.lang.String</code> <code>fooInContainer</code>, "
53                         + "or <code>null</code> if not present"));
54
55         final File fooDataFile = generatedFiles.get("FooData.java");
56         assertNotNull(fooDataFile);
57         assertTrue(findInFile(fooDataFile, "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present"));
58
59         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
60     }
61
62     private static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
63         final Scanner scanner = new Scanner(file);
64         while (scanner.hasNextLine()) {
65             final String nextLine = scanner.nextLine();
66             if (nextLine.contains(searchText)) {
67                 return true;
68             }
69         }
70         return false;
71     }
72
73     private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
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             } else {
91                 files.put(file.getName(), file);
92             }
93         }
94         return files;
95     }
96 }