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 | |
| 31 | private MappingEvent event1; |
| 32 | private MappingEvent sameAsEvent1; |
| 33 | private MappingEvent event2; |
| 34 | |
| 35 | /** |
| 36 | * Creates a mock of mapping object from mappingId and deviceId. |
| 37 | * |
| 38 | * @param mid mapping identifier |
| 39 | * @param did device identifier |
| 40 | * @return mock of mapping object |
| 41 | */ |
| 42 | private Mapping mockMapping(long mid, String did) { |
| 43 | Mapping.Builder builder = new DefaultMapping.Builder(); |
| 44 | DeviceId deviceId = DeviceId.deviceId(did); |
| 45 | |
| 46 | return builder |
| 47 | .withId(mid) |
| 48 | .withKey(new DefaultMappingKey()) |
| 49 | .withValue(new DefaultMappingValue()) |
| 50 | .forDevice(deviceId) |
| 51 | .build(); |
| 52 | } |
| 53 | |
| 54 | @Before |
| 55 | public void setup() { |
| 56 | final Mapping mapping1 = mockMapping(1, "lisp:10.1.1.1"); |
| 57 | final Mapping mapping2 = mockMapping(2, "lisp:10.1.1.2"); |
| 58 | |
| 59 | event1 = new MappingEvent(MappingEvent.Type.MAPPING_ADDED, mapping1); |
| 60 | sameAsEvent1 = new MappingEvent(MappingEvent.Type.MAPPING_ADDED, mapping1); |
| 61 | event2 = new MappingEvent(MappingEvent.Type.MAPPING_ADD_REQUESTED, mapping2); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testEquals() { |
| 69 | new EqualsTester() |
| 70 | .addEqualityGroup(event1) |
| 71 | .addEqualityGroup(sameAsEvent1) |
| 72 | .addEqualityGroup(event2) |
| 73 | .testEquals(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Tests creation of a MappingEvent. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testConstructor() { |
| 81 | final long time = System.currentTimeMillis(); |
| 82 | final Mapping mapping = mockMapping(1, "lisp:10.1.1.1"); |
| 83 | final MappingEvent event = new MappingEvent(MappingEvent.Type.MAPPING_UPDATED, mapping); |
| 84 | validateEvent(event, MappingEvent.Type.MAPPING_UPDATED, mapping, time, |
| 85 | time + TimeUnit.SECONDS.toMillis(30)); |
| 86 | } |
| 87 | } |