blob: 73246f17cd8a5d029153275bb490909f8a2a8cb7 [file] [log] [blame]
Jian Lie9af3b42016-08-08 15:50:01 +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.types;
17
18import com.google.common.testing.EqualsTester;
Jian Li76ea0572016-08-29 12:41:16 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Lie9af3b42016-08-08 15:50:01 +090021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
Jian Li76ea0572016-08-29 12:41:16 +090024import org.onosproject.lisp.msg.exceptions.LispParseError;
25import org.onosproject.lisp.msg.exceptions.LispReaderException;
26import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li84b75822016-10-04 23:10:35 +090027import org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentAddressBuilder;
Jian Li5e505c62016-12-05 02:44:24 +090028import org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentLcafAddressReader;
29import org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentLcafAddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090030
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
33
34/**
35 * Unit tests for LispSegmentLcafAddress class.
36 */
37public class LispSegmentLcafAddressTest {
38
39 private LispSegmentLcafAddress address1;
40 private LispSegmentLcafAddress sameAsAddress1;
41 private LispSegmentLcafAddress address2;
42
43 @Before
44 public void setup() {
45
Jian Li84b75822016-10-04 23:10:35 +090046 SegmentAddressBuilder builder1 = new SegmentAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090047
Jian Li115d8602016-08-15 20:21:53 +090048 LispIpv4Address ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
49
50 address1 = builder1
51 .withIdMaskLength((byte) 0x01)
52 .withInstanceId(1)
53 .withAddress(ipv4Address1)
54 .build();
55
Jian Li84b75822016-10-04 23:10:35 +090056 SegmentAddressBuilder builder2 = new SegmentAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090057
58 sameAsAddress1 = builder2
59 .withIdMaskLength((byte) 0x01)
60 .withInstanceId(1)
61 .withAddress(ipv4Address1)
62 .build();
63
Jian Li84b75822016-10-04 23:10:35 +090064 SegmentAddressBuilder builder3 = new SegmentAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090065
66 LispIpv4Address ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
Jian Li115d8602016-08-15 20:21:53 +090067 address2 = builder3
68 .withIdMaskLength((byte) 0x02)
69 .withInstanceId(2)
70 .withAddress(ipv4Address2)
71 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090072 }
73
74 @Test
75 public void testEquality() {
76 new EqualsTester()
77 .addEqualityGroup(address1, sameAsAddress1)
78 .addEqualityGroup(address2).testEquals();
79 }
80
81 @Test
82 public void testConstruction() {
83 LispSegmentLcafAddress segmentLcafAddress = address1;
84
85 LispIpv4Address ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
86
87 assertThat(segmentLcafAddress.getIdMaskLength(), is((byte) 0x01));
88 assertThat(segmentLcafAddress.getInstanceId(), is(1));
89 assertThat(segmentLcafAddress.getAddress(), is(ipv4Address));
90 }
Jian Li76ea0572016-08-29 12:41:16 +090091
92 @Test
93 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
94 ByteBuf byteBuf = Unpooled.buffer();
95
96 SegmentLcafAddressWriter writer = new SegmentLcafAddressWriter();
97 writer.writeTo(byteBuf, address1);
98
99 SegmentLcafAddressReader reader = new SegmentLcafAddressReader();
100 LispSegmentLcafAddress deserialized = reader.readFrom(byteBuf);
101
Jian Li5e505c62016-12-05 02:44:24 +0900102 new EqualsTester().addEqualityGroup(address1, deserialized).testEquals();
Jian Li76ea0572016-08-29 12:41:16 +0900103 }
Jian Lie9af3b42016-08-08 15:50:01 +0900104}