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.test.util.YangParserTestUtils;
28 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
30 public class WadlGenTest {
32 private static final String FS = File.separator;
33 private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
34 private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
38 if (GENERATOR_OUTPUT_DIR.exists()) {
39 deleteTestDir(GENERATOR_OUTPUT_DIR);
41 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
45 public void cleanUp() {
46 if (GENERATOR_OUTPUT_DIR.exists()) {
47 deleteTestDir(GENERATOR_OUTPUT_DIR);
52 public void testListGeneration() throws Exception {
53 final List<File> sourceFiles = getSourceFiles("/wadl-gen");
54 final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
55 final WadlGenerator generator = new WadlGenerator();
56 generator.setBuildContext(new DefaultBuildContext());
57 Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR,
58 Set.copyOf(context.getModules()), (module, representation) -> Optional.empty());
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 EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
67 final WadlGenerator generator = new WadlGenerator();
68 generator.setBuildContext(new DefaultBuildContext());
69 Collection<File> generatedWadlFiles = generator.generateSources(context, null, Set.copyOf(context.getModules()),
70 (module, representation) -> Optional.empty());
71 assertEquals(3, generatedWadlFiles.size());
72 generatedWadlFiles.forEach(file -> {
74 assertFalse(file.exists());
78 private static List<File> getSourceFiles(final String path) throws Exception {
79 final URI resPath = WadlGenTest.class.getResource(path).toURI();
80 final File sourcesDir = new File(resPath);
81 if (!sourcesDir.exists()) {
82 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
84 final List<File> sourceFiles = new ArrayList<>();
85 final File[] fileArray = sourcesDir.listFiles();
86 if (fileArray == null) {
87 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
89 sourceFiles.addAll(Arrays.asList(fileArray));
93 private static void deleteTestDir(final 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");