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;
29 import org.onap.appc.adapter.netconf.internal.NetconfAdapter;
31 import java.io.IOException;
32 import java.io.PipedInputStream;
33 import java.io.PipedOutputStream;
35 public class TestNetconfAdapter {
37 private static final String EOM = "]]>]]>";
40 public void testReceiveMessage() throws IOException {
41 PipedOutputStream pos = new PipedOutputStream();
42 PipedInputStream is = new PipedInputStream(pos);
44 PipedInputStream pis = new PipedInputStream();
45 PipedOutputStream os = new PipedOutputStream(pis);
47 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
49 String request = "Hello, netconf!";
50 pos.write(request.getBytes());
51 pos.write(EOM.getBytes());
52 String response = netconfAdapter.receiveMessage();
53 Assert.assertNotNull(response);
54 Assert.assertEquals(request, response.trim());
58 public void testSendMessage() throws IOException {
59 PipedOutputStream pos = new PipedOutputStream();
60 PipedInputStream is = new PipedInputStream(pos);
62 PipedInputStream pis = new PipedInputStream();
63 PipedOutputStream os = new PipedOutputStream(pis);
65 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
67 String request = "Hello, netconf!";
68 netconfAdapter.sendMessage(request);
69 byte[] bytes = new byte[request.length()+EOM.length()+2];
70 int count = pis.read(bytes);
71 String response = new String(bytes, 0, count);
72 Assert.assertNotNull(response);
73 Assert.assertTrue(response.endsWith(EOM));
74 response = response.substring(0, response.length() - EOM.length()).trim();
75 Assert.assertEquals(request, response);
79 public void testSendReceive() throws IOException {
80 PipedOutputStream os = new PipedOutputStream();
81 PipedInputStream is = new PipedInputStream(os);
83 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
85 String request = "Hello, netconf!";
86 netconfAdapter.sendMessage(request);
87 String response = netconfAdapter.receiveMessage();
88 Assert.assertNotNull(response);
89 Assert.assertEquals(request, response.trim());