1 package org.opendaylight.yangtools.yang.wadl.generator.maven;
3 import static org.junit.Assert.*;
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;
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;
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;
29 if (GENERATOR_OUTPUT_DIR.exists()) {
30 deleteTestDir(GENERATOR_OUTPUT_DIR);
32 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
33 parser = new YangParserImpl();
37 public void cleanUp() {
38 if (GENERATOR_OUTPUT_DIR.exists()) {
39 deleteTestDir(GENERATOR_OUTPUT_DIR);
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());
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);
62 sourceFiles.addAll(Arrays.asList(fileArray));
65 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
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) {
79 throw new RuntimeException("Failed to clean up after test");