Jian Li | 3ff578d | 2017-02-05 15:34:37 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.lcaf; |
| 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.LispIpv4Address; |
| 28 | import org.onosproject.lisp.msg.types.lcaf.LispNonceLcafAddress.NonceAddressBuilder; |
| 29 | import org.onosproject.lisp.msg.types.lcaf.LispNonceLcafAddress.NonceLcafAddressReader; |
| 30 | import org.onosproject.lisp.msg.types.lcaf.LispNonceLcafAddress.NonceLcafAddressWriter; |
| 31 | |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.is; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for LispNonceLcafAddress class. |
| 37 | */ |
| 38 | public class LispNonceLcafAddressTest { |
| 39 | |
| 40 | private static final String IP_ADDRESS_1 = "192.168.1.1"; |
| 41 | private static final String IP_ADDRESS_2 = "192.168.1.2"; |
| 42 | |
| 43 | private static final int NONCE_1 = 1048576; |
| 44 | private static final int NONCE_2 = 1; |
| 45 | |
| 46 | private LispNonceLcafAddress address1; |
| 47 | private LispNonceLcafAddress sameAsAddress1; |
| 48 | private LispNonceLcafAddress address2; |
| 49 | |
| 50 | @Before |
| 51 | public void setup() { |
| 52 | |
| 53 | NonceAddressBuilder builder1 = new NonceAddressBuilder(); |
| 54 | |
| 55 | LispIpv4Address ipv4Address1 = |
| 56 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1)); |
| 57 | |
| 58 | address1 = builder1 |
| 59 | .withNonce(NONCE_1) |
| 60 | .withAddress(ipv4Address1) |
| 61 | .build(); |
| 62 | |
| 63 | NonceAddressBuilder builder2 = new NonceAddressBuilder(); |
| 64 | |
| 65 | sameAsAddress1 = builder2 |
| 66 | .withNonce(NONCE_1) |
| 67 | .withAddress(ipv4Address1) |
| 68 | .build(); |
| 69 | |
| 70 | NonceAddressBuilder builder3 = new NonceAddressBuilder(); |
| 71 | |
| 72 | LispIpv4Address ipv4Address2 = |
| 73 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2)); |
| 74 | |
| 75 | address2 = builder3 |
| 76 | .withNonce(NONCE_2) |
| 77 | .withAddress(ipv4Address2) |
| 78 | .build(); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testEquality() { |
| 83 | new EqualsTester() |
| 84 | .addEqualityGroup(address1, sameAsAddress1) |
| 85 | .addEqualityGroup(address2).testEquals(); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testConstruction() { |
| 90 | LispNonceLcafAddress nonceLcafAddress = address1; |
| 91 | |
| 92 | LispIpv4Address ipv4Address = |
| 93 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1)); |
| 94 | |
| 95 | assertThat(nonceLcafAddress.getNonce(), is(NONCE_1)); |
| 96 | assertThat(nonceLcafAddress.getAddress(), is(ipv4Address)); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testSerialization() throws LispWriterException, LispParseError, |
| 101 | LispReaderException { |
| 102 | ByteBuf byteBuf = Unpooled.buffer(); |
| 103 | |
| 104 | NonceLcafAddressWriter writer = new NonceLcafAddressWriter(); |
| 105 | writer.writeTo(byteBuf, address1); |
| 106 | |
| 107 | NonceLcafAddressReader reader = new NonceLcafAddressReader(); |
| 108 | LispNonceLcafAddress deserialized = reader.readFrom(byteBuf); |
| 109 | |
| 110 | new EqualsTester().addEqualityGroup(address1, deserialized).testEquals(); |
| 111 | } |
| 112 | } |