blob: 3ea802d6a7958329e4fedb93be74fab981fa8854 [file] [log] [blame]
Jon Hall3557db52017-04-05 10:12:21 -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.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 {
30 private final long time = System.currentTimeMillis();
31 private final PartitionId pid1 = PartitionId.from(1);
32 private final PartitionId pid2 = PartitionId.from(2);
33 private final NodeId nid1 = NodeId.nodeId("10.0.0.1");
34 private final NodeId nid2 = NodeId.nodeId("10.0.0.2");
35 private final ControllerNode n1 =
36 new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
37 private final ControllerNode n2 =
38 new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876);
39 private final Partition p1 = new DefaultPartition(pid1, ImmutableSet.of(nid1));
40 private final Partition p2 = new DefaultPartition(pid2, ImmutableSet.of(nid1, nid2));
41 private final Partition p3 = new DefaultPartition(pid2, ImmutableSet.of(nid2));
42 private final ClusterMetadata metadata1 =
43 new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1));
44 private final ClusterMetadata metadata2 =
45 new ClusterMetadata("bar", ImmutableSet.of(n1, n2), ImmutableSet.of(p1, p2));
46 private final ClusterMetadata metadata3 =
47 new ClusterMetadata("baz", ImmutableSet.of(n2), ImmutableSet.of(p3));
48
49 private final ClusterMetadataEvent event1 =
50 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1);
51 private final ClusterMetadataEvent sameAsEvent1 =
52 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1);
53 private final ClusterMetadataEvent event2 =
54 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
55 private final ClusterMetadataEvent sameAsEvent2 =
56 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata2, time);
57 private final ClusterMetadataEvent event3 =
58 new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata3);
59
60 /**
61 * Tests for proper operation of equals(), hashCode() and toString() methods.
62 */
63 @Test
64 public void checkEquals() {
65 new EqualsTester()
66 .addEqualityGroup(event1, sameAsEvent1)
67 .addEqualityGroup(event2, sameAsEvent2)
68 .addEqualityGroup(event3)
69 .testEquals();
70 }
71
72 /**
73 * Tests that objects are created properly.
74 */
75 @Test
76 public void checkConstruction() {
77 assertThat(event1.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
78 assertThat(event1.subject(), is(metadata1));
79
80 assertThat(event2.time(), is(time));
81 assertThat(event2.type(), is(ClusterMetadataEvent.Type.METADATA_CHANGED));
82 assertThat(event2.subject(), is(metadata2));
83 }
84
85}