blob: c88a72f3627522cb37fc200bc5f1968ce8140624 [file] [log] [blame]
Jian Lic565ebf2017-02-04 14:49:52 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lic565ebf2017-02-04 14:49:52 +09003 *
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.event.AbstractEventTest;
22import org.onosproject.net.DeviceId;
23
24import java.util.concurrent.TimeUnit;
25
26/**
27 * Unit tests for the MappingEvent class.
28 */
29public class MappingEventTest extends AbstractEventTest {
30
Jian Li44155b02017-02-15 17:03:38 +090031 private static final String DEVICE_ID_1 = "lisp:10.1.1.1";
32 private static final String DEVICE_ID_2 = "lisp:10.1.1.2";
33
Jian Lic565ebf2017-02-04 14:49:52 +090034 private MappingEvent event1;
35 private MappingEvent sameAsEvent1;
36 private MappingEvent event2;
37
38 /**
39 * Creates a mock of mapping object from mappingId and deviceId.
40 *
41 * @param mid mapping identifier
42 * @param did device identifier
43 * @return mock of mapping object
44 */
45 private Mapping mockMapping(long mid, String did) {
46 Mapping.Builder builder = new DefaultMapping.Builder();
47 DeviceId deviceId = DeviceId.deviceId(did);
48
49 return builder
50 .withId(mid)
Jian Li44155b02017-02-15 17:03:38 +090051 .withKey(new MappingTestMocks.MockMappingKey())
52 .withValue(new MappingTestMocks.MockMappingValue())
Jian Lic565ebf2017-02-04 14:49:52 +090053 .forDevice(deviceId)
54 .build();
55 }
56
57 @Before
58 public void setup() {
Jian Li44155b02017-02-15 17:03:38 +090059 final Mapping mapping1 = mockMapping(1, DEVICE_ID_1);
60 final Mapping mapping2 = mockMapping(2, DEVICE_ID_2);
Jian Lic565ebf2017-02-04 14:49:52 +090061
62 event1 = new MappingEvent(MappingEvent.Type.MAPPING_ADDED, mapping1);
63 sameAsEvent1 = new MappingEvent(MappingEvent.Type.MAPPING_ADDED, mapping1);
64 event2 = new MappingEvent(MappingEvent.Type.MAPPING_ADD_REQUESTED, mapping2);
65 }
66
67 /**
68 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
69 */
70 @Test
71 public void testEquals() {
72 new EqualsTester()
73 .addEqualityGroup(event1)
74 .addEqualityGroup(sameAsEvent1)
75 .addEqualityGroup(event2)
76 .testEquals();
77 }
78
79 /**
Jian Li98763102017-02-19 23:48:46 +090080 * Tests the constructor where a time is passed in.
81 */
82 @Test
83 public void testTimeConstructor() {
84 final long time = 123L;
85 final Mapping mapping = mockMapping(1, DEVICE_ID_1);
86 final MappingEvent event =
87 new MappingEvent(MappingEvent.Type.MAPPING_REMOVE_REQUESTED, mapping, time);
88 validateEvent(event, MappingEvent.Type.MAPPING_REMOVE_REQUESTED, mapping, time);
89 }
90
91 /**
Jian Lic565ebf2017-02-04 14:49:52 +090092 * Tests creation of a MappingEvent.
93 */
94 @Test
95 public void testConstructor() {
96 final long time = System.currentTimeMillis();
Jian Li44155b02017-02-15 17:03:38 +090097 final Mapping mapping = mockMapping(1, DEVICE_ID_1);
Jian Lic565ebf2017-02-04 14:49:52 +090098 final MappingEvent event = new MappingEvent(MappingEvent.Type.MAPPING_UPDATED, mapping);
99 validateEvent(event, MappingEvent.Type.MAPPING_UPDATED, mapping, time,
100 time + TimeUnit.SECONDS.toMillis(30));
101 }
102}