Jian Li | a445d3d | 2017-03-18 20:18:30 +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.drivers.lisp.extensions; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onosproject.mapping.addresses.MappingAddress; |
| 23 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 24 | |
| 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for LispNonceAddress extension class. |
| 30 | */ |
| 31 | public class LispNonceAddressTest { |
| 32 | |
| 33 | private static final IpPrefix IP_ADDRESS_1 = IpPrefix.valueOf("1.2.3.4/24"); |
| 34 | private static final IpPrefix IP_ADDRESS_2 = IpPrefix.valueOf("5.6.7.8/24"); |
| 35 | |
| 36 | private static final int NONCE_1 = 1048576; |
| 37 | private static final int NONCE_2 = 1; |
| 38 | |
| 39 | private LispNonceAddress address1; |
| 40 | private LispNonceAddress sameAsAddress1; |
| 41 | private LispNonceAddress address2; |
| 42 | |
| 43 | @Before |
| 44 | public void setUp() { |
| 45 | |
| 46 | MappingAddress ma1 = MappingAddresses.ipv4MappingAddress(IP_ADDRESS_1); |
| 47 | |
| 48 | address1 = new LispNonceAddress.Builder() |
| 49 | .withNonce(NONCE_1) |
| 50 | .withAddress(ma1) |
| 51 | .build(); |
| 52 | |
| 53 | sameAsAddress1 = new LispNonceAddress.Builder() |
| 54 | .withNonce(NONCE_1) |
| 55 | .withAddress(ma1) |
| 56 | .build(); |
| 57 | |
| 58 | MappingAddress ma2 = MappingAddresses.ipv4MappingAddress(IP_ADDRESS_2); |
| 59 | |
| 60 | address2 = new LispNonceAddress.Builder() |
| 61 | .withNonce(NONCE_2) |
| 62 | .withAddress(ma2) |
| 63 | .build(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testEquality() { |
| 68 | new EqualsTester() |
| 69 | .addEqualityGroup(address1, sameAsAddress1) |
| 70 | .addEqualityGroup(address2).testEquals(); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testConstruction() { |
| 75 | LispNonceAddress address = address1; |
| 76 | |
| 77 | MappingAddress ma = MappingAddresses.ipv4MappingAddress(IP_ADDRESS_1); |
| 78 | |
| 79 | assertThat(address.getNonce(), is(NONCE_1)); |
| 80 | assertThat(address.getAddress(), is(ma)); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testSerialization() { |
| 85 | LispNonceAddress other = new LispNonceAddress(); |
| 86 | other.deserialize(address1.serialize()); |
| 87 | |
| 88 | new EqualsTester() |
| 89 | .addEqualityGroup(address1, other) |
| 90 | .testEquals(); |
| 91 | } |
| 92 | } |