Change nexus values to properties
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / test / java / org / openecomp / appc / adapter / netconf / internal / TestNetconfAdapter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.adapter.netconf.internal;
23
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.openecomp.appc.adapter.netconf.internal.NetconfAdapter;
27
28 import java.io.IOException;
29 import java.io.PipedInputStream;
30 import java.io.PipedOutputStream;
31
32 public class TestNetconfAdapter {
33
34         private static final String EOM = "]]>]]>";
35
36         @Test
37         public void testReceiveMessage() throws IOException {
38                 PipedOutputStream pos = new PipedOutputStream();
39                 PipedInputStream is = new PipedInputStream(pos);
40
41                 PipedInputStream pis = new PipedInputStream();
42                 PipedOutputStream os = new PipedOutputStream(pis);
43
44                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
45
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());
52         }
53
54         @Test
55         public void testSendMessage() throws IOException {
56                 PipedOutputStream pos = new PipedOutputStream();
57                 PipedInputStream is = new PipedInputStream(pos);
58
59                 PipedInputStream pis = new PipedInputStream();
60                 PipedOutputStream os = new PipedOutputStream(pis);
61
62                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
63
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);
73         }
74
75         @Test
76         public void testSendReceive() throws IOException {
77                 PipedOutputStream os = new PipedOutputStream();
78                 PipedInputStream is = new PipedInputStream(os);
79
80                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
81
82                 String request = "Hello, netconf!";
83                 netconfAdapter.sendMessage(request);
84                 String response = netconfAdapter.receiveMessage();
85                 Assert.assertNotNull(response);
86                 Assert.assertEquals(request, response.trim());
87         }
88 }