blob: d09e5661dd4b50747a3da0a4eec8c4d8acd183ca [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;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080025import org.projectfloodlight.openflow.protocol.OFType;
Ray Milkey4bf8a582015-11-04 08:26:19 -080026
27import com.google.common.collect.ImmutableList;
28
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
31import static org.hamcrest.Matchers.notNullValue;
32
33/**
34 * Tests for the OpenFlow message encoder.
35 */
36public class OFMessageEncoderTest {
37
38 static class MockOfMessage extends OfMessageAdapter {
39 static int nextId = 1;
40 final int id;
41
42 MockOfMessage() {
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080043 super(OFType.ERROR);
Ray Milkey4bf8a582015-11-04 08:26:19 -080044 id = nextId++;
45 }
46
47 @Override
48 public void writeTo(ChannelBuffer channelBuffer) {
49 String message = "message" + Integer.toString(id) + " ";
50 channelBuffer.writeBytes(message.getBytes(StandardCharsets.UTF_8));
51 }
52 }
53
54 /**
55 * Tests that encoding a non-list returns the object specified.
56 *
57 * @throws Exception on exception in the encoder
58 */
59 @Test
60 public void testNoList() throws Exception {
61 OFMessageEncoder encoder = new OFMessageEncoder();
62 MockOfMessage message = new MockOfMessage();
63 OFMessage returnedMessage =
64 (OFMessage) encoder.encode(null, null, message);
65 assertThat(message, is(returnedMessage));
66 }
67
68 /**
69 * Tests that encoding a list returns the proper encoded payload.
70 *
71 * @throws Exception on exception in the encoder
72 */
73 @Test
74 public void testList() throws Exception {
75 OFMessageEncoder encoder = new OFMessageEncoder();
76 MockOfMessage message1 = new MockOfMessage();
77 MockOfMessage message2 = new MockOfMessage();
78 MockOfMessage message3 = new MockOfMessage();
79 List<MockOfMessage> messages = ImmutableList.of(message1, message2, message3);
80 ChannelBuffer returnedChannel =
81 (ChannelBuffer) encoder.encode(null, null, messages);
82 assertThat(returnedChannel, notNullValue());
83 byte[] channelBytes = returnedChannel.array();
84 String expectedListMessage = "message1 message2 message3 ";
85 String listMessage =
86 (new String(channelBytes, StandardCharsets.UTF_8))
87 .substring(0, expectedListMessage.length());
88 assertThat(listMessage, is(expectedListMessage));
89 }
90}