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