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