blob: f7c5e491b26847548f6271b752c8b0410dff6fec [file] [log] [blame]
Jian Li18f3bce2016-08-04 17:36:41 +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 */
16package org.onosproject.lisp.msg.protocols;
17
18import com.google.common.testing.EqualsTester;
Jian Lie4ba2a42016-08-29 20:24:15 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Li18f3bce2016-08-04 17:36:41 +090021import org.junit.Before;
22import org.junit.Test;
Jian Lie4ba2a42016-08-29 20:24:15 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
24import org.onosproject.lisp.msg.exceptions.LispReaderException;
25import org.onosproject.lisp.msg.exceptions.LispWriterException;
26import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader;
27import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterWriter;
Jian Li18f3bce2016-08-04 17:36:41 +090028
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
31
32/**
33 * Unit tests for DefaultLispMapRegister class.
34 */
Jian Li47671902016-08-11 01:18:18 +090035public final class DefaultLispMapRegisterTest {
Jian Li18f3bce2016-08-04 17:36:41 +090036
37 private LispMapRegister register1;
38 private LispMapRegister sameAsRegister1;
39 private LispMapRegister register2;
40
41 @Before
42 public void setup() {
43
44 LispMapRegister.RegisterBuilder builder1 =
45 new DefaultLispMapRegister.DefaultRegisterBuilder();
46
47 register1 = builder1
48 .withIsProxyMapReply(true)
49 .withIsWantMapNotify(false)
50 .withKeyId((short) 1)
51 .withNonce(1L)
Jian Lie4ba2a42016-08-29 20:24:15 +090052 .withRecordCount((byte) 0)
Jian Li18f3bce2016-08-04 17:36:41 +090053 .build();
54
55 LispMapRegister.RegisterBuilder builder2 =
56 new DefaultLispMapRegister.DefaultRegisterBuilder();
57
58 sameAsRegister1 = builder2
59 .withIsProxyMapReply(true)
60 .withIsWantMapNotify(false)
61 .withKeyId((short) 1)
62 .withNonce(1L)
Jian Lie4ba2a42016-08-29 20:24:15 +090063 .withRecordCount((byte) 0)
Jian Li18f3bce2016-08-04 17:36:41 +090064 .build();
65
66 LispMapRegister.RegisterBuilder builder3 =
67 new DefaultLispMapRegister.DefaultRegisterBuilder();
68
69 register2 = builder3
70 .withIsProxyMapReply(true)
71 .withIsWantMapNotify(false)
72 .withKeyId((short) 2)
73 .withNonce(2L)
74 .withRecordCount((byte) 0x02)
75 .build();
76 }
77
78 @Test
79 public void testEquality() {
80 new EqualsTester()
81 .addEqualityGroup(register1, sameAsRegister1)
82 .addEqualityGroup(register2).testEquals();
83 }
84
85 @Test
86 public void testConstruction() {
87 DefaultLispMapRegister register = (DefaultLispMapRegister) register1;
88
89 assertThat(register.isProxyMapReply(), is(true));
90 assertThat(register.isWantMapNotify(), is(false));
91 assertThat(register.getKeyId(), is((short) 1));
92 assertThat(register.getNonce(), is(1L));
Jian Lie4ba2a42016-08-29 20:24:15 +090093 assertThat(register.getRecordCount(), is((byte) 0));
94 }
95
96 @Test
97 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
98 ByteBuf byteBuf = Unpooled.buffer();
99
100 RegisterWriter writer = new RegisterWriter();
101 writer.writeTo(byteBuf, register1);
102
103 RegisterReader reader = new RegisterReader();
104 LispMapRegister deserialized = reader.readFrom(byteBuf);
105
106 new EqualsTester()
107 .addEqualityGroup(register1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900108 }
109}