Adjust to CodeGenerator using EffectiveModelContext
[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.model.api.Module;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
30
31 public class WadlGenTest {
32
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);
36
37     @Before
38     public void init() {
39         if (GENERATOR_OUTPUT_DIR.exists()) {
40             deleteTestDir(GENERATOR_OUTPUT_DIR);
41         }
42         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
43     }
44
45     @After
46     public void cleanUp() {
47         if (GENERATOR_OUTPUT_DIR.exists()) {
48             deleteTestDir(GENERATOR_OUTPUT_DIR);
49         }
50     }
51
52     @Test
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()));
63     }
64
65     @Test
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 -> {
76             deleteTestDir(file);
77             assertFalse(file.exists());
78         });
79     }
80
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() + ")");
86         }
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);
91         }
92         sourceFiles.addAll(Arrays.asList(fileArray));
93         return sourceFiles;
94     }
95
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) {
101                     deleteTestDir(f);
102                 }
103             }
104         }
105         if (!file.delete()) {
106             throw new RuntimeException("Failed to clean up after test");
107         }
108     }
109 }