Updating licenses in all files
[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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.netconf.internal;
24
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.appc.adapter.netconf.internal.NetconfAdapter;
28
29 import java.io.IOException;
30 import java.io.PipedInputStream;
31 import java.io.PipedOutputStream;
32
33 public class TestNetconfAdapter {
34
35         private static final String EOM = "]]>]]>";
36
37         @Test
38         public void testReceiveMessage() throws IOException {
39                 PipedOutputStream pos = new PipedOutputStream();
40                 PipedInputStream is = new PipedInputStream(pos);
41
42                 PipedInputStream pis = new PipedInputStream();
43                 PipedOutputStream os = new PipedOutputStream(pis);
44
45                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
46
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());
53         }
54
55         @Test
56         public void testSendMessage() throws IOException {
57                 PipedOutputStream pos = new PipedOutputStream();
58                 PipedInputStream is = new PipedInputStream(pos);
59
60                 PipedInputStream pis = new PipedInputStream();
61                 PipedOutputStream os = new PipedOutputStream(pis);
62
63                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
64
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);
74         }
75
76         @Test
77         public void testSendReceive() throws IOException {
78                 PipedOutputStream os = new PipedOutputStream();
79                 PipedInputStream is = new PipedInputStream(os);
80
81                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
82
83                 String request = "Hello, netconf!";
84                 netconfAdapter.sendMessage(request);
85                 String response = netconfAdapter.receiveMessage();
86                 Assert.assertNotNull(response);
87                 Assert.assertEquals(request, response.trim());
88         }
89 }