unit tests for NetconfSessionImpl

Change-Id: Ic1b7d0221343f288b98c41f456809b59689e198f
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java
index ecc924c..90f3148 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/TargetConfig.java
@@ -27,21 +27,13 @@
         this.name = name;
     }
 
-    TargetConfig toTargetConfig(String targetConfig) {
-        switch (targetConfig) {
-            case "running":
-                return RUNNING;
-            case "candidate":
-                return CANDIDATE;
-            case "startup":
-                return STARTUP;
-            default:
-                return null;
-        }
+    public static TargetConfig toTargetConfig(String targetConfig) {
+        return valueOf(targetConfig.toUpperCase());
     }
 
     @Override
     public String toString() {
         return this.name;
     }
+
 }
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java
index 07afac8..7da836c 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java
@@ -399,12 +399,12 @@
 
     @Override
     public String getConfig(String netconfTargetConfig) throws NetconfException {
-        return getConfig(TargetConfig.valueOf(netconfTargetConfig));
+        return getConfig(TargetConfig.toTargetConfig(netconfTargetConfig));
     }
 
     @Override
     public String getConfig(String netconfTargetConfig, String configurationFilterSchema) throws NetconfException {
-        return getConfig(TargetConfig.valueOf(netconfTargetConfig), configurationFilterSchema);
+        return getConfig(TargetConfig.toTargetConfig(netconfTargetConfig), configurationFilterSchema);
     }
 
     @Override
@@ -442,7 +442,7 @@
     @Override
     public boolean editConfig(String netconfTargetConfig, String mode, String newConfiguration)
             throws NetconfException {
-        return editConfig(TargetConfig.valueOf(netconfTargetConfig), mode, newConfiguration);
+        return editConfig(TargetConfig.toTargetConfig(netconfTargetConfig), mode, newConfiguration);
     }
 
     @Override
@@ -479,7 +479,7 @@
 
     @Override
     public boolean copyConfig(String netconfTargetConfig, String newConfiguration) throws NetconfException {
-        return copyConfig(TargetConfig.valueOf(netconfTargetConfig), newConfiguration);
+        return copyConfig(TargetConfig.toTargetConfig(netconfTargetConfig), newConfiguration);
     }
 
     @Override
