[ONOS-4350] Inter Jar dependency implementation and code restructuring.
Change-Id: Iacac75e4187aed93ce1754c170a9c19707e5b8c3
diff --git a/plugin/src/test/java/org/onosproject/yangutils/ietfyang/IetfYangFileTest.java b/plugin/src/test/java/org/onosproject/yangutils/ietfyang/IetfYangFileTest.java
new file mode 100644
index 0000000..0f0a514
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/ietfyang/IetfYangFileTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.ietfyang;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.Test;
+import org.onosproject.yangutils.linker.impl.YangLinkerManager;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.plugin.manager.YangUtilManager;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
+
+import java.io.IOException;
+
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Test cases for testing IETF YANG files.
+ */
+public class IetfYangFileTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+ private final YangUtilManager utilManager = new YangUtilManager();
+ private final YangLinkerManager yangLinkerManager = new YangLinkerManager();
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ * Reference: https://datatracker.ietf.org/doc/draft-zha-l3sm-l3vpn-onos-deployment
+ */
+ @Test
+ public void l3vpnserviceyang()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/ietfyang/l3vpnservice";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.resolveDependenciesUsingLinker();
+
+ String userDir = System.getProperty("user.dir");
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/ietfyang/l3vpnservice/");
+
+ utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/ietfyang/");
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/linker/InterFileLinkingTest.java b/plugin/src/test/java/org/onosproject/yangutils/linker/InterFileLinkingTest.java
new file mode 100644
index 0000000..fd7b8ad
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/linker/InterFileLinkingTest.java
@@ -0,0 +1,741 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.linker;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.ListIterator;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangUses;
+import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
+import org.onosproject.yangutils.linker.impl.YangLinkerManager;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.plugin.manager.YangFileInfo;
+import org.onosproject.yangutils.plugin.manager.YangUtilManager;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
+import static org.onosproject.yangutils.datamodel.YangNodeType.MODULE_NODE;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Test cases for testing inter file linking.
+ */
+public class InterFileLinkingTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+ private final YangUtilManager utilManager = new YangUtilManager();
+ private final YangLinkerManager yangLinkerManager = new YangLinkerManager();
+
+ /**
+ * Checks inter file type linking.
+ */
+ @Test
+ public void processInterFileTypeLinking()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfiletype";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ if (yangFileInfo.getRootNode().getName().equals("module1")) {
+ selfNode = yangFileInfo.getRootNode();
+ refNode = yangFileInfoIterator.next().getRootNode();
+ } else {
+ refNode = yangFileInfo.getRootNode();
+ selfNode = yangFileInfoIterator.next().getRootNode();
+ }
+
+ // 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("module1"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) refNode.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks inter file uses linking.
+ */
+ @Test
+ public void processInterFileUsesLinking()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfileuses";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ if (yangFileInfo.getRootNode().getName().equals("module1")) {
+ selfNode = yangFileInfo.getRootNode();
+ refNode = yangFileInfoIterator.next().getRootNode();
+ } else {
+ refNode = yangFileInfo.getRootNode();
+ selfNode = yangFileInfoIterator.next().getRootNode();
+ }
+
+ // 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(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) selfNode;
+ assertThat(yangNode.getName(), is("module1"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the sibling of module's child.
+ assertThat((refNode.getChild() instanceof YangGrouping), is(true));
+
+ YangGrouping grouping = (YangGrouping) refNode.getChild();
+ leafIterator = grouping.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether uses is module's child.
+ assertThat((yangNode.getChild() instanceof YangUses), is(true));
+ YangUses uses = (YangUses) yangNode.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+// leafIterator = yangNode.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under module.
+// assertThat(leafInfo.getName(), is("hello"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ }
+
+ /**
+ * Checks inter file type linking with include list.
+ */
+ @Test
+ public void processInterFileTypeLinkingWithIncludeList()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfiletypewithinclude";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Carry out linking of sub module with module.
+ yangLinkerManager.linkSubModulesToParentModule(utilManager.getYangFileInfoSet());
+
+ // Add reference to include list.
+ yangLinkerManager.addRefToYangFilesIncludeList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ if (yangFileInfo.getRootNode().getName().equals("module1")) {
+ selfNode = yangFileInfo.getRootNode();
+ refNode = yangFileInfoIterator.next().getRootNode();
+ } else {
+ refNode = yangFileInfo.getRootNode();
+ selfNode = yangFileInfoIterator.next().getRootNode();
+ }
+
+ // 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("module1"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) refNode.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks inter file uses linking with include list.
+ */
+ @Test
+ public void processInterFileUsesLinkingWithInclude()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfileuseswithinclude";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Carry out linking of sub module with module.
+ yangLinkerManager.linkSubModulesToParentModule(utilManager.getYangFileInfoSet());
+
+ // Add reference to include list.
+ yangLinkerManager.addRefToYangFilesIncludeList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ if (yangFileInfo.getRootNode().getName().equals("module1")) {
+ selfNode = yangFileInfo.getRootNode();
+ refNode = yangFileInfoIterator.next().getRootNode();
+ } else {
+ refNode = yangFileInfo.getRootNode();
+ selfNode = yangFileInfoIterator.next().getRootNode();
+ }
+
+ // 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(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) selfNode;
+ assertThat(yangNode.getName(), is("module1"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the sibling of module's child.
+ assertThat((refNode.getChild() instanceof YangGrouping), is(true));
+
+ YangGrouping grouping = (YangGrouping) refNode.getChild();
+ leafIterator = grouping.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether uses is module's child.
+ assertThat((yangNode.getChild() instanceof YangUses), is(true));
+ YangUses uses = (YangUses) yangNode.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+// leafIterator = yangNode.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under module.
+// assertThat(leafInfo.getName(), is("hello"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ }
+
+ /**
+ * Checks inter file type linking with revision.
+ */
+ @Test
+ public void processInterFileTypeLinkingWithRevision()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfiletypewithrevision";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ if (yangFileInfo.getRootNode().getName().equals("module1")) {
+ selfNode = yangFileInfo.getRootNode();
+ refNode = yangFileInfoIterator.next().getRootNode();
+ } else {
+ refNode = yangFileInfo.getRootNode();
+ selfNode = yangFileInfoIterator.next().getRootNode();
+ }
+
+ // 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("module1"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) refNode.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks inter file type linking with revision in name.
+ */
+ @Test
+ public void processInterFileTypeLinkingWithRevisionInName()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfiletypewithrevisioninname";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ if (yangFileInfo.getRootNode().getName().equals("module1")) {
+ selfNode = yangFileInfo.getRootNode();
+ refNode = yangFileInfoIterator.next().getRootNode();
+ } else {
+ refNode = yangFileInfo.getRootNode();
+ selfNode = yangFileInfoIterator.next().getRootNode();
+ }
+
+ // 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("module1"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) refNode.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks hierarchical inter file type linking.
+ */
+ @Test
+ public void processHierarchicalInterFileTypeLinking()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/hierarchicalinterfiletype";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode1 = null;
+ YangNode refNode2 = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ for (YangFileInfo yangFile : utilManager.getYangFileInfoSet()) {
+ if (yangFile.getRootNode().getName().equals("ietf-network-topology")) {
+ selfNode = yangFile.getRootNode();
+ } else if (yangFile.getRootNode().getName().equals("ietf-network")) {
+ refNode1 = yangFile.getRootNode();
+ } else {
+ refNode2 = yangFile.getRootNode();
+ }
+ }
+
+ // 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("ietf-network-topology"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("source-node"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("node-id"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) refNode1.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ */
+ @Test
+ public void processHierarchicalIntraWithInterFileTypeLinking()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/hierarchicalintrawithinterfiletype";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+
+ YangNode refNode1 = null;
+ YangNode selfNode = null;
+
+ // Create YANG node set
+ yangLinkerManager.createYangNodeSet(utilManager.getYangFileInfoSet());
+
+ // Add references to import list.
+ yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangFileInfoSet());
+
+ // Carry out inter-file linking.
+ yangLinkerManager.processInterFileLinking(utilManager.getYangFileInfoSet());
+
+ for (YangFileInfo yangFile : utilManager.getYangFileInfoSet()) {
+ if (yangFile.getRootNode().getName().equals("ietf-network")) {
+ selfNode = yangFile.getRootNode();
+ } else if (yangFile.getRootNode().getName().equals("ietf-inet-types")) {
+ refNode1 = yangFile.getRootNode();
+ }
+ }
+
+ // 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("ietf-network"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("node-ref"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("node-id"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) selfNode.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ */
+ @Test
+ public void interFileWithUsesReferringType()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfilewithusesreferringtype";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.resolveDependenciesUsingLinker();
+
+ String userDir = System.getProperty("user.dir");
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/interfilewithusesreferringtype/");
+
+ utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/interfilewithusesreferringtype/");
+
+ }
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ */
+ @Test
+ public void file1UsesFile2TypeDefFile3Type()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/file1UsesFile2TypeDefFile3Type";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.resolveDependenciesUsingLinker();
+
+ String userDir = System.getProperty("user.dir");
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/file1UsesFile2TypeDefFile3Type/");
+
+ utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/file1UsesFile2TypeDefFile3Type/");
+
+ }
+
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ */
+ @Test
+ public void interFileIetf()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/interfileietf";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.resolveDependenciesUsingLinker();
+
+ String userDir = System.getProperty("user.dir");
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/interfileietf/");
+
+ utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/interfileietf/");
+
+ }
+
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ */
+ @Test
+ public void usesInContainer()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/usesInContainer";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.resolveDependenciesUsingLinker();
+
+ String userDir = System.getProperty("user.dir");
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/usesInContainer/");
+
+ utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/usesInContainer/");
+
+ }
+
+
+ /**
+ * Checks hierarchical intra with inter file type linking.
+ */
+ @Test
+ public void groupingNodeSameAsModule()
+ throws IOException, ParserException, MojoExecutionException {
+
+ String searchDir = "src/test/resources/groupingNodeSameAsModule";
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+ utilManager.parseYangFileInfoSet();
+ utilManager.resolveDependenciesUsingLinker();
+
+ String userDir = System.getProperty("user.dir");
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/groupingNodeSameAsModule/");
+
+ utilManager.translateToJava(utilManager.getYangFileInfoSet(), yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/groupingNodeSameAsModule/");
+
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/linker/InterJarLinkingTest.java b/plugin/src/test/java/org/onosproject/yangutils/linker/InterJarLinkingTest.java
new file mode 100644
index 0000000..39b03fe
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/linker/InterJarLinkingTest.java
@@ -0,0 +1,217 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.linker;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.plugin.manager.YangFileInfo;
+import org.onosproject.yangutils.plugin.manager.YangUtilManager;
+import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deSerializeDataModel;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.parseJarFile;
+
+/**
+ * Unit test case for inter-jar linker.
+ */
+public class InterJarLinkingTest {
+
+ private final YangUtilManager utilManager = new YangUtilManager();
+
+ private static final String TARGET = "target/interJarFileLinking";
+ private static final String SEARCH_DIR_FOR_YANG_FILES = "src/test/resources/interJarFileLinking/yangFiles";
+ private static final String SEARCH_DIR_FOR_SINGLE_JAR_FILES = "src/test/resources/interJarFileLinking/"
+ + "jarFiles/single";
+ private static final String SEARCH_DIR_FOR_MULTI_JAR_FILES = "src/test/resources/interJarFileLinking/"
+ + "jarFiles/multi";
+
+ /**
+ * Unit test case for a single jar dependency.
+ *
+ * @throws IOException when fails to do IO operations
+ * @throws MojoExecutionException when fails to do mojo operations
+ */
+ @Test
+ public void processSingleJarLinking()
+ throws IOException, MojoExecutionException {
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(SEARCH_DIR_FOR_YANG_FILES));
+
+ int size1 = utilManager.getYangFileInfoSet().size();
+
+ for (String file : getListOfTestJar(SEARCH_DIR_FOR_SINGLE_JAR_FILES)) {
+ addInterJarRootNodes(file);
+ }
+
+ utilManager.parseYangFileInfoSet();
+
+ utilManager.resolveDependenciesUsingLinker();
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ int size2 = utilManager.getYangFileInfoSet().size();
+ assertThat(true, is(size1 != size2));
+ assertThat(true, is(yangFileInfo.getRootNode().getName().equals("port-pair")));
+
+ deleteDirectory(TARGET);
+
+ }
+
+ /**
+ * Unit test case for a multiple jar dependency.
+ *
+ * @throws IOException when fails to do IO operations
+ * @throws MojoExecutionException when fails to do mojo operations
+ */
+ @Test
+ public void processMultipleJarLinking()
+ throws IOException, MojoExecutionException {
+ utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(SEARCH_DIR_FOR_YANG_FILES));
+
+ int size1 = utilManager.getYangFileInfoSet().size();
+
+ for (String file : getListOfTestJar(SEARCH_DIR_FOR_MULTI_JAR_FILES)) {
+ addInterJarRootNodes(file);
+ }
+
+ utilManager.parseYangFileInfoSet();
+
+ utilManager.resolveDependenciesUsingLinker();
+
+ Iterator<YangFileInfo> yangFileInfoIterator = utilManager.getYangFileInfoSet().iterator();
+
+ YangFileInfo yangFileInfo = yangFileInfoIterator.next();
+
+ int size2 = utilManager.getYangFileInfoSet().size();
+ assertThat(true, is(size1 != size2));
+ assertThat(true, is(yangFileInfo.getRootNode().getName().equals("port-pair")));
+
+ yangFileInfo = yangFileInfoIterator.next();
+ assertThat(true, is(yangFileInfo.getRootNode().getName().equals("flow-classifier")));
+
+ /*
+ * grouping flow-classifier {
+ * container flow-classifier {
+ * leaf id {
+ * type flow-classifier-id;
+ * }
+ *
+ * leaf tenant-id {
+ * type port-pair:tenant-id;
+ * }
+ * .
+ * .
+ * .
+ *
+ */
+
+ YangNode node = yangFileInfo.getRootNode();
+ node = node.getChild();
+ while (node != null) {
+ if (node instanceof YangGrouping) {
+ break;
+ }
+ node = node.getNextSibling();
+ }
+
+ node = node.getChild();
+ ListIterator<YangLeaf> leafIterator = ((YangContainer) node).getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("id"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("flow-classifier-id"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("tenant-id"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(true, is(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef()
+ .getName().equals("tenant-id")));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ deleteDirectory(TARGET);
+ }
+
+ /**
+ * Returns list of test jar files.
+ *
+ * @param searchdir search directory
+ * @return list of test jar files
+ */
+ private List<String> getListOfTestJar(String searchdir) {
+ List<String> jarFiles = new ArrayList<>();
+
+ File directory = new File(searchdir + "/");
+ File[] files = directory.listFiles();
+
+ for (File file : files) {
+ jarFiles.add(file.toString());
+ }
+
+ return jarFiles;
+ }
+
+ /**
+ * Adds data model nodes of jar to file info set.
+ *
+ * @param jarFile jar file name
+ * @throws IOException when fails to do IO operations
+ */
+ private void addInterJarRootNodes(String jarFile) throws IOException {
+ try {
+ List<YangNode> interJarResolvedNodes = deSerializeDataModel(parseJarFile(jarFile, TARGET));
+
+ for (YangNode node : interJarResolvedNodes) {
+ YangFileInfo dependentFileInfo = new YangFileInfo();
+ dependentFileInfo.setRootNode(node);
+ dependentFileInfo.setForTranslator(false);
+ dependentFileInfo.setYangFileName(node.getName());
+ utilManager.getYangFileInfoSet().add(dependentFileInfo);
+ }
+ } catch (IOException e) {
+ throw new IOException("failed to resolve in interjar scenario.");
+ }
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/linker/IntraFileTypeLinkingTest.java b/plugin/src/test/java/org/onosproject/yangutils/linker/IntraFileTypeLinkingTest.java
new file mode 100644
index 0000000..9fba40e
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/linker/IntraFileTypeLinkingTest.java
@@ -0,0 +1,565 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.linker;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.linker.exceptions.LinkerException;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
+import static org.onosproject.yangutils.datamodel.YangNodeType.MODULE_NODE;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.INTRA_FILE_RESOLVED;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
+
+/**
+ * Test cases for testing "type" intra file linking.
+ */
+public class IntraFileTypeLinkingTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks self resolution when typedef and leaf using type are siblings.
+ */
+ @Test
+ public void processSelfResolutionWhenTypeAndTypedefAtRootLevel()
+ throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SelfResolutionWhenTypeAndTypedefAtRootLevel.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when typedef and leaf using type are at different
+ * level where typedef is at the root.
+ */
+ @Test
+ public void processSelfFileLinkingTypedefAtRootTypeTwoLevelInHierarchy()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingTypedefAtRootTypeTwoLevelInHierarchy.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when typedef and leaf using type are at different
+ * level where typedef is at the root and defined after parent holder
+ * of type.
+ */
+ @Test
+ public void processSelfFileLinkingTypedefAtRootIsAfterContainerHavingType()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingTypedefAtRootIsAfterContainerHavingType.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild().getNextSibling()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when typedef and leaf using type are at different
+ * level where typedef is at the level of root+1 and defined after parent
+ * holder of type.
+ */
+ @Test
+ public void processSelfFileLinkingTypedefAtMiddleLevelAfterParentHolder()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingTypedefAtMiddleLevelAfterParentHolder.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangContainer.getChild().getNextSibling()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when typedef hierarchical references are present.
+ */
+ @Test
+ public void processSelfFileLinkingWithTypdefHierarchicalReference()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingWithTypdefHierarchicalReference.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("FirstClass"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangList.getChild()));
+ assertThat(leafInfo.getDataType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
+
+ assertThat(((YangDerivedInfo<?>) typeDef1.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangContainer.getChild().getNextSibling()));
+ assertThat(typeDef1.getTypeDefBaseType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
+
+ assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+ assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(INT32));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when typedef hierarchical references are present
+ * with last type is unresolved.
+ */
+ @Test
+ public void processSelfFileLinkingWithTypdefHierarchicalRefUnresolved()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingWithTypdefHierarchicalRefUnresolved.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("FirstClass"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangList.getChild()));
+ assertThat(leafInfo.getDataType().getResolvableStatus(),
+ is(INTRA_FILE_RESOLVED));
+
+ YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
+
+ assertThat(((YangDerivedInfo<?>) typeDef1.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangContainer.getChild().getNextSibling()));
+ assertThat(typeDef1.getTypeDefBaseType().getResolvableStatus(),
+ is(INTRA_FILE_RESOLVED));
+
+ YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
+
+ assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+ assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
+ is(INTRA_FILE_RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(nullValue()));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when type uses prefix of self module.
+ */
+ @Test
+ public void processSelfFileLinkingWithTypeWithSelfModulePrefix()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingWithTypeWithSelfModulePrefix.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("FirstClass"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangList.getChild()));
+ assertThat(leafInfo.getDataType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
+
+ assertThat(((YangDerivedInfo<?>) typeDef1.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangContainer.getChild().getNextSibling()));
+ assertThat(typeDef1.getTypeDefBaseType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
+
+ assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+ assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(INT32));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Checks self resolution when some type uses prefix of self module
+ * some uses external prefix.
+ */
+ @Test
+ public void processSelfFileLinkingWithTypeWithSelfAndExternalPrefixMix()
+ throws IOException, ParserException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingWithTypeWithSelfAndExternalPrefixMix.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("FirstClass"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) yangList.getChild()));
+ assertThat(leafInfo.getDataType().getResolvableStatus(),
+ is(INTRA_FILE_RESOLVED));
+
+ YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
+
+ YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
+
+ assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+ assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
+ is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(nullValue()));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+
+ /**
+ * Check self resolution when type referred typedef is not available in
+ * file.
+ */
+ @Test(expected = LinkerException.class)
+ public void processSelfResolutionWhenTypeReferredTypedefNotDefined()
+ throws IOException, LinkerException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfResolutionWhenTypeReferredTypedefNotDefined.yang");
+ }
+
+ /**
+ * Checks self resolution when typedef and leaf using type are at different
+ * level where typedef is is not an ancestor of type.
+ */
+ @Test(expected = LinkerException.class)
+ public void processSelfFileLinkingTypedefNotFound()
+ throws IOException, LinkerException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SelfFileLinkingTypedefNotFound.yang");
+ }
+
+ /**
+ * Checks hierarchical self resolution with self resolution failure scenario.
+ */
+ @Test(expected = LinkerException.class)
+ public void processSelfFileLinkingWithHierarchicalTypeFailureScenario()
+ throws IOException, LinkerException {
+
+ YangNode node =
+ manager.getDataModel("src/test/resources/SelfFileLinkingWithHierarchicalTypeFailureScenario.yang");
+ }
+
+ /**
+ * Checks self resolution when typedef and leaf using type are siblings for binary type.
+ */
+ @Test
+ public void processSelfResolutionWhenTypeAndTypedefAtRootLevelForBinary()
+ throws IOException, ParserException {
+
+ YangNode node
+ = manager.getDataModel("src/test/resources/SelfResolutionWhenTypeAndTypedefAtRootLevelForBinary.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("ospf"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("typedef14"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("type14"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(BINARY));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(nullValue()));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/linker/IntraFileUsesLinkingTest.java b/plugin/src/test/java/org/onosproject/yangutils/linker/IntraFileUsesLinkingTest.java
new file mode 100644
index 0000000..e960d62
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/linker/IntraFileUsesLinkingTest.java
@@ -0,0 +1,756 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.linker;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangUses;
+import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
+import org.onosproject.yangutils.linker.exceptions.LinkerException;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing uses intra file linking.
+ */
+public class IntraFileUsesLinkingTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks self resolution when grouping and uses are siblings.
+ * Grouping followed by uses.
+ */
+ @Test
+ public void processSelfResolutionWhenUsesAndGroupingAtRootLevel()
+ throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SelfResolutionWhenUsesAndGroupingAtRootLevel.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the sibling of module's child.
+ assertThat((yangNode.getChild().getNextSibling() instanceof YangGrouping), is(true));
+
+ YangGrouping grouping = (YangGrouping) yangNode.getChild().getNextSibling();
+ leafIterator = grouping.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether uses is module's child.
+ assertThat((yangNode.getChild() instanceof YangUses), is(true));
+ YangUses uses = (YangUses) yangNode.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+// leafIterator = yangNode.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under module.
+// assertThat(leafInfo.getName(), is("hello"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ }
+
+ /**
+ * Checks self resolution when grouping and uses are siblings.
+ * Grouping has a child node.
+ */
+ @Test
+ public void processSelfResolutionWhenUsesAndGroupingAtRootLevelGroupingWithChild()
+ throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel(
+ "src/test/resources/SelfResolutionWhenUsesAndGroupingAtRootLevelGroupingWithChild.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the sibling of module's child.
+ assertThat((yangNode.getChild().getNextSibling() instanceof YangGrouping), is(true));
+
+ YangGrouping grouping = (YangGrouping) yangNode.getChild().getNextSibling();
+ leafIterator = grouping.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("treat"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether container is the child of grouping.
+ assertThat((grouping.getChild() instanceof YangContainer), is(true));
+ YangContainer container = (YangContainer) grouping.getChild();
+
+ // Check whether the container name is set correctly which is under grouping.
+ assertThat(container.getName(), is("test"));
+
+ leafIterator = container.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under container which is under grouping.
+ assertThat(leafInfo.getName(), is("leaf2"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether uses is module's child.
+ assertThat((yangNode.getChild() instanceof YangUses), is(true));
+ YangUses uses = (YangUses) yangNode.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+// leafIterator = yangNode.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under module.
+// assertThat(leafInfo.getName(), is("treat"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+// // Check whether container is the child of module.
+// assertThat((grouping.getNextSibling() instanceof YangContainer), is(true));
+// container = (YangContainer) grouping.getNextSibling();
+//
+// // Check whether the container name is set correctly which is under module.
+// assertThat(container.getName(), is("test"));
+//
+// leafIterator = container.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under container which is under module.
+// assertThat(leafInfo.getName(), is("leaf2"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ }
+
+ /**
+ * Checks self resolution when grouping in rpc and uses in output of the same rpc.
+ * Uses is followed by grouping.
+ */
+ @Test
+ public void processSelfResolutionGroupingInRpcAndUsesInOutput()
+ throws IOException, ParserException {
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfResolutionGroupingInRpcAndUsesInOutput.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the child of rpc.
+ assertThat((yangNode.getChild().getChild() instanceof YangGrouping), is(true));
+ YangGrouping grouping = (YangGrouping) yangNode.getChild().getChild();
+
+ // Check whether the grouping name is set correctly.
+ assertThat(grouping.getName(), is("hello"));
+
+ // Check whether list is the child of grouping.
+ assertThat((grouping.getChild() instanceof YangList), is(true));
+ YangList yangListNode = (YangList) grouping.getChild();
+
+ // Check whether the list name is set correctly.
+ assertThat(yangListNode.getName(), is("valid"));
+
+ leafIterator = yangListNode.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under list which is under grouping.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+
+ // Check whether uses is input's child.
+ assertThat((yangNode.getChild().getChild().getNextSibling().getChild() instanceof YangUses), is(true));
+ YangUses uses = (YangUses) yangNode.getChild().getChild().getNextSibling().getChild();
+
+ // Check whether uses get resolved.
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+// // Check whether list is the sibling of uses which has been deep copied from grouping.
+// assertThat((yangNode.getChild().getChild().getNextSibling().getChild().getNextSibling() instanceof YangList),
+// is(true));
+// YangList yangList = (YangList) yangNode.getChild().getChild().getNextSibling().getChild().getNextSibling();
+//
+// // Check whether the list name is set correctly.
+// assertThat(yangList.getName(), is("valid"));
+//
+// leafIterator = yangList.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under list which is deep copied.
+// assertThat(leafInfo.getName(), is("invalid-interval"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+// assertThat(leafInfo.getUnits(), is("\"seconds\""));
+// assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+//
+// // Check whether uses is output's child.
+// assertThat((yangNode.getChild().getChild().getNextSibling().getNextSibling().getChild() instanceof YangUses),
+// is(true));
+// YangUses usesInOuput = (YangUses) yangNode.getChild().getChild().getNextSibling().getNextSibling().getChild();
+//
+// // Check whether uses get resolved.
+// assertThat(usesInOuput.getResolvableStatus(),
+// is(ResolvableStatus.RESOLVED));
+//
+// // Check whether list is the sibling of uses which has been deep copied from grouping.
+// assertThat((yangNode.getChild().getChild().getNextSibling().getChild().getNextSibling() instanceof YangList),
+// is(true));
+//
+// YangList yangListInOutput = (YangList) yangNode.getChild().getChild().getNextSibling().getNextSibling()
+// .getChild().getNextSibling();
+//
+// // Check whether the list name is set correctly.
+// assertThat(yangListInOutput.getName(), is("valid"));
+//
+// leafIterator = yangListInOutput.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under list which is deep copied.
+// assertThat(leafInfo.getName(), is("invalid-interval"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+// assertThat(leafInfo.getUnits(), is("\"seconds\""));
+// assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks the failure scenario when uses is referring to its own grouping directly.
+ */
+ @Test
+ public void processSelfResolutionGroupingReferencingItselfFailureScenerio()
+ throws IOException {
+
+ thrown.expect(LinkerException.class);
+ thrown.expectMessage(
+ "YANG file error: Duplicate input identifier detected, same as leaf \"zip-code\"");
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfResolutionGroupingReferencingItselfFailureScenerio.yang");
+
+ }
+
+ /**
+ * Checks the when multiple uses are present and are referred to the grouping at different levels.
+ */
+ @Test
+ public void processSelfResolutionGroupingWithMultipleUses()
+ throws IOException, ParserException {
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfResolutionGroupingWithMultipleUses.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the child of container.
+ assertThat((yangNode.getChild().getChild() instanceof YangGrouping), is(true));
+ YangGrouping grouping = (YangGrouping) yangNode.getChild().getChild();
+
+ // Check whether the grouping name is set correctly.
+ assertThat(grouping.getName(), is("endpoint"));
+
+ // Check whether uses is endpoint-grouping's child.
+ assertThat((grouping.getChild() instanceof YangUses), is(true));
+ YangUses firstUses = (YangUses) grouping.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(firstUses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check whether container is the sibling of uses.
+ assertThat((firstUses.getNextSibling() instanceof YangContainer), is(true));
+ YangContainer yangContainer = (YangContainer) firstUses.getNextSibling();
+
+ // Check whether the container name is set correctly.
+ assertThat(yangContainer.getName(), is("design"));
+
+// leafIterator = yangContainer.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under design-container.
+// assertThat(leafInfo.getName(), is("ink"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("int32"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+
+ // Check whether uses is design-container's child.
+ assertThat((yangContainer.getChild() instanceof YangUses), is(true));
+ YangUses secondUses = (YangUses) yangContainer.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(secondUses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check whether container is the sibling of uses.
+ assertThat((secondUses.getNextSibling() instanceof YangContainer), is(true));
+ YangContainer yangContainer2 = (YangContainer) secondUses.getNextSibling();
+ assertThat(yangContainer2.getName(), is("correct"));
+
+ leafIterator = yangContainer2.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under correct-container.
+ assertThat(leafInfo.getName(), is("newone"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether uses is correct container's child.
+ assertThat((yangContainer2.getChild() instanceof YangUses), is(true));
+ YangUses thirdUses = (YangUses) yangContainer2.getChild();
+
+ // Check whether uses get resolved.
+ assertThat(thirdUses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+// // Check whether container is the sibling of uses.
+// assertThat((thirdUses.getNextSibling() instanceof YangContainer), is(true));
+//
+// YangContainer yangContainer3 = (YangContainer) thirdUses.getNextSibling();
+// assertThat(yangContainer3.getName(), is("value"));
+//
+// leafIterator = yangContainer3.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under container
+// // which has been deep copied from grouping.
+// assertThat(leafInfo.getName(), is("zip-code"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+//
+// // Check whether uses is the sibling of container-design.
+// assertThat((yangContainer.getNextSibling() instanceof YangUses), is(true));
+// YangUses fourthUses = (YangUses) yangContainer.getNextSibling();
+//
+// // Check whether uses get resolved.
+// assertThat(fourthUses.getResolvableStatus(),
+// is(ResolvableStatus.RESOLVED));
+//
+// // Check whether uses is the sibling of previous uses.
+// assertThat((fourthUses.getNextSibling() instanceof YangUses), is(true));
+// YangUses fifthUses = (YangUses) fourthUses.getNextSibling();
+//
+// // Check whether uses get resolved.
+// assertThat(fifthUses.getResolvableStatus(),
+// is(ResolvableStatus.RESOLVED));
+//
+// // Check whether list is the sibling of uses.
+// assertThat((fifthUses.getNextSibling() instanceof YangList), is(true));
+// YangList yangList = (YangList) fifthUses.getNextSibling();
+// assertThat(yangList.getName(), is("valid"));
+//
+// leafIterator = yangList.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under list which has been deep copied from grouping.
+// assertThat(leafInfo.getName(), is("invalid-interval"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+// assertThat(leafInfo.getUnits(), is("\"seconds\""));
+// assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+//
+// // Check whether typedef is the sibling of list.
+// assertThat((yangList.getNextSibling() instanceof YangTypeDef), is(true));
+// YangTypeDef yangTypeDef = (YangTypeDef) yangList.getNextSibling();
+// assertThat(yangTypeDef.getName(), is("my-type"));
+//
+// leafIterator = grouping.getListOfLeaf().listIterator();
+// leafInfo = leafIterator.next();
+//
+// // Check whether the information in the leaf is correct under grouping.
+// assertThat(leafInfo.getName(), is("zip-code"));
+// assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+// assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+//
+// // Check whether uses is endpoint-grouping's sibling.
+// assertThat((grouping.getNextSibling() instanceof YangUses), is(true));
+// YangUses endpointUses = (YangUses) grouping.getNextSibling();
+//
+// // Check whether uses get resolved.
+// assertThat(endpointUses.getResolvableStatus(),
+// is(ResolvableStatus.RESOLVED));
+//
+// assertThat((endpointUses.getNextSibling().getNextSibling().getNextSibling().getNextSibling().getNextSibling()
+// .getNextSibling() instanceof YangUses), is(true));
+//
+// YangUses yangUsesInEndpoint = (YangUses) endpointUses.getNextSibling().getNextSibling().getNextSibling()
+// .getNextSibling().getNextSibling().getNextSibling();
+// assertThat(yangUsesInEndpoint.getResolvableStatus(),
+// is(ResolvableStatus.RESOLVED));
+//
+// assertThat((yangUsesInEndpoint.getNextSibling() instanceof YangContainer), is(true));
+// YangContainer yangContainerInEndPoint = (YangContainer) yangUsesInEndpoint.getNextSibling();
+//
+// assertThat(yangContainerInEndPoint.getName(), is("design"));
+ }
+
+ /**
+ * Checks the failure scenario when uses is present under the same node many times.
+ */
+ @Test
+ public void processSelfResolutionGroupingHavingSameUsesManyTimes()
+ throws IOException, ParserException {
+
+ thrown.expect(ParserException.class);
+ thrown.expectMessage(
+ "YANG file error: Duplicate input identifier detected, same as uses \"failure\"");
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfResolutionGroupingHavingSameUsesManyTimes.yang");
+ }
+
+ /**
+ * Checks the rpc having both typedef and grouping.
+ * It also checks that the grouping under different nodes will not give any problem in resolving uses.
+ */
+ @Test
+ public void processSelfResolutionRpcWithOneTypedefAndTwoGroupingUnderDifferentNode()
+ throws IOException, ParserException {
+
+ YangNode node = manager
+ .getDataModel(
+ "src/test/resources/SelfResolutionRpcWithOneTypedefAndTwoGroupingUnderDifferentNode.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+
+ // Check whether grouping is the child of input.
+ assertThat((yangNode.getChild().getChild().getChild() instanceof YangGrouping), is(true));
+ YangGrouping groupingUnderInput = (YangGrouping) yangNode.getChild().getChild().getChild();
+
+ // Check whether the grouping name is set correctly.
+ assertThat(groupingUnderInput.getName(), is("creative"));
+
+ leafIterator = groupingUnderInput.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("carry"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ // Check whether grouping is the child of output.
+ assertThat((yangNode.getChild().getChild().getNextSibling().getChild() instanceof YangGrouping), is(true));
+ YangGrouping grouping = (YangGrouping) yangNode.getChild().getChild().getNextSibling().getChild();
+ assertThat(grouping.getName(), is("creative"));
+
+ // Check whether typedef is the sibling of grouping.
+ assertThat((grouping.getNextSibling() instanceof YangTypeDef), is(true));
+
+ YangTypeDef typedef = (YangTypeDef) grouping.getNextSibling();
+ assertThat(typedef.getName(), is("my-type"));
+
+ // Check whether uses is the sibling of typedef.
+ assertThat((typedef.getNextSibling() instanceof YangUses), is(true));
+
+ // Check whether uses get resolved.
+ YangUses uses = (YangUses) typedef.getNextSibling();
+ assertThat(uses.getName(), is("creative"));
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+ }
+
+ /**
+ * Checks the failure scenario when uses is cannot resolve its grouping.
+ */
+ @Test
+ public void processSelfResolutionNestedGroupingWithUnresolvedUses()
+ throws IOException, LinkerException {
+
+ thrown.expect(LinkerException.class);
+ thrown.expectMessage(
+ "YANG file error: Unable to find base grouping for given uses");
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfResolutionNestedGroupingWithUnresolvedUses.yang");
+ }
+
+ /**
+ * Checks self resolution when typedef hierarchical references are present
+ * with last type is unresolved.
+ */
+ @Test
+ public void processSelfFileLinkingWithGroupingHierarchicalRefUnresolved()
+ throws IOException, ParserException {
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfFileLinkingWithGroupingHierarchicalRefUnresolved.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether container is the sibling of grouping.
+ assertThat((yangNode.getChild().getNextSibling() instanceof YangContainer), is(true));
+ YangContainer containerWithUses = (YangContainer) yangNode.getChild().getNextSibling();
+ assertThat(containerWithUses.getName(), is("test"));
+
+ // Check whether uses is the child of container.
+ assertThat((containerWithUses.getChild() instanceof YangUses), is(true));
+ YangUses uses = (YangUses) containerWithUses.getChild();
+ assertThat(uses.getName(), is("create"));
+
+ // Check whether uses is getting resolved.
+ assertThat(uses.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check whether grouping is the child of module.
+ assertThat((yangNode.getChild() instanceof YangGrouping), is(true));
+ YangGrouping groupingWithUses = (YangGrouping) yangNode.getChild();
+ assertThat(groupingWithUses.getName(), is("create"));
+
+ // Check whether uses with prefix from from other file, is the child of grouping.
+ assertThat((groupingWithUses.getChild() instanceof YangUses), is(true));
+ YangUses uses1 = (YangUses) groupingWithUses.getChild();
+ assertThat(uses1.getName(), is("valid"));
+
+ // Check whether this uses is getting intra-file-resolved.
+ assertThat(uses1.getResolvableStatus(),
+ is(ResolvableStatus.INTRA_FILE_RESOLVED));
+ }
+
+ /**
+ * Checks self resolution when uses has prefix of self module.
+ */
+ @Test
+ public void processSelfFileLinkingWithGroupingWithSelfModulePrefix()
+ throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SelfFileLinkingWithGroupingWithSelfModulePrefix.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether container is the sibling of grouping.
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ // Check whether list is the child of container.
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ // Check whether uses is the child of list.
+ assertThat((yangList.getChild() instanceof YangUses), is(true));
+ YangUses yangUses1 = (YangUses) yangList.getChild();
+ assertThat(yangUses1.getName(), is("FirstClass"));
+
+ // Check whether uses is getting resolved.
+ assertThat(yangUses1.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check whether grouping is the sibling of uses.
+ YangGrouping yangGrouping1 = (YangGrouping) yangUses1.getNextSibling();
+ assertThat(yangGrouping1.getName(), is("FirstClass"));
+
+ // Check whether uses is the child of grouping.
+ YangUses yangUses2 = (YangUses) yangGrouping1.getChild();
+ assertThat(yangUses2.getName(), is("PassingClass"));
+
+ // Check the uses gets resolved.
+ assertThat(yangUses2.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check whether grouping is the sibling of list.
+ YangGrouping yangGrouping2 = (YangGrouping) yangList.getNextSibling();
+ assertThat(yangGrouping2.getName(), is("PassingClass"));
+
+ // Check uses is the child of that grouping which has prefix of the same module.
+ YangUses yangUses3 = (YangUses) yangGrouping2.getChild();
+ assertThat(yangUses3.getName(), is("Percentage"));
+
+ // Check uses is getting resolved.
+ assertThat(yangUses3.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check grouping is the child of module.
+ YangGrouping yangGrouping3 = (YangGrouping) node.getChild();
+
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+ leafIterator = yangGrouping3.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+
+ }
+
+ /**
+ * Checks self resolution when some type uses prefix of self module
+ * some uses external prefix.
+ */
+ @Test
+ public void processSelfFileLinkingWithGroupingWithSelfAndExternalPrefixMix()
+ throws IOException, ParserException {
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/SelfFileLinkingWithGroupingWithSelfAndExternalPrefixMix.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether container is the sibling of grouping.
+ YangContainer yangContainer = (YangContainer) node.getChild().getNextSibling();
+
+ // Check whether list is the child of container.
+ YangList yangList = (YangList) yangContainer.getChild();
+
+ // Check whether uses is the child of list.
+ assertThat((yangList.getChild() instanceof YangUses), is(true));
+ YangUses yangUses1 = (YangUses) yangList.getChild();
+ assertThat(yangUses1.getName(), is("FirstClass"));
+
+ // Check whether uses is getting resolved.
+ assertThat(yangUses1.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check whether grouping is the sibling of uses.
+ YangGrouping yangGrouping1 = (YangGrouping) yangUses1.getNextSibling();
+ assertThat(yangGrouping1.getName(), is("FirstClass"));
+
+ // Check whether uses is the child of grouping which has prefix from other module.
+ YangUses yangUses2 = (YangUses) yangGrouping1.getChild();
+ assertThat(yangUses2.getName(), is("PassingClass"));
+
+ // Check whether uses gets intra-file-resolved.
+ assertThat(yangUses2.getResolvableStatus(),
+ is(ResolvableStatus.INTRA_FILE_RESOLVED));
+
+ // Check whether grouping is the sibling of list.
+ YangGrouping yangGrouping2 = (YangGrouping) yangList.getNextSibling();
+ assertThat(yangGrouping2.getName(), is("PassingClass"));
+
+ // Check uses is the child of that grouping which has prefix of the same module.
+ YangUses yangUses3 = (YangUses) yangGrouping2.getChild();
+ assertThat(yangUses3.getName(), is("Percentage"));
+
+ // Check uses is getting resolved.
+ assertThat(yangUses3.getResolvableStatus(),
+ is(ResolvableStatus.RESOLVED));
+
+ // Check grouping is the child of module.
+ YangGrouping yangGrouping3 = (YangGrouping) node.getChild();
+ ListIterator<YangLeaf> leafIterator;
+ YangLeaf leafInfo;
+ leafIterator = yangGrouping3.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+
+ // Check whether the information in the leaf is correct under grouping.
+ assertThat(leafInfo.getName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/linker/RestrictionResolutionTest.java b/plugin/src/test/java/org/onosproject/yangutils/linker/RestrictionResolutionTest.java
new file mode 100644
index 0000000..211f924
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/linker/RestrictionResolutionTest.java
@@ -0,0 +1,835 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.linker;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.ListIterator;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangPatternRestriction;
+import org.onosproject.yangutils.datamodel.YangRangeInterval;
+import org.onosproject.yangutils.datamodel.YangRangeRestriction;
+import org.onosproject.yangutils.datamodel.YangStringRestriction;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt32;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64;
+import org.onosproject.yangutils.linker.exceptions.LinkerException;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
+import static org.onosproject.yangutils.datamodel.YangNodeType.MODULE_NODE;
+import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
+
+/**
+ * Test cases for testing restriction resolution.
+ */
+public final class RestrictionResolutionTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks length restriction in typedef.
+ */
+ @Test
+ public void processLengthRestrictionInTypedef()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthRestrictionInTypedef.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+
+ /**
+ * Checks length restriction in referred type.
+ */
+ @Test
+ public void processLengthRestrictionInRefType()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthRestrictionInRefType.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(notNullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+
+ /**
+ * Checks length restriction in typedef and in type with stricter value.
+ */
+ @Test
+ public void processLengthRestrictionInTypedefAndTypeValid()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthRestrictionInTypedefAndTypeValid.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(notNullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval1 = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval1.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval1.getEndValue()).getValue(), is(BigInteger.valueOf(20)));
+
+ YangRangeInterval rangeInterval2 = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval2.getStartValue()).getValue(), is(BigInteger.valueOf(201)));
+ assertThat(((YangUint64) rangeInterval2.getEndValue()).getValue(), is(BigInteger.valueOf(300)));
+ }
+
+ /**
+ * Checks length restriction in typedef and in type with not stricter value.
+ */
+ @Test(expected = LinkerException.class)
+ public void processLengthRestrictionInTypedefAndTypeInValid()
+ throws IOException, DataModelException {
+ YangNode node = manager.getDataModel("src/test/resources/LengthRestrictionInTypedefAndTypeInValid.yang");
+ }
+
+ /**
+ * Checks range restriction in typedef.
+ */
+ @Test
+ public void processRangeRestrictionInTypedef()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInTypedef.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(INT32));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval1 = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval1.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval1.getEndValue()).getValue(), is(4));
+
+ YangRangeInterval rangeInterval2 = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval2.getStartValue()).getValue(), is(10));
+ assertThat(((YangInt32) rangeInterval2.getEndValue()).getValue(), is(20));
+ }
+
+ /**
+ * Checks range restriction in referred type.
+ */
+ @Test
+ public void processRangeRestrictionInRefType()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInRefType.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(INT32));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(notNullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval1 = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval1.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval1.getEndValue()).getValue(), is(4));
+
+ YangRangeInterval rangeInterval2 = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval2.getStartValue()).getValue(), is(10));
+ assertThat(((YangInt32) rangeInterval2.getEndValue()).getValue(), is(20));
+ }
+
+ /**
+ * Checks range restriction in typedef and stricter in referred type.
+ */
+ @Test
+ public void processRangeRestrictionInRefTypeAndTypedefValid()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInRefTypeAndTypedefValid.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(INT32));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(notNullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval1 = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval1.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval1.getEndValue()).getValue(), is(4));
+
+ YangRangeInterval rangeInterval2 = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval2.getStartValue()).getValue(), is(10));
+ assertThat(((YangInt32) rangeInterval2.getEndValue()).getValue(), is(20));
+ }
+
+ /**
+ * Checks range restriction in typedef and not stricter in referred type.
+ */
+ @Test(expected = LinkerException.class)
+ public void processRangeRestrictionInRefTypeAndTypedefInValid()
+ throws IOException, ParserException, DataModelException {
+ YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInRefTypeAndTypedefInValid.yang");
+ }
+
+ /**
+ * Checks range restriction for string.
+ */
+ @Test(expected = ParserException.class)
+ public void processRangeRestrictionInString()
+ throws IOException, ParserException, DataModelException {
+ YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInString.yang");
+ }
+
+ /**
+ * Checks range restriction for string in referred type.
+ */
+ @Test(expected = LinkerException.class)
+ public void processRangeRestrictionInStringInRefType()
+ throws IOException, DataModelException {
+ YangNode node = manager.getDataModel("src/test/resources/RangeRestrictionInStringInRefType.yang");
+ }
+
+ /**
+ * Checks pattern restriction in typedef.
+ */
+ @Test
+ public void processPatternRestrictionInTypedef()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternRestrictionInTypedef.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(nullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangPatternRestriction patternRestriction = stringRestriction.getPatternRestriction();
+
+ ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
+ String pattern1 = patternListIterator.next();
+
+ assertThat(pattern1, is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks pattern restriction in referred type.
+ */
+ @Test
+ public void processPatternRestrictionInRefType()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternRestrictionInRefType.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(notNullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangPatternRestriction patternRestriction = stringRestriction.getPatternRestriction();
+
+ ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
+ String pattern1 = patternListIterator.next();
+
+ assertThat(pattern1, is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks pattern restriction in referred type and typedef.
+ */
+ @Test
+ public void processPatternRestrictionInRefTypeAndTypedef()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternRestrictionInRefTypeAndTypedef.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(notNullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangPatternRestriction patternRestriction = stringRestriction.getPatternRestriction();
+
+ ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
+ String pattern1 = patternListIterator.next();
+
+ assertThat(pattern1, is("[a-zA-Z]"));
+
+ String pattern2 = patternListIterator.next();
+
+ assertThat(pattern2, is("[0-9]"));
+ }
+
+ /**
+ * Checks multiple pattern restriction in referred type and typedef.
+ */
+ @Test
+ public void processMultiplePatternRestriction()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MultiplePatternRestrictionInRefTypeAndTypedef.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(notNullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+ YangPatternRestriction patternRestriction = stringRestriction.getPatternRestriction();
+
+ ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
+ String pattern1 = patternListIterator.next();
+
+ assertThat(pattern1, is("[a-z]"));
+
+ String pattern2 = patternListIterator.next();
+
+ assertThat(pattern2, is("[A-Z]"));
+
+ String pattern3 = patternListIterator.next();
+
+ assertThat(pattern3, is("[0-9]"));
+
+ String pattern4 = patternListIterator.next();
+
+ assertThat(pattern4, is("[\\n]"));
+ }
+
+ /**
+ * Checks multiple pattern and length restriction in referred type and
+ * typedef.
+ */
+ @Test
+ public void processMultiplePatternAndLengthRestriction()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MultiplePatternAndLengthRestriction.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(notNullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(notNullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+
+ // Check for pattern restriction.
+ YangPatternRestriction patternRestriction = stringRestriction.getPatternRestriction();
+ ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
+ String pattern1 = patternListIterator.next();
+
+ assertThat(pattern1, is("[a-z]"));
+
+ String pattern2 = patternListIterator.next();
+
+ assertThat(pattern2, is("[A-Z]"));
+
+ String pattern3 = patternListIterator.next();
+
+ assertThat(pattern3, is("[0-9]"));
+
+ String pattern4 = patternListIterator.next();
+
+ assertThat(pattern4, is("[\\n]"));
+
+ // Check for length restriction.
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval1 = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval1.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval1.getEndValue()).getValue(), is(BigInteger.valueOf(20)));
+
+ YangRangeInterval rangeInterval2 = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval2.getStartValue()).getValue(), is(BigInteger.valueOf(201)));
+ assertThat(((YangUint64) rangeInterval2.getEndValue()).getValue(), is(BigInteger.valueOf(300)));
+ }
+
+ /**
+ * Checks multiple pattern and length restriction in referred type and
+ * typedef.
+ */
+ @Test
+ public void processMultiplePatternAndLengthRestrictionValid()
+ throws IOException, ParserException, DataModelException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MultiplePatternAndLengthRestrictionValid.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(DERIVED));
+
+ assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
+ is((YangTypeDef) node.getChild()));
+
+ assertThat(leafInfo.getDataType().getResolvableStatus(), is(RESOLVED));
+
+ YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ // Check for the effective built-in type.
+ assertThat(derivedInfo.getEffectiveBuiltInType(), is(STRING));
+
+ // Check for the restriction.
+ assertThat(derivedInfo.getLengthRestrictionString(), is(notNullValue()));
+ assertThat(derivedInfo.getRangeRestrictionString(), is(nullValue()));
+ assertThat(derivedInfo.getPatternRestriction(), is(notNullValue()));
+ assertThat(derivedInfo.getResolvedExtendedInfo(), is(notNullValue()));
+
+ // Check for the restriction value.
+ YangStringRestriction stringRestriction = (YangStringRestriction) derivedInfo.getResolvedExtendedInfo();
+
+ // Check for pattern restriction.
+ YangPatternRestriction patternRestriction = stringRestriction.getPatternRestriction();
+ ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
+ String pattern1 = patternListIterator.next();
+
+ assertThat(pattern1, is("[a-z]"));
+
+ String pattern2 = patternListIterator.next();
+
+ assertThat(pattern2, is("[A-Z]"));
+
+ String pattern3 = patternListIterator.next();
+
+ assertThat(pattern3, is("[0-9]"));
+
+ String pattern4 = patternListIterator.next();
+
+ assertThat(pattern4, is("[\\n]"));
+
+ // Check for length restriction.
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval1 = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval1.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval1.getEndValue()).getValue(), is(BigInteger.valueOf(20)));
+
+ YangRangeInterval rangeInterval2 = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval2.getStartValue()).getValue(), is(BigInteger.valueOf(100)));
+ assertThat(((YangUint64) rangeInterval2.getEndValue()).getValue(),
+ is(new BigInteger("18446744073709551615")));
+ }
+
+ /**
+ * Checks multiple pattern and length restriction in referred type and
+ * typedef invalid scenario.
+ */
+ @Test(expected = LinkerException.class)
+ public void processMultiplePatternAndLengthRestrictionInValid()
+ throws IOException, DataModelException {
+ YangNode node = manager.getDataModel("src/test/resources/MultiplePatternAndLengthRestrictionInValid.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/CustomExceptionMatcher.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/CustomExceptionMatcher.java
new file mode 100644
index 0000000..44d0c67
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/CustomExceptionMatcher.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl;
+
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+
+/**
+ * ExpectedException framework can use the Hamcrest matcher's to test
+ * custom/extended exceptions. This class extends the type safe matcher to
+ * define the custom exception matcher.
+ */
+public final class CustomExceptionMatcher extends TypeSafeMatcher<ParserException> {
+
+ private int actualLine;
+ private final int expectedLine;
+ private int actualCharPosition;
+ private final int expectedCharPosition;
+
+ /**
+ * Customized exception matcher to match error location.
+ *
+ * @param line error line
+ * @param charPosition error character position
+ * @return customized exception matcher to match error location
+ */
+ public static CustomExceptionMatcher errorLocation(int line, int charPosition) {
+ return new CustomExceptionMatcher(line, charPosition);
+ }
+
+ private CustomExceptionMatcher(int expectedLine, int expectedCharPosition) {
+ this.expectedLine = expectedLine;
+ this.expectedCharPosition = expectedCharPosition;
+ }
+
+ @Override
+ protected boolean matchesSafely(final ParserException exception) {
+ actualLine = exception.getLineNumber();
+ actualCharPosition = exception.getCharPositionInLine();
+ return ((actualLine == expectedLine) && (actualCharPosition == expectedCharPosition));
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(" Error reported location ")
+ .appendText("Line " + actualLine + ", " + "CharPosition " + actualCharPosition)
+ .appendText(" instead of expected ")
+ .appendText("Line " + expectedLine + ", " + "CharPosition " + expectedCharPosition);
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/TreeWalkListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/TreeWalkListenerTest.java
new file mode 100644
index 0000000..1056d98
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/TreeWalkListenerTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl;
+
+import java.io.IOException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+
+/**
+ * Test cases for testing tree walk listener functionality.
+ */
+public class TreeWalkListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Checks whether exception is thrown for ordered statement.
+ */
+ @Test
+ public void processOrderedByStatement() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : \"ordered-by\" is not supported in current version, please check wiki" +
+ " for YANG utils road map.");
+ manager.getDataModel("src/test/resources/OrderedByStatement.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown for anyxml statement.
+ */
+ @Test
+ public void processAnyXmlStatement() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : \"anyxml\" is not supported.");
+ manager.getDataModel("src/test/resources/AnyxmlStatement.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when extra brace is added in the EOF.
+ */
+ @Test
+ public void processFileWithExtraBrace() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input '}' expecting <EOF>");
+ manager.getDataModel("src/test/resources/ProcessFileWithExtraBrace.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf is given after module ends.
+ */
+ @Test
+ public void processFileWithExtraLeaf() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'leaf' expecting <EOF>");
+ manager.getDataModel("src/test/resources/ProcessFileWithExtraLeaf.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when extra brace is added in between the EOF.
+ */
+ @Test
+ public void processFileWithExtraBraceInBetween() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'container' expecting <EOF>");
+ manager.getDataModel("src/test/resources/ProcessFileWithExtraBraceInBetween.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/YangUtilsParserManagerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/YangUtilsParserManagerTest.java
new file mode 100644
index 0000000..9b3effc
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/YangUtilsParserManagerTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+/**
+ * Test case for testing YANG utils parser manager.
+ */
+public class YangUtilsParserManagerTest {
+
+ YangUtilsParserManager manager = new YangUtilsParserManager();
+ File file;
+ BufferedWriter out;
+
+ @Before
+ public void setUp() throws Exception {
+ file = new File("demo.yang");
+ out = new BufferedWriter(new FileWriter(file));
+ }
+ @After
+ public void tearDown() throws Exception {
+ file.delete();
+ }
+
+ /**
+ * This test case checks whether the null pointer exception is generated
+ * when the input YANG file is null.
+ */
+ @Test(expected = NullPointerException.class)
+ public void getDataModelNullFileTest() throws IOException, ParserException {
+ YangUtilsParserManager manager = new YangUtilsParserManager();
+ YangNode node = manager.getDataModel(null);
+ }
+
+ /**
+ * This test case checks whether the io exception is generated
+ * when the input YANG file is non existent.
+ */
+ @Test(expected = ParserException.class)
+ public void getDataModelNonExistentFileTest() throws IOException, ParserException {
+
+ YangUtilsParserManager manager = new YangUtilsParserManager();
+ YangNode node = manager.getDataModel("nonexistent.yang");
+ }
+
+ /**
+ * This test case checks if the input YANG file is correct no exception
+ * should be generated.
+ */
+ @Test
+ public void getDataModelCorrectFileTest() throws IOException, ParserException {
+
+ out.write("module ONOS {\n");
+ out.write("yang-version 1;\n");
+ out.write("namespace urn:ietf:params:xml:ns:yang:ietf-ospf;\n");
+ out.write("prefix On;\n");
+ out.write("}\n");
+ out.close();
+
+ YangNode node = manager.getDataModel("demo.yang");
+ }
+
+ /**
+ * This test case checks if the input YANG file with wrong YANG constructs
+ * than parser exception should be generated.
+ */
+ @Test(expected = ParserException.class)
+ public void getDataModelIncorrectFileTest() throws IOException, ParserException {
+
+ out.write("module ONOS {\n");
+ out.write("yang-version 1\n");
+ out.write("namespace urn:ietf:params:xml:ns:yang:ietf-ospf;\n");
+ out.write("prefix On;\n");
+ out.write("}\n");
+ out.close();
+
+ YangNode node = manager.getDataModel("demo.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/AugmentListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/AugmentListenerTest.java
new file mode 100644
index 0000000..3ce2fd0
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/AugmentListenerTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangAugment;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing augment listener functionality.
+ */
+public class AugmentListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid augment statement.
+ */
+ @Test
+ public void processValidAugmentStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidAugmentStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangAugment yangAugment = (YangAugment) yangNode.getChild();
+ ListIterator<YangNodeIdentifier> nodeIdentifierIterator = yangAugment.getTargetNode().listIterator();
+ YangNodeIdentifier yangNodeIdentifier = nodeIdentifierIterator.next();
+ assertThat(yangNodeIdentifier.getPrefix(), is("if"));
+ assertThat(yangNodeIdentifier.getName(), is("interfaces"));
+
+ ListIterator<YangLeaf> leafIterator = yangAugment.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("ds0ChannelNumber"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("ChannelNumber"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BaseFileListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BaseFileListenerTest.java
new file mode 100644
index 0000000..ee418c2
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BaseFileListenerTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+
+/**
+ * Test cases for testing base rule listener functionality.
+ */
+public class BaseFileListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Checks for exception if stack of parsable data is not empty at the entry
+ * of yang base rule.
+ */
+ @Test
+ public void processYangFileEntryNonEmptyStack() {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Invalid holder for yangbase before processing.");
+
+ YangModule tmpModule = new YangModule();
+ TreeWalkListener listener = new TreeWalkListener();
+ listener.getParsedDataStack().push(tmpModule);
+ GeneratedYangParser.YangfileContext ctx = null;
+ BaseFileListener.processYangFileEntry(listener, ctx);
+ }
+
+ /**
+ * Checks that exception shouldn't be generated if stack of parsable data is
+ * empty at the entry of yang base rule.
+ */
+ @Test
+ public void processYangFileEntryEmptyStack() {
+
+ TreeWalkListener listener = new TreeWalkListener();
+ GeneratedYangParser.YangfileContext ctx = null;
+ BaseFileListener.processYangFileEntry(listener, ctx);
+ }
+
+ /**
+ * Checks that exception should be generated if stack of parsable data is
+ * not empty at the exit of yang base rule.
+ */
+ @Test
+ public void processYangFileExitEmptyStack() {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Missing holder at yangbase after processing.");
+
+ TreeWalkListener listener = new TreeWalkListener();
+ GeneratedYangParser.YangfileContext ctx = null;
+ BaseFileListener.processYangFileExit(listener, ctx);
+ }
+
+ /**
+ * Checks that exception shouldn't be generated if stack of parsable data is
+ * empty at the exit of yang base rule.
+ */
+ @Test
+ public void processYangFileExitNonEmptyStack() {
+
+ TreeWalkListener listener = new TreeWalkListener();
+ GeneratedYangParser.YangfileContext ctx = null;
+ BaseFileListener.processYangFileEntry(listener, ctx);
+ }
+
+ /**
+ * Checks that after popping out the parsable node from stack it should be
+ * empty.
+ */
+ @Test
+ public void processYangFileExitStackErrorExtraEntryTest() {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Invalid holder for yangbase after processing.");
+
+ YangModule tmpModule = new YangModule();
+ YangModule tmpModule2 = new YangModule();
+ TreeWalkListener listener = new TreeWalkListener();
+ listener.getParsedDataStack().push(tmpModule);
+ listener.getParsedDataStack().push(tmpModule2);
+ GeneratedYangParser.YangfileContext ctx = null;
+ BaseFileListener.processYangFileExit(listener, ctx);
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BelongstoListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BelongstoListenerTest.java
new file mode 100644
index 0000000..927a2d6
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BelongstoListenerTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangSubModule;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing belongsto listener functionality.
+ */
+public class BelongstoListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if mandatory belongsto parameter "prefix" is not present.
+ */
+ @Test
+ public void processBelongsToWithoutPrefix() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input '}' expecting 'prefix'");
+ thrown.expect(CustomExceptionMatcher.errorLocation(4, 0));
+ YangNode node = manager.getDataModel("src/test/resources/BelongsToWithoutPrefix.yang");
+ }
+
+ /**
+ * Checks that prefix must be present only once in belongsto.
+ */
+ @Test
+ public void processBelongsToDualPrefix() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'prefix' expecting '}'");
+ thrown.expect(CustomExceptionMatcher.errorLocation(5, 0));
+ YangNode node = manager.getDataModel("src/test/resources/BelongsToDualPrefix.yang");
+ }
+
+ /**
+ * Checks if belongsto listener updates the date model tree.
+ */
+ @Test
+ public void processBelongsToWithPrefix() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/BelongsToWithPrefix.yang");
+ YangSubModule yangNode = (YangSubModule) node;
+ assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
+ }
+
+ /**
+ * Checks if mandatory parameter "belongsto" is present.
+ */
+ @Test
+ public void processSubModuleWithoutBelongsTo() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input '}' expecting 'belongs-to'");
+ thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
+ YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutBelongsTo.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java
new file mode 100644
index 0000000..6487172
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/BitListenerTest.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangBit;
+import org.onosproject.yangutils.datamodel.YangBits;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangUnion;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;
+
+/**
+ * Test cases for bit listener.
+ */
+public class BitListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks bit statement without position.
+ */
+ @Test
+ public void processBitTypeStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("mybits"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
+ assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
+ is("mybits"));
+
+ Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
+ for (YangBit tmp : bitSet) {
+ if (tmp.getBitName().equals("disable-nagle")) {
+ assertThat(tmp.getPosition(), is(0));
+ } else if (tmp.getBitName().equals("auto-sense-speed")) {
+ assertThat(tmp.getPosition(), is(1));
+ } else if (tmp.getBitName().equals("Ten-Mb-only")) {
+ assertThat(tmp.getPosition(), is(2));
+ }
+ }
+ }
+
+ /**
+ * Checks bit statement with typedef.
+ */
+ @Test
+ public void processBitTypedefStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/BitTypedefStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
+ assertThat(typedef.getName(), is("type15"));
+
+ YangType type = typedef.getTypeList().iterator().next();
+ assertThat(type.getDataType(), is(YangDataTypes.BITS));
+ assertThat(type.getDataTypeName(), is("bits"));
+ Set<YangBit> bitSet = ((YangBits) type.getDataTypeExtendedInfo()).getBitSet();
+ for (YangBit tmp : bitSet) {
+ if (tmp.getBitName().equals("disable-nagle")) {
+ assertThat(tmp.getPosition(), is(0));
+ } else if (tmp.getBitName().equals("auto-sense-speed")) {
+ assertThat(tmp.getPosition(), is(1));
+ }
+ }
+ }
+
+ /**
+ * Checks bit statement with union.
+ */
+ @Test
+ public void processBitUnionStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/BitUnionStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("type15"));
+
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
+
+ YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ List<YangType<?>> typeList = yangUnion.getTypeList();
+ ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
+ YangType<?> yangType = typeListIterator.next();
+
+ assertThat(yangType.getDataType(), is(YangDataTypes.BITS));
+ assertThat(yangType.getDataTypeName(), is("bits"));
+ Set<YangBit> bitSet = ((YangBits) yangType.getDataTypeExtendedInfo()).getBitSet();
+ for (YangBit tmp : bitSet) {
+ if (tmp.getBitName().equals("disable-nagle")) {
+ assertThat(tmp.getPosition(), is(0));
+ } else if (tmp.getBitName().equals("auto-sense-speed")) {
+ assertThat(tmp.getPosition(), is(1));
+ }
+ }
+ }
+
+ /**
+ * Checks if enum with same name is not allowed.
+ */
+ @Test(expected = ParserException.class)
+ public void processBitWithDuplicateName() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/CaseListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/CaseListenerTest.java
new file mode 100644
index 0000000..675a9e9
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/CaseListenerTest.java
@@ -0,0 +1,205 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangCase;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+/**
+ * Test cases for case listener.
+ */
+public class CaseListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks multiple case statement.
+ */
+ @Test
+ public void processCaseStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/CaseStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+
+ YangCase yangCase1 = (YangCase) yangChoice.getChild();
+ assertThat(yangCase1.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo1 = leafIterator1.next();
+
+ assertThat(leafInfo1.getName(), is("pretzel"));
+
+ YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
+ assertThat(yangCase2.getName(), is("late-night"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
+ YangLeaf leafInfo2 = leafIterator2.next();
+
+ assertThat(leafInfo2.getName(), is("chocolate"));
+ }
+
+ /**
+ * Checks duplicate case in choice.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateCaseInChoice() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateCaseInChoice.yang");
+ }
+
+ /**
+ * Checks duplicate leaf at different hierarchy.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateLeafInHierarchy() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInHierarchy.yang");
+ }
+
+ /**
+ * Checks duplicate leaf in different case within choice.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateLeafInChoice() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateLeafInChoice.yang");
+ }
+
+ /**
+ * Checks same case within different choice.
+ */
+ @Test
+ public void processCaseStatementSameEntryDifferentChoice() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/CaseStatementSameEntryDifferentChoice.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+
+ YangCase yangCase1 = (YangCase) yangChoice.getChild();
+ assertThat(yangCase1.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo1 = leafIterator1.next();
+
+ assertThat(leafInfo1.getName(), is("pretzel"));
+
+ YangChoice yangChoice2 = (YangChoice) yangChoice.getNextSibling();
+ assertThat(yangChoice2.getName(), is("lunch"));
+
+ YangCase yangCase2 = (YangCase) yangChoice2.getChild();
+ assertThat(yangCase2.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator2 = yangCase2.getListOfLeaf().listIterator();
+ YangLeaf leafInfo2 = leafIterator2.next();
+
+ assertThat(leafInfo2.getName(), is("chocolate"));
+ }
+
+ /**
+ * Checks case choice hierarchy.
+ */
+ @Test
+ public void processCaseChoiceHierarchy() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/CaseChoiceHierarchy.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice1 = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice1.getName(), is("snack"));
+
+ YangCase yangCase1 = (YangCase) yangChoice1.getChild();
+ assertThat(yangCase1.getName(), is("sports-arena"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator1 = yangCase1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo1 = leafIterator1.next();
+
+ assertThat(leafInfo1.getName(), is("pretzel"));
+
+ YangCase yangCase2 = (YangCase) yangCase1.getNextSibling();
+ assertThat(yangCase2.getName(), is("late-night"));
+
+ YangChoice yangChoice2 = (YangChoice) yangCase2.getChild();
+ assertThat(yangChoice2.getName(), is("dinner"));
+
+ YangCase yangCase3 = (YangCase) yangChoice2.getChild();
+ assertThat(yangCase3.getName(), is("late-night"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator2 = yangCase3.getListOfLeaf().listIterator();
+ YangLeaf leafInfo2 = leafIterator2.next();
+ assertThat(leafInfo2.getName(), is("beer"));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListenerTest.java
new file mode 100644
index 0000000..9dcf448
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ChoiceListenerTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+/**
+ * Test cases for choice listener.
+ */
+public class ChoiceListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks choice statement without body.
+ */
+ @Test
+ public void processChoiceStatementWithoutBody() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementWithoutBody.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+ }
+
+ /**
+ * Checks choice statement with stmt end.
+ */
+ @Test
+ public void processChoiceStatementWithStmtend() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementWithStmtend.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+ }
+
+ /**
+ * Checks choice statement duplicate entry.
+ */
+ @Test(expected = ParserException.class)
+ public void processChoiceStatementDuplicateEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementDuplicateEntry.yang");
+ }
+
+ /**
+ * Checks choice statement with same entry in two different container.
+ */
+ @Test
+ public void processChoiceStatementSameEntryDifferentContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementSameEntryDifferentContainer.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer1 = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer1.getName(), is("food1"));
+
+ YangChoice yangChoice1 = (YangChoice) yangContainer1.getChild();
+ assertThat(yangChoice1.getName(), is("snack"));
+
+ YangContainer yangContainer2 = (YangContainer) yangNode.getChild().getNextSibling();
+ assertThat(yangContainer2.getName(), is("food2"));
+
+ YangChoice yangChoice2 = (YangChoice) yangContainer2.getChild();
+ assertThat(yangChoice2.getName(), is("snack"));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java
new file mode 100644
index 0000000..d129e38
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java
@@ -0,0 +1,534 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for config listener.
+ */
+public class ConfigListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid config statement.
+ */
+ @Test
+ public void processConfigTrue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ConfigTrue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the Config value is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isConfig(), is(true));
+ }
+
+ /**
+ * Checks valid config statement.
+ */
+ @Test
+ public void processConfigFalse() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalse.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the Config value is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isConfig(), is(false));
+ }
+
+ /**
+ * Checks invalid config statement and expects parser exception.
+ */
+ @Test
+ public void processConfigWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing ';' at '}'");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks invalid config statement and expects parser exception.
+ */
+ @Test
+ public void processConfigInvalidValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : config value invalid is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigInvalidValue.yang");
+ }
+
+ /**
+ * Checks invalid config statement and expects parser exception.
+ */
+ @Test
+ public void processConfigEmptyValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("no viable alternative at input ';'");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigEmptyValue.yang");
+ }
+
+ /**
+ * Checks config statement as sub-statement of module.
+ */
+ @Test
+ public void processModuleSubStatementConfig() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'config' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ + "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ + " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ + " 'revision', 'rpc', 'typedef', 'uses', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementConfig.yang");
+ }
+
+ /**
+ * Checks config statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementConfig() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementConfig.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.isConfig(), is(true));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks config statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementConfig() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementConfig.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module and config value is set.
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.isConfig(), is(true));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks valid config statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementConfig() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementConfig.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether config value is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.isConfig(), is(true));
+ }
+
+ /**
+ * Checks config statement's default Value.
+ */
+ @Test
+ public void processConfigDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ConfigDefaultValue.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.isConfig(), is(true));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isConfig(), is(true));
+ }
+
+ /**
+ * Checks whether exception is throw when node's parent config set to false,
+ * no node underneath it can have config set to true.
+ */
+ @Test
+ public void processConfigFalseParentContainerChildLeafList() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Unhandled parsed data at container \"valid\" after "
+ + "processing.\nError Information: If a container has \"config\" set to \"false\", no node underneath "
+ + "it can have \"config\" set to \"true\".");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentContainerChildLeafList.yang");
+ }
+
+ /**
+ * Checks whether exception is throw when node's parent config set to false,
+ * no node underneath it can have config set to true.
+ */
+ @Test
+ public void processConfigFalseParentContainerChildLeaf() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Unhandled parsed data at container \"valid\" after "
+ + "processing.\nError Information: If a container has \"config\" set to \"false\", no node underneath "
+ + "it can have \"config\" set to \"true\".");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentContainerChildLeaf.yang");
+ }
+
+ /**
+ * Checks whether exception is throw when node's parent config set to false,
+ * no node underneath it can have config set to true.
+ */
+ @Test
+ public void processConfigFalseParentListChildLeafList() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Unhandled parsed data at list \"valid\" after"
+ + " processing.\nError Information: If a list has \"config\" set to \"false\", no node underneath"
+ + " it can have \"config\" set to \"true\".");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentListChildLeafList.yang");
+ }
+
+ /**
+ * Checks whether exception is throw when node's parent config set to false,
+ * no node underneath it can have config set to true.
+ */
+ @Test
+ public void processConfigFalseParentListChildLeaf() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("Internal parser error detected: Unhandled parsed data at list \"valid\" after"
+ + " processing.\nError Information: If a list has \"config\" set to \"false\", no node underneath"
+ + " it can have \"config\" set to \"true\".");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentListChildLeaf.yang");
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigContainerSubStatementContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementContainer.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("hello"));
+ assertThat(container.isConfig(), is(true));
+
+ YangNode containerNode = container.getChild();
+ assertThat(containerNode instanceof YangContainer, is(true));
+ YangContainer childContainer = (YangContainer) containerNode;
+ assertThat(childContainer.getName(), is("valid"));
+ assertThat(childContainer.isConfig(), is(true));
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigContainerSubStatementLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.isConfig(), is(true));
+
+ ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether config value is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.isConfig(), is(true));
+
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigContainerSubStatementLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementLeaf.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.isConfig(), is(true));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isConfig(), is(true));
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigContainerSubStatementList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("hello"));
+ assertThat(container.isConfig(), is(true));
+
+ YangNode listNode = container.getChild();
+ assertThat(listNode instanceof YangList, is(true));
+ YangList childList = (YangList) listNode;
+ assertThat(childList.getName(), is("valid"));
+ assertThat(childList.isConfig(), is(true));
+
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigListSubStatementContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementContainer.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangList list1 = (YangList) yangNode.getChild();
+ assertThat(list1.getName(), is("list1"));
+ assertThat(list1.isConfig(), is(true));
+
+ YangNode containerNode = list1.getChild();
+ assertThat(containerNode instanceof YangContainer, is(true));
+ YangContainer childContainer = (YangContainer) containerNode;
+ assertThat(childContainer.getName(), is("container1"));
+ assertThat(childContainer.isConfig(), is(true));
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigListSubStatementLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangList list1 = (YangList) yangNode.getChild();
+ assertThat(list1.getName(), is("valid"));
+ assertThat(list1.isConfig(), is(true));
+
+ ListIterator<YangLeafList> leafListIterator = list1.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether config value is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.isConfig(), is(true));
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigListSubStatementLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementLeaf.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangList list1 = (YangList) yangNode.getChild();
+ assertThat(list1.getName(), is("valid"));
+ assertThat(list1.isConfig(), is(true));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = list1.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isConfig(), is(true));
+ }
+
+ /**
+ * Checks when config is not specified, the default is same as the parent's schema node's
+ * config statement's value.
+ */
+ @Test
+ public void processNoConfigListSubStatementList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the config value is set correctly.
+ YangList list1 = (YangList) yangNode.getChild();
+ assertThat(list1.getName(), is("valid"));
+ assertThat(list1.isConfig(), is(true));
+
+ YangNode listNode = list1.getChild();
+ assertThat(listNode instanceof YangList, is(true));
+ YangList childList = (YangList) listNode;
+ assertThat(childList.getName(), is("list1"));
+ assertThat(childList.isConfig(), is(true));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ContactListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ContactListenerTest.java
new file mode 100644
index 0000000..7c8c5e9
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ContactListenerTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing contact listener functionality.
+ */
+public class ContactListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if contact listener updates the data model tree.
+ */
+ @Test
+ public void processContactValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContactValidEntry.yang");
+
+ // Checks for the contact value in data model tree.
+ assertThat(((YangModule) node).getContact(), is("\"WG List: <mailto:spring@ietf.org>\nEditor: "
+ + "Stephane Litkowski\n " + "<mailto:stephane.litkowski@orange.com>\""));
+ }
+
+ /**
+ * Checks that contact must be present only once.
+ */
+ @Test(expected = ParserException.class)
+ public void processContactDualEntryTest() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContactDualEntryTest.yang");
+
+ }
+
+ /**
+ * Checks that contact can have a string value without double quotes.
+ */
+ @Test
+ public void processContactWithoutQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContactWithoutQuotes.yang");
+
+ // Checks for the contact value in data model tree.
+ assertThat(((YangModule) node).getContact(), is("WG"));
+ }
+
+ /**
+ * Checks if contact is not empty.
+ */
+ @Test(expected = ParserException.class)
+ public void processContactWithEmptyString() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContactWithEmptyString.yang");
+ }
+
+ /**
+ * Checks that contact must be present after namespace.
+ */
+ @Test(expected = ParserException.class)
+ public void processContactIncorrectOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContactIncorrectOrder.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ContainerListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ContainerListenerTest.java
new file mode 100644
index 0000000..a149da2
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ContainerListenerTest.java
@@ -0,0 +1,232 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing container listener.
+ */
+public class ContainerListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks container statement as sub-statement of module.
+ */
+ @Test
+ public void processModuleSubStatementContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementContainer.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the container is child of module
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("valid"));
+ }
+
+ /**
+ * Checks if container identifier in module is duplicate.
+ */
+ @Test(expected = ParserException.class)
+ public void processModuleDuplicateContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleDuplicateContainer.yang");
+ }
+
+ /**
+ * Checks if container identifier in container is duplicate.
+ */
+ @Test(expected = ParserException.class)
+ public void processContainerDuplicateContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerDuplicateContainer.yang");
+ }
+
+ /**
+ * Checks if container identifier in list is duplicate.
+ */
+ @Test(expected = ParserException.class)
+ public void processListDuplicateContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListDuplicateContainer.yang");
+ }
+
+ /**
+ * Checks if container identifier collides with list at same level.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateContainerAndList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateContainerAndList.yang");
+ }
+
+ /**
+ * Checks container statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementContainer.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the container is child of module
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("ospf"));
+
+ // Check whether the container is child of container
+ YangContainer yangContainer1 = (YangContainer) yangContainer.getChild();
+ assertThat(yangContainer1.getName(), is("valid"));
+ }
+
+ /**
+ * Checks container statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementContainer.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList1 = (YangList) yangNode.getChild();
+ assertThat(yangList1.getName(), is("ospf"));
+
+ ListIterator<String> keyList = yangList1.getKeyList().listIterator();
+ assertThat(keyList.next(), is("process-id"));
+
+ // Check whether the list is child of list
+ YangContainer yangContainer = (YangContainer) yangList1.getChild();
+ assertThat(yangContainer.getName(), is("interface"));
+ }
+
+ /**
+ * Checks container with all its sub-statements.
+ */
+ @Test
+ public void processContainerSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the container is child of module
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+
+ // Check whether container properties as set correctly.
+ assertThat(yangContainer.getName(), is("ospf"));
+
+ assertThat(yangContainer.isConfig(), is(true));
+ assertThat(yangContainer.getPresence(), is("\"ospf logs\""));
+ assertThat(yangContainer.getDescription(), is("\"container description\""));
+ assertThat(yangContainer.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(yangContainer.getReference(), is("\"container reference\""));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangContainer.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks cardinality of sub-statements of container.
+ */
+ @Test
+ public void processContainerSubStatementCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"container valid\".");
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang");
+ }
+
+ /**
+ * Checks container as root node.
+ */
+ @Test
+ public void processContainerRootNode() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("no viable alternative at input 'container'");
+ YangNode node = manager.getDataModel("src/test/resources/ContainerRootNode.yang");
+ }
+
+ /**
+ * Checks invalid identifier for container statement.
+ */
+ @Test
+ public void processContainerInvalidIdentifier() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : container name 1valid is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/ContainerInvalidIdentifier.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
new file mode 100644
index 0000000..2aab556
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing default listener functionality.
+ */
+public class DefaultListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if default value is set correctly.
+ */
+ @Test
+ public void processLeafSubStatementDefault() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafSubStatementDefault.yang");
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDefaultValueInString(), is("\"1\""));
+ }
+
+ /**
+ * Checks if default value is set correctly.
+ */
+ @Test
+ public void processChoiceSubStatementDefault() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceSubStatementDefault.yang");
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+ assertThat(yangChoice.getDefaultValueInString(), is("\"hello\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DescriptionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DescriptionListenerTest.java
new file mode 100644
index 0000000..c3989a6
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DescriptionListenerTest.java
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for description listener.
+ */
+public class DescriptionListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid description statement.
+ */
+ @Test
+ public void processDescriptionValidStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DescriptionValidStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the description is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ }
+
+ /**
+ * Checks whether exception is thrown for invalid description statement.
+ */
+ @Test
+ public void processDescriptionWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("extraneous input '}' expecting {';', '+'}");
+ YangNode node = manager.getDataModel("src/test/resources/DescriptionWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks valid description statement as sub-statement of module.
+ */
+ @Test
+ public void processModuleSubStatementDescription() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementDescription.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the description is set correctly.
+ assertThat(yangNode.getDescription(), is("\"Interval before a route is declared invalid\""));
+ }
+
+ /**
+ * Checks valid description statement as sub-statement of module.
+ */
+ @Test
+ public void processDescriptionEmptyStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DescriptionEmptyStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the description is set correctly.
+ assertThat(yangNode.getDescription(), is("\"\""));
+ }
+
+ /**
+ * Checks valid description statement as sub-statement of revision.
+ */
+ @Test
+ public void processRevisionSubStatementRevision() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementRevision.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the description is set correctly.
+ assertThat(yangNode.getDescription(), is("\"module description\""));
+ assertThat(yangNode.getRevision().getDescription(), is("\"revision description\""));
+ }
+
+ /**
+ * Checks description statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementDescription() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementDescription.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the description value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.getDescription(), is("\"container description\""));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks description statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementDescription() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementDescription.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module and description value is set correctly.
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.isConfig(), is(true));
+ assertThat(yangList.getDescription(), is("\"list description\""));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks valid description statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementDescription() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementDescription.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether description value is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/EnumListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/EnumListenerTest.java
new file mode 100644
index 0000000..a0bff1b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/EnumListenerTest.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangEnum;
+import org.onosproject.yangutils.datamodel.YangEnumeration;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.ListIterator;
+import java.util.SortedSet;
+
+/**
+ * Test cases for enum listener.
+ */
+public class EnumListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks enum statement without value.
+ */
+ @Test
+ public void processEnumTypeStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/EnumTypeStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("speed"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
+ assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
+ is("speed_enum"));
+
+ SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
+ for (YangEnum tmp : enumSet) {
+ if (tmp.getNamedValue().equals("10m")) {
+ assertThat(tmp.getValue(), is(0));
+ } else if (tmp.getNamedValue().equals("100m")) {
+ assertThat(tmp.getValue(), is(1));
+ } else if (tmp.getNamedValue().equals("auto")) {
+ assertThat(tmp.getValue(), is(2));
+ }
+ }
+ }
+
+ /**
+ * Checks if enum with same name is not allowed.
+ */
+ @Test(expected = ParserException.class)
+ public void processEnumWithDuplicateName() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/EnumWithDuplicateName.yang");
+ }
+
+ /**
+ * Checks enum boundary value.
+ */
+ @Test
+ public void processEnumBoundaryValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : value value 21474836472147483647 is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/EnumBoundaryValue.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown if value is not specified following max enum value.
+ */
+ @Test
+ public void processEnumMaxNextValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : "
+ + "An enum value MUST be specified for enum substatements following the one"
+ + "with the current highest value");
+ YangNode node = manager.getDataModel("src/test/resources/EnumMaxNextValue.yang");
+ }
+
+ /**
+ * Checks enum values stored are sorted.
+ */
+ @Test
+ public void processEnumSorted() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/EnumSorted.yang");
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("ifType"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
+ assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
+ is("ifType_enum"));
+
+ SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
+ Iterator<YangEnum> enumIterator = enumSet.iterator();
+ assertThat(enumIterator.next().getNamedValue(), is("five"));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/GroupingListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/GroupingListenerTest.java
new file mode 100644
index 0000000..acd0c8a
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/GroupingListenerTest.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing grouping listener.
+ */
+public class GroupingListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks grouping statement inside module.
+ */
+ @Test
+ public void processGroupingInModule() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/GroupingInModule.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+
+ ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("address"));
+ }
+
+ /**
+ * Checks grouping statement inside container.
+ */
+ @Test
+ public void processGroupingInContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/GroupingInContainer.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("valid"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangContainer.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+
+ ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("address"));
+ }
+
+ /**
+ * Checks grouping statement inside list.
+ */
+ @Test
+ public void processGroupingInList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/GroupingInList.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangList.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+
+ ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("address"));
+ }
+
+ /**
+ * Checks grouping with attributes.
+ */
+ @Test
+ public void processGroupingAttributes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/GroupingAttributes.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangList.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+ assertThat(yangGrouping.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(yangGrouping.getReference(), is("\"RFC 6020\""));
+ assertThat(yangGrouping.getDescription(), is("\"grouping under test\""));
+
+ ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("address"));
+ }
+
+ /**
+ * Checks duplicate grouping in list.
+ */
+ @Test(expected = ParserException.class)
+ public void processDuplicateGroupingInList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInList.yang");
+ }
+
+ /**
+ * Checks duplicate grouping in container.
+ */
+ @Test (expected = ParserException.class)
+ public void processDuplicateGroupingInContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInContainer.yang");
+ }
+
+ /**
+ * Checks duplicate grouping in module.
+ */
+ @Test (expected = ParserException.class)
+ public void processDuplicateGroupingInModule() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInModule.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ImportListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ImportListenerTest.java
new file mode 100644
index 0000000..9797e5e
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ImportListenerTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing import listener functionality.
+ */
+public class ImportListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if mandatory parameter prefix is present in import.
+ */
+ @Test(expected = ParserException.class)
+ public void processImportWithoutPrefix() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ImportWithoutPrefix.yang");
+ }
+
+ /**
+ * Checks that prefix must be present only once in import.
+ */
+ @Test(expected = ParserException.class)
+ public void processImportWithDualPrefix() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ImportWithDualPrefix.yang");
+ }
+
+ /**
+ * Checks for the correct order of prefix in import.
+ */
+ @Test(expected = ParserException.class)
+ public void processImportInvalidOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ImportInvalidOrder.yang");
+ }
+
+ /**
+ * Checks if import listener updates the data model tree.
+ */
+ @Test
+ public void processImportValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ImportValidEntry.yang");
+
+ // Checks for the revision value in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
+ // Checks for the prefix id in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
+ // Checks for the module name in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
+ }
+
+ /**
+ * Checks if optional parameter revision is not mandatory in import.
+ */
+ @Test
+ public void processImportWithoutRevision() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ImportWithoutRevision.yang");
+
+ // Checks for the prefix id in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
+ // Checks for the module name in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
+ }
+
+ /**
+ * Checks if multiple imports are allowed.
+ */
+ @Test()
+ public void processImportMultipleInstance() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ImportMultipleInstance.yang");
+
+ // Checks for the prefix id in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
+ // Checks for the module name in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
+
+ // Checks for the prefix id in data model tree.
+ assertThat(((YangModule) node).getImportList().get(1).getPrefixId(), is("On3"));
+ // Checks for the module name in data model tree.
+ assertThat(((YangModule) node).getImportList().get(1).getModuleName(), is("itut"));
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/IncludeListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/IncludeListenerTest.java
new file mode 100644
index 0000000..452fc89
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/IncludeListenerTest.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing include listener functionality.
+ */
+public class IncludeListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if include listener with ; is valid and updates the data
+ * model tree.
+ */
+ @Test
+ public void processIncludeWithStmtend() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeWithStmtend.yang");
+
+ // Checks for the sub module name in data model tree.
+ assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
+ }
+
+ /**
+ * Checks if include listener with braces and without revision date is valid
+ * and updates the data model tree.
+ */
+ @Test
+ public void processIncludeWithEmptyBody() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeWithEmptyBody.yang");
+
+ // Checks for the sub module name in data model tree.
+ assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
+ }
+
+ /**
+ * Checks if include listener with braces and with revision date is valid
+ * and updates the data model tree.
+ */
+ @Test
+ public void processIncludeWithDate() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeWithDate.yang");
+
+ // Checks for the sub module name in data model tree.
+ assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
+ assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
+ }
+
+ /**
+ * Checks if include has more than one occurrence.
+ */
+ @Test
+ public void processIncludeMultiInstance() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeMultiInstance.yang");
+
+ // Checks for the sub module name in data model tree.
+ assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
+ assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
+ }
+
+ /**
+ * Checks if include and import can come in any order.
+ */
+ @Test
+ public void processIncludeImportAnyOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeImportAnyOrder.yang");
+
+ // Checks for the sub module name in data model tree.
+ assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
+ assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
+ }
+
+ /**
+ * Checks if syntax of Include is not correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processIncludeInvalidSyntax() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidSyntax.yang");
+ }
+
+ /**
+ * Checks if syntax of revision date in Include is not correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processIncludeInvalidDateSyntax() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidDateSyntax.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/InputListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/InputListenerTest.java
new file mode 100644
index 0000000..c85c1d5
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/InputListenerTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangRpc;
+import org.onosproject.yangutils.datamodel.YangInput;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing Input listener functionality.
+ */
+public class InputListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks input statements with data definition statements as sub-statements.
+ */
+ @Test
+ public void processInputStatementWithDataDefinition() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/InputStatementWithDataDefinition.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ YangRpc yangRpc = (YangRpc) yangNode.getChild();
+ assertThat(yangRpc.getName(), is("activate-software-image"));
+
+ YangInput yangInput = (YangInput) yangRpc.getChild();
+ assertThat(yangInput.getName(), is("activate-software-image_input"));
+ ListIterator<YangLeaf> leafIterator = yangInput.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("image-name"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+
+ YangList yangList = (YangList) yangInput.getChild();
+ assertThat(yangList.getName(), is("ospf"));
+ assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
+ assertThat(yangList.isConfig(), is(true));
+ assertThat(yangList.getMaxElements(), is(10));
+ assertThat(yangList.getMinElements(), is(3));
+ leafIterator = yangList.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+
+ YangContainer yangContainer = (YangContainer) yangList.getNextSibling();
+ assertThat(yangContainer.getName(), is("isis"));
+
+ leafIterator = yangContainer.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ }
+
+ /**
+ * Checks input statements with type-def statement as sub-statements.
+ */
+ @Test
+ public void processInputStatementWithTypedef() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/InputStatementWithTypedef.yang");
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ YangRpc yangRpc = (YangRpc) yangNode.getChild();
+ assertThat(yangRpc.getName(), is("activate-software-image"));
+
+ YangInput yangInput = (YangInput) yangRpc.getChild();
+ assertThat(yangInput.getName(), is("activate-software-image_input"));
+ YangTypeDef typeDef = (YangTypeDef) yangInput.getChild();
+ assertThat(typeDef.getName(), is("my-type"));
+ assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
+ assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.INT32));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java
new file mode 100644
index 0000000..510b313
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java
@@ -0,0 +1,264 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ListIterator;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for key listener.
+ */
+public class KeyListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks key statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementKey() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementKey.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<String> keyList = yangList.getKeyList().listIterator();
+ assertThat(keyList.next(), is("invalid-interval"));
+ }
+
+ /**
+ * Check multiple key values.
+ */
+ @Test
+ public void processMultipleKeyValues() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MultipleKeyValues.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ List<String> keyList = yangList.getKeyList();
+ assertThat(keyList.contains("ospf"), is(true));
+ assertThat(keyList.contains("isis"), is(true));
+ }
+
+ /**
+ * Checks key statement without statement end.
+ */
+ @Test
+ public void processKeyWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'leaf' expecting {';', '+'}");
+ YangNode node = manager.getDataModel("src/test/resources/KeyWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks key values are set correctly.
+ */
+ @Test
+ public void processConfigFalseNoKey() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseNoKey.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ }
+
+ /**
+ * Checks key values are set correctly.
+ */
+ @Test
+ public void processConfigFalseValidKeyValidLeaf() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseValidKeyValidLeaf.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<String> keyList = yangList.getKeyList().listIterator();
+ assertThat(keyList.next(), is("invalid-interval"));
+ }
+
+ /**
+ * Checks key values are set correctly.
+ */
+ @Test
+ public void processConfigFalseValidKeyValidLeafList() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/ConfigFalseValidKeyValidLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<String> keyList = yangList.getKeyList().listIterator();
+ assertThat(keyList.next(), is("invalid-interval"));
+ }
+
+ /**
+ * Checks whether exception is thrown when list's config is set to true and there is no key.
+ */
+ @Test
+ public void processConfigTrueNoKey() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("A list must have atleast one key leaf if config is true");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigTrueNoKey.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when list's config is set to true and there is no leaf.
+ */
+ @Test
+ public void processConfigTrueNoleafNoLeafList() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("A list must have atleast one key leaf if config is true");
+ YangNode node = manager.getDataModel("src/test/resources/ConfigTrueNoleafNoLeafList.yang");
+ }
+
+ /**
+ * Checks key values are set correctly.
+ */
+ @Test
+ public void processConfigTrueValidKeyValidLeaf() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/ConfigTrueValidKeyValidLeaf.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<String> keyList = yangList.getKeyList().listIterator();
+ assertThat(keyList.next(), is("invalid-interval"));
+ }
+
+ /**
+ * Checks key values are set correctly.
+ */
+ @Test
+ public void processKeyWithUsesInList() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/KeyWithUsesInList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild().getNextSibling();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<String> keyList = yangList.getKeyList().listIterator();
+ assertThat(keyList.next(), is("invalid-interval"));
+ }
+
+ /**
+ * Checks whether exception is thrown when key leaf identifier is not found in list.
+ */
+ @Test
+ public void processInvalidLeafIdentifier() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("An identifier, in key, must refer to a child leaf of the list");
+ YangNode node = manager.getDataModel("src/test/resources/InvalidLeafIdentifier.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when key leaf-list identifier is not found in list.
+ */
+ @Test
+ public void processInvalidLeafListIdentifier() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("An identifier, in key, must refer to a child leaf of the list");
+ YangNode node = manager.getDataModel("src/test/resources/InvalidLeafListIdentifier.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when key leaf-list is of type empty.
+ */
+ @Test
+ public void processKeyLeafListTypeEmpty() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("A leaf-list that is part of the key must not be the built-in type \"empty\".");
+ YangNode node = manager.getDataModel("src/test/resources/KeyLeafListTypeEmpty.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when key leaf is of type empty.
+ */
+ @Test
+ public void processKeyLeafTypeEmpty() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("A leaf that is part of the key must not be the built-in type \"empty\".");
+ YangNode node = manager.getDataModel("src/test/resources/KeyLeafTypeEmpty.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LeafListListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LeafListListenerTest.java
new file mode 100644
index 0000000..8e7489b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LeafListListenerTest.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing leaf-list listener.
+ */
+public class LeafListListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks all the values of leaf-list sub-statements are set correctly.
+ */
+ @Test
+ public void processLeafListSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatements.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafListInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafListInfo.isConfig(), is(true));
+ assertThat(leafListInfo.getMaxElelements(), is(3));
+ assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf-list identifier starts with
+ * digit.
+ */
+ @Test
+ public void processLeafListInvalidIdentifier() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : leaf-list name 1invalid-interval is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidIdentifier.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf-list keyword is incorrect.
+ */
+ @Test
+ public void processLeafListInvalidStatement() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'leaflist' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ + "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ + " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ + " 'revision', 'rpc', 'typedef', 'uses', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/LeafListInvalidStatement.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf-list keyword without Left
+ * brace as per grammar.
+ */
+ @Test
+ public void processLeafListWithoutLeftBrace() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing '{' at 'type'");
+ YangNode node = manager.getDataModel("src/test/resources/LeafListWithoutLeftBrace.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when config statement cardinality is
+ * not as per grammar.
+ */
+ @Test
+ public void processLeafListConfigInvalidCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf-list " +
+ "invalid-interval\".");
+ YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when units statement cardinality is
+ * not as per grammar.
+ */
+ @Test
+ public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf-list invalid-interval\"");
+ YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang");
+ }
+
+ /**
+ * Checks leaf-list statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeafList.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ //Check whether the container is child of module.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+
+ // Check whether leaf-list properties as set correctly.
+ ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafListInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafListInfo.isConfig(), is(true));
+ assertThat(leafListInfo.getMinElements(), is(1));
+ assertThat(leafListInfo.getMaxElelements(), is(2147483647));
+ assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks leaf-list statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ // Check whether leaf-list properties as set correctly.
+ ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafListInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafListInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafListInfo.isConfig(), is(true));
+
+ assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LeafListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LeafListenerTest.java
new file mode 100644
index 0000000..b330e53
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LeafListenerTest.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing leaf listener.
+ */
+public class LeafListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks all the values of leaf sub-statements are set correctly.
+ */
+ @Test
+ public void processLeafSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafSubStatements.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isConfig(), is(true));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf identifier starts with
+ * digit.
+ */
+ @Test
+ public void processLeafInvalidIdentifier() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : leaf name 1invalid-interval is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/LeafInvalidIdentifier.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf keyword is incorrect.
+ */
+ @Test
+ public void processLeafInvalidStatement() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'leafs' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ + "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ + " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ + " 'revision', 'rpc', 'typedef', 'uses', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/LeafInvalidStatement.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when leaf keyword without Left brace
+ * as per grammar.
+ */
+ @Test
+ public void processLeafWithoutLeftBrace() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing '{' at 'type'");
+ YangNode node = manager.getDataModel("src/test/resources/LeafWithoutLeftBrace.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when config statement cardinality is
+ * not as per grammar.
+ */
+ @Test
+ public void processLeafConfigInvalidCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf invalid-interval\".");
+ YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when mandatory statement cardinality
+ * is not as per grammar.
+ */
+ @Test
+ public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"mandatory\" is defined more than once in \"leaf invalid-interval\".");
+ YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang");
+ }
+
+ /**
+ * Checks leaf statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementLeaf.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ //Check whether the container is child of module.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isConfig(), is(true));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks duplicate leaf statement as sub-statement of module.
+ */
+ @Test(expected = ParserException.class)
+ public void processModuleWithDuplicateLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleWithDuplicateLeaf.yang");
+ }
+
+ /**
+ * Checks duplicate leaf statement as sub-statement of container.
+ */
+ @Test(expected = ParserException.class)
+ public void processContainerWithDuplicateLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerWithDuplicateLeaf.yang");
+ }
+
+ /**
+ * Checks duplicate leaf statement as sub-statement of list.
+ */
+ @Test(expected = ParserException.class)
+ public void processListWithDuplicateLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListWithDuplicateLeaf.yang");
+ }
+
+ /**
+ * Checks leaf statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementLeaf.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isConfig(), is(true));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java
new file mode 100644
index 0000000..0c862ba
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/LengthRestrictionListenerTest.java
@@ -0,0 +1,302 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangRangeInterval;
+import org.onosproject.yangutils.datamodel.YangRangeRestriction;
+import org.onosproject.yangutils.datamodel.YangStringRestriction;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for length restriction listener.
+ */
+public class LengthRestrictionListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid length statement as sub-statement of leaf statement.
+ */
+ @Test
+ public void processValidLengthStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidLengthStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+
+ /**
+ * Checks valid length statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processLengthStatementInsideLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthStatementInsideLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+
+ /**
+ * Checks valid length statement as sub-statement of typedef.
+ */
+ @Test
+ public void processLengthStatementInsideTypeDef() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthStatementInsideTypeDef.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
+ YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getTypeDefBaseType()
+ .getDataTypeExtendedInfo();
+
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+
+ /**
+ * Checks length statement with invalid type.
+ */
+ @Test
+ public void processLengthWithInvalidType() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : length name \"1..100\" can be used to restrict the built-in type" +
+ " string/binary or types derived from string/binary.");
+ YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidType.yang");
+ }
+
+ /**
+ * Checks length statement with only start interval.
+ */
+ @Test
+ public void processLengthWithOneInterval() throws IOException, ParserException {
+
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthWithOneInterval.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(1)));
+ }
+
+ /**
+ * Checks length statement with min and max.
+ */
+ @Test
+ public void processLengthWithMinMax() throws IOException, ParserException {
+
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthWithMinMax.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(new BigInteger("18446744073709551615")));
+ }
+
+ /**
+ * Checks length statement with invalid integer pattern.
+ */
+ @Test
+ public void processLengthWithInvalidIntegerPattern() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : Input value \"a\" is not a valid uint64.");
+ YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidIntegerPattern.yang");
+ }
+
+ /**
+ * Checks length statement with invalid interval.
+ */
+ @Test
+ public void processLengthWithInvalidInterval() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : 18446744073709551617 is greater than maximum value" +
+ " 18446744073709551615.");
+ YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidInterval.yang");
+ }
+
+ /**
+ * Checks valid length substatements.
+ */
+ @Test
+ public void processLengthSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthSubStatements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ assertThat(lengthRestriction.getDescription(), is("\"length description\""));
+ assertThat(lengthRestriction.getReference(), is("\"length reference\""));
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+
+ /**
+ * Checks whether space can be allowed when length statement is present.
+ */
+ @Test
+ public void processLengthStatementWithSpace() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LengthStatementWithSpace.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+ YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
+
+ ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
+ .listIterator();
+
+ YangRangeInterval rangeInterval = lengthListIterator.next();
+
+ assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
+ assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java
new file mode 100644
index 0000000..526a645
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing list listener.
+ */
+public class ListListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks list statement as sub-statement of module.
+ */
+ @Test
+ public void processModuleSubStatementList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<String> keyList = yangList.getKeyList().listIterator();
+ assertThat(keyList.next(), is("invalid-interval"));
+ }
+
+ /**
+ * Checks list statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the container is child of module
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("ospf"));
+
+ // Check whether the list is child of container
+ YangList yangList = (YangList) yangContainer.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
+ }
+
+ /**
+ * Checks list statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList1 = (YangList) yangNode.getChild();
+ assertThat(yangList1.getName(), is("ospf"));
+ assertThat(yangList1.getKeyList().contains("process-id"), is(true));
+
+ // Check whether the list is child of list
+ YangList yangList = (YangList) yangList1.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
+ }
+
+ /**
+ * Checks list with all its sub-statements.
+ */
+ @Test
+ public void processListSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+
+ // Check whether list properties as set correctly.
+ assertThat(yangList.getName(), is("ospf"));
+ assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
+
+ assertThat(yangList.isConfig(), is(true));
+ assertThat(yangList.getMaxElements(), is(10));
+ assertThat(yangList.getMinElements(), is(3));
+ assertThat(yangList.getDescription(), is("\"list description\""));
+ assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(yangList.getReference(), is("\"list reference\""));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks cardinality of sub-statements of list.
+ */
+ @Test
+ public void processListSubStatementsCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"list valid\".");
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementsCardinality.yang");
+ }
+
+ /**
+ * Checks list statement without child.
+ */
+ @Test
+ public void processListStatementWithoutChild() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: Missing \"data-def-substatements\" in \"list valid\".");
+ YangNode node = manager.getDataModel("src/test/resources/ListStatementWithoutChild.yang");
+ }
+
+ /**
+ * Checks list as root node.
+ */
+ @Test
+ public void processListAsRootNode() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("no viable alternative at input 'list'");
+ YangNode node = manager.getDataModel("src/test/resources/ListAsRootNode.yang");
+ }
+
+ /**
+ * Checks invalid identifier for list statement.
+ */
+ @Test
+ public void processListInvalidIdentifier() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : list name 1valid is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/ListInvalidIdentifier.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MandatoryListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MandatoryListenerTest.java
new file mode 100644
index 0000000..52e43a9
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MandatoryListenerTest.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test case for mandatory listener.
+ */
+public class MandatoryListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid mandatory with value true statement.
+ */
+ @Test
+ public void processMandatoryTrue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MandatoryTrue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the mandatory value is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isMandatory(), is(true));
+ }
+
+ /**
+ * Checks valid mandatory with value false statement.
+ */
+ @Test
+ public void processMandatoryFalse() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MandatoryFalse.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the mandatory value is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isMandatory(), is(false));
+ }
+
+ /**
+ * Checks default value of mandatory statement.
+ */
+ @Test
+ public void processMandatoryDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MandatoryDefaultValue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the mandatory value is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.isMandatory(), is(false));
+ }
+
+ /**
+ * Checks invalid of mandatory statement and expects exception.
+ */
+ @Test
+ public void processMandatoryEmptyStatement() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("no viable alternative at input ';'");
+ YangNode node = manager.getDataModel("src/test/resources/MandatoryEmptyStatement.yang");
+ }
+
+ /**
+ * Checks invalid mandatory statement(without statement end) and expects
+ * exception.
+ */
+ @Test
+ public void processMandatoryWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing ';' at '}'");
+ YangNode node = manager.getDataModel("src/test/resources/MandatoryWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks mandatory statement as sub-statement of module and expects
+ * exception.
+ */
+ @Test
+ public void processModuleSubStatementMandatory() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'mandatory' expecting {'anyxml', 'augment', 'choice', 'contact',"
+ + " 'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ + " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
+ + " 'revision', 'rpc', 'typedef', 'uses', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementMandatory.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MaxElementsListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MaxElementsListenerTest.java
new file mode 100644
index 0000000..aa0cee2
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MaxElementsListenerTest.java
@@ -0,0 +1,190 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing max-elements listener.
+ */
+public class MaxElementsListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks max-elements as sub-statements of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementMaxElements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementMaxElements.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getMaxElelements(), is(3));
+ }
+
+ /**
+ * Checks max-elements as sub-statements of list.
+ */
+ @Test
+ public void processListSubStatementMaxElements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementMaxElements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.getMaxElements(), is(3));
+ }
+
+ /**
+ * Checks whether exception is thrown when invalid max-elements keyword is
+ * given as input.
+ */
+ @Test
+ public void processMaxElementsInvalidStatement() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("extraneous input 'max-element' expecting {'config', 'description', 'if-feature',"
+ + " 'max-elements', 'min-elements', 'must', 'ordered-by', 'reference', 'status', 'type', 'units', "
+ + "'when', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/MaxElementsInvalidStatement.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when max-elements statement without
+ * statement end is given as input.
+ */
+ @Test
+ public void processMaxElementsWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing ';' at 'description'");
+ YangNode node = manager.getDataModel("src/test/resources/MaxElementsWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when max-elements cardinality is not
+ * as per the grammar.
+ */
+ @Test
+ public void processMaxElementsCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"max-elements\" is defined more than once in \"leaf-list " +
+ "invalid-interval\".");
+ YangNode node = manager.getDataModel("src/test/resources/MaxElementsCardinality.yang");
+ }
+
+ /**
+ * Checks unbounded value of max-elements statement.
+ */
+ @Test
+ public void processMaxElementsUnbounded() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MaxElementsUnbounded.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getMaxElelements(), is(2147483647));
+ }
+
+ /**
+ * Checks default value of max-elements statement.
+ */
+ @Test
+ public void processMaxElementsDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MaxElementsDefaultValue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getMaxElelements(), is(2147483647));
+ }
+
+ /**
+ * Checks whether exception is thrown when invalid min-elements value is
+ * given as input.
+ */
+ @Test
+ public void processMaxElementsMaxValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : max-elements value 77777777777777777777777 is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/MaxElementsMaxValue.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MinElementsListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MinElementsListenerTest.java
new file mode 100644
index 0000000..c2f668b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/MinElementsListenerTest.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing min-elements listener.
+ */
+public class MinElementsListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks min-elements as sub-statements of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementMinElements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementMinElements.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getMinElements(), is(3));
+ }
+
+ /**
+ * Checks min-elements as sub-statements of list.
+ */
+ @Test
+ public void processListSubStatementMinElements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementMinElements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.getMinElements(), is(3));
+ }
+
+ /**
+ * Checks whether exception is thrown when invalid min-elements keyword is
+ * given as input.
+ */
+ @Test
+ public void processMinElementsInvalidKeyword() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("extraneous input 'min-element' expecting {'config', 'description', 'if-feature',"
+ + " 'max-elements', 'min-elements', 'must', 'ordered-by', 'reference', 'status', 'type', 'units',"
+ + " 'when', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidKeyword.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when invalid min-elements value is
+ * given as input.
+ */
+ @Test
+ public void processMinElementsInvalidValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : min-elements value asd is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidValue.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when invalid min-elements value is
+ * given as input.
+ */
+ @Test
+ public void processMinElementsMaxValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : min-elements value 77777777777777777777777 is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/MinElementsMaxValue.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when min-elements statement without
+ * statement end is given as input.
+ */
+ @Test
+ public void processMinElementsWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing ';' at 'description'");
+ YangNode node = manager.getDataModel("src/test/resources/MinElementsWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when min-elements cardinality is not
+ * as per the grammar.
+ */
+ @Test
+ public void processMinElementsInvalidCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"min-elements\" is defined more than once in \"leaf-list " +
+ "invalid-interval\".");
+ YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidCardinality.yang");
+ }
+
+ /**
+ * Checks min-element's default value.
+ */
+ @Test
+ public void processMinElementsDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MinElementsDefaultValue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getMinElements(), is(0));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListenerTest.java
new file mode 100644
index 0000000..f77050a
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ModuleListenerTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing module listener functionality.
+ */
+public class ModuleListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Checks if module listener updates the data model root node.
+ */
+ @Test
+ public void processModuleValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleValidEntry.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+ }
+
+ /**
+ * Checks if module name is set correctly.
+ */
+ @Test(expected = ParserException.class)
+ public void processModuleInvalidEntryTest() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleWithInvalidIdentifier.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown when module length is greater than 64 characters.
+ */
+ @Test
+ public void processModuleInvalidIdentifierLength() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : module name Testttttttttttttttttttttttttttttttttttttttttttttttttttt" +
+ "tttttttttt is greater than 64 characters.");
+ YangNode node = manager.getDataModel("src/test/resources/ModuleInvalidIdentifierLength.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java
new file mode 100644
index 0000000..0f35705
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing namespace listener functionality.
+ */
+public class NamespaceListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks that value of namespace shouldn't have invalid spaces.
+ */
+ @Test(expected = ParserException.class)
+ public void processNamespaceWithInvalidSpaces() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NamespaceWithInvalidSpaces.yang");
+ }
+
+ /**
+ * Checks if namespace with double quotes is allowed.
+ */
+ @Test()
+ public void processNamespaceInDoubleQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NamespaceInDoubleQuotes.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getNameSpace().getUri(), is("\"urn:ietf:params:xml:ns:yang:ietf-ospf\""));
+ }
+
+ /**
+ * Checks if namespace without double quotes is allowed.
+ */
+ @Test()
+ public void processNamespaceWithoutQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NamespaceWithoutQuotes.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getNameSpace().getUri(), is("urn:ietf:params:xml:ns:yang:ietf-ospf"));
+ }
+
+ /**
+ * Checks if namespace is present only once.
+ */
+ @Test(expected = ParserException.class)
+ public void processNamespaceDualEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NamespaceDualEntry.yang");
+ }
+
+ /**
+ * Checks if mandatory parameter namespace is present.
+ */
+ @Test(expected = ParserException.class)
+ public void processNamespaceNoEntryTest() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/NamespaceNoEntryTest.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NotificationListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NotificationListenerTest.java
new file mode 100644
index 0000000..7da2468
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NotificationListenerTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangNotification;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing notification listener functionality.
+ */
+public class NotificationListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid notification statement.
+ */
+ @Test
+ public void processValidNotificationStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidNotificationStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ YangNotification yangNotification = (YangNotification) yangNode.getChild();
+ assertThat(yangNotification.getName(), is("link-failure"));
+ assertThat(yangNotification.getDescription(), is("\"A link failure has been detected\""));
+ assertThat(yangNotification.getStatus(), is(YangStatusType.DEPRECATED));
+ assertThat(yangNotification.getReference(), is("\"reference\""));
+
+ YangTypeDef typeDef = (YangTypeDef) yangNotification.getChild();
+ assertThat(typeDef.getName(), is("my-type"));
+ assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
+
+ ListIterator<YangLeaf> leafIterator = yangNotification.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("if-name"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/OrganizationListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/OrganizationListenerTest.java
new file mode 100644
index 0000000..e9098d3
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/OrganizationListenerTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing organization listener functionality.
+ */
+public class OrganizationListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if organization listener updates the data model tree.
+ */
+ @Test
+ public void processOrganizationValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/OrganizationValidEntry.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getOrganization(), is("\"IETF SPRING Working Group\""));
+ }
+
+ /**
+ * Checks that organization must be present only once.
+ */
+ @Test(expected = ParserException.class)
+ public void processOrganizationDualEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/OrganizationDualEntry.yang");
+ }
+
+ /**
+ * Checks if organization entry syntax is correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processOrganizationMissingValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/OrganizationMissingValue.yang");
+ }
+
+ /**
+ * Checks if organization and namespace is present in correct order.
+ */
+ @Test(expected = ParserException.class)
+ public void processOrganizationInvalidOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/OrganizationInvalidOrder.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/OutputListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/OutputListenerTest.java
new file mode 100644
index 0000000..a727092
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/OutputListenerTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangOutput;
+import org.onosproject.yangutils.datamodel.YangRpc;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing output listener functionality.
+ */
+public class OutputListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks output statements with data definition statements as sub-statements.
+ */
+ @Test
+ public void processOutputStatementWithDataDefinition() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/OutputStatementWithDataDefinition.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ YangRpc yangRpc = (YangRpc) yangNode.getChild();
+ assertThat(yangRpc.getName(), is("activate-software-image"));
+
+ YangOutput yangOutput = (YangOutput) yangRpc.getChild();
+ assertThat(yangOutput.getName(), is("activate-software-image_output"));
+ ListIterator<YangLeaf> leafIterator = yangOutput.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("image-name"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+
+ YangList yangList = (YangList) yangOutput.getChild();
+ assertThat(yangList.getName(), is("ospf"));
+ assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
+ assertThat(yangList.isConfig(), is(true));
+ assertThat(yangList.getMaxElements(), is(10));
+ assertThat(yangList.getMinElements(), is(3));
+ leafIterator = yangList.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+
+ YangContainer yangContainer = (YangContainer) yangList.getNextSibling();
+ assertThat(yangContainer.getName(), is("isis"));
+
+ leafIterator = yangContainer.getListOfLeaf().listIterator();
+ leafInfo = leafIterator.next();
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ }
+
+ /**
+ * Checks output statements with type-def statement as sub-statements.
+ */
+ @Test
+ public void processOutputStatementWithTypedef() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/OutputStatementWithTypedef.yang");
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ YangRpc yangRpc = (YangRpc) yangNode.getChild();
+ assertThat(yangRpc.getName(), is("activate-software-image"));
+
+ YangOutput yangOutput = (YangOutput) yangRpc.getChild();
+ assertThat(yangOutput.getName(), is("activate-software-image_output"));
+ YangTypeDef typeDef = (YangTypeDef) yangOutput.getChild();
+ assertThat(typeDef.getName(), is("my-type"));
+ assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
+ assertThat(typeDef.getName(), is("my-type"));
+ assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
+ assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.INT32));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java
new file mode 100644
index 0000000..5216ef3
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PatternRestrictionListenerTest.java
@@ -0,0 +1,205 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangPatternRestriction;
+import org.onosproject.yangutils.datamodel.YangStringRestriction;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for pattern restriction listener.
+ */
+public class PatternRestrictionListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid pattern statement as sub-statement of leaf statement.
+ */
+ @Test
+ public void processValidPatternStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidPatternStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+ ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
+ .getPatternList().listIterator();
+ assertThat(patternListIterator.next(), is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks valid pattern statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processPatternStatementInsideLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+ ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
+ .getPatternList().listIterator();
+ assertThat(patternListIterator.next(), is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks valid pattern statement as sub-statement of typedef.
+ */
+ @Test
+ public void processPatternStatementInsideTypeDef() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideTypeDef.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
+ YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getTypeDefBaseType()
+ .getDataTypeExtendedInfo();
+
+ YangPatternRestriction yangPatternRestriction = stringRestriction.getPatternRestriction();
+ assertThat(yangPatternRestriction.getPatternList().listIterator().next(), is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks valid multiple pattern statements.
+ */
+ @Test
+ public void processMultiplePatternStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/MultiplePatternStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+ ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
+ .getPatternList().listIterator();
+ assertThat(patternListIterator.next(), is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks valid pattern statement with plus symbol in pattern.
+ */
+ @Test
+ public void processPatternStatementWithPlus() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternStatementWithPlus.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+ ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
+ .getPatternList().listIterator();
+ assertThat(patternListIterator.next(), is("-[0-9]+|[0-9]+"));
+ }
+
+ /**
+ * Checks valid pattern substatement.
+ */
+ @Test
+ public void processPatternSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PatternSubStatements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
+ YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+ assertThat(stringRestriction.getDescription(), is("\"pattern description\""));
+ assertThat(stringRestriction.getReference(), is("\"pattern reference\""));
+ ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction()
+ .getPatternList().listIterator();
+ assertThat(patternListIterator.next(), is("[a-zA-Z]"));
+ }
+
+ /**
+ * Checks invalid pattern sub-statement.
+ */
+ @Test(expected = ParserException.class)
+ public void processInvalidPatternSubStatements() throws IOException, ParserException {
+ YangNode node = manager.getDataModel("src/test/resources/InvalidPatternSubStatements.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java
new file mode 100644
index 0000000..7d252ba
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PositionListenerTest.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangBit;
+import org.onosproject.yangutils.datamodel.YangBits;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import java.util.Set;
+
+/**
+ * Test cases for position listener.
+ */
+public class PositionListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks explicitly configured value.
+ */
+ @Test
+ public void processPositionStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PositionStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("mybits"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
+ assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
+ is("mybits"));
+
+ Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
+ for (YangBit tmp : bitSet) {
+ if (tmp.getBitName().equals("disable-nagle")) {
+ assertThat(tmp.getPosition(), is(0));
+ } else if (tmp.getBitName().equals("auto-sense-speed")) {
+ assertThat(tmp.getPosition(), is(1));
+ } else if (tmp.getBitName().equals("Ten-Mb-only")) {
+ assertThat(tmp.getPosition(), is(2));
+ }
+ }
+ }
+
+ /**
+ * Checks position value with double quotes.
+ */
+ @Test
+ public void processPositionWithDoubleQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PositionWithDoubleQuotes.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("mybits"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
+ assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
+ is("mybits"));
+
+ Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
+ for (YangBit tmp : bitSet) {
+ if (tmp.getBitName().equals("disable-nagle")) {
+ assertThat(tmp.getPosition(), is(0));
+ } else if (tmp.getBitName().equals("auto-sense-speed")) {
+ assertThat(tmp.getPosition(), is(1));
+ } else if (tmp.getBitName().equals("Ten-Mb-only")) {
+ assertThat(tmp.getPosition(), is(2));
+ }
+ }
+ }
+
+ /**
+ * Checks explicit value and auto generated value.
+ */
+ @Test
+ public void processPositionImplicitAndExplicit() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicit.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("mybits"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
+ assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
+ is("mybits"));
+
+ Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
+ for (YangBit tmp : bitSet) {
+ if (tmp.getBitName().equals("disable-nagle")) {
+ assertThat(tmp.getPosition(), is(0));
+ } else if (tmp.getBitName().equals("auto-sense-speed")) {
+ assertThat(tmp.getPosition(), is(1));
+ } else if (tmp.getBitName().equals("Ten-Mb-only")) {
+ assertThat(tmp.getPosition(), is(2));
+ }
+ }
+ }
+
+ /**
+ * Checks explicit value should not be repeated.
+ */
+ @Test(expected = ParserException.class)
+ public void processPositionDuplication() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PositionDuplication.yang");
+ }
+
+ /**
+ * Checks explicit or auto generated value should not be repeated.
+ */
+ @Test(expected = ParserException.class)
+ public void processPositionImplicitAndExplicitDuplication() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicitDuplication.yang");
+ }
+
+ /**
+ * Checks if negative value of position is not allowed.
+ */
+ @Test(expected = ParserException.class)
+ public void processPositionNegativeValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PositionNegativeValue.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PrefixListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PrefixListenerTest.java
new file mode 100644
index 0000000..ef89a78
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PrefixListenerTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing prefix listener functionality.
+ */
+public class PrefixListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if value of prefix is correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processPrefixInvalidValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PrefixInvalidValue.yang");
+ }
+
+ /**
+ * Checks if prefix listener updates the data model tree.
+ */
+ @Test
+ public void processPrefixValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PrefixValidEntry.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getPrefix(), is("On"));
+ }
+
+ /**
+ * Checks prefix value with double quotes.
+ */
+ @Test
+ public void processPrefixWithDoubleQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PrefixWithDoubleQuotes.yang");
+ assertThat(((YangModule) node).getPrefix(), is("On"));
+ }
+
+ /**
+ * Checks that prefix should be present just once.
+ */
+ @Test(expected = ParserException.class)
+ public void processPrefixDualEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PrefixDualEntry.yang");
+ }
+
+ /**
+ * Checks if prefix syntax is followed.
+ */
+ @Test(expected = ParserException.class)
+ public void processPrefixMissingValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PrefixMissingValue.yang");
+ }
+
+ /**
+ * Checks that exception should be reported if prefix is missing.
+ */
+ @Test(expected = ParserException.class)
+ public void processPrefixOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PrefixOrder.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PresenceListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PresenceListenerTest.java
new file mode 100644
index 0000000..2d0e376
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/PresenceListenerTest.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+
+/**
+ * Test cases for presence listener.
+ */
+public class PresenceListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks presence statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementPresence() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementPresence.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("valid"));
+ assertThat(yangContainer.getPresence(), is("\"invalid\""));
+ }
+
+ /**
+ * checks default value of presence statement.
+ */
+ @Test
+ public void processPresenceDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/PresenceDefaultValue.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("valid"));
+ assertThat(yangContainer.getPresence(), is(nullValue()));
+ }
+
+ /**
+ * Checks presence statement without statement end.
+ */
+ @Test
+ public void processPresenceWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'leaf' expecting {';', '+'}");
+ YangNode node = manager.getDataModel("src/test/resources/PresenceWithoutStatementEnd.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RangeRestrictionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RangeRestrictionListenerTest.java
new file mode 100644
index 0000000..0ed801f
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RangeRestrictionListenerTest.java
@@ -0,0 +1,239 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangRangeInterval;
+import org.onosproject.yangutils.datamodel.YangRangeRestriction;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt32;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for range restriction listener.
+ */
+public class RangeRestrictionListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid range statement as sub-statement of leaf statement.
+ */
+ @Test
+ public void processValidRangeStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidRangeStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("int32"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = rangeListIterator.next();
+ assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
+ }
+
+ /**
+ * Checks valid range statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processRangeStatementInsideLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeStatementInsideLeafList.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
+ }
+
+ /**
+ * Checks valid range statement with one interval.
+ */
+ @Test
+ public void processRangeWithOneInterval() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeWithOneInterval.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(1));
+ }
+
+ /**
+ * Checks valid range statement with min and max.
+ */
+ @Test
+ public void processRangeWithMinMax() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeWithMinMax.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo
+ .getDataType().getDataTypeExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = rangeListIterator.next();
+
+ assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(-2147483648));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(2147483647));
+ }
+
+ /**
+ * Checks valid range statement with invalid integer pattern.
+ */
+ @Test
+ public void processRangeWithInvalidIntegerPattern() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : Input value \"a\" is not a valid int32.");
+ YangNode node = manager.getDataModel("src/test/resources/RangeWithInvalidIntegerPattern.yang");
+ }
+
+ /**
+ * Checks valid range statement with description.
+ */
+ @Test
+ public void processRangeSubStatements() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeSubStatements.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("int32"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+
+ assertThat(rangeRestriction.getDescription(), is("\"range description\""));
+ assertThat(rangeRestriction.getReference(), is("\"range reference\""));
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = rangeListIterator.next();
+ assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
+ }
+
+ /**
+ * Checks whether space can be allowed when range statement is present.
+ */
+ @Test
+ public void processRangeStatementWithSpace() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RangeStatementWithSpace.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("int32"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
+ YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafInfo
+ .getDataType().getDataTypeExtendedInfo();
+
+ ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
+ .listIterator();
+ YangRangeInterval rangeInterval = rangeListIterator.next();
+ assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
+ assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ReferenceListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ReferenceListenerTest.java
new file mode 100644
index 0000000..17a679a
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ReferenceListenerTest.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test case for reference listener.
+ */
+public class ReferenceListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid reference statement.
+ */
+ @Test
+ public void processReferenceStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ReferenceStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the reference is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks whether exception is thrown for invalid reference statement.
+ */
+ @Test
+ public void processReferenceWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
+ YangNode node = manager.getDataModel("src/test/resources/ReferenceWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks valid reference statement under module.
+ */
+ @Test
+ public void processModuleSubStatementReference() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementReference.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the reference is set correctly.
+ assertThat(yangNode.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks valid reference statement under module.
+ */
+ @Test
+ public void processReferenceEmptyStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ReferenceEmptyStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the reference is set correctly.
+ assertThat(yangNode.getReference(), is("\"\""));
+ }
+
+ /**
+ * Checks valid reference statement as sub-statement of revision.
+ */
+ @Test
+ public void processRevisionSubStatementReference() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionSubStatementReference.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the reference is set correctly.
+ assertThat(yangNode.getRevision().getReference(), is("\"revision reference\""));
+ }
+
+ /**
+ * Checks reference statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementReference() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementReference.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the reference value is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.getReference(), is("\"container reference\""));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks reference statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementReference() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementReference.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module and description value is set correctly.
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.isConfig(), is(true));
+ assertThat(yangList.getReference(), is("\"list reference\""));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks valid reference statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementReference() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementReference.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether description value is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getReference(), is("\"RFC 6020\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListenerTest.java
new file mode 100644
index 0000000..c12b377
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RevisionDateListenerTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing revision date listener functionality.
+ */
+public class RevisionDateListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if revision date syntax is correct in include.
+ */
+ @Test(expected = ParserException.class)
+ public void processRevisionDateInvalidSyntaxAtInclude() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalidSyntaxAtInclude.yang");
+ }
+
+ /**
+ * Checks if revision date syntax is correct in import.
+ */
+ @Test(expected = ParserException.class)
+ public void processRevisionDateInvalidSyntaxAtImport() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalidSyntaxAtImport.yang");
+ }
+
+ /**
+ * Checks revision date in quotes inside include.
+ */
+ @Test
+ public void processRevisionDateInQuotesAtInclude() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateInQuotesAtInclude.yang");
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
+ }
+
+ /**
+ * Checks revision date in quotes inside import.
+ */
+ @Test
+ public void processRevisionDateInQuotesAtImport() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateInQuotesAtImport.yang");
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
+ }
+
+ /**
+ * Checks if revision date follows YYYY-MM-DD format.
+ */
+ @Test(expected = ParserException.class)
+ public void processRevisionDateInvalidFormat() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalidFormat.yang");
+ }
+
+ /**
+ * Checks if revision date is correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processRevisionDateInvalid() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalid.yang");
+ }
+
+ /**
+ * Checks if revision date listener updates the data model tree.
+ */
+ @Test
+ public void processRevisionDateValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionDateValidEntry.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
+ assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListenerTest.java
new file mode 100644
index 0000000..a5eeb04
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RevisionListenerTest.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing revision listener functionality.
+ */
+public class RevisionListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if revision doesn't have optional parameters "revision and
+ * description".
+ */
+ @Test
+ public void processRevisionNoOptionalParameter() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionNoOptionalParameter.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getRevision().getRevDate(), is("2016-02-03"));
+ }
+
+ /**
+ * Checks if the syntax of revision is correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processRevisionInValidSyntax() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionInValidSyntax.yang");
+ }
+
+ /**
+ * Checks if the correct order is followed.
+ */
+ @Test(expected = ParserException.class)
+ public void processRevisionInValidOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionInValidOrder.yang");
+ }
+
+ /**
+ * Checks the revision with current date is created for empty revision statement.
+ */
+ @Test
+ public void processWithoutRevision() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/RevisionAbsence.yang");
+ assertThat(((YangModule) node).getRevision().getRevDate(), notNullValue());
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RpcListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RpcListenerTest.java
new file mode 100644
index 0000000..3af045d
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/RpcListenerTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangRpc;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing Rpc listener functionality.
+ */
+public class RpcListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid rpc statements.
+ */
+ @Test
+ public void processValidRpcStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidRpcStatement.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("rock"));
+
+ YangRpc yangRpc = (YangRpc) yangNode.getChild();
+ assertThat(yangRpc.getName(), is("rock-the-house"));
+ assertThat(yangRpc.getDescription(), is("\"description\""));
+ assertThat(yangRpc.getReference(), is("\"reference\""));
+ assertThat(yangRpc.getStatus(), is(YangStatusType.CURRENT));
+
+ YangTypeDef typeDef = (YangTypeDef) yangRpc.getChild();
+ assertThat(typeDef.getName(), is("my-type"));
+ assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
+ assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.INT32));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ShortCaseListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ShortCaseListenerTest.java
new file mode 100644
index 0000000..132f7f4
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ShortCaseListenerTest.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangCase;
+import org.onosproject.yangutils.datamodel.YangChoice;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+/**
+ * Test cases for short case listener.
+ */
+public class ShortCaseListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks short case listener with container.
+ */
+ @Test
+ public void processShortCaseListenerWithContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ShortCaseListenerWithContainer.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+
+ YangCase yangCase = (YangCase) yangChoice.getChild();
+ assertThat(yangCase.getName(), is("sports-arena"));
+
+ YangContainer yangContainer1 = (YangContainer) yangCase.getChild();
+ assertThat(yangContainer1.getName(), is("sports-arena"));
+ }
+
+ /**
+ * Checks short case listener with list.
+ */
+ @Test
+ public void processShortCaseListenerWithList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ShortCaseListenerWithList.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangContainer yangContainer = (YangContainer) yangNode.getChild();
+ assertThat(yangContainer.getName(), is("food"));
+
+ YangChoice yangChoice = (YangChoice) yangContainer.getChild();
+ assertThat(yangChoice.getName(), is("snack"));
+
+ YangCase yangCase = (YangCase) yangChoice.getChild();
+ assertThat(yangCase.getName(), is("sports-arena"));
+
+ YangList yangList = (YangList) yangCase.getChild();
+ assertThat(yangList.getName(), is("sports-arena"));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/StatusListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/StatusListenerTest.java
new file mode 100644
index 0000000..b2cbd71
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/StatusListenerTest.java
@@ -0,0 +1,288 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for status listener.
+ */
+public class StatusListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid status statement.
+ */
+ @Test
+ public void processStatusStatementCurrent() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/StatusStatementCurrent.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the status is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ }
+
+ /**
+ * Checks valid status statement.
+ */
+ @Test
+ public void processStatusStatementDeprecated() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/StatusStatementDeprecated.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the status is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.DEPRECATED));
+ }
+
+ /**
+ * Checks valid status statement.
+ */
+ @Test
+ public void processStatusStatementObsolete() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/StatusStatementObsolete.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether the status is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.OBSOLETE));
+ }
+
+ /**
+ * Checks whether exception is thrown for invalid status statement.
+ */
+ @Test
+ public void processStatusWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("missing ';' at '}'");
+ YangNode node = manager.getDataModel("src/test/resources/StatusWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks whether exception is thrown for invalid status statement.
+ */
+ @Test
+ public void processStatusInvalidValue() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : status invalid is not valid.");
+ YangNode node = manager.getDataModel("src/test/resources/StatusInvalidValue.yang");
+ }
+
+ /**
+ * Checks status statement as sub-statement of module.
+ */
+ @Test
+ public void processModuleSubStatementStatus() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'status' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ + "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import',"
+ + " 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference', "
+ + "'revision', 'rpc', 'typedef', 'uses', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementStatus.yang");
+ }
+
+ /**
+ * Checks status statement as sub-statement of container.
+ */
+ @Test
+ public void processContainerSubStatementStatus() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementStatus.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether status is set correctly.
+ YangContainer container = (YangContainer) yangNode.getChild();
+ assertThat(container.getName(), is("valid"));
+ assertThat(container.isConfig(), is(true));
+ assertThat(container.getStatus(), is(YangStatusType.OBSOLETE));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks status statement as sub-statement of list.
+ */
+ @Test
+ public void processListSubStatementStatus() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ListSubStatementStatus.yang");
+
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ // Check whether the list is child of module and status is set.
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+ assertThat(yangList.isConfig(), is(true));
+ assertThat(yangList.getStatus(), is(YangStatusType.CURRENT));
+
+ // Check whether leaf properties as set correctly.
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks valid status statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementStatus() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementStatus.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether status is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.isConfig(), is(true));
+ assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
+ }
+
+ /**
+ * Checks default value of status statement.
+ */
+ @Test
+ public void processStatusDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/StatusDefaultValue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether status is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.isConfig(), is(true));
+ assertThat(leafListInfo.getStatus(), is(YangStatusType.CURRENT));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListenerTest.java
new file mode 100644
index 0000000..6a53cbe
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/SubModuleListenerTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangSubModule;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing submodule listener functionality.
+ */
+public class SubModuleListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if the sub module listeners updates the data model tree.
+ */
+ @Test
+ public void processSubModuleValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SubModuleValidEntry.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangSubModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
+
+ YangSubModule yangNode = (YangSubModule) node;
+ // Check whether the module name is set correctly.
+ assertThat(yangNode.getName(), is("Test"));
+ // Checks for the version value in data model tree.
+ assertThat(yangNode.getVersion(), is((byte) 1));
+ // Checks identifier of belongsto in data model tree.
+ assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
+ // Checks for the version value in data model tree.
+ assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
+ }
+
+ /**
+ * Checks if the yang version and belongs to can come in any order in sub
+ * module.
+ */
+ @Test
+ public void processSubModuleOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SubModuleOrder.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangSubModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
+
+ YangSubModule yangNode = (YangSubModule) node;
+ // Check whether the module name is set correctly.
+ assertThat(yangNode.getName(), is("Test"));
+ // Checks for the version value in data model tree.
+ assertThat(yangNode.getVersion(), is((byte) 1));
+ // Checks identifier of belongsto in data model tree.
+ assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
+ // Checks for the version value in data model tree.
+ assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
+ }
+
+ /**
+ * Checks if yang version is optional.
+ */
+ @Test
+ public void processSubModuleWithoutVersion() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutVersion.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangSubModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
+
+ YangSubModule yangNode = (YangSubModule) node;
+ // Check whether the module name is set correctly.
+ assertThat(yangNode.getName(), is("Test"));
+ // Checks identifier of belongsto in data model tree.
+ assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
+ // Checks for the version value in data model tree.
+ assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
+ //Checks the revision with current date is created for empty revision statement.
+ assertThat(((YangSubModule) node).getRevision().getRevDate(), notNullValue());
+ }
+
+ /**
+ * Checks if sub module name is correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processSubModuleInvalidName() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SubModuleInvalidName.yang");
+ }
+
+ /**
+ * Checks if sub module has invalid modules construct eg namespace.
+ */
+ @Test(expected = ParserException.class)
+ public void processSubModuleWithNamespace() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/SubModuleWithNamespace.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/TypeListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/TypeListenerTest.java
new file mode 100644
index 0000000..1b6a7c6
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/TypeListenerTest.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test case for type listener.
+ */
+public class TypeListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks derived statement without contraints.
+ */
+ @Test
+ public void processDerivedTypeStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
+ }
+
+ /**
+ * Checks valid yang data type.
+ */
+ @Test
+ public void processIntegerTypeStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ }
+
+ /**
+ * Checks type for leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementType() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
+ }
+
+ /**
+ * Checks for unsupported type leafref.
+ */
+ @Test
+ public void processLeafrefType() throws IOException, ParserException {
+
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : \"leafref\" is not supported in current version,"
+ + " please check wiki for YANG utils road map.");
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/LeafrefInvalidIdentifier.yang");
+ }
+
+ /**
+ * Checks for unsupported type identityref.
+ */
+ @Test
+ public void processIdentityrefType() throws IOException, ParserException {
+
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : \"identityref\" is not supported in current version,"
+ + " please check wiki for YANG utils road map.");
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/IdentityrefInvalidIdentifier.yang");
+ }
+
+ /**
+ * Checks for unsupported type instance identifier.
+ */
+ @Test
+ public void processInstanceIdentifierType() throws IOException, ParserException {
+
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : \"instance-identifier\" is not supported in current version,"
+ + " please check wiki for YANG utils road map.");
+
+ YangNode node = manager
+ .getDataModel("src/test/resources/InstanceIdentifierInvalidIdentifier.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnionListenerTest.java
new file mode 100644
index 0000000..1ac199f
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnionListenerTest.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ListIterator;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.YangUnion;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing union listener.
+ */
+public class UnionListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks union when type is in leaf.
+ */
+ @Test
+ public void processUnionWhenTypeInLeaf() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeaf.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
+
+ YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
+
+ List<YangType<?>> typeList = yangUnion.getTypeList();
+ ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
+ YangType<?> yangType = typeListIterator.next();
+
+ assertThat(yangType.getDataTypeName(), is("int32"));
+ assertThat(yangType.getDataType(), is(YangDataTypes.INT32));
+
+ YangType<?> yangTypeEnum = typeListIterator.next();
+
+ assertThat(yangTypeEnum.getDataTypeName(), is("enumeration"));
+ assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION));
+ }
+
+ /**
+ * Checks union when type is in leaflist.
+ */
+ @Test
+ public void processUnionWhenTypeInLeafList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeafList.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangList yangList = (YangList) yangNode.getChild();
+ assertThat(yangList.getName(), is("valid"));
+
+ ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+
+ assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
+ assertThat(leafListInfo.getDataType().getDataTypeName(), is("union"));
+
+ YangUnion yangUnion = (YangUnion) leafListInfo.getDataType().getDataTypeExtendedInfo();
+
+ List<YangType<?>> typeList = yangUnion.getTypeList();
+ ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
+ YangType<?> yangType = typeListIterator.next();
+
+ assertThat(yangType.getDataTypeName(), is("int32"));
+ assertThat(yangType.getDataType(), is(YangDataTypes.INT32));
+
+ YangType<?> yangTypeEnum = typeListIterator.next();
+
+ assertThat(yangTypeEnum.getDataTypeName(), is("enumeration"));
+ assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION));
+ }
+
+ /**
+ * Checks union with empty type.
+ */
+ @Test (expected = ParserException.class)
+ public void processUnionWithEmptyType() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UnionWithEmptyType.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnitsListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnitsListenerTest.java
new file mode 100644
index 0000000..6684df5
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnitsListenerTest.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeafList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for units listener.
+ */
+public class UnitsListenerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks valid units statement.
+ */
+ @Test
+ public void processUnitsStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UnitsStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether units value is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ }
+
+ /**
+ * Checks invalid units statement as sub-statement of module.
+ */
+ @Test
+ public void processModuleSubStatementUnits() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input 'type' expecting {'anyxml', 'augment', 'choice', 'contact', "
+ + "'container', 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity',"
+ + " 'import', 'include', 'leaf', 'leaf-list', 'list', 'notification', 'organization', "
+ + "'reference', 'revision', 'rpc', 'typedef', 'uses', '}'}");
+ YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementUnits.yang");
+ }
+
+ /**
+ * Checks invalid units statement(without statement end).
+ */
+ @Test
+ public void processUnitsWithoutStatementEnd() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
+ YangNode node = manager.getDataModel("src/test/resources/UnitsWithoutStatementEnd.yang");
+ }
+
+ /**
+ * Checks order of units statement in leaf.
+ */
+ @Test
+ public void processUnitsStatementOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UnitsStatementOrder.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ // Check whether leaf properties is set correctly.
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
+ assertThat(leafInfo.getUnits(), is("\"seconds\""));
+ assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
+ assertThat(leafInfo.isConfig(), is(true));
+ assertThat(leafInfo.isMandatory(), is(true));
+ assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
+ }
+
+ /**
+ * Checks the default value of unit statement.
+ */
+ @Test
+ public void processUnitsDefaultValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UnitsDefaultValue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("invalid-interval"));
+ assertThat(leafInfo.getUnits(), is(nullValue()));
+ }
+
+ /**
+ * Checks invalid occurance of units statement as sub-statement of leaf.
+ */
+ @Test
+ public void processUnitsStatementCardinality() throws IOException, ParserException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf invalid-interval\".");
+ YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang");
+ }
+
+ /**
+ * Checks valid units statement as sub-statement of leaf-list.
+ */
+ @Test
+ public void processLeafListSubStatementUnits() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementUnits.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat(node instanceof YangModule, is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
+ YangLeafList leafListInfo = leafListIterator.next();
+
+ // Check whether units value is set correctly.
+ assertThat(leafListInfo.getName(), is("invalid-interval"));
+ assertThat(leafListInfo.getUnits(), is("\"seconds\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UsesListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UsesListenerTest.java
new file mode 100644
index 0000000..a20c43e
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UsesListenerTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangContainer;
+import org.onosproject.yangutils.datamodel.YangGrouping;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangStatusType;
+import org.onosproject.yangutils.datamodel.YangUses;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing uses listener.
+ */
+public class UsesListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks uses statement inside module.
+ */
+ @Test
+ public void processUsesInModule() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UsesInModule.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+
+ YangUses yangUses = (YangUses) yangGrouping.getNextSibling();
+ assertThat(yangUses.getName(), is("endpoint"));
+ }
+
+ /**
+ * Checks uses statement inside container.
+ */
+ @Test
+ public void processUsesInContainer() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UsesInContainer.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+
+ YangContainer yangContainer = (YangContainer) yangGrouping.getNextSibling();
+ assertThat(yangContainer.getName(), is("valid"));
+
+ YangUses yangUses = (YangUses) yangContainer.getChild();
+ assertThat(yangUses.getName(), is("endpoint"));
+
+ // Check attributes associated with uses.
+ assertThat(yangUses.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(yangUses.getReference(), is("\"RFC 6020\""));
+ assertThat(yangUses.getDescription(), is("\"grouping under test\""));
+ }
+
+ /**
+ * Checks uses statement inside list.
+ */
+ @Test
+ public void processUsesInList() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/UsesInList.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ YangGrouping yangGrouping = (YangGrouping) yangNode.getChild();
+ assertThat(yangGrouping.getName(), is("endpoint"));
+
+ YangList yangList = (YangList) yangGrouping.getNextSibling();
+ assertThat(yangList.getName(), is("valid"));
+
+ YangUses yangUses = (YangUses) yangList.getChild();
+ assertThat(yangUses.getName(), is("endpoint"));
+
+ // Check attributes associated with uses.
+ assertThat(yangUses.getStatus(), is(YangStatusType.CURRENT));
+ assertThat(yangUses.getReference(), is("\"RFC 6020\""));
+ assertThat(yangUses.getDescription(), is("\"grouping under test\""));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ValueListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ValueListenerTest.java
new file mode 100644
index 0000000..6dae702
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ValueListenerTest.java
@@ -0,0 +1,216 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangEnum;
+import org.onosproject.yangutils.datamodel.YangEnumeration;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.ListIterator;
+import java.util.Set;
+
+/**
+ * Test cases for value listener.
+ */
+public class ValueListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks explicitly configured value.
+ */
+ @Test
+ public void processValueStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValueStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("speed"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
+ assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
+ is("speed_enum"));
+
+ Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
+ for (YangEnum tmp : enumSet) {
+ if (tmp.getNamedValue().equals("10m")) {
+ assertThat(tmp.getValue(), is(10));
+ } else if (tmp.getNamedValue().equals("100m")) {
+ assertThat(tmp.getValue(), is(100));
+ } else if (tmp.getNamedValue().equals("auto")) {
+ assertThat(tmp.getValue(), is(1000));
+ }
+ }
+ }
+
+ /**
+ * Checks explicitly configured negative value.
+ */
+ @Test
+ public void processValueStatementWithNegativeValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValueStatementWithNegativeValue.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("speed"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
+ assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
+ is("speed_enum"));
+
+ Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
+ for (YangEnum tmp : enumSet) {
+ if (tmp.getNamedValue().equals("10m")) {
+ assertThat(tmp.getValue(), is(-2));
+ } else if (tmp.getNamedValue().equals("100m")) {
+ assertThat(tmp.getValue(), is(-1));
+ } else if (tmp.getNamedValue().equals("auto")) {
+ assertThat(tmp.getValue(), is(0));
+ }
+ }
+ }
+
+ /**
+ * Checks explicitly configured value with double quotes.
+ */
+ @Test
+ public void processValueStatementWithQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValueStatementWithQuotes.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("speed"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
+ assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
+ is("speed_enum"));
+
+ Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
+ for (YangEnum tmp : enumSet) {
+ if (tmp.getNamedValue().equals("10m")) {
+ assertThat(tmp.getValue(), is(10));
+ } else if (tmp.getNamedValue().equals("100m")) {
+ assertThat(tmp.getValue(), is(100));
+ } else if (tmp.getNamedValue().equals("auto")) {
+ assertThat(tmp.getValue(), is(1000));
+ }
+ }
+ }
+
+ /**
+ * Checks explicit value and auto generated value.
+ */
+ @Test
+ public void processValueAndAutoStatement() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValueAndAutoStatement.yang");
+
+ // Check whether the data model tree returned is of type module.
+ assertThat((node instanceof YangModule), is(true));
+
+ // Check whether the node type is set properly to module.
+ assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+ // Check whether the module name is set correctly.
+ YangModule yangNode = (YangModule) node;
+ assertThat(yangNode.getName(), is("Test"));
+
+ ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+ YangLeaf leafInfo = leafIterator.next();
+
+ assertThat(leafInfo.getName(), is("speed"));
+ assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
+ assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
+ assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
+ is("speed_enum"));
+
+ Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
+ for (YangEnum tmp : enumSet) {
+ if (tmp.getNamedValue().equals("10m")) {
+ assertThat(tmp.getValue(), is(10));
+ } else if (tmp.getNamedValue().equals("100m")) {
+ assertThat(tmp.getValue(), is(11));
+ } else if (tmp.getNamedValue().equals("auto")) {
+ assertThat(tmp.getValue(), is(1000));
+ }
+ }
+ }
+
+ /**
+ * Checks explicit value should not be repeated.
+ */
+ @Test(expected = ParserException.class)
+ public void processValueDuplication() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValueDuplication.yang");
+ }
+
+ /**
+ * Checks explicit or auto generated value should not be repeated.
+ */
+ @Test(expected = ParserException.class)
+ public void processValueExplicitAndAutoDuplication() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValueExplicitAndAutoDuplication.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/VersionListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/VersionListenerTest.java
new file mode 100644
index 0000000..3d9de83
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/VersionListenerTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.listeners;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangModule;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test cases for testing version listener functionality.
+ */
+public class VersionListenerTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks if value of version is correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processVersionInvalidValue() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/VersionInvalidValue.yang");
+ }
+
+ /**
+ * Checks if version listener updates the data model tree.
+ */
+ @Test
+ public void processVersionValidEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/VersionValidEntry.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getVersion(), is((byte) 1));
+ }
+
+ /**
+ * Checks version in double quotes.
+ */
+ @Test
+ public void processValidVersionWithDoubleQuotes() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/ValidVersionWithDoubleQuotes.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getVersion(), is((byte) 1));
+ }
+
+ /**
+ * Checks if version which is optional paramater is not present.
+ */
+ @Test
+ public void processVersionNotPresent() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/VersionNotPresent.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getVersion(), is((byte) 1));
+ }
+
+ /**
+ * Checks that version should be present only once.
+ */
+ @Test(expected = ParserException.class)
+ public void processVersionDualEntry() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/VersionDualEntry.yang");
+ }
+
+ /**
+ * Checks if version can appear in any order in module header.
+ */
+ @Test
+ public void processVersionOrder() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/VersionOrder.yang");
+
+ // Checks for the version value in data model tree.
+ assertThat(((YangModule) node).getVersion(), is((byte) 1));
+ }
+
+ /**
+ * Checks if sytax of version entry is not correct.
+ */
+ @Test(expected = ParserException.class)
+ public void processVersionInvalidSyntax() throws IOException, ParserException {
+
+ YangNode node = manager.getDataModel("src/test/resources/VersionInvalidSyntax.yang");
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/AugmentListnerUtilTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/AugmentListnerUtilTest.java
new file mode 100644
index 0000000..3cf6eaa
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/AugmentListnerUtilTest.java
@@ -0,0 +1,320 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.parseutils;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.clearOccurrenceCount;
+import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.createValidNameForAugment;
+import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.getAugmentJavaFileNameList;
+import static org.onosproject.yangutils.parser.impl.parserutils.AugmentListenerUtil.updateNameWhenHasMultipleOuccrrence;
+
+/**
+ * Unit test case for augment listener utility.
+ */
+public class AugmentListnerUtilTest {
+
+ private static final String TEST1 = "test1Node";
+ private static final String PARENT_PREFIX = "if";
+ private static final String NODE_PREFIX = "rf";
+
+ private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX = "AugmentedTest1Node";
+ private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI1 = "AugmentedTest1Node1";
+ private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2 = "AugmentedTest1Node2";
+ private static final String TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3 = "AugmentedTest1Node3";
+
+ private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX = "AugmentedRfTest1Node";
+ private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI1 = "AugmentedRfTest1Node1";
+ private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI2 = "AugmentedRfTest1Node2";
+ private static final String TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI3 = "AugmentedRfTest1Node3";
+
+ private static String testString = "";
+
+ /**
+ * Unit test case when parent's prefix is present and one occurrence of augment node to update same target node.
+ */
+ @Test
+ public void testForAugmentNameWhenOneOuccrrenceWithParentPrefix() {
+ clearData();
+ testString = createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX)));
+ }
+
+ /**
+ * Unit test case when no prefix and one occurrence of augment node to update same target node.
+ */
+ @Test
+ public void testForAugmentNameWhenOneOuccrrenceWithNoPrefix() {
+ clearData();
+ testString = createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX)));
+ }
+
+ /**
+ * Unit test case when different prefix then parent is present and
+ * one occurrence of augment node to update same target node.
+ */
+ @Test
+ public void testForAugmentNameWhenOneOuccrrenceWithDiffPrefix() {
+ clearData();
+ testString = createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX)));
+ }
+
+ /**
+ * Unit test case when parent's prefix is present and two occurrence of augment node to update
+ * same target node is present.
+ */
+ @Test
+ public void testForAugmentNameWhenTwoOuccrrenceWithParentPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+ testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2)));
+ }
+
+ /**
+ * Unit test case when no prefix and two occurrence of augment node to update
+ * same target node is present.
+ */
+ @Test
+ public void testForAugmentNameWhenTwoOuccrrenceWithNoPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2)));
+ }
+
+ /**
+ * Unit test case when different prefix then parent is present and
+ * two occurrence of augment node to update same target node is present.
+ */
+ @Test
+ public void testForAugmentNameWhenTwoOuccrrenceWithDiffPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI2)));
+ }
+
+ /**
+ * Unit test case when parent prefix and three occurrence of augment node to update
+ * same target node is present.
+ */
+ @Test
+ public void testForAugmentNameWhenThreeOuccrrenceWithParentPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+
+ testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3)));
+ }
+
+ /**
+ * Unit test case when no prefix and three occurrence of augment node to update
+ * same target node is present.
+ */
+ @Test
+ public void testForAugmentNameWhenThreeOuccrrenceNoPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+
+ testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3)));
+ }
+
+ /**
+ * Unit test case when different prefix and three occurrence of augment node to update
+ * same target node is present.
+ */
+ @Test
+ public void testForAugmentNameWhenThreeOuccrrenceWithDiffPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+
+ testString = updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI3)));
+ }
+
+ /**
+ * Unit test case for when three occurrence is there and parent prefix is present,
+ * all the names need to be updated in list.
+ */
+ @Test
+ public void testForPreviousNamesGotUpdatedWhenParentPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithParentPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithParentPrefix()));
+
+ testString = getAugmentJavaFileNameList().get(0);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI1)));
+
+ testString = getAugmentJavaFileNameList().get(1);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2)));
+
+ testString = getAugmentJavaFileNameList().get(2);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3)));
+ }
+
+ /**
+ * Unit test case for when three occurrence is there and no prefix is present,
+ * all the names need to be updated in list.
+ */
+ @Test
+ public void testForPreviousNamesGotUpdatedWhenNoPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithNoPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithNoPrefix()));
+
+ testString = getAugmentJavaFileNameList().get(0);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI1)));
+
+ testString = getAugmentJavaFileNameList().get(1);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI2)));
+
+ testString = getAugmentJavaFileNameList().get(2);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITHOUT_PREFIX_MULTI3)));
+ }
+
+ /**
+ * Unit test case for when three occurrence is there and different prefix is present,
+ * all the names need to be updated in list.
+ */
+ @Test
+ public void testForPreviousNamesGotUpdatedWhenDifferentPrefix() {
+ clearData();
+
+ createValidNameForAugment(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+ updateNameWhenHasMultipleOuccrrence(getStubNodeIdetifierWithDiffPrefix(),
+ isPrefixPresent(getStubNodeIdetifierWithDiffPrefix()));
+
+ testString = getAugmentJavaFileNameList().get(0);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI1)));
+
+ testString = getAugmentJavaFileNameList().get(1);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI2)));
+
+ testString = getAugmentJavaFileNameList().get(2);
+ assertThat(true, is(testString.equals(TEST1_AUGMENTED_NAME_WITH_PREFIX_MULTI3)));
+ }
+
+ /**
+ * Returns stub node identifier when parent prefix is used.
+ *
+ * @param name name of node
+ * @param prefix prefix of node
+ * @return node identifier for node
+ */
+ private YangNodeIdentifier getStubNodeIdetifierWithParentPrefix() {
+ YangNodeIdentifier nodeId = new YangNodeIdentifier();
+ nodeId.setName(TEST1);
+ nodeId.setPrefix(PARENT_PREFIX);
+ return nodeId;
+ }
+
+ /**
+ * Returns stub node identifier when no prefix is used.
+ *
+ * @param name name of node
+ * @param prefix prefix of node
+ * @return node identifier for node
+ */
+ private YangNodeIdentifier getStubNodeIdetifierWithNoPrefix() {
+ YangNodeIdentifier nodeId = new YangNodeIdentifier();
+ nodeId.setName(TEST1);
+ nodeId.setPrefix(null);
+ return nodeId;
+ }
+
+ /**
+ * Returns stub node identifier when different prefix is used.
+ *
+ * @param name name of node
+ * @param prefix prefix of node
+ * @return node identifier for node
+ */
+ private YangNodeIdentifier getStubNodeIdetifierWithDiffPrefix() {
+ YangNodeIdentifier nodeId = new YangNodeIdentifier();
+ nodeId.setName(TEST1);
+ nodeId.setPrefix(NODE_PREFIX);
+ return nodeId;
+ }
+
+ /**
+ * Returns true if a prefix is present and it is not equals to parents prefix.
+ *
+ * @param nodeId YANG node identifier
+ * @param parentsPrefix parent's prefix
+ * @return true if a prefix is present and it is not equals to parents prefix
+ */
+ private static boolean isPrefixPresent(YangNodeIdentifier nodeId) {
+ return nodeId.getPrefix() != null && nodeId.getPrefix() != PARENT_PREFIX;
+ }
+
+ /**
+ * Clears list of names and occurrence count after each test case.
+ */
+ private void clearData() {
+ getAugmentJavaFileNameList().clear();
+ clearOccurrenceCount();
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerErrorMessageConstructionTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerErrorMessageConstructionTest.java
new file mode 100644
index 0000000..bcfaf20
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerErrorMessageConstructionTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.parseutils;
+
+import org.junit.Test;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CONTACT_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
+
+/**
+ * Test case for testing listener error message construction util.
+ */
+public class ListenerErrorMessageConstructionTest {
+
+ /**
+ * Checks for error message construction with parsable data type name.
+ */
+ @Test
+ public void checkErrorMsgConstructionWithName() {
+
+ // Create an test error message
+ String testErrorMessage = constructListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA, "Test Instance", ENTRY);
+
+ // Check message.
+ assertThat(testErrorMessage, is("Internal parser error detected: Invalid holder for contact "
+ + "\"Test Instance\" before processing."));
+ }
+
+ /**
+ * Checks for error message construction without parsable data type name.
+ */
+ @Test
+ public void checkErrorMsgConstructionWithoutName() {
+
+ // Create an test error message
+ String testErrorMessage = constructListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA, "Test Instance", ENTRY);
+
+ // Check message.
+ assertThat(testErrorMessage,
+ is("Internal parser error detected: Invalid holder for contact \"Test Instance\""
+ + " before processing."));
+ }
+
+ /**
+ * Checks for extended error message construction with parsable data type
+ * name.
+ */
+ @Test
+ public void checkExtendedErrorMsgConstructionWithName() {
+
+ // Create an test error message
+ String testErrorMessage = constructExtendedListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA,
+ "Test Instance", ENTRY,
+ "Extended Information");
+
+ // Check message.
+ assertThat(testErrorMessage,
+ is("Internal parser error detected: Invalid holder for contact \"Test Instance\""
+ + " before processing.\n" + "Error Information: Extended Information"));
+ }
+
+ /**
+ * Checks for extended error message construction without parsable data type
+ * name.
+ */
+ @Test
+ public void checkExtendedErrorMsgConstructionWithoutName() {
+
+ // Create an test error message
+ String testErrorMessage = constructExtendedListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA, "", ENTRY,
+ "Extended Information");
+
+ // Check message.
+ assertThat(testErrorMessage, is("Internal parser error detected: Invalid holder for contact"
+ + " before processing.\n" + "Error Information: Extended Information"));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerUtilTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerUtilTest.java
new file mode 100644
index 0000000..c29b126
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerUtilTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.parseutils;
+
+import java.io.IOException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+/**
+ * Test case for testing listener util.
+ */
+public class ListenerUtilTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks whether exception is thrown when identifier starts with xml.
+ */
+ @Test
+ public void validateIdentifierStartsWithXml() throws IOException {
+ thrown.expect(ParserException.class);
+ thrown.expectMessage("YANG file error : module identifier xMlTest must not start" +
+ " with (('X'|'x') ('M'|'m') ('L'|'l'))");
+ manager.getDataModel("src/test/resources/InValidIdentifierXML.yang");
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerValidationTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerValidationTest.java
new file mode 100644
index 0000000..ce347d6
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ListenerValidationTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.parseutils;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.datamodel.YangRevision;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.YANGBASE_DATA;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
+
+/**
+ * Test case for testing listener validation util.
+ */
+public class ListenerValidationTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Checks for exception in case parsable stack is empty while validating for
+ * not empty scenario.
+ */
+ @Test
+ public void validateStackIsNotEmptyForEmptyStack() {
+
+ String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
+
+ // Get the exception occurred during parsing.
+ thrown.expect(ParserException.class);
+ thrown.expectMessage(expectedError);
+
+ // Create test walker and assign test error to it.
+ TreeWalkListener testWalker = new TreeWalkListener();
+
+ checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
+ }
+
+ /**
+ * Checks if there is no exception in case parsable stack is not empty while
+ * validating for not empty scenario.
+ */
+ @Test
+ public void validateStackIsNotEmptyForNonEmptyStack() {
+
+ // Create test walker and assign test error to it.
+ TreeWalkListener testWalker = new TreeWalkListener();
+
+ // Create a temporary node of parsable.
+ YangRevision tmpNode = new YangRevision();
+ testWalker.getParsedDataStack().push(tmpNode);
+
+ checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
+ }
+
+ /**
+ * Checks for exception in case parsable stack is not empty while validating
+ * for empty scenario.
+ */
+ @Test
+ public void validateStackIsEmptyForNonEmptyStack() {
+
+ String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
+
+ // Get the exception occurred during parsing.
+ thrown.expect(ParserException.class);
+ thrown.expectMessage(expectedError);
+
+ // Create test walker and assign test error to it.
+ TreeWalkListener testWalker = new TreeWalkListener();
+
+ // Create a temporary node of parsable.
+ YangRevision tmpNode = new YangRevision();
+ testWalker.getParsedDataStack().push(tmpNode);
+
+ checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
+ }
+
+ /**
+ * Checks if there is no exception in case parsable stack is empty while
+ * validating for empty scenario.
+ */
+ @Test
+ public void validateStackIsEmptyForEmptyStack() {
+
+ // Create test walker and assign test error to it.
+ TreeWalkListener testWalker = new TreeWalkListener();
+
+ checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ParseTreeErrorListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ParseTreeErrorListenerTest.java
new file mode 100644
index 0000000..4489e77
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/parseutils/ParseTreeErrorListenerTest.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.parser.impl.parseutils;
+
+import org.antlr.v4.runtime.ANTLRFileStream;
+import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.tree.ParseTree;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
+import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Test case for testing parse tree error listener.
+ */
+public class ParseTreeErrorListenerTest {
+
+ YangUtilsParserManager manager = new YangUtilsParserManager();
+ File file;
+ BufferedWriter out;
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Checks that no exception is generated for YANG file with valid syntax.
+ */
+ @Test
+ public void checkValidYangFileForNoSyntaxError() throws IOException {
+
+ ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang");
+
+ // Create a lexer that feeds off of input char stream.
+ GeneratedYangLexer lexer = new GeneratedYangLexer(input);
+ // Create a buffer of tokens pulled from the lexer.
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ // Create a parser that feeds off the tokens buffer.
+ GeneratedYangParser parser = new GeneratedYangParser(tokens);
+ // Remove console error listener.
+ parser.removeErrorListeners();
+ // Create instance of customized error listener.
+ ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
+ // Add customized error listener to catch errors during parsing.
+ parser.addErrorListener(parseTreeErrorListener);
+ // Begin parsing YANG file and generate parse tree.
+ ParseTree tree = parser.yangfile();
+ }
+
+ /**
+ * Checks that exception is generated for YANG file with invalid syntax.
+ */
+ @Test
+ public void checkInvalidYangFileForSyntaxError() throws IOException {
+
+ // Get the exception occurred during parsing.
+ thrown.expect(ParserException.class);
+ thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
+ thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'");
+
+ ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang");
+
+ // Create a lexer that feeds off of input char stream.
+ GeneratedYangLexer lexer = new GeneratedYangLexer(input);
+ // Create a buffer of tokens pulled from the lexer.
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ // Create a parser that feeds off the tokens buffer.
+ GeneratedYangParser parser = new GeneratedYangParser(tokens);
+ // Remove console error listener.
+ parser.removeErrorListeners();
+ // Create instance of customized error listener.
+ ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
+ // Add customized error listener to catch errors during parsing.
+ parser.addErrorListener(parseTreeErrorListener);
+ // Begin parsing YANG file and generate parse tree.
+ ParseTree tree = parser.yangfile();
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataTypeTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataTypeTest.java
new file mode 100644
index 0000000..f2ca36e
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataTypeTest.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT8;
+import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType;
+import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
+import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
+import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
+
+/**
+ * Unit test case for attribute java data type.
+ */
+public class AttributesJavaDataTypeTest {
+
+ private static final YangDataTypes TYPE1 = STRING;
+ private static final YangDataTypes TYPE2 = INT32;
+ private static final YangDataTypes TYPE3 = BOOLEAN;
+ private static final YangDataTypes TYPE4 = UINT8;
+ private static final YangDataTypes TYPE_DEF = DERIVED;
+ private static final String CLASS_INFO1 = "String";
+ private static final String CLASS_INFO2 = "int";
+ private static final String CLASS_INFO3 = "boolean";
+ private static final String CLASS_INFO4 = "short";
+ private static final String CLASS_INFO5 = "Integer";
+ private static final String TYPE_DEF_PKG = "target.test";
+ private static String test = "";
+ private static YangToJavaNamingConflictUtil pluginConfig = null;
+
+ /**
+ * Unit test for private constructor.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {AttributesJavaDataType.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * Unit test for java class info method test.
+ */
+ @Test
+ public void testgetJavaClassInfo() {
+ test = getJavaImportClass(getStubYangType(TYPE1), false, pluginConfig);
+ assertThat(true, is(test.equals(CLASS_INFO1)));
+
+ test = getJavaImportClass(getStubYangType(TYPE2), true, pluginConfig);
+ assertThat(true, is(test.equals(CLASS_INFO5)));
+
+ test = getJavaImportClass(getStubYangType(TYPE3), false, pluginConfig);
+ assertThat(null, is(test));
+
+ test = getJavaImportClass(getStubYangType(TYPE4), false, pluginConfig);
+ assertThat(null, is(test));
+ }
+
+ /**
+ * Unit test for java data type method.
+ */
+ @Test
+ public void testgetJavaDataType() {
+ test = getJavaDataType(getStubYangType(TYPE1));
+ assertThat(true, is(test.equals(CLASS_INFO1)));
+
+ test = getJavaDataType(getStubYangType(TYPE2));
+ assertThat(true, is(test.equals(CLASS_INFO2)));
+
+ test = getJavaDataType(getStubYangType(TYPE3));
+ assertThat(true, is(test.equals(CLASS_INFO3)));
+
+ test = getJavaDataType(getStubYangType(TYPE4));
+ assertThat(true, is(test.equals(CLASS_INFO4)));
+ }
+
+ /**
+ * Unit test for java package info method.
+ */
+ @Test
+ public void testgetJavaPkgInfo() {
+ test = getJavaImportPackage(getStubYangType(TYPE1), false, pluginConfig);
+ assertThat(true, is(test.equals(JAVA_LANG)));
+
+ test = getJavaImportPackage(getStubYangType(TYPE2), true, pluginConfig);
+ assertThat(true, is(test.equals(JAVA_LANG)));
+
+ test = getJavaImportPackage(getStubYangType(TYPE3), false, pluginConfig);
+ assertThat(null, is(test));
+
+ test = getJavaImportPackage(getStubYangType(TYPE4), false, pluginConfig);
+ assertThat(null, is(test));
+ }
+
+ /**
+ * Unit test case for typedef.
+ *
+ * @throws DataModelException when fails to do data model operations
+ */
+ @Test
+ public void testForTypeDef() throws DataModelException {
+ test = getJavaImportPackage(getStubExtendedInfo(getStubYangType(TYPE_DEF)), false, pluginConfig);
+ assertThat(true, is(test.equals(TYPE_DEF_PKG)));
+ }
+
+ /**
+ * Returns stub YANG type for test.
+ *
+ * @param dataTypes YANG data types
+ * @return YANG type
+ */
+ private YangType<?> getStubYangType(YangDataTypes dataTypes) {
+ YangType<?> type = new YangType<>();
+ type.setDataType(dataTypes);
+ return type;
+ }
+
+ /**
+ * Returns YANG type with extended info.
+ *
+ * @param type YANG type
+ * @return YANG type with extended info
+ * @throws DataModelException when fails to do data model operations
+ */
+ @SuppressWarnings("unchecked")
+ private YangType<?> getStubExtendedInfo(YangType<?> type) throws DataModelException {
+ YangJavaTypeDef typedef = new YangJavaTypeDef();
+ getStubParent().addChild(typedef);
+ YangDerivedInfo<?> derInfo = new YangDerivedInfo<>();
+ derInfo.setReferredTypeDef(typedef);
+ ((YangType<YangDerivedInfo<?>>) type).setDataTypeExtendedInfo(derInfo);
+ return type;
+ }
+
+ /**
+ * Returns java file info.
+ *
+ * @return java file info
+ */
+ private JavaFileInfo addStubJavaFileInfo() {
+ JavaFileInfo fileInfo = new JavaFileInfo();
+ fileInfo.setJavaName("test");
+ fileInfo.setPackage("target");
+ return fileInfo;
+ }
+
+ /**
+ * Adds stub parent module for typedef.
+ *
+ * @return stub parent module
+ */
+ private YangNode getStubParent() {
+ YangJavaModule parent = new YangJavaModule();
+ parent.setJavaFileInfo(addStubJavaFileInfo());
+ return parent;
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/ChoiceCaseTranslatorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/ChoiceCaseTranslatorTest.java
new file mode 100644
index 0000000..74043f6
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/ChoiceCaseTranslatorTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Unit tests for choice-case translator.
+ */
+public final class ChoiceCaseTranslatorTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks choice-case translation should not result in any exception.
+ */
+ @Test
+ public void processChoiceCaseTranslator() throws IOException, ParserException {
+
+ String userDir = System.getProperty("user.dir");
+ YangNode node = manager.getDataModel("src/test/resources/ChoiceCaseTranslator.yang");
+
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir(userDir + "/target/ChoiceCaseTestGenFile/");
+
+ generateJavaCode(node, yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/ChoiceCaseTestGenFile/");
+ }
+ // TODO enhance the test cases, after having a framework of translator test.
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGeneratorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGeneratorTest.java
new file mode 100644
index 0000000..39d5205
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGeneratorTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Test;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
+import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
+
+/**
+ * Unit tests for class definition generator for generated files.
+ */
+public final class ClassDefinitionGeneratorTest {
+
+ private static final String CLASS_NAME = "TestClass";
+ private static final String INTERFACE_CLASS_DEF = "public interface TestClass {\n";
+ private static final String BULDER_INTERFACE_CLASS_DEF = "interface TestClassBuilder {\n\n";
+ private static final String BUILDER_CLASS_DEF = "public class TestClassBuilder implements "
+ + "TestClass.TestClassBuilder {\n";
+ private static final String IMPL_CLASS_DEF = "public final class TestClassImpl implements TestClass {\n";
+ private static final String TYPE_DEF_CLASS_DEF = "public final class TestClass {\n";
+
+ /**
+ * Unit test for private constructor.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * Unit test for builder class definition.
+ */
+ @Test
+ public void generateBuilderClassDefinitionTest() {
+ String builderClassDefinition = generateClassDefinition(BUILDER_CLASS_MASK, CLASS_NAME);
+ assertThat(true, is(builderClassDefinition.equals(BUILDER_CLASS_DEF)));
+ }
+
+ /**
+ * Unit test for builder interface definition.
+ */
+ @Test
+ public void generateBuilderInterfaceDefinitionTest() {
+ String builderInterfaceDefinition = generateClassDefinition(BUILDER_INTERFACE_MASK, CLASS_NAME);
+ assertThat(true, is(builderInterfaceDefinition.equals(BULDER_INTERFACE_CLASS_DEF)));
+ }
+
+ /**
+ * Unit test for impl class definition.
+ */
+ @Test
+ public void generateImplDefinitionTest() {
+ String implDefinition = generateClassDefinition(IMPL_CLASS_MASK, CLASS_NAME);
+ assertThat(true, is(implDefinition.equals(IMPL_CLASS_DEF)));
+ }
+
+ /**
+ * Unit test for interface definition.
+ */
+ @Test
+ public void generateinterfaceDefinitionTest() {
+ // TODO: need to add this test case.
+ }
+
+ /**
+ * Unit test for typedef generated type.
+ */
+ @Test
+ public void generateTypeDefTest() {
+ String typeDef = generateClassDefinition(GENERATE_TYPEDEF_CLASS, CLASS_NAME);
+ assertThat(true, is(typeDef.equals(TYPE_DEF_CLASS_DEF)));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/EnumTranslatorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/EnumTranslatorTest.java
new file mode 100644
index 0000000..2a950fc
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/EnumTranslatorTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Unit test case for enum translator.
+ */
+public final class EnumTranslatorTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks enum translation should not result in any exception.
+ */
+ @Test
+ public void processEnumTranslator()
+ throws IOException, ParserException {
+
+ String userDir = System.getProperty("user.dir");
+ YangNode node = manager.getDataModel("src/test/resources/EnumTranslator.yang");
+
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir(userDir + "/target/EnumTestGenFile/");
+
+ generateJavaCode(node, yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/EnumTestGenFile/");
+ }
+ // TODO enhance the test cases, after having a framework of translator test.
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java
new file mode 100644
index 0000000..fd5c0d3
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getImportText;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
+import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
+import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
+import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
+import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
+import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
+import static org.onosproject.yangutils.utils.UtilConstants.LIST;
+import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
+import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
+import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
+import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
+import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
+import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
+
+/**
+ * Unit test cases for java code snippet generator.
+ */
+public class JavaCodeSnippetGenTest {
+
+ private static final String PKG_INFO = "org.onosproject.unittest";
+ private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
+ private static final String YANG_NAME = "Test";
+
+ /**
+ * Unit test for private constructor.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * Unit test case for import text.
+ */
+ @Test
+ public void testForImportText() {
+ JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
+ importInfo.setPkgInfo(PKG_INFO);
+ importInfo.setClassInfo(CLASS_INFO);
+
+ String imports = getImportText(importInfo);
+
+ assertThat(true, is(imports.equals(IMPORT + PKG_INFO + PERIOD + CLASS_INFO + SEMI_COLAN + NEW_LINE)));
+ }
+
+ /**
+ * Unit test case for java class interface definition close.
+ */
+ @Test
+ public void testForJavaClassDefClose() {
+ String interfaceDef = getJavaClassDefClose();
+ assertThat(true, is(interfaceDef.equals(CLOSE_CURLY_BRACKET)));
+ }
+
+ /**
+ * Unit test case for java attribute info.
+ */
+ @Test
+ public void testForJavaAttributeInfo() {
+
+ String attributeWithoutTypePkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, false);
+ assertThat(true, is(attributeWithoutTypePkg.equals(
+ PRIVATE + SPACE + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
+
+ String attributeWithTypePkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, false);
+ assertThat(true, is(attributeWithTypePkg.equals(PRIVATE + SPACE + JAVA_LANG + PERIOD
+ + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
+
+ String attributeWithListPkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, true);
+ assertThat(true, is(attributeWithListPkg.equals(
+ PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + JAVA_LANG + PERIOD + STRING_DATA_TYPE
+ + DIAMOND_CLOSE_BRACKET + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
+
+ String attributeWithListWithoutPkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, true);
+ assertThat(true, is(attributeWithListWithoutPkg.equals(
+ PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET + SPACE
+ + YANG_NAME + SEMI_COLAN + NEW_LINE)));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java
new file mode 100644
index 0000000..6b86b9c
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java
@@ -0,0 +1,284 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.translator.exception.TranslatorException;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
+import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
+import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
+
+/**
+ * Unit tests for java identifier syntax.
+ */
+public final class JavaIdentifierSyntaxTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private static final String PARENT_PACKAGE = "test5/test6/test7";
+ private static final String CHILD_PACKAGE = "test1:test2:test3";
+ private static final String DATE1 = "2000-1-5";
+ private static final String DATE2 = "1992-01-25";
+ private static final String PARENT_WITH_PERIOD = "test5.test6.test7";
+ private static final String CHILD_WITH_PERIOD = "test1.test2.test3";
+ private static final String DATE_WITH_REV1 = "rev20000105";
+ private static final String DATE_WITH_REV2 = "rev19920125";
+ private static final String VERSION_NUMBER = "v1";
+ private static final String VALID_PREFIX = "123add-prefix";
+ private static final String INVALID_PREFIX = "-*()&^&#$%";
+ private static final String INVALID_PREFIX1 = "abc~!@#$%^&*()_+}{:<>?`1234567890-=[]''|,./SS";
+ private static final String INVALID_NAME_SPACE_FOR_INVALID_PREFIX = "try:#test3:9case3";
+ private static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
+ private static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
+ private static final String INVALID_NAME_SPACE3 = "CONST:TRY://9test3";
+ private static final String VALID_NAME_SPACE1 = "123addprefixbyte.test2.123addprefix9test3";
+ private static final String VALID_NAME_SPACE2 = "yangautoprefixconst.test2.yangautoprefix9test3";
+ private static final String VALID_NAME_SPACE3 = "abc1234567890ssconst.test2.abc1234567890ss9test3";
+ private static final String VALID_NAME_SPACE4 = "yangautoprefixconst.yangautoprefixtry.yangautoprefix9test3";
+ private static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
+ private static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
+ private static final String WITHOUT_CAMEL_CASE1 = ".-_try-._-.123";
+ private static final String WITH_CAMEL_CASE1 = "try123";
+ private static final String WITHOUT_CAMEL_CASE2 = "_try";
+ private static final String WITH_CAMEL_CASE2 = "yangAutoPrefixTry";
+ private static final String WITHOUT_CAMEL_CASE3 = "-1-123g-123ga-a";
+ private static final String WITH_CAMEL_CASE3 = "yangAutoPrefix1123G123Gaa";
+ private static final String WITHOUT_CAMEL_CASE4 = "a-b-c-d-e-f-g-h";
+ private static final String WITH_CAMEL_CASE4 = "aBcDeFgh";
+ private static final String WITHOUT_CAMEL_CASE5 = "TestName";
+ private static final String WITH_CAMEL_CASE5 = "testName";
+ private static final String WITHOUT_CAMEL_CASE6 = "TEST-NAME";
+ private static final String WITH_CAMEL_CASE6 = "testName";
+ private static final String WITHOUT_CAMEL_CASE7 = "TESTNAME";
+ private static final String WITH_CAMEL_CASE7 = "testname";
+ private static final String WITHOUT_CAMEL_CASE8 = "TE-ST-NA-ME";
+ private static final String WITH_CAMEL_CASE8 = "teStNaMe";
+ private static final String WITHOUT_CAMEL_CASE9 = "TEST3NAME";
+ private static final String WITH_CAMEL_CASE9 = "test3Name";
+ private static final String WITHOUT_CAMEL_CASE10 = "TEST3";
+ private static final String WITH_CAMEL_CASE10 = "test3";
+ private static final String WITHOUT_CAMEL_CASE11 = "TEST3nAMe";
+ private static final String WITH_CAMEL_CASE11 = "test3Name";
+ private static final String WITHOUT_CAMEL_CASE12 = "TEST3name";
+ private static final String WITH_CAMEL_CASE12 = "test3Name";
+ private static final String WITHOUT_CAMEL_CASE13 = "t-RY";
+ private static final String WITH_CAMEL_CASE13 = "tRy";
+ private static final String WITHOUT_CAMEL_CASE14 = "TRY";
+ private static final String WITH_CAMEL_CASE14 = "yangAutoPrefixTry";
+ private static final String WITHOUT_CAPITAL = "test_this";
+ private static final String WITH_CAPITAL = "Test_this";
+ private static final String WITH_SMALL = "test_this";
+ private static final String WITH_CAMEL_CASE_WITH_PREFIX = "123addPrefixTry";
+ private static final String WITH_CAMEL_CASE_WITH_PREFIX1 = "abc1234567890Ss1123G123Gaa";
+ private static YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
+
+ /**
+ * Unit test for private constructor.
+ *
+ * @throws SecurityException if any security violation is observed.
+ * @throws NoSuchMethodException if when the method is not found.
+ * @throws IllegalArgumentException if there is illegal argument found.
+ * @throws InstantiationException if instantiation is provoked for the
+ * private constructor.
+ * @throws IllegalAccessException if instance is provoked or a method is
+ * provoked.
+ * @throws InvocationTargetException when an exception occurs by the method
+ * or constructor.
+ */
+ @Test
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * Unit test for root package generation with revision complexity.
+ */
+ @Test
+ public void getRootPackageTest() {
+ conflictResolver.setPrefixForIdentifier(null);
+ String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1, conflictResolver);
+ assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
+ }
+
+ /**
+ * Unit test for root package generation with invalid prefix.
+ */
+ @Test
+ public void getRootPackageWithInvalidPrefix() throws TranslatorException {
+ thrown.expect(TranslatorException.class);
+ thrown.expectMessage("The given prefix in pom.xml is invalid.");
+ conflictResolver.setPrefixForIdentifier(INVALID_PREFIX);
+ String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE_FOR_INVALID_PREFIX, DATE1, conflictResolver);
+ }
+
+ /**
+ * Unit test for root package generation with special characters presence.
+ */
+ @Test
+ public void getRootPackageWithSpecialCharactersTest() {
+ conflictResolver.setPrefixForIdentifier(VALID_PREFIX);
+ String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1, conflictResolver);
+ assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ + PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
+ conflictResolver.setPrefixForIdentifier(null);
+ String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1, conflictResolver);
+ assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ + PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
+ String rootPackage2 = getRootPackage((byte) 1, INVALID_NAME_SPACE3, DATE1, conflictResolver);
+ assertThat(rootPackage2.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ + PERIOD + VALID_NAME_SPACE4 + PERIOD + DATE_WITH_REV1), is(true));
+ conflictResolver.setPrefixForIdentifier(INVALID_PREFIX1);
+ String rootPackage3 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1, conflictResolver);
+ assertThat(rootPackage3.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ + PERIOD + VALID_NAME_SPACE3 + PERIOD + DATE_WITH_REV1), is(true));
+
+ }
+
+ /**
+ * Unit test for root package generation without complexity in revision.
+ */
+ @Test
+ public void getRootPackageWithRevTest() {
+ String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2, null);
+ assertThat(rootPkgWithRev.equals(
+ DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
+ is(true));
+ }
+
+ /**
+ * Unit test for capitalizing the incoming string.
+ */
+ @Test
+ public void getCapitalCaseTest() {
+ String capitalCase = getCapitalCase(WITHOUT_CAPITAL);
+ assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
+ }
+
+ /**
+ * Unit test for getting the camel case for the received string.
+ */
+ @Test
+ public void getCamelCaseTest() {
+ conflictResolver.setPrefixForIdentifier(null);
+ String camelCase = getCamelCase(WITHOUT_CAMEL_CASE, conflictResolver);
+ assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
+ String camelCase1 = getCamelCase(WITHOUT_CAMEL_CASE1, conflictResolver);
+ assertThat(camelCase1.equals(WITH_CAMEL_CASE1), is(true));
+ String camelCase2 = getCamelCase(WITHOUT_CAMEL_CASE2, conflictResolver);
+ assertThat(camelCase2.equals(WITH_CAMEL_CASE2), is(true));
+ String camelCase3 = getCamelCase(WITHOUT_CAMEL_CASE3, conflictResolver);
+ assertThat(camelCase3.equals(WITH_CAMEL_CASE3), is(true));
+ String camelCase4 = getCamelCase(WITHOUT_CAMEL_CASE4, conflictResolver);
+ assertThat(camelCase4.equals(WITH_CAMEL_CASE4), is(true));
+ String camelCase5 = getCamelCase(WITHOUT_CAMEL_CASE5, conflictResolver);
+ assertThat(camelCase5.equals(WITH_CAMEL_CASE5), is(true));
+ String camelCase6 = getCamelCase(WITHOUT_CAMEL_CASE6, conflictResolver);
+ assertThat(camelCase6.equals(WITH_CAMEL_CASE6), is(true));
+ String camelCase7 = getCamelCase(WITHOUT_CAMEL_CASE7, conflictResolver);
+ assertThat(camelCase7.equals(WITH_CAMEL_CASE7), is(true));
+ String camelCase8 = getCamelCase(WITHOUT_CAMEL_CASE8, conflictResolver);
+ assertThat(camelCase8.equals(WITH_CAMEL_CASE8), is(true));
+ String camelCase9 = getCamelCase(WITHOUT_CAMEL_CASE9, conflictResolver);
+ assertThat(camelCase9.equals(WITH_CAMEL_CASE9), is(true));
+ String camelCase10 = getCamelCase(WITHOUT_CAMEL_CASE10, conflictResolver);
+ assertThat(camelCase10.equals(WITH_CAMEL_CASE10), is(true));
+ String camelCase11 = getCamelCase(WITHOUT_CAMEL_CASE11, conflictResolver);
+ assertThat(camelCase11.equals(WITH_CAMEL_CASE11), is(true));
+ String camelCase12 = getCamelCase(WITHOUT_CAMEL_CASE12, conflictResolver);
+ assertThat(camelCase12.equals(WITH_CAMEL_CASE12), is(true));
+ String camelCase13 = getCamelCase(WITHOUT_CAMEL_CASE13, conflictResolver);
+ assertThat(camelCase13.equals(WITH_CAMEL_CASE13), is(true));
+ String camelCase14 = getCamelCase(WITHOUT_CAMEL_CASE14, conflictResolver);
+ assertThat(camelCase14.equals(WITH_CAMEL_CASE14), is(true));
+ }
+
+ /**
+ * Unit test for getting the camel case along with the prefix provided.
+ */
+ @Test
+ public void getCamelCaseWithPrefixTest() {
+
+ conflictResolver.setPrefixForIdentifier(VALID_PREFIX);
+ String camelCase = getCamelCase(WITHOUT_CAMEL_CASE2, conflictResolver);
+ assertThat(camelCase.equals(WITH_CAMEL_CASE_WITH_PREFIX), is(true));
+ conflictResolver.setPrefixForIdentifier(INVALID_PREFIX1);
+ String camelCase2 = getCamelCase(WITHOUT_CAMEL_CASE3, conflictResolver);
+ assertThat(camelCase2.equals(WITH_CAMEL_CASE_WITH_PREFIX1), is(true));
+ }
+
+ /**
+ * Unit test for getting the camel case along with the invalid prefix provided.
+ */
+ @Test
+ public void getCamelCaseWithInvalidPrefixTest() throws TranslatorException {
+
+ thrown.expect(TranslatorException.class);
+ thrown.expectMessage("The given prefix in pom.xml is invalid.");
+ conflictResolver.setPrefixForIdentifier(INVALID_PREFIX);
+ String camelCase = getCamelCase(WITHOUT_CAMEL_CASE3, conflictResolver);
+ }
+
+ /**
+ * Unit test for getting the camel case for the received string.
+ */
+ @Test
+ public void getSmallCaseTest() {
+ String smallCase = getSmallCase(WITHOUT_CAPITAL);
+ assertThat(smallCase.equals(WITH_SMALL), is(true));
+ }
+
+ /**
+ * Unit test for getting the camel case for the received string.
+ */
+ @Test
+ public void getPackageFromPathTest() {
+ String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
+ assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
+ }
+
+ /**
+ * Unit test for getting the camel case for the received string.
+ */
+ @Test
+ public void getPathFromPackageTest() {
+ String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
+ assertThat(path.equals(PARENT_PACKAGE), is(true));
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGeneratorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGeneratorTest.java
new file mode 100644
index 0000000..05fe52d
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGeneratorTest.java
@@ -0,0 +1,318 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
+import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuild;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildForInterface;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getCheckNotNull;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForInterface;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForInterface;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
+import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
+import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
+import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
+import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
+import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
+import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
+import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
+import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
+import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
+import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
+import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
+import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
+import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
+import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
+import static org.onosproject.yangutils.utils.UtilConstants.NEW;
+import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
+import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
+import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
+import static org.onosproject.yangutils.utils.UtilConstants.OF;
+import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
+import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
+import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE;
+import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
+import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
+import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
+import static org.onosproject.yangutils.utils.UtilConstants.RETURN;
+import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
+import static org.onosproject.yangutils.utils.UtilConstants.SET_METHOD_PREFIX;
+import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION;
+import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
+import static org.onosproject.yangutils.utils.UtilConstants.STATIC;
+import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
+import static org.onosproject.yangutils.utils.UtilConstants.SUFFIX_S;
+import static org.onosproject.yangutils.utils.UtilConstants.THIS;
+import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
+import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
+import static org.onosproject.yangutils.utils.UtilConstants.VOID;
+
+/**
+ * Unit tests for generated methods from the file type.
+ */
+public final class MethodsGeneratorTest {
+
+ private static final String CLASS_NAME = "testname";
+ private static final String ATTRIBUTE_NAME = "testname";
+
+ /**
+ * Unit test for private constructor.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {MethodsGenerator.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * Unit test case for checking the parse builder and type constructor.
+ */
+ @Test
+ public void getTypeConstructorTest() {
+
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String test = getTypeConstructorStringAndJavaDoc(testAttr, CLASS_NAME, pluginConfig);
+ assertThat(true, is(test.contains(PUBLIC + SPACE + CLASS_NAME + OPEN_PARENTHESIS)));
+ }
+
+ /**
+ * Test for build method for class.
+ */
+ @Test
+ public void getBuildTest() {
+ String method = getBuild(CLASS_NAME);
+ assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + PUBLIC + SPACE + CLASS_NAME + SPACE + BUILD
+ + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
+ + RETURN + SPACE + NEW + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS
+ + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET)));
+
+ }
+
+ /**
+ * Test for build method of interface.
+ */
+ @Test
+ public void getBuildForInterfaceTest() {
+ String method = getBuildForInterface(CLASS_NAME);
+ assertThat(true, is(method.equals(FOUR_SPACE_INDENTATION + CLASS_NAME + SPACE + BUILD +
+ OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
+ }
+
+ /**
+ * Test for check not null method.
+ */
+ @Test
+ public void getCheckNotNullTest() {
+ String method = getCheckNotNull(CLASS_NAME);
+ assertThat(true, is(method.equals(EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + OPEN_PARENTHESIS
+ + CLASS_NAME + COMMA + SPACE + CLASS_NAME + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE)));
+ }
+
+ /**
+ * Test case for constructor.
+ */
+ @Test
+ public void getConstructorTest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ String method = getConstructor(CLASS_NAME, testAttr, GENERATE_SERVICE_AND_MANAGER, pluginConfig);
+ assertThat(true, is(method.contains(THIS + PERIOD + CLASS_NAME + SPACE + EQUAL + SPACE + "builder" + OBJECT
+ + PERIOD + GET_METHOD_PREFIX + "Testname" + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN)));
+ }
+
+ /**
+ * Test for constrcutor start method.
+ */
+ @Test
+ public void getConstructorStartTest() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ String method = getConstructorStart(CLASS_NAME, pluginConfig);
+ assertThat(true, is(method.contains(PUBLIC + SPACE + CLASS_NAME + IMPL + OPEN_PARENTHESIS + CLASS_NAME
+ + BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE
+ + OPEN_CURLY_BRACKET + NEW_LINE)));
+ }
+
+ /**
+ * Test case for equals method.
+ */
+ @Test
+ public void getEqualsMethodTest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String method = getEqualsMethod(testAttr);
+ assertThat(true, is(method.contains(SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD
+ + EQUALS_STRING + OPEN_PARENTHESIS)));
+ }
+
+ /**
+ * Test for to string method.
+ */
+ @Test
+ public void getToStringMethodTest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String method = getToStringMethod(testAttr);
+ assertThat(true, is(method.equals(
+ TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + testAttr.getAttributeName()
+ + QUOTES + COMMA + SPACE + testAttr.getAttributeName() + CLOSE_PARENTHESIS)));
+ }
+
+ /**
+ * Test for getter method of class.
+ */
+ @Test
+ public void getGetterForClassTest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String method = getGetterForClass(testAttr, GENERATE_SERVICE_AND_MANAGER);
+ assertThat(true, is(method.contains(PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
+ }
+
+ /**
+ * Test for getter of interface.
+ */
+ @Test
+ public void getGetterForInterfaceTest() {
+ String method = getGetterForInterface(CLASS_NAME, STRING_DATA_TYPE, false, GENERATE_SERVICE_AND_MANAGER);
+ assertThat(true, is(method.contains(STRING_DATA_TYPE + SPACE + GET_METHOD_PREFIX)));
+ }
+
+ /**
+ * Test case for setter method of class.
+ */
+ @Test
+ public void getSetterForClassTest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String method = getSetterForClass(testAttr, CLASS_NAME, GENERATE_SERVICE_AND_MANAGER);
+ assertThat(true, is(
+ method.contains(PUBLIC + SPACE + VOID + SPACE +
+ SET_METHOD_PREFIX + getCapitalCase(CLASS_NAME) + OPEN_PARENTHESIS +
+ STRING_DATA_TYPE + SPACE + ATTRIBUTE_NAME)));
+ }
+
+ /**
+ * Test for setter method of interface.
+ */
+ @Test
+ public void getSetterForInterfaceTest() {
+ String method = getSetterForInterface(CLASS_NAME, STRING_DATA_TYPE, CLASS_NAME, false,
+ GENERATE_SERVICE_AND_MANAGER);
+ assertThat(true, is(method.contains(VOID + SPACE +
+ SET_METHOD_PREFIX + "Testname")));
+ }
+
+ /**
+ * Test case for of method.
+ */
+ @Test
+ public void getOfMethodest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String method = getOfMethod(CLASS_NAME, testAttr);
+ assertThat(true, is(method.contains(PUBLIC + SPACE + STATIC + SPACE + CLASS_NAME + SPACE + OF + OPEN_PARENTHESIS
+ + STRING_DATA_TYPE + SPACE + VALUE + CLOSE_PARENTHESIS)));
+ }
+
+ /**
+ * Test case for setter in type def class.
+ */
+ @Test
+ public void getSetterForTypeDefClassTest() {
+ JavaAttributeInfo testAttr = getTestAttribute();
+ String method = getSetterForTypeDefClass(testAttr);
+ assertThat(true, is(method.contains(PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX)));
+ }
+
+ /**
+ * Test case for over ride string.
+ */
+ @Test
+ public void getOverRideStringTest() {
+ String method = getOverRideString();
+ assertThat(true, is(method.contains(OVERRIDE)));
+ }
+
+ /**
+ * Returns java attribute.
+ *
+ * @return java attribute
+ */
+ private JavaAttributeInfo getTestAttribute() {
+ JavaAttributeInfo testAttr = new JavaAttributeInfo(getTestYangType(), ATTRIBUTE_NAME, false, false);
+ testAttr.setAttributeName(ATTRIBUTE_NAME);
+ testAttr.setAttributeType(getTestYangType());
+ testAttr.setImportInfo(getTestJavaQualifiedTypeInfo());
+ return testAttr;
+ }
+
+ /**
+ * Returns java qualified info.
+ *
+ * @return java qualified info
+ */
+ private JavaQualifiedTypeInfo getTestJavaQualifiedTypeInfo() {
+ JavaQualifiedTypeInfo info = new JavaQualifiedTypeInfo();
+ info.setPkgInfo(JAVA_LANG);
+ info.setClassInfo(STRING_DATA_TYPE);
+ return info;
+ }
+
+ /**
+ * Returns stub YANG type.
+ *
+ * @return test YANG type
+ */
+ private YangType<?> getTestYangType() {
+ YangType<?> attrType = new YangType<>();
+ attrType.setDataTypeName(STRING_DATA_TYPE);
+ attrType.setDataType(STRING);
+ return attrType;
+ }
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/NotificationTranslatorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/NotificationTranslatorTest.java
new file mode 100644
index 0000000..f4d8bf8
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/NotificationTranslatorTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Unit tests for union translator.
+ */
+public final class NotificationTranslatorTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks union translation should not result in any exception.
+ */
+ @Test
+ public void processUnionTranslator()
+ throws IOException, ParserException {
+
+ String userDir = System.getProperty("user.dir");
+ YangNode node = manager.getDataModel("src/test/resources/NotificationTest.yang");
+
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir(userDir + "/target/NotificationTest/");
+
+ generateJavaCode(node, yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/NotificationTest/");
+ }
+
+ // TODO enhance the test cases, after having a framework of translator test.
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/RpcTranslatorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/RpcTranslatorTest.java
new file mode 100644
index 0000000..9b8077c
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/RpcTranslatorTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Unit tests for rpc translator.
+ */
+public final class RpcTranslatorTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks rpc translation should not result in any exception.
+ */
+ @Test
+ public void processRpcTranslator()
+ throws IOException, ParserException {
+
+ String userDir = System.getProperty("user.dir");
+ YangNode node = manager.getDataModel("src/test/resources/RpcTranslator.yang");
+
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir(userDir + "/target/RpcTestGenFile/");
+
+ generateJavaCode(node, yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/RpcTestGenFile/");
+ }
+ // TODO enhance the test cases, after having a framework of translator test.
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/UnionTranslatorTest.java b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/UnionTranslatorTest.java
new file mode 100644
index 0000000..9dcdfdf
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/translator/tojava/utils/UnionTranslatorTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+
+import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Unit tests for union translator.
+ */
+public final class UnionTranslatorTest {
+
+ private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+ /**
+ * Checks union translation should not result in any exception.
+ */
+ @Test
+ public void processUnionTranslator()
+ throws IOException, ParserException {
+
+ String userDir = System.getProperty("user.dir");
+ YangNode node = manager.getDataModel("src/test/resources/UnionTranslator.yang");
+
+ YangPluginConfig yangPluginConfig = new YangPluginConfig();
+ yangPluginConfig.setCodeGenDir("target/UnionTestGenFile/");
+
+ generateJavaCode(node, yangPluginConfig);
+
+ deleteDirectory(userDir + "/target/UnionTestGenFile/");
+ }
+
+ // TODO enhance the test cases, after having a framework of translator test.
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/UtilConstantsTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/UtilConstantsTest.java
new file mode 100644
index 0000000..a63d432
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/UtilConstantsTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test case for testing the util constants.
+ */
+public final class UtilConstantsTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {UtilConstants.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
new file mode 100644
index 0000000..40ac92c
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Calendar;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.apache.commons.io.FileUtils.contentEquals;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
+
+/**
+ * Unit Tests for the CopyrightHeader contents.
+ */
+public final class CopyrightHeaderTest {
+
+ private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
+ + "-present Open Networking Laboratory\n";
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * Unit test for testing private constructor.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {CopyrightHeader.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks the received copyright header contents.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void testGetCopyrightHeader() throws IOException {
+
+ String path = "src/test/resources/CopyrightHeader.txt";
+
+ File testRsc = new File(path);
+ FileInputStream in = new FileInputStream(testRsc);
+
+ File testFile = new File("target/TestHeader.txt");
+ FileOutputStream out = new FileOutputStream(testFile);
+
+ out.write(COPYRIGHTS_FIRST_LINE.getBytes());
+ int c = 0;
+ while ((c = in.read()) != -1) {
+ out.write(c);
+ }
+
+ String licenseHeader = getCopyrightHeader();
+ File test = new File("target/TestCopyrightHeader.txt");
+
+ FileWriter writer = new FileWriter(test);
+ writer.write(licenseHeader);
+ writer.close();
+ out.close();
+ out.flush();
+ in.close();
+
+ assertThat(true, is(contentEquals(test, testFile)));
+ test.delete();
+ testFile.delete();
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java
new file mode 100644
index 0000000..5fd728b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.doesPackageExist;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
+
+/**
+ * Tests the file handle utilities.
+ */
+public final class FileSystemUtilTest {
+
+ private static final String BASE_DIR_PKG = "target.UnitTestCase.";
+ private static final String BASE_PKG = "target/UnitTestCase";
+ private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
+ private static final String TEST_DATA_2 = "This is next second line\n";
+ private static final String TEST_DATA_3 = "This is next third line in the file";
+ private static final String TEST_FILE = "testFile";
+ private static final String SOURCE_TEST_FILE = "sourceTestFile";
+ private static final String DIR_PATH = "exist1.exist2.exist3";
+ private static final String PKG_INFO = "package-info.java";
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {FileSystemUtil.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks the contents to be written in the file.
+ *
+ * @throws IOException when fails to create a test file
+ */
+ @Test
+ public void updateFileHandleTest() throws IOException {
+
+ File dir = new File(BASE_PKG + SLASH + TEST_FILE);
+ dir.mkdirs();
+ File createFile = new File(dir + TEST_FILE);
+ createFile.createNewFile();
+ File createSourceFile = new File(dir + SOURCE_TEST_FILE);
+ createSourceFile.createNewFile();
+ updateFileHandle(createFile, TEST_DATA_1, false);
+ updateFileHandle(createFile, TEST_DATA_2, false);
+ updateFileHandle(createFile, TEST_DATA_3, false);
+ appendFileContents(createFile, createSourceFile);
+ updateFileHandle(createFile, null, true);
+ deleteDirectory(dir);
+ }
+
+ /**
+ * This test case checks whether the package is existing.
+ *
+ * @throws IOException when failed to create a test file
+ */
+ @Test
+ public void packageExistTest() throws IOException {
+
+ String strPath = BASE_DIR_PKG + DIR_PATH;
+ File createDir = new File(strPath.replace(PERIOD, SLASH));
+ createDir.mkdirs();
+ File createFile = new File(createDir + SLASH + PKG_INFO);
+ createFile.createNewFile();
+ assertThat(true, is(doesPackageExist(strPath)));
+ createPackage(getStubNode());
+ createDir.delete();
+ deleteDirectory(createDir);
+ }
+
+ /**
+ * Returns stub YANG node.
+ *
+ * @return stub node
+ */
+ private YangNode getStubNode() {
+ YangJavaModule module = new YangJavaModule();
+ module.setName(TEST_DATA_1);
+ JavaFileInfo javafileInfo = new JavaFileInfo();
+ javafileInfo.setJavaName(TEST_DATA_1);
+ javafileInfo.setBaseCodeGenPath("");
+ javafileInfo.setPackageFilePath(BASE_PKG);
+ javafileInfo.setPluginConfig(getStubPluginConfig());
+ module.setJavaFileInfo(javafileInfo);
+ return module;
+ }
+
+ /**
+ * Returns stub pluginConfig.
+ *
+ * @return stub pluginConfig
+ */
+ private YangPluginConfig getStubPluginConfig() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ pluginConfig.setConflictResolver(null);
+ return pluginConfig;
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
new file mode 100644
index 0000000..2e9f436
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
+
+/**
+ * Tests the java doc that is generated.
+ */
+public final class JavaDocGenTest {
+
+ private static final String TEST_NAME = "testName";
+ private static final String END_STRING = " */\n";
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * This test case checks the content received for the builder class java doc.
+ */
+ @Test
+ public void builderClassGenerationTest() {
+ String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of")
+ && builderClassJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the builder interface ge java doc.
+ */
+ @Test
+ public void builderInterfaceGenerationTest() {
+ String builderInterfaceJavaDoc = getJavaDoc(BUILDER_INTERFACE, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(builderInterfaceJavaDoc.contains("Builder for")
+ && builderInterfaceJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the build java doc.
+ */
+ @Test
+ public void buildGenerationTest() {
+ String buildDoc = getJavaDoc(BUILD_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(buildDoc.contains("Builds object of") && buildDoc.contains(END_STRING)));
+ }
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {JavaDocGen.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks the content received for the constructor java doc.
+ */
+ @Test
+ public void constructorGenerationTest() {
+ String constructorDoc = getJavaDoc(CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(constructorDoc.contains("Creates an instance of ")
+ && constructorDoc.contains("builder object of")
+ && constructorDoc.contains("@param") && constructorDoc.contains("*/\n")));
+ }
+
+ /**
+ * This test case checks the content received for the default constructor java doc.
+ */
+ @Test
+ public void defaultConstructorGenerationTest() {
+ String defaultConstructorDoc = getJavaDoc(DEFAULT_CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(defaultConstructorDoc.contains("Creates an instance of ")
+ && defaultConstructorDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the getter java doc.
+ */
+ @Test
+ public void getterGenerationTest() {
+ String getterJavaDoc = getJavaDoc(GETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the impl class java doc.
+ */
+ @Test
+ public void implClassGenerationTest() {
+ String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(implClassJavaDoc.contains("Represents the implementation of")
+ && implClassJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the interface java doc.
+ */
+ @Test
+ public void interfaceGenerationTest() {
+ String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of")
+ && interfaceJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the package info java doc.
+ */
+ @Test
+ public void packageInfoGenerationTest() {
+ String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the package info java doc.
+ */
+ @Test
+ public void packageInfoGenerationForChildNodeTest() {
+ String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true, getStubPluginConfig());
+ assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes")
+ && packageInfo.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the setter java doc.
+ */
+ @Test
+ public void setterGenerationTest() {
+ String setterJavaDoc = getJavaDoc(SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true,
+ is(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(END_STRING)));
+ }
+
+ /**
+ * This test case checks the content received for the typedef setter java doc.
+ */
+ @Test
+ public void typeDefSetterGenerationTest() {
+ String typeDefSetter = getJavaDoc(TYPE_DEF_SETTER_METHOD, TEST_NAME, false, getStubPluginConfig());
+ assertThat(true, is(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(END_STRING)));
+ }
+
+ /**
+ * Returns stub pluginConfig.
+ *
+ * @return stub pluginConfig
+ */
+ private YangPluginConfig getStubPluginConfig() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ pluginConfig.setConflictResolver(null);
+ return pluginConfig;
+ }
+}
\ No newline at end of file
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
new file mode 100644
index 0000000..566d24b
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static java.io.File.separator;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles;
+import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
+
+/**
+ * Test the file scanner service.
+ */
+public final class YangFileScannerTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ String baseDir = "target/UnitTestCase";
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {YangFileScanner.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks for a .java file inside the specified dir.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void checkJavaFileInsideDirTest() throws IOException {
+
+ String dir = baseDir + separator + "scanner2";
+ File path = createDirectory(dir);
+ createFile(path, "testScanner.java");
+ List<String> dirContents = getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java");
+ assertThat(true, is(dirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+ /**
+ * Method used for creating multiple directories inside the target file.
+ *
+ * @param path where directories should be created
+ * @return the directory path that is created
+ */
+ private File createDirectory(String path) {
+
+ File myDir = new File(path);
+ myDir.mkdirs();
+ return myDir;
+ }
+
+ /**
+ * Method used for creating file inside the specified directory.
+ *
+ * @param myDir the path where file has to be created inside
+ * @param fileName the name of the file to be created
+ */
+ private void createFile(File myDir, String fileName) throws IOException {
+
+ File file = null;
+ file = new File(myDir + separator + fileName);
+ file.createNewFile();
+ }
+
+ /**
+ * This testcase checks for a java file inside an empty directory.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void emptyDirJavaScannerTest() throws IOException {
+
+ String emptyDir = baseDir + separator + "scanner1";
+ File path = createDirectory(emptyDir);
+ List<String> emptyDirContents = getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertThat(true, is(emptyDirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+ /**
+ * This testcase checks for a yang file inside an empty directory.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void emptyDirYangScannerTest() throws IOException {
+
+ String emptyYangDir = baseDir + separator + "scanner1";
+ File path = createDirectory(emptyYangDir);
+ List<String> emptyDirContents = getYangFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertThat(true, is(emptyDirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+ /**
+ * This test case checks with the sub directories in the given path for java files.
+ *
+ * @throws IOException when fails to do IO operations
+ */
+ @Test
+ public void emptySubDirScannerTest() throws IOException {
+
+ String dir = baseDir + separator + "scanner3";
+ File path = createDirectory(dir);
+ String subDir = path.toString() + separator + "subDir1";
+ createDirectory(subDir);
+ createFile(path, "invalidFile.txt");
+ List<String> emptySubDirContents = getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertThat(true, is(emptySubDirContents.equals(expectedContents)));
+ deleteDirectory(path);
+ }
+
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
new file mode 100644
index 0000000..c10ed03
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.project.MavenProject;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+import org.onosproject.yangutils.utils.UtilConstants;
+import org.sonatype.plexus.build.incremental.BuildContext;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
+
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToCompilationRoot;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
+
+/**
+ * Unit tests for YANG IO utils.
+ */
+public final class YangIoUtilsTest {
+
+ private static final String BASE_DIR = "target/UnitTestCase";
+ private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
+ private static final String CHECK_STRING = "one, two, three, four, five, six";
+ private static final String TRIM_STRING = "one, two, three, four, five, ";
+ private static final String CHECK1 = "check1";
+ private static final String PKG_INFO = "package-info.java";
+ private static final String PATH = "src/main/yangmodel/";
+ private static final String MSG = "Exception occured while creating package info file.";
+
+ /**
+ * Expected exceptions.
+ */
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ /**
+ * This test case checks whether the package-info file is created.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoTest() throws IOException {
+
+ File dirPath = new File(CREATE_PATH);
+ dirPath.mkdirs();
+ addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
+ File filePath = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath.isFile(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks with an additional info in the path.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoWithPathTest() throws IOException {
+
+ File dirPath = new File(CREATE_PATH);
+ dirPath.mkdirs();
+ addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false, getStubPluginConfig());
+ File filePath = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath.isFile(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks with a child node.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoWithChildNode() throws IOException {
+
+ File dirPath = new File(CREATE_PATH);
+ dirPath.mkdirs();
+ addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true, getStubPluginConfig());
+ File filePath = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath.isFile(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks whether the package-info file is created when invalid path is given.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void addPackageInfoWithEmptyPathTest() throws IOException {
+
+ File dirPath = new File("invalid/check");
+ thrown.expect(IOException.class);
+ thrown.expectMessage(MSG);
+ addPackageInfo(dirPath, CHECK1, CREATE_PATH, false, getStubPluginConfig());
+ File filePath1 = new File(dirPath + File.separator + PKG_INFO);
+ assertThat(filePath1.isFile(), is(false));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed
+ * @throws NoSuchMethodException if when the method is not found
+ * @throws IllegalArgumentException if there is illegal argument found
+ * @throws InstantiationException if instantiation is provoked for the private constructor
+ * @throws IllegalAccessException if instance is provoked or a method is provoked
+ * @throws InvocationTargetException when an exception occurs by the method or constructor
+ */
+ @Test
+ public void callPrivateConstructors()
+ throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {YangIoUtils.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertThat(null, not(constructor.newInstance()));
+ }
+ }
+
+ /**
+ * This test case checks if the directory is cleaned.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void cleanGeneratedDirTest() throws IOException {
+
+ File baseDirPath = new File(BASE_DIR);
+ File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
+ createNewDir.mkdirs();
+ File createFile = new File(createNewDir + File.separator + "check1.java");
+ createFile.createNewFile();
+ deleteDirectory(baseDirPath.getAbsolutePath());
+ FileUtils.deleteDirectory(createNewDir);
+ FileUtils.deleteDirectory(baseDirPath);
+ }
+
+ /**
+ * This test case checks the cleaning method when an invalid path is provided.
+ *
+ * @throws IOException when fails to do IO operations for test case
+ */
+ @Test
+ public void cleanWithInvalidDirTest() throws IOException {
+
+ File baseDirPath = new File(BASE_DIR + "invalid");
+ deleteDirectory(baseDirPath.getAbsolutePath());
+ }
+
+ /**
+ * This test case tests whether the directories are getting created.
+ */
+ @Test
+ public void createDirectoryTest() throws IOException {
+
+ File dirPath = createDirectories(CREATE_PATH);
+ assertThat(dirPath.isDirectory(), is(true));
+ FileUtils.deleteDirectory(dirPath);
+ }
+
+ /**
+ * This test case checks whether the source is getting added.
+ */
+ @Test
+ public void testForAddSource() throws IOException {
+
+ MavenProject project = new MavenProject();
+ BuildContext context = new DefaultBuildContext();
+ File sourceDir = new File(BASE_DIR + File.separator + "yang");
+ sourceDir.mkdirs();
+ addToCompilationRoot(sourceDir.toString(), project, context);
+ FileUtils.deleteDirectory(sourceDir);
+ }
+
+ /**
+ * Unit test case for trim at last method.
+ */
+ @Test
+ public void testForTrimAtLast() {
+
+ String test = trimAtLast(CHECK_STRING, "six");
+ assertThat(test.contains(TRIM_STRING), is(true));
+ }
+
+ /**
+ * Returns stub pluginConfig.
+ *
+ * @return stub pluginConfig
+ */
+ private YangPluginConfig getStubPluginConfig() {
+ YangPluginConfig pluginConfig = new YangPluginConfig();
+ pluginConfig.setConflictResolver(null);
+ return pluginConfig;
+ }
+}