Cleaned up Java Binding code from YANG Tools
[mdsal.git] / binding / maven-sal-api-gen-plugin / src / test / java / org / opendaylight / yangtools / yang / wadl / generator / maven / WadlGenTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.wadl.generator.maven;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
29
30 public class WadlGenTest {
31     public static final String FS = File.separator;
32     private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
33     private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
34     private YangParserImpl parser;
35
36     @Before
37     public void init() {
38         if (GENERATOR_OUTPUT_DIR.exists()) {
39             deleteTestDir(GENERATOR_OUTPUT_DIR);
40         }
41         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
42         parser = new YangParserImpl();
43     }
44
45     @After
46     public void cleanUp() {
47         if (GENERATOR_OUTPUT_DIR.exists()) {
48             deleteTestDir(GENERATOR_OUTPUT_DIR);
49         }
50     }
51
52     @Test
53     public void testListGeneration() throws Exception {
54         final List<File> sourceFiles = getSourceFiles("/wadl-gen");
55         final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
56         final SchemaContext context = parser.resolveSchemaContext(modulesToBuild);
57         final BasicCodeGenerator generator = new WadlGenerator();
58         Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modulesToBuild);
59         assertEquals(3, generatedWadlFiles.size());
60     }
61
62     private static List<File> getSourceFiles(String path) throws Exception {
63         final URI resPath = WadlGenTest.class.getResource(path).toURI();
64         final File sourcesDir = new File(resPath);
65         if (sourcesDir.exists()) {
66             final List<File> sourceFiles = new ArrayList<>();
67             final File[] fileArray = sourcesDir.listFiles();
68             if (fileArray == null) {
69                 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
70             }
71             sourceFiles.addAll(Arrays.asList(fileArray));
72             return sourceFiles;
73         } else {
74             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
75         }
76     }
77
78     private static void deleteTestDir(File file) {
79         if (file.isDirectory()) {
80             File[] filesToDelete = file.listFiles();
81             if (filesToDelete != null) {
82                 for (File f : filesToDelete) {
83                     deleteTestDir(f);
84                 }
85             }
86         }
87         if (!file.delete()) {
88             throw new RuntimeException("Failed to clean up after test");
89         }
90     }
91
92 }