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