covered cases for NetconfAdaptor2 15/63915/2
authorGanesh Chandrasekaran <ganesh.c@samsung.com>
Thu, 30 Aug 2018 23:57:24 +0000 (08:57 +0900)
committerTakamune Cho <tc012c@att.com>
Wed, 5 Sep 2018 23:19:27 +0000 (23:19 +0000)
Issue-ID: APPC-1182

Change-Id: Ib3e96b5869ce95299cf6e83a6f25ad04f7373382
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/onap/appc/adapter/netconf/internal/NetconfAdapter2.java
appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/test/java/org/onap/appc/adapter/netconf/internal/TestNetconfAdapter2.java [new file with mode: 0644]

index 567a45a..be286cd 100644 (file)
@@ -52,6 +52,19 @@ public class NetconfAdapter2 {
         out = new PipedOutputStream(pipedInOut);
     }
 
+    /**
+     * Constructor.
+     *
+     * @param in  InputStream this instance will read netconf messages from
+     * @param out OutputStream this instance will write netconf messages to
+     * @throws IOException
+     */
+    public NetconfAdapter2(PipedInputStream in, PipedOutputStream out) throws IOException {
+        this.in = in;
+        this.out = out;
+
+    }
+
     /**
      * @return InputStream this instance will read netconf messages from.
      */
diff --git a/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/test/java/org/onap/appc/adapter/netconf/internal/TestNetconfAdapter2.java b/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/test/java/org/onap/appc/adapter/netconf/internal/TestNetconfAdapter2.java
new file mode 100644 (file)
index 0000000..3441709
--- /dev/null
@@ -0,0 +1,83 @@
+package org.onap.appc.adapter.netconf.internal;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+
+public class TestNetconfAdapter2 {
+
+    private static final String EOM = "]]>]]>";
+
+    @Test (expected = IOException.class)
+    public void testReceiveMessage() throws IOException {
+        PipedOutputStream pos = new PipedOutputStream();
+        PipedInputStream is = new PipedInputStream(pos);
+
+        PipedInputStream pis = new PipedInputStream();
+        PipedOutputStream os = new PipedOutputStream(pis);
+
+        NetconfAdapter2 netconfAdapter = new NetconfAdapter2(is, os);
+
+        String request = "Hello, netconf!";
+        pos.write(request.getBytes());
+        pos.write(EOM.getBytes());
+        String response = netconfAdapter.receiveMessage();
+        Assert.assertNotNull(response);
+        Assert.assertEquals(request, response.trim());
+    }
+
+    @Test (expected = IOException.class)
+    public void testSendMessage() throws IOException {
+        PipedOutputStream pos = new PipedOutputStream();
+        PipedInputStream is = new PipedInputStream(pos);
+
+        PipedInputStream pis = new PipedInputStream();
+        PipedOutputStream os = new PipedOutputStream(pis);
+
+        NetconfAdapter2 netconfAdapter = new NetconfAdapter2(is, os);
+
+        String request = "Hello, netconf!";
+        netconfAdapter.sendMessage(request);
+        byte[] bytes = new byte[request.length()+EOM.length()+2];
+        int count = pis.read(bytes);
+        String response = new String(bytes, 0, count);
+        Assert.assertNotNull(response);
+        Assert.assertTrue(response.endsWith(EOM));
+        response = response.substring(0, response.length() - EOM.length()).trim();
+        Assert.assertEquals(request, response);
+    }
+
+    @Test (expected = IOException.class)
+    public void testSendReceive() throws IOException {
+        PipedOutputStream os = new PipedOutputStream();
+        PipedInputStream is = new PipedInputStream(os);
+
+        NetconfAdapter2 netconfAdapter = new NetconfAdapter2(is, os);
+
+        String request = "Hello, netconf!";
+        netconfAdapter.sendMessage(request);
+        String response = netconfAdapter.receiveMessage();
+        Assert.assertNotNull(response);
+        Assert.assertEquals(request, response.trim());
+    }
+
+    @Test
+    public void testDefaultSendReceive() throws IOException {
+
+        NetconfAdapter2 netconfAdapter = new NetconfAdapter2();
+
+        String request = "Hello, netconf!";
+        netconfAdapter.sendMessage(request);
+
+        InputStream in = netconfAdapter.getIn();
+        OutputStream out = netconfAdapter.getOut();
+
+        Assert.assertNotNull(in);
+        Assert.assertNotNull(out);
+    }
+}
\ No newline at end of file