Bump to odlparent-8.0.0/yangtools-8.0.0-SNAPSHOT
[mdsal.git] / binding / maven-sal-api-gen-plugin / src / test / java / org / opendaylight / mdsal / binding / yang / unified / doc / generator / maven / DocGenTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.yang.unified.doc.generator.maven;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.net.URI;
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;
21 import java.util.Set;
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.test.util.YangParserTestUtils;
27 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
28
29 public class DocGenTest {
30     public static final String FS = File.separator;
31     private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
32     private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
33
34     @Before
35     public void init() {
36         if (GENERATOR_OUTPUT_DIR.exists()) {
37             deleteTestDir(GENERATOR_OUTPUT_DIR);
38         }
39         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
40     }
41
42     @After
43     public void cleanUp() {
44         if (GENERATOR_OUTPUT_DIR.exists()) {
45             deleteTestDir(GENERATOR_OUTPUT_DIR);
46         }
47     }
48
49     @Test
50     public void testListGeneration() throws Exception {
51         final List<File> sourceFiles = getSourceFiles("/doc-gen");
52         final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
53         final DocumentationGeneratorImpl generator = new DocumentationGeneratorImpl();
54         generator.setBuildContext(new DefaultBuildContext());
55         Collection<File> generatedFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR,
56             Set.copyOf(context.getModules()), (module, representation) -> Optional.empty());
57         assertEquals(4, generatedFiles.size());
58     }
59
60     private static List<File> getSourceFiles(final String path) throws Exception {
61         final URI resPath = DocGenTest.class.getResource(path).toURI();
62         final File sourcesDir = new File(resPath);
63         if (!sourcesDir.exists()) {
64             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
65         }
66
67         final List<File> sourceFiles = new ArrayList<>();
68         final File[] fileArray = sourcesDir.listFiles();
69         if (fileArray == null) {
70             throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
71         }
72         sourceFiles.addAll(Arrays.asList(fileArray));
73         return sourceFiles;
74     }
75
76     private static void deleteTestDir(final File file) {
77         if (file.isDirectory()) {
78             File[] filesToDelete = file.listFiles();
79             if (filesToDelete != null) {
80                 for (File f : filesToDelete) {
81                     deleteTestDir(f);
82                 }
83             }
84         }
85         if (!file.delete()) {
86             throw new RuntimeException("Failed to clean up after test");
87         }
88     }
89
90 }