Jon Hall | f074af9 | 2017-04-05 15:10:46 -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 | f074af9 | 2017-04-05 15:10:46 -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.store.primitives; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.cluster.DefaultPartition; |
| 22 | import org.onosproject.cluster.NodeId; |
| 23 | import org.onosproject.cluster.Partition; |
| 24 | import org.onosproject.cluster.PartitionId; |
| 25 | |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for the mastership event. |
| 31 | */ |
| 32 | public class PartitionEventTest { |
| 33 | private final long time = System.currentTimeMillis(); |
| 34 | private final long time0 = 0; |
| 35 | |
| 36 | private final PartitionId pid1 = PartitionId.from(1); |
| 37 | private final PartitionId pid2 = PartitionId.from(2); |
| 38 | |
| 39 | private final NodeId nid1 = NodeId.nodeId("10.0.0.1"); |
| 40 | private final NodeId nid2 = NodeId.nodeId("10.0.0.2"); |
| 41 | |
| 42 | |
| 43 | private final Partition p1 = |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 44 | new DefaultPartition(pid1, ImmutableSet.of(nid1)); |
Jon Hall | f074af9 | 2017-04-05 15:10:46 -0700 | [diff] [blame] | 45 | private final Partition p2 = |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 46 | new DefaultPartition(pid2, ImmutableSet.of(nid1, nid2)); |
Jon Hall | f074af9 | 2017-04-05 15:10:46 -0700 | [diff] [blame] | 47 | |
| 48 | private final PartitionEvent event1 = |
| 49 | new PartitionEvent(PartitionEvent.Type.UPDATED, p1, time); |
| 50 | private final PartitionEvent sameAsEvent1 = |
| 51 | new PartitionEvent(PartitionEvent.Type.UPDATED, p1, time); |
| 52 | private final PartitionEvent event2 = |
| 53 | new PartitionEvent(PartitionEvent.Type.OPENED, p1, time); |
| 54 | private final PartitionEvent event3 = |
| 55 | new PartitionEvent(PartitionEvent.Type.CLOSED, p1, time); |
| 56 | private final PartitionEvent event4 = |
| 57 | new PartitionEvent(PartitionEvent.Type.AVAILABLE, p1, time); |
| 58 | private final PartitionEvent event5 = |
| 59 | new PartitionEvent(PartitionEvent.Type.UNAVAILABLE, p1, time); |
| 60 | private final PartitionEvent event6 = |
| 61 | new PartitionEvent(PartitionEvent.Type.UPDATED, p2, time); |
| 62 | private final PartitionEvent event7 = |
| 63 | new PartitionEvent(PartitionEvent.Type.UPDATED, p1, time0); |
| 64 | |
| 65 | /** |
| 66 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 67 | */ |
| 68 | @Test |
| 69 | public void checkEquals() { |
| 70 | new EqualsTester() |
| 71 | .addEqualityGroup(event1, sameAsEvent1) |
| 72 | .addEqualityGroup(event2) |
| 73 | .addEqualityGroup(event3) |
| 74 | .addEqualityGroup(event4) |
| 75 | .addEqualityGroup(event5) |
| 76 | .addEqualityGroup(event6) |
| 77 | .addEqualityGroup(event7) |
| 78 | .testEquals(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests that objects are created properly. |
| 83 | */ |
| 84 | @Test |
| 85 | public void checkConstruction() { |
| 86 | assertThat(event1.type(), is(PartitionEvent.Type.UPDATED)); |
| 87 | assertThat(event1.subject(), is(p1)); |
| 88 | assertThat(event1.time(), is(time)); |
| 89 | } |
| 90 | |
| 91 | } |