Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.buffer.Unpooled; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 24 | import org.onlab.packet.IpAddress; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 27 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 28 | import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder; |
| 29 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 30 | |
| 31 | import java.util.List; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.is; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 35 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.*; |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 36 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Unit tests for DefaultLispMapNotify class. |
| 40 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 41 | public final class DefaultLispMapNotifyTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 42 | |
| 43 | private LispMapNotify notify1; |
| 44 | private LispMapNotify sameAsNotify1; |
| 45 | private LispMapNotify notify2; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 50 | NotifyBuilder builder1 = |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 51 | new DefaultNotifyBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 52 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 53 | List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord()); |
| 54 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 55 | notify1 = builder1 |
| 56 | .withKeyId((short) 1) |
| 57 | .withNonce(1L) |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 58 | .withMapRecords(records1) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 59 | .build(); |
| 60 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 61 | NotifyBuilder builder2 = |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 62 | new DefaultNotifyBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 63 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 64 | List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord()); |
| 65 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 66 | sameAsNotify1 = builder2 |
| 67 | .withKeyId((short) 1) |
| 68 | .withNonce(1L) |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 69 | .withMapRecords(records2) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 70 | .build(); |
| 71 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 72 | NotifyBuilder builder3 = |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 73 | new DefaultNotifyBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 74 | |
| 75 | notify2 = builder3 |
| 76 | .withKeyId((short) 2) |
| 77 | .withNonce(2L) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 78 | .build(); |
| 79 | } |
| 80 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 81 | 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 Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 96 | @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 Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame^] | 109 | assertThat(notify.getRecordCount(), is(2)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 110 | } |
| 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 Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 124 | } |
| 125 | } |