2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.openflow.controller.impl;
19 import org.jboss.netty.buffer.ChannelBuffer;
20 import org.jboss.netty.buffer.ChannelBuffers;
21 import org.junit.Test;
22 import org.onosproject.openflow.ChannelAdapter;
23 import org.onosproject.openflow.ChannelHandlerContextAdapter;
24 import org.projectfloodlight.openflow.protocol.OFHello;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.instanceOf;
28 import static org.hamcrest.Matchers.notNullValue;
29 import static org.hamcrest.Matchers.nullValue;
32 * Tests for the OpenFlow message decoder.
34 public class OFMessageDecoderTest {
36 static class ConnectedChannel extends ChannelAdapter {
38 public boolean isConnected() {
43 private ChannelBuffer getHelloMessageBuffer() {
44 // OFHello, OF version 1, xid of 0, total of 8 bytes
45 byte[] messageData = {0x1, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0};
46 ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
47 channelBuffer.writeBytes(messageData);
52 * Tests decoding a message on a closed channel.
54 * @throws Exception when an exception is thrown from the decoder
57 public void testDecodeNoChannel() throws Exception {
58 OFMessageDecoder decoder = new OFMessageDecoder();
59 ChannelBuffer channelBuffer = getHelloMessageBuffer();
61 decoder.decode(new ChannelHandlerContextAdapter(),
64 assertThat(message, nullValue());
68 * Tests decoding a message.
70 * @throws Exception when an exception is thrown from the decoder
73 public void testDecode() throws Exception {
74 OFMessageDecoder decoder = new OFMessageDecoder();
75 ChannelBuffer channelBuffer = getHelloMessageBuffer();
77 decoder.decode(new ChannelHandlerContextAdapter(),
78 new ConnectedChannel(),
80 assertThat(message, notNullValue());
81 assertThat(message, instanceOf(OFHello.class));