Merge branch 'blueprint' from controller
[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.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
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 SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
56         final Set<Module> modules = context.getModules();
57         final BasicCodeGenerator generator = new WadlGenerator();
58         Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modules,
59             module -> Optional.empty());
60         assertEquals(3, generatedWadlFiles.size());
61         generatedWadlFiles.forEach(file -> assertTrue(file.exists()));
62     }
63
64     @Test
65     public void testListGenerationWithoutPath() throws Exception {
66         final List<File> sourceFiles = getSourceFiles("/wadl-gen");
67         final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
68         final Set<Module> modules = context.getModules();
69         final BasicCodeGenerator generator = new WadlGenerator();
70         Collection<File> generatedWadlFiles = generator.generateSources(context, null, modules,
71             module -> Optional.empty());
72         assertEquals(3, generatedWadlFiles.size());
73         generatedWadlFiles.forEach(file -> {
74             deleteTestDir(file);
75             assertFalse(file.exists());
76         });
77     }
78
79     private static List<File> getSourceFiles(final String path) throws Exception {
80         final URI resPath = WadlGenTest.class.getResource(path).toURI();
81         final File sourcesDir = new File(resPath);
82         if (sourcesDir.exists()) {
83             final List<File> sourceFiles = new ArrayList<>();
84             final File[] fileArray = sourcesDir.listFiles();
85             if (fileArray == null) {
86                 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
87             }
88             sourceFiles.addAll(Arrays.asList(fileArray));
89             return sourceFiles;
90         } else {
91             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
92         }
93     }
94
95     private static void deleteTestDir(final File file) {
96         if (file.isDirectory()) {
97             File[] filesToDelete = file.listFiles();
98             if (filesToDelete != null) {
99                 for (File f : filesToDelete) {
100                     deleteTestDir(f);
101                 }
102             }
103         }
104         if (!file.delete()) {
105             throw new RuntimeException("Failed to clean up after test");
106         }
107     }
108 }