From: Ganesh Chandrasekaran Date: Thu, 30 Aug 2018 23:57:24 +0000 (+0900) Subject: covered cases for NetconfAdaptor2 X-Git-Tag: 1.4.0~40 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F15%2F63915%2F2;p=appc.git covered cases for NetconfAdaptor2 Issue-ID: APPC-1182 Change-Id: Ib3e96b5869ce95299cf6e83a6f25ad04f7373382 Signed-off-by: Ganesh Chandrasekaran --- diff --git a/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/onap/appc/adapter/netconf/internal/NetconfAdapter2.java b/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/onap/appc/adapter/netconf/internal/NetconfAdapter2.java index 567a45a96..be286cd48 100644 --- a/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/onap/appc/adapter/netconf/internal/NetconfAdapter2.java +++ b/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/onap/appc/adapter/netconf/internal/NetconfAdapter2.java @@ -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 index 000000000..344170967 --- /dev/null +++ b/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/test/java/org/onap/appc/adapter/netconf/internal/TestNetconfAdapter2.java @@ -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