ed1db238fda0fb01f5b3dc29865d27c0224816f5
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onosproject.openflow.controller.impl;
17
18
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;
25
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;
30
31 /**
32  * Tests for the OpenFlow message decoder.
33  */
34 public class OFMessageDecoderTest {
35
36     static class ConnectedChannel extends ChannelAdapter {
37         @Override
38         public boolean isConnected() {
39             return true;
40         }
41     }
42
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);
48         return channelBuffer;
49     }
50
51     /**
52      * Tests decoding a message on a closed channel.
53      *
54      * @throws Exception when an exception is thrown from the decoder
55      */
56     @Test
57     public void testDecodeNoChannel() throws Exception {
58         OFMessageDecoder decoder = new OFMessageDecoder();
59         ChannelBuffer channelBuffer = getHelloMessageBuffer();
60         Object message =
61                 decoder.decode(new ChannelHandlerContextAdapter(),
62                                new ChannelAdapter(),
63                                channelBuffer);
64         assertThat(message, nullValue());
65     }
66
67     /**
68      * Tests decoding a message.
69      *
70      * @throws Exception when an exception is thrown from the decoder
71      */
72     @Test
73     public void testDecode() throws Exception {
74         OFMessageDecoder decoder = new OFMessageDecoder();
75         ChannelBuffer channelBuffer = getHelloMessageBuffer();
76         Object message =
77                 decoder.decode(new ChannelHandlerContextAdapter(),
78                                new ConnectedChannel(),
79                                channelBuffer);
80         assertThat(message, notNullValue());
81         assertThat(message, instanceOf(OFHello.class));
82     }
83
84 }