[ONOS-5281] JUNIT Test cases for device-controllers and device-setcontrollers as FUJITSU NETCONF

Change-Id: I2a6b30d357061ca265175a0a6057728bf83fa5a7
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfig.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfig.java
index f06ad81..07edd32 100644
--- a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfig.java
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfig.java
@@ -44,6 +44,7 @@
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -57,33 +58,20 @@
     private static final String RESOURCE_XML = "voltcontrollers.xml";
 
     private static final String DOT = ".";
-    private static final String L_ANGLE_BR = "<";
-    private static final String R_ANGLE_BR = "/>";
-    private static final String VOLT_NE_NAMESPACE =
-            "xmlns=\"http://fujitsu.com/ns/volt/1.1\"";
     private static final String DATA = "data";
-    private static final String VOLT_NE = "volt-ne";
     private static final String VOLT_OFCONFIG = "volt-ofconfig";
     private static final String OF_CONTROLLERS = "of-controllers";
     private static final String OF_CONTROLLER = "of-controller";
     private static final String CONTROLLER_INFO = "controller-info";
-    private static final String REPORT_ALL = "report-all";
     private static final String IP_ADDRESS = "ip-address";
     private static final String PORT = "port";
     private static final String PROTOCOL = "protocol";
     private static final String CONFIG = "config";
     private static final String OFCONFIG_ID = "ofconfig-id";
-    private static final String EDIT_CONFIG = "edit-config";
     private static final String TARGET = "target";
-    private static final String RUNNING = "running";
     private static final String MERGE = "merge";
     private static final String DEFAULT_OPERATION = "default-operation";
 
-    private static final String VOLT_NE_OPEN = "<" + VOLT_NE + " ";
-    private static final String VOLT_NE_CLOSE = "</" + VOLT_NE + ">";
-    private static final String VOLT_OFCONFIG_EL = "<" + VOLT_OFCONFIG + "/>\n";
-    private static final String TARGET_OPEN = "<" + TARGET + ">";
-    private static final String TARGET_CLOSE = "</" + TARGET + ">";
     private static final String END_LICENSE_HEADER = "-->";
 
     private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT +
@@ -95,11 +83,9 @@
     private static final String CONTROLLER_INFO_IP = CONTROLLER_INFO + DOT + IP_ADDRESS;
     private static final String CONTROLLER_INFO_PORT = CONTROLLER_INFO + DOT + PORT;
     private static final String CONTROLLER_INFO_PROTOCOL = CONTROLLER_INFO + DOT + PROTOCOL;
-
     private static final String VOLT_EDITCONFIG = EDIT_CONFIG + DOT +
             CONFIG + DOT + VOLT_NE + DOT + VOLT_OFCONFIG + DOT + OF_CONTROLLERS;
 
