e2d5f8da3d95fa9439380ecb7b7cac7f88c77797
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfEOMAggregatorTest.java
1 /*
2  * Copyright (c) 2018 FRINX s.r.o., and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.nettyutil.handler;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.nio.charset.Charset;
15 import java.util.LinkedList;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19
20 public class NetconfEOMAggregatorTest {
21
22     private static final String COMM_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
23             + "<rpc-reply message-id=\"105\"\n"
24             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
25             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
26             + "<users>\n"
27             + "<user><name>root</name><type>superuser</type></user>\n"
28             + "<user><name>fred</name><type>admin</type></user>\n"
29             + "<user><name>barney</name><type>admin</type></user>\n"
30             + "</users>\n"
31             + "</config>\n"
32             + "</rpc-reply>\n"
33             + "]]>]]>"
34             + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
35             + "<rpc-reply message-id=\"106\"\n"
36             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
37             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
38             + "<users>\n"
39             + "<user><name>root</name><type>superuser</type></user>\n"
40             + "<user><name>fred</name><type>admin</type></user>\n"
41             + "<user><name>barney</name><type>admin</type></user>\n"
42             + "<user><name>joe</name><type>user</type></user>\n"
43             + "</users>\n"
44             + "</config>\n"
45             + "</rpc-reply>\n"
46             + "]]>]]>";
47
48     private static final String COMM_1_M_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
49             + "<rpc-reply message-id=\"105\"\n"
50             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
51             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
52             + "<users>\n"
53             + "<user><name>root</name><type>superuser</type></user>\n"
54             + "<user><name>fred</name><type>admin</type></user>\n"
55             + "<user><name>barney</name><type>admin</type></user>\n"
56             + "</users>\n"
57             + "</config>\n"
58             + "</rpc-reply>\n";
59     private static final String COMM_1_M_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
60             + "<rpc-reply message-id=\"106\"\n"
61             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
62             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
63             + "<users>\n"
64             + "<user><name>root</name><type>superuser</type></user>\n"
65             + "<user><name>fred</name><type>admin</type></user>\n"
66             + "<user><name>barney</name><type>admin</type></user>\n"
67             + "<user><name>joe</name><type>user</type></user>\n"
68             + "</users>\n"
69             + "</config>\n"
70             + "</rpc-reply>\n";
71
72     private static final String COMM_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
73             + "<rpc-reply message-id=\"107\"\n"
74             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
75             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
76             + "<cars>\n"
77             + "<car><name>porsche</name></car>\n"
78             + "<car><name>ford</name></car>\n"
79             + "</cars>\n"
80             + "</config>\n"
81             + "</rpc-reply>\n"
82             + "]]>]]>";
83
84     private static final String COMM_2_M_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
85             + "<rpc-reply message-id=\"107\"\n"
86             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
87             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
88             + "<cars>\n"
89             + "<car><name>porsche</name></car>\n"
90             + "<car><name>ford</name></car>\n"
91             + "</cars>\n"
92             + "</config>\n"
93             + "</rpc-reply>\n";
94
95     private static final String COMM_3_S_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
96             + "<rpc-reply message-id=\"105\"\n"
97             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
98             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
99             + "<users>\n"
100             + "<user><name>root</name><type>superuser</type></user>\n"
101             + "<user><name>fred</name><type>admin</type></user>\n"
102             + "<user><name>barney</name><type>admin</type></user>\n"
103             + "</users>\n"
104             + "</config>\n"
105             + "</rpc-reply>\n"
106             + "]]>]]>";
107     private static final String COMM_3_S_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
108             + "<rpc-reply message-id=\"107\"\n"
109             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
110             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
111             + "<cars>\n";
112     private static final String COMM_3_S_3 = "<car><name>porsche</name></car>\n"
113             + "<car><name>ford</name></car>\n"
114             + "</cars>\n"
115             + "</config>\n"
116             + "</rpc-reply>\n"
117             + "]]>]]>";
118
119     private static final String COMM_3_M_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
120             + "<rpc-reply message-id=\"105\"\n"
121             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
122             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
123             + "<users>\n"
124             + "<user><name>root</name><type>superuser</type></user>\n"
125             + "<user><name>fred</name><type>admin</type></user>\n"
126             + "<user><name>barney</name><type>admin</type></user>\n"
127             + "</users>\n"
128             + "</config>\n"
129             + "</rpc-reply>\n";
130     private static final String COMM_3_M_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
131             + "<rpc-reply message-id=\"107\"\n"
132             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
133             + "<config xmlns=\"http://example.com/schema/1.2/config\">\n"
134             + "<cars>\n"
135             + "<car><name>porsche</name></car>\n"
136             + "<car><name>ford</name></car>\n"
137             + "</cars>\n"
138             + "</config>\n"
139             + "</rpc-reply>\n";
140
141     private static NetconfEOMAggregator aggregator;
142
143     @Before
144     public void setUp() throws Exception {
145         aggregator = new NetconfEOMAggregator();
146     }
147
148     @Test
149     public void testDecodeMessagesReadAtOnce() {
150         final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
151         final List<Object> out = new LinkedList<>();
152
153         aggregator.decode(null, in, out);
154         assertEquals(2, out.size());
155         assertEquals(COMM_1_M_1, byteBufToString((ByteBuf) out.get(0)));
156         assertEquals(COMM_1_M_2, byteBufToString((ByteBuf) out.get(1)));
157     }
158
159     @Test
160     public void testDecodeMessagesReadByteByByte() {
161         final ByteBuf in = Unpooled.buffer();
162         final List<Object> out = new LinkedList<>();
163
164         for (final byte b : COMM_1.getBytes()) {
165             in.writeByte(b);
166             aggregator.decode(null, in, out);
167         }
168
169         assertEquals(2, out.size());
170         assertEquals(COMM_1_M_1, byteBufToString((ByteBuf) out.get(0)));
171         assertEquals(COMM_1_M_2, byteBufToString((ByteBuf) out.get(1)));
172     }
173
174     @Test
175     public void testDecodeMultipleStreams() {
176         final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
177         final List<Object> out = new LinkedList<>();
178
179         aggregator.decode(null, in, out);
180         assertEquals(2, out.size());
181         assertEquals(COMM_1_M_1, byteBufToString((ByteBuf) out.get(0)));
182         assertEquals(COMM_1_M_2, byteBufToString((ByteBuf) out.get(1)));
183
184         final ByteBuf in2 = Unpooled.copiedBuffer(COMM_2.getBytes());
185         aggregator.decode(null, in2, out);
186         assertEquals(3, out.size());
187         assertEquals(COMM_2_M_1, byteBufToString((ByteBuf) out.get(2)));
188     }
189
190     @Test
191     public void testDecodeBufferReset() {
192         final ByteBuf in = Unpooled.buffer();
193         final List<Object> out = new LinkedList<>();
194
195         in.writeBytes((COMM_3_S_1 + COMM_3_S_2).getBytes());
196
197         aggregator.decode(null, in, out);
198         assertEquals(1, out.size());
199         assertEquals(COMM_3_M_1, byteBufToString((ByteBuf) out.get(0)));
200
201         aggregator.decode(null, in, out);
202         assertEquals(1, out.size());
203
204         in.clear();
205         in.writeBytes((COMM_3_S_2 + COMM_3_S_3).getBytes());
206
207         aggregator.decode(null, in, out);
208         assertEquals(2, out.size());
209         assertEquals(COMM_3_M_2, byteBufToString((ByteBuf) out.get(1)));
210     }
211
212     @Test
213     public void testDecodeEmptyMessage() {
214         final ByteBuf in = Unpooled.buffer();
215         final List<Object> out = new LinkedList<>();
216
217         for (final byte b : MessageParts.END_OF_MESSAGE) {
218             in.writeByte(b);
219             aggregator.decode(null, in, out);
220             assertEquals(0, aggregator.bodyLength());
221         }
222
223         assertEquals(1, out.size());
224         assertEquals("", byteBufToString((ByteBuf) out.get(0)));
225     }
226
227     private static String byteBufToString(final ByteBuf byteBuf) {
228         return byteBuf.toString(Charset.defaultCharset());
229     }
230 }