@@ -508,12 +508,12 @@
 
     @Override
     public boolean deleteConfig(String netconfTargetConfig) throws NetconfException {
-        return deleteConfig(TargetConfig.valueOf(netconfTargetConfig));
+        return deleteConfig(TargetConfig.toTargetConfig(netconfTargetConfig));
     }
 
     @Override
     public boolean deleteConfig(TargetConfig netconfTargetConfig) throws NetconfException {
-        if (netconfTargetConfig.equals("running")) {
+        if (netconfTargetConfig.equals(TargetConfig.RUNNING)) {
             log.warn("Target configuration for delete operation can't be \"running\"",
                      netconfTargetConfig);
             return false;
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSessionImplTest.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSessionImplTest.java
index fe2a0d5..2dcb14f 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSessionImplTest.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSessionImplTest.java
@@ -18,7 +18,9 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.onosproject.netconf.TargetConfig.*;
+import static org.junit.Assert.assertFalse;
+import static org.onosproject.netconf.TargetConfig.RUNNING;
+import static org.onosproject.netconf.TargetConfig.CANDIDATE;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -64,6 +66,7 @@
     private static final String TEST_USERNAME = "netconf";
     private static final String TEST_PASSWORD = "netconf123";
     private static final String TEST_HOSTNAME = "127.0.0.1";
+
     private static final String TEST_SERFILE =
             System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "testkey.ser";
 
@@ -72,6 +75,17 @@
             + "<some-child-element/>"
             + "</some-yang-element>";
 
+    private static final String EDIT_CONFIG_REQUEST =
+            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rpc message-id=\"6\"  "
+                    + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                    + "<edit-config>\n"
+                    + "<target><running/></target>\n"
+                    + "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                    + "<some-yang-element xmlns=\"some-namespace\">"
+                    + "<some-child-element/></some-yang-element></config>\n"
+                    + "</edit-config>\n"
+                    + "</rpc>]]>]]>";
+
     private static NetconfSession session1;
     private static NetconfSession session2;
     private static SshServer sshServerNetconf;
@@ -130,7 +144,9 @@
         log.info("Starting edit-config async");
         assertNotNull("Incorrect sessionId", session1.getSessionId());
         try {
-            assertTrue("NETCONF edit-config command failed", session1.editConfig(RUNNING, null, SAMPLE_REQUEST));
+            assertTrue("NETCONF edit-config command failed",
+                       session1.editConfig(TargetConfig.RUNNING.toString(),
+                                           null, SAMPLE_REQUEST));
         } catch (NetconfException e) {
             e.printStackTrace();
             fail("NETCONF edit-config test failed: " + e.getMessage());
@@ -139,11 +155,41 @@
     }
 
     @Test
+    public void testEditConfigRequestWithOnlyNewConfiguration() {
+        log.info("Starting edit-config async");
+        assertNotNull("Incorrect sessionId", session1.getSessionId());
+        try {
+            assertTrue("NETCONF edit-config command failed",
+                       session1.editConfig(EDIT_CONFIG_REQUEST));
+        } catch (NetconfException e) {
+            e.printStackTrace();
+            fail("NETCONF edit-config test failed: " + e.getMessage());
+        }
+        log.info("Finishing edit-config async");
+    }
+
+    @Test
+    public void testDeleteConfigRequestWithRunningTargetConfiguration() {
+        log.info("Starting delete-config async");
+        assertNotNull("Incorrect sessionId", session1.getSessionId());
+        try {
+            assertFalse("NETCONF delete-config command failed",
+                        session1.deleteConfig(TargetConfig.RUNNING));
+        } catch (NetconfException e) {
+            e.printStackTrace();
+            fail("NETCONF delete-config test failed: " + e.getMessage());
+        }
+        log.info("Finishing delete-config async");
+    }
+
+    @Test
     public void testCopyConfigRequest() {
         log.info("Starting copy-config async");
         assertNotNull("Incorrect sessionId", session1.getSessionId());
         try {
-            assertTrue("NETCONF edit-config command failed", session1.copyConfig(RUNNING, "candidate"));
+            assertTrue("NETCONF copy-config command failed",
+                       session1.copyConfig(TargetConfig.RUNNING.toString(),
+                                           "candidate"));
         } catch (NetconfException e) {
             e.printStackTrace();
             fail("NETCONF edit-config test failed: " + e.getMessage());
@@ -185,6 +231,33 @@
     }
 
     @Test
+    public void testLockRequest() {
+        log.info("Starting lock async");
+        assertNotNull("Incorrect sessionId", session1.getSessionId());
+        try {
+            assertTrue("NETCONF lock request failed", session1.lock());
+        } catch (NetconfException e) {
+            e.printStackTrace();
+            fail("NETCONF lock test failed: " + e.getMessage());
+        }
+        log.info("Finishing lock async");
+    }
+
+    @Test
+    public void testUnLockRequest() {
+        log.info("Starting unlock async");
+        assertNotNull("Incorrect sessionId", session1.getSessionId());
+        try {
+            assertTrue("NETCONF unlock request failed", session1.unlock());
+        } catch (NetconfException e) {
+            e.printStackTrace();
+            fail("NETCONF unlock test failed: " + e.getMessage());
+        }
+        log.info("Finishing unlock async");
+    }
+
+
+    @Test
     public void testConcurrentSameSessionAccess() throws InterruptedException {
         NCCopyConfigCallable testCopyConfig1 = new NCCopyConfigCallable(session1, RUNNING, "candidate");
         NCCopyConfigCallable testCopyConfig2 = new NCCopyConfigCallable(session1, RUNNING, "startup");
@@ -304,24 +377,56 @@
             Pattern.compile("(<\\?xml).*"
                     + "(<rpc message-id=\")[0-9]*(\") *(xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">)\\R?"
                     + "(<edit-config>)\\R?"
-                    + "(<target>\\R?((<candidate/>)|(<running/>)|(<startup/>))\\R?</target>)\\R?"
+                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
+                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
                     + "(<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">)\\R?"
                     + ".*"
                     + "(</config>)\\R?(</edit-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
 
+
+    public static final Pattern LOCK_REQ_PATTERN =
+            Pattern.compile("(<\\?xml).*"
+                                    + "(<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "
+                                    + "message-id=\")[0-9]*(\">)\\R?"
+                                    + "(<lock>)\\R?"
+                                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
+                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
+                                    + "(</lock>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
+
+    public static final Pattern UNLOCK_REQ_PATTERN =
+            Pattern.compile("(<\\?xml).*"
+                                    + "(<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "
+                                    + "message-id=\")[0-9]*(\">)\\R?"
+                                    + "(<unlock>)\\R?"
+                                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
+                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
+                                    + "(</unlock>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
+
     public static final Pattern COPY_CONFIG_REQ_PATTERN =
             Pattern.compile("(<\\?xml).*"
                     + "(<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\")[0-9]*(\">)\\R?"
                     + "(<copy-config>)\\R?"
-                    + "(<target>\\R?((<candidate/>)|(<running/>)|(<startup/>))\\R?</target>)\\R?"
-                    + "(<source>)\\R?(<config>)((candidate)|(running)|(startup))(</config>)\\R?(</source>)\\R?"
-                    + "(</copy-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
+                    + "(<target>\\R?((<" + TargetConfig.CANDIDATE.toString() + "/>)|"
+                                    + "(<" + TargetConfig.RUNNING.toString() + "/>)|"
+                                    + "(<" + TargetConfig.STARTUP.toString() + "/>))\\R?</target>)\\R?"
+                                    + "(<source>)\\R?(<config>)(("
+                                    + TargetConfig.CANDIDATE.toString() + ")|("
+                                    + TargetConfig.RUNNING.toString() + ")|("
+                                    + TargetConfig.STARTUP.toString()
+                                    + "))(</config>)\\R?(</source>)\\R?"
+                                    + "(</copy-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
 
     public static final Pattern GET_CONFIG_REQ_PATTERN =
             Pattern.compile("(<\\?xml).*"
                     + "(<rpc message-id=\")[0-9]*(\"  xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">)\\R?"
-                    + "(<get-config>)\\R?"
-                    + "(<source>)\\R?((<candidate/>)|(<running/>)|(<startup/>))\\R?(</source>)\\R?"
+                    + "(<get-config>)\\R?" + "(<source>)\\R?((<"
+                                    + TargetConfig.CANDIDATE.toString()
+                                    + "/>)|(<" + TargetConfig.RUNNING.toString()
+                                    + "/>)|(<" + TargetConfig.STARTUP.toString()
+                                    + "/>))\\R?(</source>)\\R?"
                     + "(<filter type=\"subtree\">).*(</filter>)\\R?"
                     + "(</get-config>)\\R?(</rpc>)\\R?", Pattern.DOTALL);
 
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSshdTestSubsystem.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSshdTestSubsystem.java
index b2f53f5..f959038 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSshdTestSubsystem.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfSshdTestSubsystem.java
@@ -194,7 +194,9 @@
                                 outputStream.write(helloReply + END_PATTERN);
                                 outputStream.flush();
                             } else if (NetconfSessionImplTest.EDIT_CONFIG_REQ_PATTERN.matcher(deviceRequest).matches()
-                                 || NetconfSessionImplTest.COPY_CONFIG_REQ_PATTERN.matcher(deviceRequest).matches()) {
+                                 || NetconfSessionImplTest.COPY_CONFIG_REQ_PATTERN.matcher(deviceRequest).matches()
+                                    || NetconfSessionImplTest.LOCK_REQ_PATTERN.matcher(deviceRequest).matches()
+                                    || NetconfSessionImplTest.UNLOCK_REQ_PATTERN.matcher(deviceRequest).matches()) {
                                 outputStream.write(NetconfSessionImplTest.getOkReply(messageId) + END_PATTERN);
                                 outputStream.flush();
                             } else if (NetconfSessionImplTest.GET_CONFIG_REQ_PATTERN.matcher(deviceRequest).matches()