-
     @Override
     public List<ControllerInfo> getControllers() {
         DriverHandler handler = handler();
@@ -112,13 +98,15 @@
             try {
                 StringBuilder request = new StringBuilder();
                 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE).append(">\n");
-                request.append(VOLT_OFCONFIG_EL);
+                request.append(buildEmptyTag(VOLT_OFCONFIG));
                 request.append(VOLT_NE_CLOSE);
 
                 String reply;
-                reply = controller.
-                        getDevicesMap().get(ncDeviceId).getSession().
-                        get(request.toString(), REPORT_ALL);
+                reply = controller
+                            .getDevicesMap()
+                            .get(ncDeviceId)
+                            .getSession()
+                            .get(request.toString(), REPORT_ALL);
                 log.debug("Reply XML {}", reply);
                 controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
                         loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
@@ -150,7 +138,7 @@
                 device.getSession().editConfig(config.substring(
                         config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length()));
             } catch (NetconfException e) {
-                log.error("Cannot communicate to device {} , exception ", ncdeviceId, e);
+                log.error("Cannot communicate to device {} , exception {}", ncdeviceId, e);
             }
         } else {
             log.warn("I'm not master for {} please use master, {} to execute command",
@@ -201,7 +189,7 @@
      * @param controllers list of controllers
      * @return XML string
      */
-    public static String createVoltControllersConfig(HierarchicalConfiguration cfg,
+    private String createVoltControllersConfig(HierarchicalConfiguration cfg,
                                                      String target, String netconfOperation,
                                                      List<ControllerInfo> controllers) {
         XMLConfiguration editcfg = null;
@@ -234,8 +222,11 @@
             e.printStackTrace();
         }
         String s = stringWriter.toString();
-        s = s.replace(TARGET_OPEN + target + TARGET_CLOSE,
-                      TARGET_OPEN + L_ANGLE_BR + target + R_ANGLE_BR + TARGET_CLOSE);
+        String fromStr = buildStartTag(TARGET, false) + target +
+                   buildEndTag(TARGET, false);
+        String toStr = buildStartTag(TARGET, false) +
+                   buildEmptyTag(target, false) + buildEndTag(TARGET, false);
+        s = s.replace(fromStr, toStr);
         return s;
     }
 
diff --git a/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfigTest.java b/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfigTest.java
new file mode 100644
index 0000000..e25aae0
--- /dev/null
+++ b/drivers/fujitsu/src/test/java/org/onosproject/drivers/fujitsu/FujitsuVoltControllerConfigTest.java
@@ -0,0 +1,243 @@
+/*
+ * 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.drivers.fujitsu;
+
+import org.apache.commons.io.IOUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.behaviour.ControllerInfo;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
+
+import java.nio.charset.StandardCharsets;
+import java.io.InputStream;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
+
+
+/**
+ * Unit tests for methods of FujitsuVoltControllerConfig.
+ */
+public class FujitsuVoltControllerConfigTest {
+
+    private FujitsuNetconfControllerMock controller;
+    private FujitsuDriverHandlerAdapter driverHandler;
+    private FujitsuVoltControllerConfig voltConfig;
+
+    private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
+
+    private static final String TEST_VOLT_OFCONFIG = "volt-ofconfig";
+    private static final String TEST_OFCONFIG_ID = "ofconfig-id";
+    private static final String TEST_END_LICENSE_HEADER = "-->";
+    private static final int FIRST_PART = 0;
+    private static final int SECOND_PART = 1;
+    private static final int THIRD_PART = 2;
+
+    private static final Map<Integer, String> GET_CONTROLLERS = new HashMap<Integer, String>() {
+        {
+            put(1, "tcp:172.10.10.45:6633");
+            put(2, "tcp:100.0.0.22:5555");
+        }
+    };
+    private static final Map<Integer, String> SET_CONTROLLERS = new HashMap<Integer, String>() {
+        {
+            put(1, "tcp:172.10.10.55:2222");
+            put(3, "tcp:172.20.33.11:6633");
+        }
+    };
+    private static final String GET_CONTROLLERS_RSP_FILE = "/getcontrollers.xml";
+    private static final String SET_CONTROLLERS_REQ_FILE = "/setcontrollers.xml";
+
+
+    @Before
+    public void setUp() throws Exception {
+        controller = new FujitsuNetconfControllerMock();
+        driverHandler = controller.setUp(listener);
+        voltConfig = new FujitsuVoltControllerConfig();
+        voltConfig.setHandler(driverHandler);
+    }
+
+    /**
+     * Run to verify handling of valid get operation.
+     */
+    @Test
+    public void testGetControllers() throws Exception {
+        List<ControllerInfo> controllers;
+        List<ControllerInfo> expectedControllers = new ArrayList<>();
+
+        for (Integer key : GET_CONTROLLERS.keySet()) {
+            String target = GET_CONTROLLERS.get(key);
+            String[] data = target.split(TEST_COLON);
+
+            Annotations annotations = DefaultAnnotations
+                                          .builder()
+                                          .set(TEST_OFCONFIG_ID, key.toString())
+                                          .build();
+            ControllerInfo controller = new ControllerInfo(
+                    IpAddress.valueOf(data[SECOND_PART]),
+                    Integer.parseInt(data[THIRD_PART]),
+                        data[FIRST_PART], annotations);
+            expectedControllers.add(controller);
+        }
+
+        controllers = voltConfig.getControllers();
+        assertTrue("Incorrect response", controllers.equals(expectedControllers));
+    }
+
+    /**
+     * Run to verify handling of valid set operation.
+     */
+    @Test
+    public void testSetControllers() throws Exception {
+        List<ControllerInfo> controllers = new ArrayList<>();
+
+        for (Integer key : SET_CONTROLLERS.keySet()) {
+            String target = SET_CONTROLLERS.get(key);
+            String[] data = target.split(TEST_COLON);
+
+            Annotations annotations = DefaultAnnotations
+                                          .builder()
+                                          .set(TEST_OFCONFIG_ID, key.toString())
+                                          .build();
+            ControllerInfo controller = new ControllerInfo(
+                    IpAddress.valueOf(data[SECOND_PART]),
+                    Integer.parseInt(data[THIRD_PART]),
+                        data[FIRST_PART], annotations);
+            controllers.add(controller);
+        }
+
+        voltConfig.setControllers(controllers);
+    }
+
+    /**
+     * Verifies XML request string by comparing with generated string.
+     *
+     * @param request XML string for set operation
+     * @return true if XML string matches with generated
+     */
+    private boolean verifyGetRequest(String request) {
+        StringBuilder rpc = new StringBuilder();
+        rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE);
+        rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE);
+        rpc.append(emptyTag(TEST_VOLT_OFCONFIG));
+        rpc.append(endTag(TEST_VOLT_NE));
+
+        String testRequest = rpc.toString();
+        testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
+        request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
+        boolean result = request.equals(testRequest);
+        assertTrue("Does not match with generated string", result);
+        return result;
+    }
+
+    /**
+     * Verifies XML request string by comparing with generated string.
+     *
+     * @param request XML string for set operation
+     * @return true if XML string matches with generated
+     */
+    private boolean verifyEditConfigRequest(String request) {
+        String testRequest;
+
+        try {
+            InputStream fileStream = getClass().getResourceAsStream(
+                        SET_CONTROLLERS_REQ_FILE);
+            testRequest = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
+            testRequest = testRequest.substring(testRequest.indexOf(
+                    TEST_END_LICENSE_HEADER) + TEST_END_LICENSE_HEADER.length());
+        } catch (IOException e) {
+            fail("IOException while reading: " + SET_CONTROLLERS_REQ_FILE);
+            return false;
+        }
+
+        testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
+        request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING);
+        boolean result = request.equals(testRequest);
+        assertTrue("Does not match with string in " + SET_CONTROLLERS_REQ_FILE, result);
+
+        return result;
+    }
+
+    /**
+     * Internal listener for device service events.
+     */
+    private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
+        @Override
+        public boolean verifyEditConfig(String request) {
+            boolean result;
+
+            request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
+            assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
+                    request.contains(TEST_VOLT_NAMESPACE));
+
+            result = verifyEditConfigRequest(request);
+            assertTrue("XML verification failure", result);
+            return result;
+        }
+
+        @Override
+        public boolean verifyEditConfig(String target, String mode, String request) {
+            return false;
+        }
+
+        @Override
+        public boolean verifyGet(String filterSchema, String withDefaultsMode) {
+            boolean result;
+
+            assertTrue("Incorrect withDefaultsMode",
+                    withDefaultsMode.equals(TEST_REPORT_ALL));
+            filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
+            assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
+                    filterSchema.contains(TEST_VOLT_NAMESPACE));
+
+            result = verifyGetRequest(filterSchema);
+            assertTrue("XML verification failure", result);
+            return result;
+        }
+
+        @Override
+        public String buildGetReply() {
+            try {
+                InputStream fileStream = getClass().getResourceAsStream(
+                        GET_CONTROLLERS_RSP_FILE);
+                String reply = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
+                return (reply);
+            } catch (IOException e) {
+                return null;
+            }
+        }
+
+        @Override
+        public boolean verifyWrappedRpc(String request) {
+            return false;
+        }
+
+        @Override
+        public void verifyStartSubscription(String filterSchema) {
+        }
+    }
+
+}
diff --git a/drivers/fujitsu/src/test/resources/getcontrollers.xml b/drivers/fujitsu/src/test/resources/getcontrollers.xml
new file mode 100644
index 0000000..1e01f36
--- /dev/null
+++ b/drivers/fujitsu/src/test/resources/getcontrollers.xml
@@ -0,0 +1,52 @@
+<!--
+  ~ 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.
+  -->
+
+<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+  <data>
+    <volt-ne xmlns="http://fujitsu.com/ns/volt/1.1">
+      <volt-ofconfig>
+        <of-controllers>
+          <of-controller>
+            <ofconfig-id>1</ofconfig-id>
+            <controller-info>
+              <id>1</id>
+              <ip-address>172.10.10.45</ip-address>
+              <port>6633</port>
+              <protocol>tcp</protocol>
+            </controller-info>
+            <state>
+              <connection-state>up</connection-state>
+              <local-port-in-use>12345</local-port-in-use>
+            </state>
+          </of-controller>
+          <of-controller>
+            <ofconfig-id>2</ofconfig-id>
+            <controller-info>
+              <id>2</id>
+              <ip-address>100.0.0.22</ip-address>
+              <port>5555</port>
+              <protocol>tcp</protocol>
+            </controller-info>
+            <state>
+              <connection-state>up</connection-state>
+              <local-port-in-use>12345</local-port-in-use>
+            </state>
+          </of-controller>
+        </of-controllers>
+      </volt-ofconfig>
+    </volt-ne>
+  </data>
+</rpc-reply>
diff --git a/drivers/fujitsu/src/test/resources/setcontrollers.xml b/drivers/fujitsu/src/test/resources/setcontrollers.xml
new file mode 100644
index 0000000..6311281
--- /dev/null
+++ b/drivers/fujitsu/src/test/resources/setcontrollers.xml
@@ -0,0 +1,48 @@
+<!--
+  ~ 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.
+  -->
+
+<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+  <edit-config>
+    <target><running/></target>
+    <default-operation>merge</default-operation>
+    <config>
+      <volt-ne xmlns="http://fujitsu.com/ns/volt/1.1">
+        <volt-ofconfig>
+          <of-controllers>
+            <of-controller>
+              <ofconfig-id>1</ofconfig-id>
+              <controller-info>
+                <id>1</id>
+                <ip-address>172.10.10.55</ip-address>
+                <port>2222</port>
+                <protocol>tcp</protocol>
+              </controller-info>
+            </of-controller>
+            <of-controller>
+              <ofconfig-id>3</ofconfig-id>
+              <controller-info>
+                <id>3</id>
+                <ip-address>172.20.33.11</ip-address>
+                <port>6633</port>
+                <protocol>tcp</protocol>
+              </controller-info>
+            </of-controller>
+          </of-controllers>
+        </volt-ofconfig>
+      </volt-ne>
+    </config>
+  </edit-config>
+</rpc>