blob: 375ef259c6048487eeddf11b96cb6cdf1c7dd526 [file] [log] [blame]
Jian Lie4f12162016-09-13 00:09:09 +09001/*
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 */
Jian Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Lie4f12162016-09-13 00:09:09 +090017
18import com.google.common.collect.ImmutableList;
Jian Liafe2d3f2016-11-01 02:49:07 +090019import com.google.common.collect.Lists;
Jian Lie4f12162016-09-13 00:09:09 +090020import io.netty.buffer.ByteBuf;
21import io.netty.buffer.Unpooled;
Jian Liafe2d3f2016-11-01 02:49:07 +090022import io.netty.channel.socket.DatagramPacket;
Jian Lie4f12162016-09-13 00:09:09 +090023import org.junit.Test;
24import org.onosproject.lisp.msg.protocols.LispType;
25
26import java.nio.charset.StandardCharsets;
27import java.util.List;
28
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
31import static org.hamcrest.Matchers.notNullValue;
32
33/**
34 * Tests for LISP message encoder.
35 */
Jian Liafe2d3f2016-11-01 02:49:07 +090036public class LispMessageEncoderTest {
Jian Lie4f12162016-09-13 00:09:09 +090037
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 Lie4f12162016-09-13 00:09:09 +090057
Jian Liafe2d3f2016-11-01 02:49:07 +090058 List<DatagramPacket> list = Lists.newArrayList();
59 encoder.encode(null, message, list);
60
61 assertThat(list, notNullValue());
Jian Lie4f12162016-09-13 00:09:09 +090062
63 String expected = "LISP message [LISP_MAP_REQUEST] ";
Jian Liafe2d3f2016-11-01 02:49:07 +090064 String returned = new String(list.get(0).content().array(),
65 StandardCharsets.UTF_8).substring(0, expected.length());
Jian Lie4f12162016-09-13 00:09:09 +090066 assertThat(returned, is(expected));
67 }
68
Jian Li2174e322016-12-14 03:28:59 +090069 @Test
Jian Lie4f12162016-09-13 00:09:09 +090070 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();
Jian Li2174e322016-12-14 03:28:59 +090078 List<DatagramPacket> list = Lists.newArrayList();
Jian Lie4f12162016-09-13 00:09:09 +090079 List<MockLispMessage> messages = ImmutableList.of(request, reply, register, notify);
Jian Li2174e322016-12-14 03:28:59 +090080 encoder.encode(null, messages, list);
81
82 list.forEach(p -> {
83 byte[] tmp = new byte[p.content().writerIndex()];
84 p.content().readBytes(tmp);
85 buff.writeBytes(tmp);
86 });
Jian Lie4f12162016-09-13 00:09:09 +090087
88 assertThat(buff, notNullValue());
89
90 StringBuilder expBuilder = new StringBuilder();
91 expBuilder.append("LISP message [LISP_MAP_REQUEST] ");
92 expBuilder.append("LISP message [LISP_MAP_REPLY] ");
93 expBuilder.append("LISP message [LISP_MAP_REGISTER] ");
94 expBuilder.append("LISP message [LISP_MAP_NOTIFY] ");
95
96 String expected = expBuilder.toString();
Jian Li5e505c62016-12-05 02:44:24 +090097 String returned = new String(buff.array(),
98 StandardCharsets.UTF_8).substring(0, expected.length());
Jian Lie4f12162016-09-13 00:09:09 +090099 assertThat(returned, is(expected));
100 }
101}