blob: 9e2acd2b485d9e612d08c0a7b93d4baf5547f388 [file] [log] [blame]
Jon Hall672a3ed2017-04-05 13:03:37 -07001/*
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.mastership;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20
21import org.onosproject.cluster.NodeId;
22import org.onosproject.cluster.RoleInfo;
23import org.onosproject.net.DeviceId;
24
25import java.util.Arrays;
26
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.assertThat;
29
30/**
31 * Unit tests for the mastership event.
32 */
33public 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}