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;
18 import java.nio.charset.StandardCharsets;
19 import java.util.List;
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.junit.Test;
23 import org.onosproject.openflow.OfMessageAdapter;
24 import org.projectfloodlight.openflow.protocol.OFMessage;
26 import com.google.common.collect.ImmutableList;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.is;
30 import static org.hamcrest.Matchers.notNullValue;
33 * Tests for the OpenFlow message encoder.
35 public class OFMessageEncoderTest {
37 static class MockOfMessage extends OfMessageAdapter {
38 static int nextId = 1;
46 public void writeTo(ChannelBuffer channelBuffer) {
47 String message = "message" + Integer.toString(id) + " ";
48 channelBuffer.writeBytes(message.getBytes(StandardCharsets.UTF_8));
53 * Tests that encoding a non-list returns the object specified.
55 * @throws Exception on exception in the encoder
58 public void testNoList() throws Exception {
59 OFMessageEncoder encoder = new OFMessageEncoder();
60 MockOfMessage message = new MockOfMessage();
61 OFMessage returnedMessage =
62 (OFMessage) encoder.encode(null, null, message);
63 assertThat(message, is(returnedMessage));
67 * Tests that encoding a list returns the proper encoded payload.
69 * @throws Exception on exception in the encoder
72 public void testList() throws Exception {
73 OFMessageEncoder encoder = new OFMessageEncoder();
74 MockOfMessage message1 = new MockOfMessage();
75 MockOfMessage message2 = new MockOfMessage();
76 MockOfMessage message3 = new MockOfMessage();
77 List<MockOfMessage> messages = ImmutableList.of(message1, message2, message3);
78 ChannelBuffer returnedChannel =
79 (ChannelBuffer) encoder.encode(null, null, messages);
80 assertThat(returnedChannel, notNullValue());
81 byte[] channelBytes = returnedChannel.array();
82 String expectedListMessage = "message1 message2 message3 ";
84 (new String(channelBytes, StandardCharsets.UTF_8))
85 .substring(0, expectedListMessage.length());
86 assertThat(listMessage, is(expectedListMessage));