1d1c8f142a0803888dc9badbd02b526b8d3041f6
[appc.git] /
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
30 import java.io.IOException;
31 import java.io.PipedInputStream;
32 import java.io.PipedOutputStream;
33
34 public class TestNetconfAdapter {
35
36         private static final String EOM = "]]>]]>";
37
38         @Test
39         public void testReceiveMessage() throws IOException {
40                 PipedOutputStream pos = new PipedOutputStream();
41                 PipedInputStream is = new PipedInputStream(pos);
42
43                 PipedInputStream pis = new PipedInputStream();
44                 PipedOutputStream os = new PipedOutputStream(pis);
45
46                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
47
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());
54         }
55
56         @Test
57         public void testSendMessage() throws IOException {
58                 PipedOutputStream pos = new PipedOutputStream();
59                 PipedInputStream is = new PipedInputStream(pos);
60
61                 PipedInputStream pis = new PipedInputStream();
62                 PipedOutputStream os = new PipedOutputStream(pis);
63
64                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
65
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);
75         }
76
77         @Test
78         public void testSendReceive() throws IOException {
79                 PipedOutputStream os = new PipedOutputStream();
80                 PipedInputStream is = new PipedInputStream(os);
81
82                 NetconfAdapter netconfAdapter = new NetconfAdapter(is, os);
83
84                 String request = "Hello, netconf!";
85                 netconfAdapter.sendMessage(request);
86                 String response = netconfAdapter.receiveMessage();
87                 Assert.assertNotNull(response);
88                 Assert.assertEquals(request, response.trim());
89         }
90 }