config ang key validation and UT for the same

Change-Id: I507740fc9da3f3da5fb3c88a7414f87db6251c5b
diff --git a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java
index c8db91a..bd634bb 100644
--- a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java
+++ b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ConfigListenerTest.java
@@ -240,4 +240,296 @@
         assertThat(leafListInfo.getLeafName(), 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.getLeafName(), 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.getLeafName(), 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.getLeafName(), 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.getLeafName(), 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.getLeafName(), 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));
+    }
 }
\ No newline at end of file
diff --git a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java
index 727c1d2..542cd5a 100644
--- a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java
+++ b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/KeyListenerTest.java
@@ -104,4 +104,141 @@
         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 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("Leaf identifier 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("Leaf-list identifier 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/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java
index f552387..0d2d29b 100644
--- a/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java
+++ b/utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/ListListenerTest.java
@@ -68,7 +68,7 @@
         assertThat(yangList.getName(), is("valid"));
 
         ListIterator<String> keyList = yangList.getKeyList().listIterator();
-        assertThat(keyList.next(), is("invalid"));
+        assertThat(keyList.next(), is("invalid-interval"));
     }
 
     /**
@@ -95,7 +95,7 @@
         // Check whether the list is child of container
         YangList yangList = (YangList) yangContainer.getChild();
         assertThat(yangList.getName(), is("valid"));
-        assertThat(yangList.getKeyList().contains("invalid"), is(true));
+        assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
     }
 
     /**
@@ -123,7 +123,7 @@
         // Check whether the list is child of list
         YangList yangList = (YangList) yangList1.getChild();
         assertThat(yangList.getName(), is("valid"));
-        assertThat(yangList.getKeyList().contains("invalid"), is(true));
+        assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
     }
 
     /**
@@ -148,7 +148,7 @@
 
         // Check whether list properties as set correctly.
         assertThat(yangList.getName(), is("ospf"));
-        assertThat(yangList.getKeyList().contains("process-id"), is(true));
+        assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
 
         assertThat(yangList.isConfig(), is(true));
         assertThat(yangList.getMaxElelements(), is(10));