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