Added initial version of documentation generator.
[mdsal.git] / code-generator / maven-sal-api-gen-plugin / src / test / java / org / opendaylight / yangtools / yang / unified / doc / generator / maven / DocGenTest.java
1 package org.opendaylight.yangtools.yang.unified.doc.generator.maven;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.Set;
11
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
16 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
20 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
21
22 public class DocGenTest {
23
24     public static final String FS = File.separator;
25     static final String BASE_PKG = "org.opendaylight.yang.gen.v1";
26
27     static final String TEST_PATH = "target" + FS + "test";
28     static final File TEST_DIR = new File(TEST_PATH);
29
30     static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
31     static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin";
32     static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH);
33
34
35     protected YangParserImpl parser;
36     protected BindingGenerator bindingGenerator;
37
38     @BeforeClass
39     public static void createTestDirs() {
40         if (TEST_DIR.exists()) {
41             deleteTestDir(TEST_DIR);
42         }
43         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
44         assertTrue(COMPILER_OUTPUT_DIR.mkdirs());
45     }
46
47     @Before
48     public void init() {
49         parser = new YangParserImpl();
50         bindingGenerator = new BindingGeneratorImpl();
51     }
52
53     @Test
54     public void testListGeneration() throws Exception {
55         final List<File> sourceFiles = getSourceFiles("/doc-gen");
56         final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
57         final SchemaContext context = parser.resolveSchemaContext(modulesToBuild);
58         final CodeGenerator generator = new DocumentationGeneratorImpl();
59         generator.generateSources(context, GENERATOR_OUTPUT_DIR, modulesToBuild);
60
61
62     }
63
64     static List<File> getSourceFiles(String path) throws FileNotFoundException {
65         final String resPath = DocGenTest.class.getResource(path).getPath();
66         final File sourcesDir = new File(resPath);
67         if (sourcesDir.exists()) {
68             final List<File> sourceFiles = new ArrayList<>();
69             final File[] fileArray = sourcesDir.listFiles();
70             if (fileArray == null) {
71                 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
72             }
73             sourceFiles.addAll(Arrays.asList(fileArray));
74             return sourceFiles;
75         } else {
76             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
77         }
78     }
79
80
81     static void deleteTestDir(File file) {
82         if (file.isDirectory()) {
83             File[] filesToDelete = file.listFiles();
84             if (filesToDelete != null) {
85                 for (File f : filesToDelete) {
86                     deleteTestDir(f);
87                 }
88             }
89         }
90         if (!file.delete()) {
91             throw new RuntimeException("Failed to clean up after test");
92         }
93     }
94 }