2 * Copyright (c) 2018 FRINX s.r.o., and others. All rights reserved.
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
8 package org.opendaylight.netconf.nettyutil.handler;
10 import static org.junit.Assert.assertEquals;
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;
20 public class NetconfEOMAggregatorTest {
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"
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"
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"
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"
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"
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"
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"
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"
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"
77 + "<car><name>porsche</name></car>\n"
78 + "<car><name>ford</name></car>\n"
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"
89 + "<car><name>porsche</name></car>\n"
90 + "<car><name>ford</name></car>\n"
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"
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"
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"
112 private static final String COMM_3_S_3 = "<car><name>porsche</name></car>\n"
113 + "<car><name>ford</name></car>\n"
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"
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"
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"
135 + "<car><name>porsche</name></car>\n"
136 + "<car><name>ford</name></car>\n"
141 private static NetconfEOMAggregator aggregator;
144 public void setUp() throws Exception {
145 aggregator = new NetconfEOMAggregator();
149 public void testDecodeMessagesReadAtOnce() {
150 final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
151 final List<Object> out = new LinkedList<>();
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)));
160 public void testDecodeMessagesReadByteByByte() {
161 final ByteBuf in = Unpooled.buffer();
162 final List<Object> out = new LinkedList<>();
164 for (final byte b : COMM_1.getBytes()) {
166 aggregator.decode(null, in, out);
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)));
175 public void testDecodeMultipleStreams() {
176 final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
177 final List<Object> out = new LinkedList<>();
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)));
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)));
191 public void testDecodeBufferReset() {
192 final ByteBuf in = Unpooled.buffer();
193 final List<Object> out = new LinkedList<>();
195 in.writeBytes((COMM_3_S_1 + COMM_3_S_2).getBytes());
197 aggregator.decode(null, in, out);
198 assertEquals(1, out.size());
199 assertEquals(COMM_3_M_1, byteBufToString((ByteBuf) out.get(0)));
201 aggregator.decode(null, in, out);
202 assertEquals(1, out.size());
205 in.writeBytes((COMM_3_S_2 + COMM_3_S_3).getBytes());
207 aggregator.decode(null, in, out);
208 assertEquals(2, out.size());
209 assertEquals(COMM_3_M_2, byteBufToString((ByteBuf) out.get(1)));
213 public void testDecodeEmptyMessage() {
214 final ByteBuf in = Unpooled.buffer();
215 final List<Object> out = new LinkedList<>();
217 for (final byte b : MessageParts.END_OF_MESSAGE) {
219 aggregator.decode(null, in, out);
220 assertEquals(0, aggregator.bodyLength());
223 assertEquals(1, out.size());
224 assertEquals("", byteBufToString((ByteBuf) out.get(0)));
227 private static String byteBufToString(final ByteBuf byteBuf) {
228 return byteBuf.toString(Charset.defaultCharset());