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