Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openflow.controller.impl; |
| 17 | |
| 18 | import java.nio.charset.StandardCharsets; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.openflow.OfMessageAdapter; |
| 24 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.OFType; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 26 | |
| 27 | import com.google.common.collect.ImmutableList; |
| 28 | |
| 29 | import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | import static org.hamcrest.Matchers.is; |
| 31 | import static org.hamcrest.Matchers.notNullValue; |
| 32 | |
| 33 | /** |
| 34 | * Tests for the OpenFlow message encoder. |
| 35 | */ |
| 36 | public class OFMessageEncoderTest { |
| 37 | |
| 38 | static class MockOfMessage extends OfMessageAdapter { |
| 39 | static int nextId = 1; |
| 40 | final int id; |
| 41 | |
| 42 | MockOfMessage() { |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 43 | super(OFType.ERROR); |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 44 | 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 | } |