1 package org.opendaylight.yangtools.yang.wadl.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.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;
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;
28 if (GENERATOR_OUTPUT_DIR.exists()) {
29 deleteTestDir(GENERATOR_OUTPUT_DIR);
31 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
32 parser = new YangParserImpl();
36 public void cleanUp() {
37 if (GENERATOR_OUTPUT_DIR.exists()) {
38 deleteTestDir(GENERATOR_OUTPUT_DIR);
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);
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);
60 sourceFiles.addAll(Arrays.asList(fileArray));
63 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
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) {
77 throw new RuntimeException("Failed to clean up after test");