2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.netconf.internal;
27 import org.junit.Assert;
28 import org.junit.Test;
30 import java.io.IOException;
31 import java.io.PipedInputStream;
32 import java.io.PipedOutputStream;
34 public class TestNetconfAdapter {
36 private static final String EOM = "]]>]]>";
39 public void testReceiveMessage() throws IOException {
40 PipedOutputStream pos = new PipedOutputStream();
41 PipedInputStream is = new PipedInputStream(pos);
43 PipedInputStream pis = new PipedInputStream();
44 PipedOutputStream os = new PipedOutputStream(pis);
46 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
48 String request = "Hello, netconf!";
49 pos.write(request.getBytes());
50 pos.write(EOM.getBytes());
51 String response = netconfAdapter.receiveMessage();
52 Assert.assertNotNull(response);
53 Assert.assertEquals(request, response.trim());
57 public void testSendMessage() throws IOException {
58 PipedOutputStream pos = new PipedOutputStream();
59 PipedInputStream is = new PipedInputStream(pos);
61 PipedInputStream pis = new PipedInputStream();
62 PipedOutputStream os = new PipedOutputStream(pis);
64 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
66 String request = "Hello, netconf!";
67 netconfAdapter.sendMessage(request);
68 byte[] bytes = new byte[request.length()+EOM.length()+2];
69 int count = pis.read(bytes);
70 String response = new String(bytes, 0, count);
71 Assert.assertNotNull(response);
72 Assert.assertTrue(response.endsWith(EOM));
73 response = response.substring(0, response.length() - EOM.length()).trim();
74 Assert.assertEquals(request, response);
78 public void testSendReceive() throws IOException {
79 PipedOutputStream os = new PipedOutputStream();
80 PipedInputStream is = new PipedInputStream(os);
82 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
84 String request = "Hello, netconf!";
85 netconfAdapter.sendMessage(request);
86 String response = netconfAdapter.receiveMessage();
87 Assert.assertNotNull(response);
88 Assert.assertEquals(request, response.trim());