identity ref issue fix in yang tools
Change-Id: If2d722fd98b714b879a82b7f66e586f31835ac4f
diff --git a/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTranslatorTest.java b/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTranslatorTest.java
index bb02522..2835a64 100644
--- a/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTranslatorTest.java
+++ b/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/IdentityTranslatorTest.java
@@ -18,6 +18,10 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;
+import org.onosproject.yang.compiler.datamodel.YangIdentity;
+import org.onosproject.yang.compiler.datamodel.YangModule;
+import org.onosproject.yang.compiler.datamodel.YangNode;
+import org.onosproject.yang.compiler.linker.impl.YangLinkerManager;
import org.onosproject.yang.compiler.utils.io.YangPluginConfig;
import org.onosproject.yang.compiler.utils.io.impl.YangFileScanner;
import org.onosproject.yang.compiler.utils.io.impl.YangIoUtils;
@@ -26,12 +30,20 @@
import java.io.File;
import java.io.IOException;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yang.compiler.datamodel.YangNodeType.MODULE_NODE;
+import static org.onosproject.yang.compiler.linker.impl.YangLinkerUtils.updateFilePriority;
+import static org.onosproject.yang.compiler.utils.io.YangPluginConfig.compileCode;
+import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.deleteDirectory;
+
/**
* Translator test case for identity.
*/
public class IdentityTranslatorTest {
private final YangUtilManager utilManager = new YangUtilManager();
+ private final YangLinkerManager yangLinkerManager = new YangLinkerManager();
private static final String DIR = "target/identity/";
private static final String COMP = System.getProperty("user.dir") + File
.separator + DIR;
@@ -44,7 +56,7 @@
@Test
public void processTranslator() throws IOException,
ParserException, MojoExecutionException {
- YangIoUtils.deleteDirectory(DIR);
+ deleteDirectory(DIR);
String searchDir = "src/test/resources/identityTranslator";
utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
utilManager.parseYangFileInfoSet();
@@ -54,7 +66,63 @@
YangPluginConfig yangPluginConfig = new YangPluginConfig();
yangPluginConfig.setCodeGenDir(DIR);
utilManager.translateToJava(yangPluginConfig);
- YangPluginConfig.compileCode(COMP);
- YangIoUtils.deleteDirectory(DIR);
+ compileCode(COMP);
+ deleteDirectory(DIR);
+ }
+
+ /**
+ * Checks translation should not result in any exception.
+ *
+ * @throws MojoExecutionException
+ */
+ @Test
+ public void processMultipleLevelIdentity() throws IOException,
+ ParserException, MojoExecutionException {
+ deleteDirectory(DIR);
+ String searchDir = "src/test/resources/multipleIdentity";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.createYangNodeSet();
+
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangNodeSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangNodeSet());
+
+ updateFilePriority(utilManager.getYangNodeSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangNodeSet());
+
+ for (YangNode rootNode : utilManager.getYangNodeSet()) {
+ if (rootNode.getName().equals("test")) {
+ selfNode = rootNode;
+ }
+ }
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(selfNode instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(selfNode.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) selfNode;
+ assertThat(yangNode.getName(), is("test"));
+
+ YangIdentity id = ((YangIdentity) yangNode.getChild());
+ assertThat(id.getName(), is("identity3"));
+
+ assertThat(id.getExtendList().get(0).getName(), is("identity2"));
+ assertThat(id.getExtendList().get(1).getName(), is("identity1"));
+
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir(DIR);
+ utilManager.translateToJava(yangPluginConfig);
+ compileCode(COMP);
+ deleteDirectory(DIR);
}
}
diff --git a/compiler/plugin/maven/src/test/resources/multipleIdentity/test.yang b/compiler/plugin/maven/src/test/resources/multipleIdentity/test.yang
new file mode 100644
index 0000000..05b4875
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/multipleIdentity/test.yang
@@ -0,0 +1,21 @@
+module test {
+ namespace "identitytest";
+ prefix test ;
+
+ identity identity3;
+
+ identity identity2 {
+ base "identity3";
+ }
+
+ identity identity1 {
+ base "identity2";
+ }
+ container node {
+ leaf node-id{
+ type identityref {
+ base "identity1";
+ }
+ }
+ }
+}
\ No newline at end of file