blob: e62345c2cf826c51ad5ca13cc3af481700fc087c [file] [log] [blame]
Ray Milkey4bf8a582015-11-04 08:26:19 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey4bf8a582015-11-04 08:26:19 -08003 *
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
Jimmy Jine9b7a022016-08-12 16:56:48 -070021import io.netty.buffer.ByteBuf;
Ray Milkey4bf8a582015-11-04 08:26:19 -080022import org.jboss.netty.buffer.ChannelBuffer;
23import org.junit.Test;
24import org.onosproject.openflow.OfMessageAdapter;
25import org.projectfloodlight.openflow.protocol.OFMessage;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080026import org.projectfloodlight.openflow.protocol.OFType;
Ray Milkey4bf8a582015-11-04 08:26:19 -080027
28import com.google.common.collect.ImmutableList;
29
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.is;
32import static org.hamcrest.Matchers.notNullValue;
33
34/**
35 * Tests for the OpenFlow message encoder.
36 */
37public class OFMessageEncoderTest {
38
39 static class MockOfMessage extends OfMessageAdapter {
40 static int nextId = 1;
41 final int id;
42
43 MockOfMessage() {
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080044 super(OFType.ERROR);
Ray Milkey4bf8a582015-11-04 08:26:19 -080045 id = nextId++;
46 }
47
48 @Override
Jimmy Jine9b7a022016-08-12 16:56:48 -070049 public void writeTo(ByteBuf byteBuf) {
Ray Milkey4bf8a582015-11-04 08:26:19 -080050 String message = "message" + Integer.toString(id) + " ";
Jimmy Jine9b7a022016-08-12 16:56:48 -070051 byteBuf.writeBytes(message.getBytes(StandardCharsets.UTF_8));
Ray Milkey4bf8a582015-11-04 08:26:19 -080052 }
53 }
54
55 /**
56 * Tests that encoding a non-list returns the object specified.
57 *
58 * @throws Exception on exception in the encoder
59 */
60 @Test
61 public void testNoList() throws Exception {
62 OFMessageEncoder encoder = new OFMessageEncoder();
63 MockOfMessage message = new MockOfMessage();
64 OFMessage returnedMessage =
65 (OFMessage) encoder.encode(null, null, message);
66 assertThat(message, is(returnedMessage));
67 }
68
69 /**
70 * Tests that encoding a list returns the proper encoded payload.
71 *
72 * @throws Exception on exception in the encoder
73 */
74 @Test
75 public void testList() throws Exception {
76 OFMessageEncoder encoder = new OFMessageEncoder();
77 MockOfMessage message1 = new MockOfMessage();
78 MockOfMessage message2 = new MockOfMessage();
79 MockOfMessage message3 = new MockOfMessage();
80 List<MockOfMessage> messages = ImmutableList.of(message1, message2, message3);
81 ChannelBuffer returnedChannel =
82 (ChannelBuffer) encoder.encode(null, null, messages);
83 assertThat(returnedChannel, notNullValue());
84 byte[] channelBytes = returnedChannel.array();
85 String expectedListMessage = "message1 message2 message3 ";
86 String listMessage =
87 (new String(channelBytes, StandardCharsets.UTF_8))
88 .substring(0, expectedListMessage.length());
89 assertThat(listMessage, is(expectedListMessage));
90 }
91}