Jian Li | 01f75b9 | 2016-12-12 03:05:07 +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.testing.EqualsTester; |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 27 | import org.onosproject.lisp.msg.types.LispAsLcafAddress.AsAddressBuilder; |
| 28 | import org.onosproject.lisp.msg.types.LispAsLcafAddress.AsLcafAddressReader; |
| 29 | import org.onosproject.lisp.msg.types.LispAsLcafAddress.AsLcafAddressWriter; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for LispAsLcafAddress class. |
| 36 | */ |
| 37 | public class LispAsLcafAddressTest { |
| 38 | |
| 39 | private LispAsLcafAddress address1; |
| 40 | private LispAsLcafAddress sameAsAddress1; |
| 41 | private LispAsLcafAddress address2; |
| 42 | |
| 43 | @Before |
| 44 | public void setup() { |
| 45 | |
| 46 | AsAddressBuilder builder1 = new AsAddressBuilder(); |
| 47 | |
| 48 | LispIpv4Address ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 49 | |
| 50 | address1 = builder1 |
| 51 | .withAsNumber(1) |
| 52 | .withAddress(ipv4Address1) |
| 53 | .build(); |
| 54 | |
| 55 | AsAddressBuilder builder2 = new AsAddressBuilder(); |
| 56 | |
| 57 | sameAsAddress1 = builder2 |
| 58 | .withAsNumber(1) |
| 59 | .withAddress(ipv4Address1) |
| 60 | .build(); |
| 61 | |
| 62 | AsAddressBuilder builder3 = new AsAddressBuilder(); |
| 63 | |
| 64 | LispIpv4Address ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1")); |
| 65 | |
| 66 | address2 = builder3 |
| 67 | .withAsNumber(2) |
| 68 | .withAddress(ipv4Address2) |
| 69 | .build(); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testEquality() { |
| 74 | new EqualsTester() |
| 75 | .addEqualityGroup(address1, sameAsAddress1) |
| 76 | .addEqualityGroup(address2).testEquals(); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testConstruction() { |
| 81 | LispAsLcafAddress asLcafAddress = address1; |
| 82 | |
| 83 | LispIpv4Address ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1")); |
| 84 | |
| 85 | assertThat(asLcafAddress.getAsNumber(), is(1)); |
| 86 | assertThat(asLcafAddress.getAddress(), is(ipv4Address)); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testSerialization() throws LispWriterException, LispParseError, LispReaderException { |
| 91 | ByteBuf byteBuf = Unpooled.buffer(); |
| 92 | |
| 93 | AsLcafAddressWriter writer = new AsLcafAddressWriter(); |
| 94 | writer.writeTo(byteBuf, address1); |
| 95 | |
| 96 | AsLcafAddressReader reader = new AsLcafAddressReader(); |
| 97 | LispAsLcafAddress deserialized = reader.readFrom(byteBuf); |
| 98 | |
| 99 | new EqualsTester().addEqualityGroup(address1, deserialized).testEquals(); |
| 100 | } |
| 101 | } |