Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jon Hall | 672a3ed | 2017-04-05 13:03:37 -0700 | [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.mastership; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.cluster.RoleInfo; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | |
| 25 | import java.util.Arrays; |
| 26 | |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.junit.Assert.assertThat; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for the mastership event. |
| 32 | */ |
| 33 | public class MastershipEventTest { |
| 34 | private final long time = System.currentTimeMillis(); |
| 35 | private final DeviceId deviceId1 = DeviceId.deviceId("foo:bar"); |
| 36 | private final DeviceId deviceId2 = DeviceId.deviceId("bar:baz"); |
| 37 | private final NodeId node1 = new NodeId("1"); |
| 38 | private final NodeId node2 = new NodeId("2"); |
| 39 | private final RoleInfo roleInfo1 = new RoleInfo(node1, Arrays.asList(node1, node2)); |
| 40 | private final RoleInfo roleInfo2 = new RoleInfo(node2, Arrays.asList(node2, node1)); |
| 41 | |
| 42 | private final MastershipEvent event1 = |
| 43 | new MastershipEvent(MastershipEvent.Type.BACKUPS_CHANGED, deviceId1, roleInfo1); |
| 44 | private final MastershipEvent event2 = |
| 45 | new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, roleInfo1); |
| 46 | private final MastershipEvent event3 = |
| 47 | new MastershipEvent(MastershipEvent.Type.SUSPENDED, deviceId1, roleInfo1); |
| 48 | private final MastershipEvent event4 = |
| 49 | new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, roleInfo2, time); |
| 50 | private final MastershipEvent sameAsEvent4 = |
| 51 | new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId1, roleInfo2, time); |
| 52 | private final MastershipEvent event5 = |
| 53 | new MastershipEvent(MastershipEvent.Type.BACKUPS_CHANGED, deviceId2, roleInfo1); |
| 54 | |
| 55 | /** |
| 56 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 57 | */ |
| 58 | @Test |
| 59 | public void checkEquals() { |
| 60 | new EqualsTester() |
| 61 | .addEqualityGroup(event1) |
| 62 | .addEqualityGroup(event2) |
| 63 | .addEqualityGroup(event3) |
| 64 | .addEqualityGroup(event4, sameAsEvent4) |
| 65 | .addEqualityGroup(event5) |
| 66 | .testEquals(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests that objects are created properly. |
| 71 | */ |
| 72 | @Test |
| 73 | public void checkConstruction() { |
| 74 | assertThat(event1.type(), is(MastershipEvent.Type.BACKUPS_CHANGED)); |
| 75 | assertThat(event1.subject(), is(deviceId1)); |
| 76 | assertThat(event1.roleInfo(), is(roleInfo1)); |
| 77 | |
| 78 | assertThat(event4.time(), is(time)); |
| 79 | assertThat(event4.type(), is(MastershipEvent.Type.MASTER_CHANGED)); |
| 80 | assertThat(event4.subject(), is(deviceId1)); |
| 81 | assertThat(event4.roleInfo(), is(roleInfo2)); |
| 82 | } |
| 83 | |
| 84 | } |