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.jupiter.api.Assertions.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.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
20 class NetconfEOMAggregatorTest {
21 private static final String COMM_1 = """
22 <?xml version="1.0" encoding="UTF-8"?>
23 <rpc-reply message-id="105"
24 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
25 <config xmlns="http://example.com/schema/1.2/config">
27 <user><name>root</name><type>superuser</type></user>
28 <user><name>fred</name><type>admin</type></user>
29 <user><name>barney</name><type>admin</type></user>
34 <?xml version="1.0" encoding="UTF-8"?>
35 <rpc-reply message-id="106"
36 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
37 <config xmlns="http://example.com/schema/1.2/config">
39 <user><name>root</name><type>superuser</type></user>
40 <user><name>fred</name><type>admin</type></user>
41 <user><name>barney</name><type>admin</type></user>
42 <user><name>joe</name><type>user</type></user>
49 private static final String COMM_1_M_1 = """
50 <?xml version="1.0" encoding="UTF-8"?>
51 <rpc-reply message-id="105"
52 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
53 <config xmlns="http://example.com/schema/1.2/config">
55 <user><name>root</name><type>superuser</type></user>
56 <user><name>fred</name><type>admin</type></user>
57 <user><name>barney</name><type>admin</type></user>
63 private static final String COMM_1_M_2 = """
64 <?xml version="1.0" encoding="UTF-8"?>
65 <rpc-reply message-id="106"
66 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
67 <config xmlns="http://example.com/schema/1.2/config">
69 <user><name>root</name><type>superuser</type></user>
70 <user><name>fred</name><type>admin</type></user>
71 <user><name>barney</name><type>admin</type></user>
72 <user><name>joe</name><type>user</type></user>
78 private static final String COMM_2 = """
79 <?xml version="1.0" encoding="UTF-8"?>
80 <rpc-reply message-id="107"
81 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
82 <config xmlns="http://example.com/schema/1.2/config">
84 <car><name>porsche</name></car>
85 <car><name>ford</name></car>
91 private static final String COMM_2_M_1 = """
92 <?xml version="1.0" encoding="UTF-8"?>
93 <rpc-reply message-id="107"
94 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
95 <config xmlns="http://example.com/schema/1.2/config">
97 <car><name>porsche</name></car>
98 <car><name>ford</name></car>
103 private static final String COMM_3_S_1 = """
104 <?xml version="1.0" encoding="UTF-8"?>
105 <rpc-reply message-id="105"
106 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
107 <config xmlns="http://example.com/schema/1.2/config">
109 <user><name>root</name><type>superuser</type></user>
110 <user><name>fred</name><type>admin</type></user>
111 <user><name>barney</name><type>admin</type></user>
116 private static final String COMM_3_S_2 = """
117 <?xml version="1.0" encoding="UTF-8"?>
118 <rpc-reply message-id="107"
119 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
120 <config xmlns="http://example.com/schema/1.2/config">
123 private static final String COMM_3_S_3 = """
124 <car><name>porsche</name></car>
125 <car><name>ford</name></car>
132 private static final String COMM_3_M_1 = """
133 <?xml version="1.0" encoding="UTF-8"?>
134 <rpc-reply message-id="105"
135 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
136 <config xmlns="http://example.com/schema/1.2/config">
138 <user><name>root</name><type>superuser</type></user>
139 <user><name>fred</name><type>admin</type></user>
140 <user><name>barney</name><type>admin</type></user>
145 private static final String COMM_3_M_2 = """
146 <?xml version="1.0" encoding="UTF-8"?>
147 <rpc-reply message-id="107"
148 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
149 <config xmlns="http://example.com/schema/1.2/config">
151 <car><name>porsche</name></car>
152 <car><name>ford</name></car>
158 private static NetconfEOMAggregator aggregator;
161 void setUp() throws Exception {
162 aggregator = new NetconfEOMAggregator();
166 void testDecodeMessagesReadAtOnce() {
167 final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
168 final List<Object> out = new LinkedList<>();
170 aggregator.decode(null, in, out);
171 assertEquals(2, out.size());
172 assertEquals(COMM_1_M_1, byteBufToString((ByteBuf) out.get(0)));
173 assertEquals(COMM_1_M_2, byteBufToString((ByteBuf) out.get(1)));
177 void testDecodeMessagesReadByteByByte() {
178 final ByteBuf in = Unpooled.buffer();
179 final List<Object> out = new LinkedList<>();
181 for (final byte b : COMM_1.getBytes()) {
183 aggregator.decode(null, in, out);
186 assertEquals(2, out.size());
187 assertEquals(COMM_1_M_1, byteBufToString((ByteBuf) out.get(0)));
188 assertEquals(COMM_1_M_2, byteBufToString((ByteBuf) out.get(1)));
192 void testDecodeMultipleStreams() {
193 final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
194 final List<Object> out = new LinkedList<>();
196 aggregator.decode(null, in, out);
197 assertEquals(2, out.size());
198 assertEquals(COMM_1_M_1, byteBufToString((ByteBuf) out.get(0)));
199 assertEquals(COMM_1_M_2, byteBufToString((ByteBuf) out.get(1)));
201 final ByteBuf in2 = Unpooled.copiedBuffer(COMM_2.getBytes());
202 aggregator.decode(null, in2, out);
203 assertEquals(3, out.size());
204 assertEquals(COMM_2_M_1, byteBufToString((ByteBuf) out.get(2)));
208 void testDecodeBufferReset() {
209 final ByteBuf in = Unpooled.buffer();
210 final List<Object> out = new LinkedList<>();
212 in.writeBytes((COMM_3_S_1 + COMM_3_S_2).getBytes());
214 aggregator.decode(null, in, out);
215 assertEquals(1, out.size());
216 assertEquals(COMM_3_M_1, byteBufToString((ByteBuf) out.get(0)));
218 aggregator.decode(null, in, out);
219 assertEquals(1, out.size());
222 in.writeBytes((COMM_3_S_2 + COMM_3_S_3).getBytes());
224 aggregator.decode(null, in, out);
225 assertEquals(2, out.size());
226 assertEquals(COMM_3_M_2, byteBufToString((ByteBuf) out.get(1)));
230 void testDecodeEmptyMessage() {
231 final ByteBuf in = Unpooled.buffer();
232 final List<Object> out = new LinkedList<>();
234 for (final byte b : MessageParts.END_OF_MESSAGE) {
236 aggregator.decode(null, in, out);
237 assertEquals(0, aggregator.bodyLength());
240 assertEquals(1, out.size());
241 assertEquals("", byteBufToString((ByteBuf) out.get(0)));
244 private static String byteBufToString(final ByteBuf byteBuf) {
245 return byteBuf.toString(Charset.defaultCharset());