Make Binding(Runtime)Generator a proper service
[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.ServiceLoader;
23 import java.util.function.Supplier;
24 import org.junit.BeforeClass;
25 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
26 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
27 import org.opendaylight.mdsal.binding.java.api.generator.YangModuleInfoTemplate;
28 import org.opendaylight.mdsal.binding.model.api.Type;
29 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33
34 public abstract class BaseCompilationTest {
35
36     private static BindingGenerator BINDING_GENERATOR;
37
38     @BeforeClass
39     public static void createTestDirs() {
40         if (CompilationTestUtils.TEST_DIR.exists()) {
41             CompilationTestUtils.deleteTestDir(CompilationTestUtils.TEST_DIR);
42         }
43         assertTrue(CompilationTestUtils.GENERATOR_OUTPUT_DIR.mkdirs());
44         assertTrue(CompilationTestUtils.COMPILER_OUTPUT_DIR.mkdirs());
45
46         BINDING_GENERATOR = ServiceLoader.load(BindingGenerator.class).findFirst().orElseThrow();
47     }
48
49     protected static final void generateTestSources(final List<Type> types, final File sourcesOutputDir)
50             throws IOException {
51         types.sort((o1, o2) -> o2.getName().compareTo(o1.getName()));
52
53         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
54         final Table<?, String, Supplier<String>> generatedFiles = generator.generateFileContent(true);
55         for (Cell<?, String, Supplier<String>> cell : generatedFiles.cellSet()) {
56             final File target = new File(sourcesOutputDir, cell.getColumnKey());
57             Files.createParentDirs(target);
58             Files.asCharSink(target, StandardCharsets.UTF_8).write(cell.getValue().get());
59         }
60     }
61
62     protected static final List<Type> generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
63             throws IOException, URISyntaxException {
64         final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
65         final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
66         final List<Type> types = BINDING_GENERATOR.generateTypes(context);
67         generateTestSources(types, sourcesOutputDir);
68
69         // Also generate YangModuleInfo
70         for (Module module : context.getModules()) {
71             final YangModuleInfoTemplate template = new YangModuleInfoTemplate(module, context,
72                 mod -> Optional.of("fake/" + mod.getName()));
73
74             final File file = new File(GeneratorJavaFile.packageToDirectory(sourcesOutputDir,
75                 BindingMapping.getRootPackageName(module.getQNameModule())),
76                 BindingMapping.MODULE_INFO_CLASS_NAME + ".java");
77             Files.asCharSink(file, StandardCharsets.UTF_8).write(template.generate());
78         }
79
80         return types;
81     }
82 }