Fix warnings reported in toaster
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / MessageParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.netconf.impl;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.util.Queue;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.channel.embedded.EmbeddedChannel;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.netconf.api.NetconfMessage;
25 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
26 import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
27 import org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder;
28 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
29 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
30 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
31 import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
32 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
33 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
34 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
35
36 public class MessageParserTest {
37
38     private NetconfMessage msg;
39     private NetconfMessageFactory msgFactory = new NetconfMessageFactory();
40
41     @Before
42     public void setUp() throws Exception {
43         this.msg = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
44     }
45
46     @Test
47     public void testChunkedFramingMechanismOnPipeline() throws Exception {
48         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
49                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
50                 new ProtocolMessageEncoder<NetconfMessage>(msgFactory),
51
52                 new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
53                 new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
54
55         testChunkChannel.writeOutbound(this.msg);
56         Queue<Object> messages = testChunkChannel.outboundMessages();
57         assertFalse(messages.isEmpty());
58
59         int msgLength = this.msgFactory.put(this.msg).length;
60         int chunkCount = msgLength / NetconfMessageConstants.MAX_CHUNK_SIZE;
61         if ((msgLength % NetconfMessageConstants.MAX_CHUNK_SIZE) != 0) {
62             chunkCount++;
63         }
64         for (int i = 1; i <= chunkCount; i++) {
65             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
66             int exptHeaderLength = NetconfMessageConstants.MAX_CHUNK_SIZE;
67             if (i == chunkCount) {
68                 exptHeaderLength = msgLength - (NetconfMessageConstants.MAX_CHUNK_SIZE * (i - 1));
69                 byte[] eom = new byte[NetconfMessageConstants.endOfChunk.length];
70                 recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfChunk.length,
71                         eom);
72                 assertArrayEquals(NetconfMessageConstants.endOfChunk, eom);
73             }
74
75             byte[] header = new byte[String.valueOf(exptHeaderLength).length()
76                     + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
77             recievedOutbound.getBytes(0, header);
78             NetconfMessageHeader messageHeader = new NetconfMessageHeader();
79             messageHeader.fromBytes(header);
80             assertEquals(exptHeaderLength, messageHeader.getLength());
81
82             testChunkChannel.writeInbound(recievedOutbound);
83         }
84         assertTrue(messages.isEmpty());
85
86         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
87         assertNotNull(receivedMessage);
88         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
89     }
90
91     @Test
92     public void testEOMFramingMechanismOnPipeline() throws Exception {
93         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
94                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
95                 new ProtocolMessageEncoder<NetconfMessage>(msgFactory), new NetconfMessageAggregator(
96                         FramingMechanism.EOM), new ProtocolMessageDecoder<NetconfMessage>(msgFactory));
97
98         testChunkChannel.writeOutbound(this.msg);
99         ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
100
101         byte[] eom = new byte[NetconfMessageConstants.endOfMessage.length];
102         recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfMessage.length, eom);
103         assertArrayEquals(NetconfMessageConstants.endOfMessage, eom);
104
105         testChunkChannel.writeInbound(recievedOutbound);
106         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
107         assertNotNull(receivedMessage);
108         assertTrue(this.msg.getDocument().isEqualNode(receivedMessage.getDocument()));
109     }
110 }