blob: eda8f0a8f6c73b2d33a80a7161a508ced7269d39 [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 Lie9af3b42016-08-08 15:50:01 +090027
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
Jian Li76ea0572016-08-29 12:41:16 +090030import static org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentLcafAddressReader;
31import static org.onosproject.lisp.msg.types.LispSegmentLcafAddress.SegmentLcafAddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090032
33/**
34 * Unit tests for LispSegmentLcafAddress class.
35 */
36public class LispSegmentLcafAddressTest {
37
38 private LispSegmentLcafAddress address1;
39 private LispSegmentLcafAddress sameAsAddress1;
40 private LispSegmentLcafAddress address2;
41
42 @Before
43 public void setup() {
44
Jian Li115d8602016-08-15 20:21:53 +090045 LispSegmentLcafAddress.SegmentAddressBuilder builder1 =
46 new LispSegmentLcafAddress.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
56 LispSegmentLcafAddress.SegmentAddressBuilder builder2 =
57 new LispSegmentLcafAddress.SegmentAddressBuilder();
58
59 sameAsAddress1 = builder2
60 .withIdMaskLength((byte) 0x01)
61 .withInstanceId(1)
62 .withAddress(ipv4Address1)
63 .build();
64
65 LispSegmentLcafAddress.SegmentAddressBuilder builder3 =
66 new LispSegmentLcafAddress.SegmentAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090067
68 LispIpv4Address ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
Jian Li115d8602016-08-15 20:21:53 +090069 address2 = builder3
70 .withIdMaskLength((byte) 0x02)
71 .withInstanceId(2)
72 .withAddress(ipv4Address2)
73 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090074 }
75
76 @Test
77 public void testEquality() {
78 new EqualsTester()
79 .addEqualityGroup(address1, sameAsAddress1)
80 .addEqualityGroup(address2).testEquals();
81 }
82
83 @Test
84 public void testConstruction() {
85 LispSegmentLcafAddress segmentLcafAddress = address1;
86
87 LispIpv4Address ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
88
89 assertThat(segmentLcafAddress.getIdMaskLength(), is((byte) 0x01));
90 assertThat(segmentLcafAddress.getInstanceId(), is(1));
91 assertThat(segmentLcafAddress.getAddress(), is(ipv4Address));
92 }
Jian Li76ea0572016-08-29 12:41:16 +090093
94 @Test
95 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
96 ByteBuf byteBuf = Unpooled.buffer();
97
98 SegmentLcafAddressWriter writer = new SegmentLcafAddressWriter();
99 writer.writeTo(byteBuf, address1);
100
101 SegmentLcafAddressReader reader = new SegmentLcafAddressReader();
102 LispSegmentLcafAddress deserialized = reader.readFrom(byteBuf);
103
104 new EqualsTester()
105 .addEqualityGroup(address1, deserialized).testEquals();
106 }
Jian Lie9af3b42016-08-08 15:50:01 +0900107}