ffe2c80fbafd94ee8f15681f7bb92b3e7d6110a8
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / test / UnionTypedefUnusedImportTest.java
1 /*
2  * Copyright (c) 2014 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.yangtools.sal.java.api.generator.test;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
13 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.FS;
14 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
15 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
16 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
17 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.URISyntaxException;
21 import java.nio.charset.Charset;
22 import java.nio.charset.StandardCharsets;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.util.HashSet;
26 import java.util.List;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.sal.binding.model.api.Type;
29 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31
32 public class UnionTypedefUnusedImportTest extends BaseCompilationTest {
33
34     @Test
35     public void testUnionTypedefUnusedImport() throws Exception {
36         final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "union-typedef");
37         assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
38         final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "union-typedef");
39         assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
40
41         final List<File> sourceFiles = getSourceFiles("/compilation/union-typedef");
42         final SchemaContext context = parser.parseFiles(sourceFiles);
43         final List<Type> types = bindingGenerator.generateTypes(context);
44         final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
45         generator.generateToFile(sourcesOutputDir);
46         final boolean isUsedImport = containsImport("org.opendaylight.yang.gen.v1.org.opendaylight.yangtools.union.typedef.rev130208.TypedefUnion");
47         assertFalse(String.format("Class shouldn't contain import for this type '%s'", types.get(1).getName()),
48                 isUsedImport);
49
50         testCompilation(sourcesOutputDir, compiledOutputDir);
51         cleanUp(sourcesOutputDir, compiledOutputDir);
52     }
53
54     private static String readFile(final String path, final Charset encoding) throws IOException {
55         byte[] encoded = Files.readAllBytes(Paths.get(path));
56         return new String(encoded, encoding);
57     }
58
59     private static boolean containsImport(final String fullImport) throws URISyntaxException, IOException {
60         final String filePath = GENERATOR_OUTPUT_PATH + FS + "union-typedef" + FS + "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1" + FS + "org" + FS + "opendaylight" + FS + "yangtools" + FS + "union" + FS + "typedef" + FS + "rev141124" + FS + "TypedefUnionBuilder.java";
61         final String fileContent = readFile(filePath, StandardCharsets.UTF_8);
62
63         if (fileContent.contains(fullImport)) {
64             return true;
65         }
66         return false;
67     }
68 }