blob: 6a87d8be1227de672daa83d989e0ccd0bdac4460 [file] [log] [blame]
Jian Lic565ebf2017-02-04 14:49:52 +09001/*
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 */
16package org.onosproject.mapping;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Before;
20import org.junit.Test;
21import org.onosproject.net.DeviceId;
22
23import static org.hamcrest.MatcherAssert.assertThat;
24import static org.hamcrest.Matchers.is;
25import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
26
27/**
28 * Unit tests for the default mapping class.
29 */
30public 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");
41 MappingKey mappingKey1 = new DefaultMappingKey();
42 MappingValue mappingValue1 = new DefaultMappingValue();
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)
65 .withKey(new DefaultMappingKey())
66 .withValue(new DefaultMappingValue())
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}