$YangModuleInfoImpl not generated for only extensions model
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / 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.test;
9
10 import static org.junit.Assert.assertTrue;
11
12 import com.google.common.collect.ImmutableSet;
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.net.URISyntaxException;
19 import java.nio.charset.StandardCharsets;
20 import java.util.List;
21 import java.util.Optional;
22 import java.util.function.Supplier;
23 import org.junit.BeforeClass;
24 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator;
25 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
26 import org.opendaylight.mdsal.binding.java.api.generator.YangModuleInfoTemplate;
27 import org.opendaylight.mdsal.binding.model.api.Type;
28 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
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<Type> types, final File sourcesOutputDir)
45             throws IOException {
46         types.sort((o1, o2) -> o2.getName().compareTo(o1.getName()));
47
48         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
49         final Table<?, String, Supplier<String>> generatedFiles = generator.generateFileContent(true);
50         for (Cell<?, String, Supplier<String>> cell : generatedFiles.cellSet()) {
51             final File target = new File(sourcesOutputDir, cell.getColumnKey());
52             Files.createParentDirs(target);
53             Files.asCharSink(target, StandardCharsets.UTF_8).write(cell.getValue().get());
54         }
55     }
56
57     protected static final List<Type> generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
58             throws IOException, URISyntaxException {
59         final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
60         final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
61         final List<Type> types = new DefaultBindingGenerator().generateTypes(context);
62         generateTestSources(types, sourcesOutputDir);
63
64         // Also generate YangModuleInfo
65         for (Module module : context.getModules()) {
66             final YangModuleInfoTemplate template = new YangModuleInfoTemplate(module, context,
67                 mod -> Optional.of("fake/" + mod.getName()));
68
69             final File file = new File(GeneratorJavaFile.packageToDirectory(sourcesOutputDir,
70                 BindingMapping.getRootPackageName(module.getQNameModule())),
71                 BindingMapping.MODULE_INFO_CLASS_NAME + ".java");
72             Files.createParentDirs(file);
73             Files.asCharSink(file, StandardCharsets.UTF_8).write(template.generate());
74         }
75
76         return types;
77     }
78 }