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 / wadl / generator / maven / WadlGenTest.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.wadl.generator.maven;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.net.URI;
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;
22 import java.util.Set;
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;
29
30 public class WadlGenTest {
31
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);
35
36     @Before
37     public void init() {
38         if (GENERATOR_OUTPUT_DIR.exists()) {
39             deleteTestDir(GENERATOR_OUTPUT_DIR);
40         }
41         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
42     }
43
44     @After
45     public void cleanUp() {
46         if (GENERATOR_OUTPUT_DIR.exists()) {
47             deleteTestDir(GENERATOR_OUTPUT_DIR);
48         }
49     }
50
51     @Test
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()));
61     }
62
63     @Test
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 -> {
73             deleteTestDir(file);
74             assertFalse(file.exists());
75         });
76     }
77
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() + ")");
83         }
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);
88         }
89         sourceFiles.addAll(Arrays.asList(fileArray));
90         return sourceFiles;
91     }
92
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) {
98                     deleteTestDir(f);
99                 }
100             }
101         }
102         if (!file.delete()) {
103             throw new RuntimeException("Failed to clean up after test");
104         }
105     }
106 }