2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.yang.wadl.generator.maven;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
15 import java.io.FileNotFoundException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.binding.yang.wadl.generator.maven.WadlGenerator;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
31 public class WadlGenTest {
33 private static final String FS = File.separator;
34 private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
35 private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
39 if (GENERATOR_OUTPUT_DIR.exists()) {
40 deleteTestDir(GENERATOR_OUTPUT_DIR);
42 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
46 public void cleanUp() {
47 if (GENERATOR_OUTPUT_DIR.exists()) {
48 deleteTestDir(GENERATOR_OUTPUT_DIR);
53 public void testListGeneration() throws Exception {
54 final List<File> sourceFiles = getSourceFiles("/wadl-gen");
55 final SchemaContext context = YangParserTestUtils.parseYangSources(sourceFiles);
56 final Set<Module> modules = context.getModules();
57 final BasicCodeGenerator generator = new WadlGenerator();
58 Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modules);
59 assertEquals(3, generatedWadlFiles.size());
60 generatedWadlFiles.forEach((file -> assertTrue(file.exists())));
64 public void testListGenerationWithoutPath() throws Exception {
65 final List<File> sourceFiles = getSourceFiles("/wadl-gen");
66 final SchemaContext context = YangParserTestUtils.parseYangSources(sourceFiles);
67 final Set<Module> modules = context.getModules();
68 final BasicCodeGenerator generator = new WadlGenerator();
69 Collection<File> generatedWadlFiles = generator.generateSources(context, null, modules);
70 assertEquals(3, generatedWadlFiles.size());
71 generatedWadlFiles.forEach((file -> {
73 assertFalse(file.exists());
77 private static List<File> getSourceFiles(String path) throws Exception {
78 final URI resPath = WadlGenTest.class.getResource(path).toURI();
79 final File sourcesDir = new File(resPath);
80 if (sourcesDir.exists()) {
81 final List<File> sourceFiles = new ArrayList<>();
82 final File[] fileArray = sourcesDir.listFiles();
83 if (fileArray == null) {
84 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
86 sourceFiles.addAll(Arrays.asList(fileArray));
89 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
93 private static void deleteTestDir(File file) {
94 if (file.isDirectory()) {
95 File[] filesToDelete = file.listFiles();
96 if (filesToDelete != null) {
97 for (File f : filesToDelete) {
102 if (!file.delete()) {
103 throw new RuntimeException("Failed to clean up after test");