Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.lisp.ctl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.buffer.Unpooled; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 22 | import io.netty.channel.socket.DatagramPacket; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 23 | import org.junit.Test; |
| 24 | import org.onosproject.lisp.msg.protocols.LispType; |
| 25 | |
| 26 | import java.nio.charset.StandardCharsets; |
| 27 | import java.util.List; |
| 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 LISP message encoder. |
| 35 | */ |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 36 | public class LispMessageEncoderTest { |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 37 | |
| 38 | static class MockLispMessage extends LispMessageAdapter { |
| 39 | LispType type; |
| 40 | |
| 41 | public MockLispMessage(LispType type) { |
| 42 | super(type); |
| 43 | this.type = type; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void writeTo(ByteBuf buffer) { |
| 48 | String message = "LISP message [" + type.toString() + "] "; |
| 49 | buffer.writeBytes(message.getBytes(StandardCharsets.UTF_8)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testEncodeOneEntry() throws Exception { |
| 55 | LispMessageEncoder encoder = new LispMessageEncoder(); |
| 56 | MockLispMessage message = new MockLispMessage(LispType.LISP_MAP_REQUEST); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 57 | |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 58 | List<DatagramPacket> list = Lists.newArrayList(); |
| 59 | encoder.encode(null, message, list); |
| 60 | |
| 61 | assertThat(list, notNullValue()); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 62 | |
| 63 | String expected = "LISP message [LISP_MAP_REQUEST] "; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 64 | String returned = new String(list.get(0).content().array(), |
| 65 | StandardCharsets.UTF_8).substring(0, expected.length()); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 66 | assertThat(returned, is(expected)); |
| 67 | } |
| 68 | |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 69 | //@Test |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 70 | public void testEncode() throws Exception { |
| 71 | LispMessageEncoder encoder = new LispMessageEncoder(); |
| 72 | MockLispMessage request = new MockLispMessage(LispType.LISP_MAP_REQUEST); |
| 73 | MockLispMessage reply = new MockLispMessage(LispType.LISP_MAP_REPLY); |
| 74 | MockLispMessage register = new MockLispMessage(LispType.LISP_MAP_REGISTER); |
| 75 | MockLispMessage notify = new MockLispMessage(LispType.LISP_MAP_NOTIFY); |
| 76 | |
| 77 | ByteBuf buff = Unpooled.buffer(); |
| 78 | List<MockLispMessage> messages = ImmutableList.of(request, reply, register, notify); |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 79 | encoder.encode(null, messages, Lists.newArrayList()); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 80 | |
| 81 | assertThat(buff, notNullValue()); |
| 82 | |
| 83 | StringBuilder expBuilder = new StringBuilder(); |
| 84 | expBuilder.append("LISP message [LISP_MAP_REQUEST] "); |
| 85 | expBuilder.append("LISP message [LISP_MAP_REPLY] "); |
| 86 | expBuilder.append("LISP message [LISP_MAP_REGISTER] "); |
| 87 | expBuilder.append("LISP message [LISP_MAP_NOTIFY] "); |
| 88 | |
| 89 | String expected = expBuilder.toString(); |
| 90 | String returned = new String(buff.array(), StandardCharsets.UTF_8).substring(0, expected.length()); |
| 91 | assertThat(returned, is(expected)); |
| 92 | } |
| 93 | } |