Jon Hall | 3557db5 | 2017-04-05 10:12:21 -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 | 3557db5 | 2017-04-05 10:12:21 -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.cluster; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | import static org.junit.Assert.assertThat; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for the Cluster Metadata event. |
| 28 | */ |
| 29 | public class ClusterMetadataEventTest { |
Yuta HIGUCHI | 4d04b2c | 2017-05-25 16:25:16 -0700 | [diff] [blame] | 30 | private final long time1 = System.currentTimeMillis() - 100; |
Jon Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 31 | 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 Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 40 | private final ClusterMetadata metadata1 = |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 41 | new ClusterMetadata("foo", n1, ImmutableSet.of(), ImmutableSet.of(n1)); |
Jon Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 42 | private final ClusterMetadata metadata2 = |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 43 | new ClusterMetadata("bar", n1, ImmutableSet.of(), ImmutableSet.of(n1, n2)); |
Jon Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 44 | private final ClusterMetadata metadata3 = |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 45 | new ClusterMetadata("baz", n1, ImmutableSet.of(), ImmutableSet.of(n2)); |
Jon Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 46 | |
| 47 | private final ClusterMetadataEvent event1 = |
Yuta HIGUCHI | 4d04b2c | 2017-05-25 16:25:16 -0700 | [diff] [blame] | 48 | new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1, time1); |
Jon Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 49 | private final ClusterMetadataEvent sameAsEvent1 = |
Yuta HIGUCHI | 4d04b2c | 2017-05-25 16:25:16 -0700 | [diff] [blame] | 50 | new ClusterMetadataEvent(ClusterMetadataEvent.Type.METADATA_CHANGED, metadata1, time1); |
Jon Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 51 | 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 Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 58 | */ |
| 59 | @Test |
Yuta HIGUCHI | 4d04b2c | 2017-05-25 16:25:16 -0700 | [diff] [blame] | 60 | 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 Hall | 3557db5 | 2017-04-05 10:12:21 -0700 | [diff] [blame] | 66 | 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 | } |