blob: 636c568d495b0865d43f9bc762d53c25d2146a63 [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;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070019import java.util.Collections;
Ray Milkey4bf8a582015-11-04 08:26:19 -080020
Jimmy Jine9b7a022016-08-12 16:56:48 -070021import io.netty.buffer.ByteBuf;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070022import io.netty.buffer.ByteBufAllocator;
23
24import org.hamcrest.Matchers;
25import org.junit.After;
26import org.junit.Before;
Ray Milkey4bf8a582015-11-04 08:26:19 -080027import org.junit.Test;
28import org.onosproject.openflow.OfMessageAdapter;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080029import org.projectfloodlight.openflow.protocol.OFType;
Ray Milkey4bf8a582015-11-04 08:26:19 -080030
Ray Milkey4bf8a582015-11-04 08:26:19 -080031import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
Ray Milkey4bf8a582015-11-04 08:26:19 -080033
34/**
35 * Tests for the OpenFlow message encoder.
36 */
37public class OFMessageEncoderTest {
38
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070039 private ByteBuf buf;
Ray Milkey4bf8a582015-11-04 08:26:19 -080040 static class MockOfMessage extends OfMessageAdapter {
41 static int nextId = 1;
42 final int id;
43
44 MockOfMessage() {
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080045 super(OFType.ERROR);
Ray Milkey4bf8a582015-11-04 08:26:19 -080046 id = nextId++;
47 }
48
49 @Override
Jimmy Jine9b7a022016-08-12 16:56:48 -070050 public void writeTo(ByteBuf byteBuf) {
Ray Milkey4bf8a582015-11-04 08:26:19 -080051 String message = "message" + Integer.toString(id) + " ";
Jimmy Jine9b7a022016-08-12 16:56:48 -070052 byteBuf.writeBytes(message.getBytes(StandardCharsets.UTF_8));
Ray Milkey4bf8a582015-11-04 08:26:19 -080053 }
54 }
55
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070056 @Before
57 public void setUp() {
58 buf = ByteBufAllocator.DEFAULT.buffer();
Ray Milkey4bf8a582015-11-04 08:26:19 -080059 }
60
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070061 @After
62 public void tearDown() {
63 buf.release();
64 }
65
Ray Milkey4bf8a582015-11-04 08:26:19 -080066 @Test
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070067 public void testEncode() throws Exception {
68 OFMessageEncoder encoder = OFMessageEncoder.getInstance();
Ray Milkey4bf8a582015-11-04 08:26:19 -080069 MockOfMessage message1 = new MockOfMessage();
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070070 encoder.encode(null, Collections.singletonList(message1), buf);
71
72 assertThat(buf.isReadable(), Matchers.is(true));
73 byte[] channelBytes = new byte[buf.readableBytes()];
74 buf.readBytes(channelBytes);
75 String expectedListMessage = "message1 ";
76 assertThat(channelBytes, is(expectedListMessage.getBytes()));
Ray Milkey4bf8a582015-11-04 08:26:19 -080077 }
78}