blob: 434e22a05f98f407b812e015637fa045c9dfbc71 [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
Jian Lib26d3502016-08-31 01:43:24 +090018import com.google.common.collect.ImmutableList;
Jian Li18f3bce2016-08-04 17:36:41 +090019import com.google.common.testing.EqualsTester;
Jian Lie4ba2a42016-08-29 20:24:15 +090020import io.netty.buffer.ByteBuf;
21import io.netty.buffer.Unpooled;
Jian Li18f3bce2016-08-04 17:36:41 +090022import org.junit.Before;
23import org.junit.Test;
Jian Lib26d3502016-08-31 01:43:24 +090024import org.onlab.packet.IpAddress;
Jian Lie4ba2a42016-08-29 20:24:15 +090025import org.onosproject.lisp.msg.exceptions.LispParseError;
26import org.onosproject.lisp.msg.exceptions.LispReaderException;
27import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Lib26d3502016-08-31 01:43:24 +090028import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
29import org.onosproject.lisp.msg.types.LispIpv4Address;
30
31import java.util.List;
Jian Li18f3bce2016-08-04 17:36:41 +090032
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
Jian Lie4ba2a42016-08-29 20:24:15 +090035import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.*;
Jian Lib26d3502016-08-31 01:43:24 +090036import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
Jian Li18f3bce2016-08-04 17:36:41 +090037
38/**
39 * Unit tests for DefaultLispMapNotify class.
40 */
Jian Li47671902016-08-11 01:18:18 +090041public final class DefaultLispMapNotifyTest {
Jian Li18f3bce2016-08-04 17:36:41 +090042
43 private LispMapNotify notify1;
44 private LispMapNotify sameAsNotify1;
45 private LispMapNotify notify2;
46
47 @Before
48 public void setup() {
49
Jian Lib26d3502016-08-31 01:43:24 +090050 NotifyBuilder builder1 =
Jian Lie4ba2a42016-08-29 20:24:15 +090051 new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090052
Jian Lib26d3502016-08-31 01:43:24 +090053 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
54
Jian Li18f3bce2016-08-04 17:36:41 +090055 notify1 = builder1
56 .withKeyId((short) 1)
57 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090058 .withMapRecords(records1)
Jian Li18f3bce2016-08-04 17:36:41 +090059 .build();
60
Jian Lib26d3502016-08-31 01:43:24 +090061 NotifyBuilder builder2 =
Jian Lie4ba2a42016-08-29 20:24:15 +090062 new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090063
Jian Lib26d3502016-08-31 01:43:24 +090064 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
65
Jian Li18f3bce2016-08-04 17:36:41 +090066 sameAsNotify1 = builder2
67 .withKeyId((short) 1)
68 .withNonce(1L)
Jian Lib26d3502016-08-31 01:43:24 +090069 .withMapRecords(records2)
Jian Li18f3bce2016-08-04 17:36:41 +090070 .build();
71
Jian Lib26d3502016-08-31 01:43:24 +090072 NotifyBuilder builder3 =
Jian Lie4ba2a42016-08-29 20:24:15 +090073 new DefaultNotifyBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090074
75 notify2 = builder3
76 .withKeyId((short) 2)
77 .withNonce(2L)
Jian Li18f3bce2016-08-04 17:36:41 +090078 .build();
79 }
80
Jian Lib26d3502016-08-31 01:43:24 +090081 private LispMapRecord getMapRecord() {
82 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
83
84 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
85
86 return builder1
87 .withRecordTtl(100)
88 .withAuthoritative(true)
89 .withMapVersionNumber((short) 1)
90 .withMaskLength((byte) 0x01)
91 .withAction(LispMapReplyAction.NativelyForward)
92 .withEidPrefixAfi(ipv4Locator1)
93 .build();
94 }
95
Jian Li18f3bce2016-08-04 17:36:41 +090096 @Test
97 public void testEquality() {
98 new EqualsTester()
99 .addEqualityGroup(notify1, sameAsNotify1)
100 .addEqualityGroup(notify2).testEquals();
101 }
102
103 @Test
104 public void testConstruction() {
105 DefaultLispMapNotify notify = (DefaultLispMapNotify) notify1;
106
107 assertThat(notify.getKeyId(), is((short) 1));
108 assertThat(notify.getNonce(), is(1L));
Jian Lib26d3502016-08-31 01:43:24 +0900109 assertThat(notify.getRecordCount(), is(2));
Jian Lie4ba2a42016-08-29 20:24:15 +0900110 }
111
112 @Test
113 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
114 ByteBuf byteBuf = Unpooled.buffer();
115
116 NotifyWriter writer = new NotifyWriter();
117 writer.writeTo(byteBuf, notify1);
118
119 NotifyReader reader = new NotifyReader();
120 LispMapNotify deserialized = reader.readFrom(byteBuf);
121
122 new EqualsTester()
123 .addEqualityGroup(notify1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900124 }
125}