Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 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.mapping; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
| 23 | import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for the default mapping class. |
| 29 | */ |
| 30 | public class DefaultMappingTest { |
| 31 | |
| 32 | private Mapping mapping1; |
| 33 | private Mapping sameAsMapping1; |
| 34 | private Mapping mapping2; |
| 35 | |
| 36 | @Before |
| 37 | public void setup() { |
| 38 | |
| 39 | Mapping.Builder builder1 = new DefaultMapping.Builder(); |
| 40 | DeviceId deviceId1 = DeviceId.deviceId("lisp:10.1.1.1"); |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 41 | MappingKey mappingKey1 = new MappingTestMocks.MockMappingKey(); |
| 42 | MappingValue mappingValue1 = new MappingTestMocks.MockMappingValue(); |
Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 43 | |
| 44 | mapping1 = builder1 |
| 45 | .withId(1) |
| 46 | .withKey(mappingKey1) |
| 47 | .withValue(mappingValue1) |
| 48 | .forDevice(deviceId1) |
| 49 | .build(); |
| 50 | |
| 51 | Mapping.Builder builder2 = new DefaultMapping.Builder(); |
| 52 | |
| 53 | sameAsMapping1 = builder2 |
| 54 | .withId(1) |
| 55 | .withKey(mappingKey1) |
| 56 | .withValue(mappingValue1) |
| 57 | .forDevice(deviceId1) |
| 58 | .build(); |
| 59 | |
| 60 | Mapping.Builder builder3 = new DefaultMapping.Builder(); |
| 61 | DeviceId deviceId2 = DeviceId.deviceId("lisp:10.1.1.2"); |
| 62 | |
| 63 | mapping2 = builder3 |
| 64 | .withId(2) |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 65 | .withKey(new MappingTestMocks.MockMappingKey()) |
| 66 | .withValue(new MappingTestMocks.MockMappingValue()) |
Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 67 | .forDevice(deviceId2) |
| 68 | .build(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks that the DefaultMapping class is immutable but can be inherited from. |
| 73 | */ |
| 74 | @Test |
| 75 | public void testImmutability() { |
| 76 | assertThatClassIsImmutableBaseClass(DefaultMapping.class); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testEquality() { |
| 84 | new EqualsTester() |
| 85 | .addEqualityGroup(mapping1, sameAsMapping1) |
| 86 | .addEqualityGroup(mapping2).testEquals(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests creation of a DefaultMapping using a mapping builder. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testConstruction() { |
| 94 | DefaultMapping mapping = (DefaultMapping) mapping1; |
| 95 | |
| 96 | assertThat(mapping.deviceId(), is(DeviceId.deviceId("lisp:10.1.1.1"))); |
| 97 | assertThat(mapping.id(), is(MappingId.valueOf(1))); |
| 98 | } |
| 99 | } |