blob: b176aeebfc7adc8f69d0466b7cdaddb00a82aba3 [file] [log] [blame]
Jon Hall3557db52017-04-05 10:12:21 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jon Hall3557db52017-04-05 10:12:21 -07003 *
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.cluster;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.testing.EqualsTester;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22
23import static org.hamcrest.Matchers.is;
24import static org.junit.Assert.assertThat;
25
26/**
27 * Unit tests for the Cluster Metadata event.
28 */
29public class ClusterMetadataEventTest {
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070030 private final long time1 = System.currentTimeMillis() - 100;
Jon Hall3557db52017-04-05 10:12:21 -070031 private final long time = System.currentTimeMillis();
32 private final PartitionId pid1 = PartitionId.from(1);
33 private final PartitionId pid2 = PartitionId.from(2);
34 private final NodeId nid1 = NodeId.nodeId("10.0.0.1");
35 private final NodeId nid2 = NodeId.nodeId("10.0.0.2");
36 private final ControllerNode n1 =
37 new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
38 private final ControllerNode n2 =
39 new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876);
Jon Hall3557db52017-04-05 10:12:21 -070040 private final ClusterMetadata metadata1 =
Jordan Halterman00e92da2018-05-22 23:05:52 -070041 new ClusterMetadata("foo", n1, ImmutableSet.of(n1));
Jon Hall3557db52017-04-05 10:12:21 -070042 private final ClusterMetadata metadata2 =
Jordan Halterman00e92da2018-05-22 23:05:52 -070043 new ClusterMetadata("bar", n1, ImmutableSet.of(n1, n2));
Jon Hall3557db52017-04-05 10:12:21 -070044 private final ClusterMetadata metadata3 =
Jordan Halterman00e92da2018-05-22 23:05:52 -070045 new ClusterMetadata("baz", n1, ImmutableSet.of(n2));
Jon Hall3557db52017-04-05 10:12:21 -070046
47 private final ClusterMetadataEvent event1 =
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070048 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1, time1);
Jon Hall3557db52017-04-05 10:12:21 -070049 private final ClusterMetadataEvent sameAsEvent1 =
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070050 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1, time1);
Jon Hall3557db52017-04-05 10:12:21 -070051 private final ClusterMetadataEvent event2 =
52 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
53 private final ClusterMetadataEvent sameAsEvent2 =
54 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
Jon Hall3557db52017-04-05 10:12:21 -070055
56 /**
57 * Tests for proper operation of equals(), hashCode() and toString() methods.
58 */
59 @Test
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070060 public void checkEquals() throws Exception {
61 // ensure event3 will have different timestamp from `time`
62 Thread.sleep(1);
63 ClusterMetadataEvent event3 =
64 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata3);
65
Jon Hall3557db52017-04-05 10:12:21 -070066 new EqualsTester()
67 .addEqualityGroup(event1, sameAsEvent1)
68 .addEqualityGroup(event2, sameAsEvent2)
69 .addEqualityGroup(event3)
70 .testEquals();
71 }
72
73 /**
74 * Tests that objects are created properly.
75 */
76 @Test
77 public void checkConstruction() {
78 assertThat(event1.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
79 assertThat(event1.subject(), is(metadata1));
80
81 assertThat(event2.time(), is(time));
82 assertThat(event2.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
83 assertThat(event2.subject(), is(metadata2));
84 }
85
86}