Changed $Yang* generation package and name
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / BaseCompilationTest.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.java.api.generator;
9
10 import static java.nio.file.Files.newOutputStream;
11 import static org.junit.Assert.assertTrue;
12
13 import com.google.common.collect.Table;
14 import com.google.common.collect.Table.Cell;
15 import com.google.common.io.Files;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.net.URISyntaxException;
20 import java.nio.charset.StandardCharsets;
21 import java.util.List;
22 import java.util.Optional;
23 import org.junit.BeforeClass;
24 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
26 import org.opendaylight.yangtools.plugin.generator.api.GeneratedFile;
27 import org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath;
28 import org.opendaylight.yangtools.yang.binding.contract.Naming;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32
33 public abstract class BaseCompilationTest {
34
35     @BeforeClass
36     public static void createTestDirs() {
37         if (CompilationTestUtils.TEST_DIR.exists()) {
38             CompilationTestUtils.deleteTestDir(CompilationTestUtils.TEST_DIR);
39         }
40         assertTrue(CompilationTestUtils.GENERATOR_OUTPUT_DIR.mkdirs());
41         assertTrue(CompilationTestUtils.COMPILER_OUTPUT_DIR.mkdirs());
42     }
43
44     protected static final void generateTestSources(final List<GeneratedType> types, final File sourcesOutputDir)
45             throws IOException {
46         types.sort((o1, o2) -> o2.getName().compareTo(o1.getName()));
47
48         final Table<?, GeneratedFilePath, GeneratedFile> generatedFiles = JavaFileGenerator.generateFiles(types, true);
49         for (Cell<?, GeneratedFilePath, GeneratedFile> cell : generatedFiles.cellSet()) {
50             final File target = new File(sourcesOutputDir, cell.getColumnKey().getPath());
51             Files.createParentDirs(target);
52
53             try (OutputStream os = newOutputStream(target.toPath())) {
54                 cell.getValue().writeBody(os);
55             }
56         }
57     }
58
59     protected static final List<GeneratedType> generateTestSources(final String resourceDirPath,
60             final File sourcesOutputDir) throws IOException, URISyntaxException {
61         final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
62         final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
63         final List<GeneratedType> types = new DefaultBindingGenerator().generateTypes(context);
64         generateTestSources(types, sourcesOutputDir);
65
66         // Also generate YangModuleInfo
67         for (Module module : context.getModules()) {
68             final YangModuleInfoTemplate template = new YangModuleInfoTemplate(module, context,
69                 mod -> Optional.of("fake/" + mod.getName()));
70
71             final File file = new File(new File(sourcesOutputDir,
72                 Naming.getServicePackageName(module.getQNameModule()).replace('.', File.separatorChar)),
73                 Naming.MODULE_INFO_CLASS_NAME + ".java");
74             Files.createParentDirs(file);
75             Files.asCharSink(file, StandardCharsets.UTF_8).write(template.generate());
76         }
77
78         return types;
79     }
80 }