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