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.unified.doc.generator.maven;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
14 import java.io.FileNotFoundException;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Optional;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
30 public class DocGenTest {
31 public static final String FS = File.separator;
32 private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
33 private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
37 if (GENERATOR_OUTPUT_DIR.exists()) {
38 deleteTestDir(GENERATOR_OUTPUT_DIR);
40 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
44 public void cleanUp() {
45 if (GENERATOR_OUTPUT_DIR.exists()) {
46 deleteTestDir(GENERATOR_OUTPUT_DIR);
51 public void testListGeneration() throws Exception {
52 final List<File> sourceFiles = getSourceFiles("/doc-gen");
53 final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
54 final Set<Module> modules = context.getModules();
55 final DocumentationGeneratorImpl generator = new DocumentationGeneratorImpl();
56 generator.setBuildContext(new DefaultBuildContext());
57 Collection<File> generatedFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modules,
58 module -> Optional.empty());
59 assertEquals(4, generatedFiles.size());
62 private static List<File> getSourceFiles(final String path) throws Exception {
63 final URI resPath = DocGenTest.class.getResource(path).toURI();
64 final File sourcesDir = new File(resPath);
65 if (!sourcesDir.exists()) {
66 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
69 final List<File> sourceFiles = new ArrayList<>();
70 final File[] fileArray = sourcesDir.listFiles();
71 if (fileArray == null) {
72 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
74 sourceFiles.addAll(Arrays.asList(fileArray));
78 private static void deleteTestDir(final File file) {
79 if (file.isDirectory()) {
80 File[] filesToDelete = file.listFiles();
81 if (filesToDelete != null) {
82 for (File f : filesToDelete) {
88 throw new RuntimeException("Failed to clean up after test");