11c5ad7060d7590f68d8db923d53ee2515ad4631
[yangtools.git] / code-generator / maven-sal-api-gen-plugin / src / test / java / org / opendaylight / yangtools / yang / wadl / generator / maven / WadlGenTest.java
1 package org.opendaylight.yangtools.yang.wadl.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.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
18 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
19
20 public class WadlGenTest {
21     public static final String FS = File.separator;
22     private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
23     private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
24     private YangParserImpl parser;
25
26     @Before
27     public void init() {
28         if (GENERATOR_OUTPUT_DIR.exists()) {
29             deleteTestDir(GENERATOR_OUTPUT_DIR);
30         }
31         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
32         parser = new YangParserImpl();
33     }
34
35     @After
36     public void cleanUp() {
37         if (GENERATOR_OUTPUT_DIR.exists()) {
38             deleteTestDir(GENERATOR_OUTPUT_DIR);
39         }
40     }
41
42     @Test
43     public void testListGeneration() throws Exception {
44         final List<File> sourceFiles = getSourceFiles("/wadl-gen");
45         final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
46         final SchemaContext context = parser.resolveSchemaContext(modulesToBuild);
47         final CodeGenerator generator = new WadlGenerator();
48         generator.generateSources(context, GENERATOR_OUTPUT_DIR, modulesToBuild);
49     }
50
51     private static List<File> getSourceFiles(String path) throws FileNotFoundException {
52         final String resPath = WadlGenTest.class.getResource(path).getPath();
53         final File sourcesDir = new File(resPath);
54         if (sourcesDir.exists()) {
55             final List<File> sourceFiles = new ArrayList<>();
56             final File[] fileArray = sourcesDir.listFiles();
57             if (fileArray == null) {
58                 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
59             }
60             sourceFiles.addAll(Arrays.asList(fileArray));
61             return sourceFiles;
62         } else {
63             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
64         }
65     }
66
67     private static void deleteTestDir(File file) {
68         if (file.isDirectory()) {
69             File[] filesToDelete = file.listFiles();
70             if (filesToDelete != null) {
71                 for (File f : filesToDelete) {
72                     deleteTestDir(f);
73                 }
74             }
75         }
76         if (!file.delete()) {
77             throw new RuntimeException("Failed to clean up after test");
78         }
79     }
80
81 }