2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.openecomp.appc.adapter.netconf.internal;
 
  24 import org.junit.Assert;
 
  25 import org.junit.Test;
 
  26 import org.openecomp.appc.adapter.netconf.internal.NetconfAdapter;
 
  28 import java.io.IOException;
 
  29 import java.io.PipedInputStream;
 
  30 import java.io.PipedOutputStream;
 
  32 public class TestNetconfAdapter {
 
  34         private static final String EOM = "]]>]]>";
 
  37         public void testReceiveMessage() throws IOException {
 
  38                 PipedOutputStream pos = new PipedOutputStream();
 
  39                 PipedInputStream is = new PipedInputStream(pos);
 
  41                 PipedInputStream pis = new PipedInputStream();
 
  42                 PipedOutputStream os = new PipedOutputStream(pis);
 
  44                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
 
  46                 String request = "Hello, netconf!";
 
  47                 pos.write(request.getBytes());
 
  48                 pos.write(EOM.getBytes());
 
  49                 String response = netconfAdapter.receiveMessage();
 
  50                 Assert.assertNotNull(response);
 
  51                 Assert.assertEquals(request, response.trim());
 
  55         public void testSendMessage() throws IOException {
 
  56                 PipedOutputStream pos = new PipedOutputStream();
 
  57                 PipedInputStream is = new PipedInputStream(pos);
 
  59                 PipedInputStream pis = new PipedInputStream();
 
  60                 PipedOutputStream os = new PipedOutputStream(pis);
 
  62                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
 
  64                 String request = "Hello, netconf!";
 
  65                 netconfAdapter.sendMessage(request);
 
  66                 byte[] bytes = new byte[request.length()+EOM.length()+2];
 
  67                 int count = pis.read(bytes);
 
  68                 String response = new String(bytes, 0, count);
 
  69                 Assert.assertNotNull(response);
 
  70                 Assert.assertTrue(response.endsWith(EOM));
 
  71                 response = response.substring(0, response.length() - EOM.length()).trim();
 
  72                 Assert.assertEquals(request, response);
 
  76         public void testSendReceive() throws IOException {
 
  77                 PipedOutputStream os = new PipedOutputStream();
 
  78                 PipedInputStream is = new PipedInputStream(os);
 
  80                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
 
  82                 String request = "Hello, netconf!";
 
  83                 netconfAdapter.sendMessage(request);
 
  84                 String response = netconfAdapter.receiveMessage();
 
  85                 Assert.assertNotNull(response);
 
  86                 Assert.assertEquals(request, response.trim());