blob: 59685f16b062e2b75a5b833a01479d3d130b64df [file] [log] [blame]
Ray Milkey4bf8a582015-11-04 08:26:19 -08001/*
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 */
16package org.onosproject.openflow.controller.impl;
17
18import java.nio.charset.StandardCharsets;
19import java.util.List;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.junit.Test;
23import org.onosproject.openflow.OfMessageAdapter;
24import org.projectfloodlight.openflow.protocol.OFMessage;
25
26import com.google.common.collect.ImmutableList;
27
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.notNullValue;
31
32/**
33 * Tests for the OpenFlow message encoder.
34 */
35public class OFMessageEncoderTest {
36
37 static class MockOfMessage extends OfMessageAdapter {
38 static int nextId = 1;
39 final int id;
40
41 MockOfMessage() {
42 id = nextId++;
43 }
44
45 @Override
46 public void writeTo(ChannelBuffer channelBuffer) {
47 String message = "message" + Integer.toString(id) + " ";
48 channelBuffer.writeBytes(message.getBytes(StandardCharsets.UTF_8));
49 }
50 }
51
52 /**
53 * Tests that encoding a non-list returns the object specified.
54 *
55 * @throws Exception on exception in the encoder
56 */
57 @Test
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));
64 }
65
66 /**
67 * Tests that encoding a list returns the proper encoded payload.
68 *
69 * @throws Exception on exception in the encoder
70 */
71 @Test
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 ";
83 String listMessage =
84 (new String(channelBytes, StandardCharsets.UTF_8))
85 .substring(0, expectedListMessage.length());
86 assertThat(listMessage, is(expectedListMessage));
87 }
88}