Jian Li | e9af3b4 | 2016-08-08 15:50:01 +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.types; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.testing.EqualsTester; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.buffer.Unpooled; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +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 | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 33 | import static org.onosproject.lisp.msg.types.LispListLcafAddress.ListLcafAddressReader; |
| 34 | import static org.onosproject.lisp.msg.types.LispListLcafAddress.ListLcafAddressWriter; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Unit tests for LispListLcafAddress class. |
| 38 | */ |
| 39 | public class LispListLcafAddressTest { |
| 40 | |
| 41 | private LispListLcafAddress address1; |
| 42 | private LispListLcafAddress sameAsAddress1; |
| 43 | private LispListLcafAddress address2; |
| 44 | |
| 45 | @Before |
| 46 | public void setup() { |
| 47 | |
| 48 | LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 49 | LispAfiAddress ipv6Address1 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")); |
| 50 | |
| 51 | List<LispAfiAddress> afiAddresses1 = Lists.newArrayList(); |
| 52 | afiAddresses1.add(ipv4Address1); |
| 53 | afiAddresses1.add(ipv6Address1); |
| 54 | |
| 55 | address1 = new LispListLcafAddress(afiAddresses1); |
| 56 | |
| 57 | sameAsAddress1 = new LispListLcafAddress(afiAddresses1); |
| 58 | |
| 59 | LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
| 60 | LispAfiAddress ipv6Address2 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8886")); |
| 61 | |
| 62 | List<LispAfiAddress> afiAddresses2 = Lists.newArrayList(); |
| 63 | afiAddresses2.add(ipv4Address2); |
| 64 | afiAddresses2.add(ipv6Address2); |
| 65 | |
| 66 | address2 = new LispListLcafAddress(afiAddresses2); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testEquality() { |
| 71 | new EqualsTester() |
| 72 | .addEqualityGroup(address1, sameAsAddress1) |
| 73 | .addEqualityGroup(address2).testEquals(); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testConstruction() { |
| 78 | LispListLcafAddress listLcafAddress = address1; |
| 79 | |
| 80 | LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 81 | LispAfiAddress ipv6Address1 = new LispIpv6Address(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")); |
| 82 | |
| 83 | List<LispAfiAddress> afiAddresses1 = Lists.newArrayList(); |
| 84 | afiAddresses1.add(ipv4Address1); |
| 85 | afiAddresses1.add(ipv6Address1); |
| 86 | |
| 87 | assertThat(listLcafAddress.getAddresses(), is(afiAddresses1)); |
| 88 | } |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 89 | |
| 90 | @Test |
| 91 | public void testSerialization() throws LispWriterException, LispParseError, LispReaderException { |
| 92 | ByteBuf byteBuf = Unpooled.buffer(); |
| 93 | |
| 94 | ListLcafAddressWriter writer = new ListLcafAddressWriter(); |
| 95 | writer.writeTo(byteBuf, address1); |
| 96 | |
| 97 | ListLcafAddressReader reader = new ListLcafAddressReader(); |
| 98 | LispListLcafAddress deserialized = reader.readFrom(byteBuf); |
| 99 | |
| 100 | new EqualsTester() |
| 101 | .addEqualityGroup(address1, deserialized).testEquals(); |
| 102 | } |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 103 | } |