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;
21 import java.util.Optional;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
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.parseYangFiles(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 module -> Optional.empty());
60 assertEquals(3, generatedWadlFiles.size());
61 generatedWadlFiles.forEach(file -> assertTrue(file.exists()));
65 public void testListGenerationWithoutPath() throws Exception {
66 final List<File> sourceFiles = getSourceFiles("/wadl-gen");
67 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
68 final Set<Module> modules = context.getModules();
69 final BasicCodeGenerator generator = new WadlGenerator();
70 Collection<File> generatedWadlFiles = generator.generateSources(context, null, modules,
71 module -> Optional.empty());
72 assertEquals(3, generatedWadlFiles.size());
73 generatedWadlFiles.forEach(file -> {
75 assertFalse(file.exists());
79 private static List<File> getSourceFiles(final String path) throws Exception {
80 final URI resPath = WadlGenTest.class.getResource(path).toURI();
81 final File sourcesDir = new File(resPath);
82 if (sourcesDir.exists()) {
83 final List<File> sourceFiles = new ArrayList<>();
84 final File[] fileArray = sourcesDir.listFiles();
85 if (fileArray == null) {
86 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
88 sourceFiles.addAll(Arrays.asList(fileArray));
91 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
95 private static void deleteTestDir(final File file) {
96 if (file.isDirectory()) {
97 File[] filesToDelete = file.listFiles();
98 if (filesToDelete != null) {
99 for (File f : filesToDelete) {
104 if (!file.delete()) {
105 throw new RuntimeException("Failed to clean up after test");