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