Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.openflow.controller.impl; |
| 17 | |
| 18 | import java.nio.charset.StandardCharsets; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 19 | import java.util.Collections; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 20 | |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 22 | import io.netty.buffer.ByteBufAllocator; |
| 23 | |
| 24 | import org.hamcrest.Matchers; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 27 | import org.junit.Test; |
| 28 | import org.onosproject.openflow.OfMessageAdapter; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFType; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 30 | |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Tests for the OpenFlow message encoder. |
| 36 | */ |
| 37 | public class OFMessageEncoderTest { |
| 38 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 39 | private ByteBuf buf; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 40 | static class MockOfMessage extends OfMessageAdapter { |
| 41 | static int nextId = 1; |
| 42 | final int id; |
| 43 | |
| 44 | MockOfMessage() { |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 45 | super(OFType.ERROR); |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 46 | id = nextId++; |
| 47 | } |
| 48 | |
| 49 | @Override |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 50 | public void writeTo(ByteBuf byteBuf) { |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 51 | String message = "message" + Integer.toString(id) + " "; |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 52 | byteBuf.writeBytes(message.getBytes(StandardCharsets.UTF_8)); |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 56 | @Before |
| 57 | public void setUp() { |
| 58 | buf = ByteBufAllocator.DEFAULT.buffer(); |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 61 | @After |
| 62 | public void tearDown() { |
| 63 | buf.release(); |
| 64 | } |
| 65 | |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 66 | @Test |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 67 | public void testEncode() throws Exception { |
| 68 | OFMessageEncoder encoder = OFMessageEncoder.getInstance(); |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 69 | MockOfMessage message1 = new MockOfMessage(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 70 | 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 Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 77 | } |
| 78 | } |