[ONOS-4910, ONOS-4921] Framework: utils validation and defect fix

Change-Id: I821920fa8c88e64406b702c2b8736bdeaf231474
diff --git a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
index 2aab556..81e973f 100644
--- a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
+++ b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/DefaultListenerTest.java
@@ -19,13 +19,21 @@
 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.YangDerivedInfo;
 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.datamodel.YangType;
+import org.onosproject.yangutils.datamodel.YangTypeDef;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt64;
 import org.onosproject.yangutils.parser.exceptions.ParserException;
 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
 
@@ -36,6 +44,8 @@
  * Test cases for testing default listener functionality.
  */
 public class DefaultListenerTest {
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
 
     private final YangUtilsParserManager manager = new YangUtilsParserManager();
 
@@ -43,9 +53,9 @@
      * Checks if default value is set correctly.
      */
     @Test
-    public void processLeafSubStatementDefault() throws IOException, ParserException {
+    public void processDefaultValueInLeafSubStatement() throws IOException, ParserException {
 
-        YangNode node = manager.getDataModel("src/test/resources/LeafSubStatementDefault.yang");
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInLeafSubStatement.yang");
         // Check whether the data model tree returned is of type module.
         assertThat((node instanceof YangModule), is(true));
 
@@ -60,16 +70,28 @@
         YangLeaf leafInfo = leafIterator.next();
 
         assertThat(leafInfo.getName(), is("invalid-interval"));
-        assertThat(leafInfo.getDefaultValueInString(), is("\"1\""));
+        assertThat(leafInfo.getDefaultValueInString(), is("1"));
     }
 
     /**
-     * Checks if default value is set correctly.
+     * Validates default invalid value in leaf.
      */
     @Test
-    public void processChoiceSubStatementDefault() throws IOException, ParserException {
+    public void processDefaultInalueInLeafSubStatement() throws IOException, ParserException {
 
-        YangNode node = manager.getDataModel("src/test/resources/ChoiceSubStatementDefault.yang");
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"x\" is not a valid uint16.");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueInLeafSubStatement.yang");
+    }
+
+    /**
+     * Validates default case value in choice statement.
+     */
+    @Test
+    public void processDefaultCaseInChoiceSubStatement() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultCaseInChoiceSubStatement.yang");
         // Check whether the data model tree returned is of type module.
         assertThat((node instanceof YangModule), is(true));
 
@@ -85,6 +107,408 @@
 
         YangChoice yangChoice = (YangChoice) yangContainer.getChild();
         assertThat(yangChoice.getName(), is("snack"));
-        assertThat(yangChoice.getDefaultValueInString(), is("\"hello\""));
+        assertThat(yangChoice.getDefaultValueInString(), is("sports-arena"));
+    }
+
+    /**
+     * Validates default invalide case in choice statement.
+     */
+    @Test
+    public void processDefaultInvalidCaseInChoiceSubStatement() throws IOException, ParserException {
+
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("Internal parser error detected: Invalid content in choice \"snack\" after processing.");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueInChoiceSubStmt.yang");
+    }
+
+    /**
+     * Validates default value in typedef.
+     */
+    @Test
+    public void processDefaultInTypedef() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInTypeDef.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 typedef
+        YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
+        assertThat(typedef.getName(), is("topInt"));
+        assertThat(typedef.getDefaultValueInString(), is("10"));
+
+        YangType type = typedef.getTypeList().iterator().next();
+        assertThat(type.getDataType(), is(YangDataTypes.INT64));
+        assertThat(type.getDataTypeName(), is("int64"));
+
+        // check leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+        assertThat(leafInfo.getName(), is("myValue"));
+
+        // Check leaf reffered typedef
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("topInt"));
+        assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
+        YangType<YangDerivedInfo> typeDerived = (YangType<YangDerivedInfo>) leafInfo.getDataType();
+        YangDerivedInfo derivedInfo = (YangDerivedInfo) typeDerived.getDataTypeExtendedInfo();
+        YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
+        assertThat(prevTypedef.getName(), is("topInt"));
+        assertThat(prevTypedef.getDefaultValueInString(), is("10"));
+        YangType topType = prevTypedef.getTypeList().iterator().next();
+        assertThat(topType.getDataType(), is(YangDataTypes.INT64));
+        assertThat(topType.getDataTypeName(), is("int64"));
+        YangType<YangInt64> typeInt64 = (YangType<YangInt64>) topType;
+        YangInt64 int64Obj = typeInt64.getDataTypeExtendedInfo();
+    }
+
+    /**
+     * Validates invalid default value in typedef.
+     */
+    @Test
+    public void processInvalidDefaultValueInTypdeDef() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"x\" is not a valid int64.");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueInTypeDef.yang");
+    }
+
+    /**
+     * Validates default value decimal64 in leaf.
+     */
+    @Test
+    public void processDefaultValueDecimal64InLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueDecimal64InLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("mydecimal"));
+        assertThat(leafInfo.getDefaultValueInString(), is("5"));
+    }
+
+    /**
+     * Validates default invalid value decimal64 in leaf.
+     */
+    @Test
+    public void processDefaultInvalidValueDecimal64InLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"x\" is not a valid decimal64.");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueDecimal64InLeaf.yang");
+    }
+
+    /**
+     * Validates default value string in leaf.
+     */
+    @Test
+    public void processDefaultValueStringInLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueStringInLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("MyString"));
+        assertThat(leafInfo.getDefaultValueInString(), is("2bB"));
+    }
+
+    /**
+     * Validates default invalid value string in leaf.
+     */
+    @Test
+    public void processDefaultInvalidValueStringInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"2bB2bB\" is not a valid string");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueStringInLeaf.yang");
+    }
+
+    /**
+     * Validates default value boolean in leaf.
+     */
+    @Test
+    public void processDefaultValueBooleanInLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBooleanInLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("myboolean"));
+        assertThat(leafInfo.getDefaultValueInString(), is("true"));
+    }
+
+    /**
+     * Validates default invalid value boolean in leaf.
+     */
+    @Test
+    public void processDefaultInvalidValueBooleanInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"yes\" is not a valid boolean");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueBooleanInLeaf.yang");
+    }
+
+    /**
+     * Validates default value enumeration in leaf.
+     */
+    @Test
+    public void processDefaultValueEnumberationInLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueEnumerationInLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("myenum"));
+        assertThat(leafInfo.getDefaultValueInString(), is("one"));
+    }
+
+    /**
+     * Validates default invalid value enumeration in leaf.
+     */
+    @Test
+    public void processDefaultInvalidValueEnumberationInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueEnumerationInLeaf.yang");
+    }
+
+    /**
+     * Validates default value bits in leaf.
+     */
+    @Test
+    public void processDefaultValueBitsInLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBitsInLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("mybits"));
+        assertThat(leafInfo.getDefaultValueInString(), is("auto-sense-speed"));
+    }
+
+    /**
+     * Validates default invalid value bits in leaf.
+     */
+    @Test
+    public void processDefaultInvalidValueBitsInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid bits");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueBitsInLeaf.yang");
+    }
+
+    /**
+     * Validates default value binary in leaf.
+     */
+    @Test
+    public void processDefaultValueBinaryInLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBinaryInLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("message"));
+        assertThat(leafInfo.getDefaultValueInString(), is("10010010"));
+    }
+
+    /**
+     * Validates default invalid value binary in leaf.
+     */
+    @Test
+    public void processDefaultInvlaidValueBinaryInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"000\" is not a valid binary");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueBinaryInLeaf.yang");
+    }
+
+    /**
+     * Validates default value empty in leaf.
+     */
+    @Test
+    public void processDefaultValueEmptyInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"something\" is not a allowed for a data type empty");
+
+        manager.getDataModel("src/test/resources/default/DefaultValueEmptyInLeaf.yang");
+    }
+
+    /**
+     * Validates default value union in leaf.
+     */
+    @Test
+    public void processDefaultValueUnionInLeaf() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueUnionInLeaf.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 leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+
+        // check the default value
+        // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
+        assertThat(leafInfo.getName(), is("message"));
+        assertThat(leafInfo.getDefaultValueInString(), is("unbounded"));
+    }
+
+    /**
+     * Validates default invalid value union in leaf.
+     */
+    @Test
+    public void processDefaultInvalidValueUnionInLeaf() throws IOException, ParserException {
+        thrown.expect(DataTypeException.class);
+        thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union");
+
+        manager.getDataModel("src/test/resources/default/DefaultInvalidValueUnionInLeaf.yang");
+    }
+
+    /**
+     * Validates default value in multiple typedef.
+     */
+    @Test
+    public void processDefaultInMultiTypedef() throws IOException, ParserException {
+
+        YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInMultiTypeDef.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 typedef
+        YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
+        assertThat(typedef.getName(), is("topInt"));
+        assertThat(typedef.getDefaultValueInString(), is("10"));
+
+        YangType type = typedef.getTypeList().iterator().next();
+        assertThat(type.getDataType(), is(YangDataTypes.INT64));
+        assertThat(type.getDataTypeName(), is("int64"));
+
+        // check leaf
+        ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
+        YangLeaf leafInfo = leafIterator.next();
+        assertThat(leafInfo.getName(), is("lowInt"));
+
+        // check leaf type
+        assertThat(leafInfo.getName(), is("lowInt"));
+        assertThat(leafInfo.getDataType().getDataTypeName(), is("midInt"));
+        YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
+        assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
+        YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
+
+        // check previous typedef
+        YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
+        assertThat(prevTypedef.getName(), is("midInt"));
+        type = prevTypedef.getTypeList().iterator().next();
+        assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
+        derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
+
+        // check top typedef
+        YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
+        assertThat(topTypedef.getName(), is("topInt"));
+        assertThat(topTypedef.getDefaultValueInString(), is("10"));
+        YangType topType = topTypedef.getTypeList().iterator().next();
+        assertThat(topType.getDataType(), is(YangDataTypes.INT64));
+        assertThat(topType.getDataTypeName(), is("int64"));
+        YangType<YangInt64> typeInt64 = (YangType<YangInt64>) topType;
+        YangInt64 int64Obj = typeInt64.getDataTypeExtendedInfo();
     }
 }