Jian Li | c565ebf | 2017-02-04 14:49:52 +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.mapping; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.event.AbstractEventTest; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for the MappingEvent class. |
| 28 | */ |
| 29 | public class MappingEventTest extends AbstractEventTest { |
| 30 | |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 31 | 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 Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 34 | 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 Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 51 | .withKey(new MappingTestMocks.MockMappingKey()) |
| 52 | .withValue(new MappingTestMocks.MockMappingValue()) |
Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 53 | .forDevice(deviceId) |
| 54 | .build(); |
| 55 | } |
| 56 | |
| 57 | @Before |
| 58 | public void setup() { |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 59 | final Mapping mapping1 = mockMapping(1, DEVICE_ID_1); |
| 60 | final Mapping mapping2 = mockMapping(2, DEVICE_ID_2); |
Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 61 | |
| 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 Li | 9876310 | 2017-02-19 23:48:46 +0900 | [diff] [blame] | 80 | * 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 Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 92 | * Tests creation of a MappingEvent. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testConstructor() { |
| 96 | final long time = System.currentTimeMillis(); |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 97 | final Mapping mapping = mockMapping(1, DEVICE_ID_1); |
Jian Li | c565ebf | 2017-02-04 14:49:52 +0900 | [diff] [blame] | 98 | 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 | } |