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.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
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 EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
56 final Set<Module> modules = context.getModules();
57 final WadlGenerator generator = new WadlGenerator();
58 generator.setBuildContext(new DefaultBuildContext());
59 Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modules,
60 module -> Optional.empty());
61 assertEquals(3, generatedWadlFiles.size());
62 generatedWadlFiles.forEach(file -> assertTrue(file.exists()));
66 public void testListGenerationWithoutPath() throws Exception {
67 final List<File> sourceFiles = getSourceFiles("/wadl-gen");
68 final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
69 final Set<Module> modules = context.getModules();
70 final WadlGenerator generator = new WadlGenerator();
71 generator.setBuildContext(new DefaultBuildContext());
72 Collection<File> generatedWadlFiles = generator.generateSources(context, null, modules,
73 module -> Optional.empty());
74 assertEquals(3, generatedWadlFiles.size());
75 generatedWadlFiles.forEach(file -> {
77 assertFalse(file.exists());
81 private static List<File> getSourceFiles(final String path) throws Exception {
82 final URI resPath = WadlGenTest.class.getResource(path).toURI();
83 final File sourcesDir = new File(resPath);
84 if (!sourcesDir.exists()) {
85 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
87 final List<File> sourceFiles = new ArrayList<>();
88 final File[] fileArray = sourcesDir.listFiles();
89 if (fileArray == null) {
90 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
92 sourceFiles.addAll(Arrays.asList(fileArray));
96 private static void deleteTestDir(final File file) {
97 if (file.isDirectory()) {
98 File[] filesToDelete = file.listFiles();
99 if (filesToDelete != null) {
100 for (File f : filesToDelete) {
105 if (!file.delete()) {
106 throw new RuntimeException("Failed to clean up after test");