First part of onap rename
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / test / java / org / onap / appc / adapter / netconf / internal / TestNetconfAdapter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.netconf.internal;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.onap.appc.adapter.netconf.internal.NetconfAdapter;
30
31 import java.io.IOException;
32 import java.io.PipedInputStream;
33 import java.io.PipedOutputStream;
34
35 public class TestNetconfAdapter {
36
37         private static final String EOM = "]]>]]>";
38
39         @Test
40         public void testReceiveMessage() throws IOException {
41                 PipedOutputStream pos = new PipedOutputStream();
42                 PipedInputStream is = new PipedInputStream(pos);
43
44                 PipedInputStream pis = new PipedInputStream();
45                 PipedOutputStream os = new PipedOutputStream(pis);
46
47                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
48
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());
55         }
56
57         @Test
58         public void testSendMessage() throws IOException {
59                 PipedOutputStream pos = new PipedOutputStream();
60                 PipedInputStream is = new PipedInputStream(pos);
61
62                 PipedInputStream pis = new PipedInputStream();
63                 PipedOutputStream os = new PipedOutputStream(pis);
64
65                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
66
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);
76         }
77
78         @Test
79         public void testSendReceive() throws IOException {
80                 PipedOutputStream os = new PipedOutputStream();
81                 PipedInputStream is = new PipedInputStream(os);
82
83                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
84
85                 String request = "Hello, netconf!";
86                 netconfAdapter.sendMessage(request);
87                 String response = netconfAdapter.receiveMessage();
88                 Assert.assertNotNull(response);
89                 Assert.assertEquals(request, response.trim());
90         }
91 }