1 package org.opendaylight.yangtools.yang.unified.doc.generator.maven;
3 import static org.junit.Assert.assertTrue;
6 import java.io.FileNotFoundException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
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;
22 public class DocGenTest {
24 public static final String FS = File.separator;
25 static final String BASE_PKG = "org.opendaylight.yang.gen.v1";
27 static final String TEST_PATH = "target" + FS + "test";
28 static final File TEST_DIR = new File(TEST_PATH);
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);
35 protected YangParserImpl parser;
36 protected BindingGenerator bindingGenerator;
39 public static void createTestDirs() {
40 if (TEST_DIR.exists()) {
41 deleteTestDir(TEST_DIR);
43 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
44 assertTrue(COMPILER_OUTPUT_DIR.mkdirs());
49 parser = new YangParserImpl();
50 bindingGenerator = new BindingGeneratorImpl();
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);
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);
73 sourceFiles.addAll(Arrays.asList(fileArray));
76 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
81 static void deleteTestDir(File file) {
82 if (file.isDirectory()) {
83 File[] filesToDelete = file.listFiles();
84 if (filesToDelete != null) {
85 for (File f : filesToDelete) {
91 throw new RuntimeException("Failed to clean up after test");