2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.BASE_PKG;
13 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.cleanUp;
14 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.getSourceFiles;
15 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.Lists;
20 import java.io.IOException;
21 import java.lang.reflect.Constructor;
22 import java.net.URISyntaxException;
24 import java.net.URLClassLoader;
25 import java.util.List;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
29 import org.opendaylight.mdsal.binding.model.api.Type;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 * Test correct functionality of copy constructor of generated builder classes.
36 public class Bug532Test extends BaseCompilationTest {
39 public void test() throws Exception {
40 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug532");
41 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug532");
42 generateTestSources("/compilation/list-gen-test", sourcesOutputDir);
44 // Test if sources are compilable
45 testCompilation(sourcesOutputDir, compiledOutputDir);
47 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
48 Class<?> linksKeyClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
49 Class<?> linksClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Links", true, loader);
50 Class<?> linksBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.LinksBuilder", true,
52 Class<?> levelClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.links.Level", true, loader);
53 Class<?> nodeClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.links.Node", true, loader);
54 Class<?> nodeListClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.links.NodeList", true,
57 // init default values
58 Byte expectedId = Byte.valueOf("5");
59 String expectedName = "test-link";
60 Integer expectedSize = 10;
63 Object expectedLevel = Mockito.mock(levelClass);
64 Integer expectedLinksId = 11;
65 Object expectedNode = Mockito.mock(nodeClass);
66 List<?> expectedNodeList = Lists.newArrayList(Mockito.mock(nodeListClass), Mockito.mock(nodeListClass));
67 Constructor<?> keyConstructor = linksKeyClass.getDeclaredConstructor(Byte.class, String.class, Integer.class);
68 Object expectedKey = keyConstructor.newInstance(expectedId, expectedName, expectedSize);
70 // create Links object
71 Object linksBuilder = linksBuilderClass.newInstance();
72 linksBuilderClass.getDeclaredMethod("setKey", linksKeyClass).invoke(linksBuilder, expectedKey);
73 linksBuilderClass.getDeclaredMethod("setLevel", levelClass).invoke(linksBuilder, expectedLevel);
74 linksBuilderClass.getDeclaredMethod("setLinksId", Integer.class).invoke(linksBuilder, expectedLinksId);
75 linksBuilderClass.getDeclaredMethod("setNode", nodeClass).invoke(linksBuilder, expectedNode);
76 linksBuilderClass.getDeclaredMethod("setNodeList", List.class).invoke(linksBuilder, expectedNodeList);
77 Object links = linksBuilderClass.getDeclaredMethod("build").invoke(linksBuilder);
79 // create LinksBuilder object with constructor with Links object
81 Constructor<?> linksBuilderConstructor = linksBuilderClass.getDeclaredConstructor(linksClass);
82 assertNotNull(linksBuilderConstructor);
83 Object linksBuilderTested = linksBuilderConstructor.newInstance(links);
85 // get values from LinksBuilder
86 Object actualKey = linksBuilderClass.getDeclaredMethod("getKey").invoke(linksBuilderTested);
87 Object actualId = linksBuilderClass.getDeclaredMethod("getId").invoke(linksBuilderTested);
88 Object actualName = linksBuilderClass.getDeclaredMethod("getName").invoke(linksBuilderTested);
89 Object actualSize = linksBuilderClass.getDeclaredMethod("getSize").invoke(linksBuilderTested);
90 Object actualLevel = linksBuilderClass.getDeclaredMethod("getLevel").invoke(linksBuilderTested);
91 Object actualLinksId = linksBuilderClass.getDeclaredMethod("getLinksId").invoke(linksBuilderTested);
92 Object actualNode = linksBuilderClass.getDeclaredMethod("getNode").invoke(linksBuilderTested);
93 Object actualNodeList = linksBuilderClass.getDeclaredMethod("getNodeList").invoke(linksBuilderTested);
96 assertEquals(expectedKey, actualKey);
97 assertEquals(expectedId, actualId);
98 assertEquals(expectedName, actualName);
99 assertEquals(expectedSize, actualSize);
100 assertEquals(expectedLevel, actualLevel);
101 assertEquals(expectedLinksId, actualLinksId);
102 assertEquals(expectedNode, actualNode);
103 assertEquals(expectedNodeList, actualNodeList);
105 cleanUp(sourcesOutputDir, compiledOutputDir);
108 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
109 throws IOException, URISyntaxException {
110 final List<File> sourceFiles = getSourceFiles(resourceDirPath);
111 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
112 final List<Type> types = bindingGenerator.generateTypes(context);
113 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
114 generator.generateToFile(sourcesOutputDir);