blob: 036839a2f66c51c87a02f905ff204a139c3e1188 [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;
Jordan Halterman07f052b2017-10-08 14:22:41 -070022import org.onosproject.core.Version;
Jon Hall3557db52017-04-05 10:12:21 -070023
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertThat;
26
27/**
28 * Unit tests for the Cluster Metadata event.
29 */
30public class ClusterMetadataEventTest {
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070031 private final long time1 = System.currentTimeMillis() - 100;
Jon Hall3557db52017-04-05 10:12:21 -070032 private final long time = System.currentTimeMillis();
33 private final PartitionId pid1 = PartitionId.from(1);
34 private final PartitionId pid2 = PartitionId.from(2);
35 private final NodeId nid1 = NodeId.nodeId("10.0.0.1");
36 private final NodeId nid2 = NodeId.nodeId("10.0.0.2");
37 private final ControllerNode n1 =
38 new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
39 private final ControllerNode n2 =
40 new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876);
Jordan Halterman07f052b2017-10-08 14:22:41 -070041 private final Partition p1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1));
42 private final Partition p2 = new DefaultPartition(pid2, Version.version("1.0.0"), ImmutableSet.of(nid1, nid2));
43 private final Partition p3 = new DefaultPartition(pid2, Version.version("1.0.0"), ImmutableSet.of(nid2));
Jon Hall3557db52017-04-05 10:12:21 -070044 private final ClusterMetadata metadata1 =
45 new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1));
46 private final ClusterMetadata metadata2 =
47 new ClusterMetadata("bar", ImmutableSet.of(n1, n2), ImmutableSet.of(p1, p2));
48 private final ClusterMetadata metadata3 =
49 new ClusterMetadata("baz", ImmutableSet.of(n2), ImmutableSet.of(p3));
50
51 private final ClusterMetadataEvent event1 =
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070052 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1, time1);
Jon Hall3557db52017-04-05 10:12:21 -070053 private final ClusterMetadataEvent sameAsEvent1 =
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070054 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1, time1);
Jon Hall3557db52017-04-05 10:12:21 -070055 private final ClusterMetadataEvent event2 =
56 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
57 private final ClusterMetadataEvent sameAsEvent2 =
58 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
Jon Hall3557db52017-04-05 10:12:21 -070059
60 /**
61 * Tests for proper operation of equals(), hashCode() and toString() methods.
62 */
63 @Test
Yuta HIGUCHI4d04b2c2017-05-25 16:25:16 -070064 public void checkEquals() throws Exception {
65 // ensure event3 will have different timestamp from `time`
66 Thread.sleep(1);
67 ClusterMetadataEvent event3 =
68 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata3);
69
Jon Hall3557db52017-04-05 10:12:21 -070070 new EqualsTester()
71 .addEqualityGroup(event1, sameAsEvent1)
72 .addEqualityGroup(event2, sameAsEvent2)
73 .addEqualityGroup(event3)
74 .testEquals();
75 }
76
77 /**
78 * Tests that objects are created properly.
79 */
80 @Test
81 public void checkConstruction() {
82 assertThat(event1.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
83 assertThat(event1.subject(), is(metadata1));
84
85 assertThat(event2.time(), is(time));
86 assertThat(event2.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
87 assertThat(event2.subject(), is(metadata2));
88 }
89
90}