Jon Hall | b471f2c | 2017-04-05 09:07:39 -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 | b471f2c | 2017-04-05 09:07:39 -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.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | |
| 22 | import static org.hamcrest.Matchers.is; |
| 23 | import static org.junit.Assert.assertThat; |
| 24 | |
| 25 | /** |
| 26 | * Unit tests for the cluster event test. |
| 27 | */ |
| 28 | public class ClusterEventTest { |
| 29 | private final NodeId node1 = new NodeId("1"); |
| 30 | private final NodeId node2 = new NodeId("2"); |
| 31 | private final IpAddress ip1 = IpAddress.valueOf("10.0.0.1"); |
| 32 | private final IpAddress ip2 = IpAddress.valueOf("10.0.0.2"); |
| 33 | private final ControllerNode cNode1 = new DefaultControllerNode(node1, ip1); |
| 34 | private final ControllerNode cNode2 = new DefaultControllerNode(node2, ip2); |
| 35 | private final ClusterEvent event1 = |
| 36 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, cNode1); |
| 37 | private final ClusterEvent event2 = |
| 38 | new ClusterEvent(ClusterEvent.Type.INSTANCE_REMOVED, cNode1); |
| 39 | private final ClusterEvent event3 = |
| 40 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ACTIVATED, cNode1); |
| 41 | private final ClusterEvent event4 = |
| 42 | new ClusterEvent(ClusterEvent.Type.INSTANCE_READY, cNode1); |
| 43 | private final ClusterEvent event5 = |
| 44 | new ClusterEvent(ClusterEvent.Type.INSTANCE_DEACTIVATED, cNode1); |
| 45 | private final ClusterEvent event6 = |
| 46 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, cNode2); |
| 47 | private final long time = System.currentTimeMillis(); |
| 48 | private final ClusterEvent event7 = |
| 49 | new ClusterEvent(ClusterEvent.Type.INSTANCE_READY, cNode2, time); |
| 50 | private final ClusterEvent sameAsEvent7 = |
| 51 | new ClusterEvent(ClusterEvent.Type.INSTANCE_READY, cNode2, time); |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame^] | 52 | private final ClusterEvent event8 = |
| 53 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, cNode2, ClusterEvent.InstanceType.ONOS); |
| 54 | private final ClusterEvent event9 = |
| 55 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, cNode2, ClusterEvent.InstanceType.STORAGE); |
| 56 | private final ClusterEvent event10 = |
| 57 | new ClusterEvent(ClusterEvent.Type.INSTANCE_REMOVED, cNode2, ClusterEvent.InstanceType.ONOS); |
| 58 | private final ClusterEvent event11 = |
| 59 | new ClusterEvent(ClusterEvent.Type.INSTANCE_REMOVED, cNode2, ClusterEvent.InstanceType.STORAGE); |
| 60 | private final ClusterEvent event12 = |
| 61 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ACTIVATED, cNode1, ClusterEvent.InstanceType.ONOS); |
| 62 | private final ClusterEvent event13 = |
| 63 | new ClusterEvent(ClusterEvent.Type.INSTANCE_ACTIVATED, cNode1, ClusterEvent.InstanceType.STORAGE); |
| 64 | private final ClusterEvent event14 = |
| 65 | new ClusterEvent(ClusterEvent.Type.INSTANCE_READY, cNode1, ClusterEvent.InstanceType.ONOS); |
| 66 | private final ClusterEvent event15 = |
| 67 | new ClusterEvent(ClusterEvent.Type.INSTANCE_READY, cNode1, ClusterEvent.InstanceType.STORAGE); |
| 68 | private final ClusterEvent event16 = |
| 69 | new ClusterEvent(ClusterEvent.Type.INSTANCE_DEACTIVATED, cNode1, ClusterEvent.InstanceType.ONOS); |
| 70 | private final ClusterEvent event17 = |
| 71 | new ClusterEvent(ClusterEvent.Type.INSTANCE_DEACTIVATED, cNode1, ClusterEvent.InstanceType.STORAGE); |
Jon Hall | b471f2c | 2017-04-05 09:07:39 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 75 | */ |
| 76 | @Test |
| 77 | public void checkEquals() { |
| 78 | new EqualsTester() |
| 79 | .addEqualityGroup(event1) |
| 80 | .addEqualityGroup(event2) |
| 81 | .addEqualityGroup(event3) |
| 82 | .addEqualityGroup(event4) |
| 83 | .addEqualityGroup(event5) |
| 84 | .addEqualityGroup(event6) |
| 85 | .addEqualityGroup(event7, sameAsEvent7) |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame^] | 86 | .addEqualityGroup(event8) |
| 87 | .addEqualityGroup(event9) |
| 88 | .addEqualityGroup(event10) |
| 89 | .addEqualityGroup(event11) |
| 90 | .addEqualityGroup(event12) |
| 91 | .addEqualityGroup(event13) |
| 92 | .addEqualityGroup(event14) |
| 93 | .addEqualityGroup(event15) |
| 94 | .addEqualityGroup(event16) |
| 95 | .addEqualityGroup(event17) |
Jon Hall | b471f2c | 2017-04-05 09:07:39 -0700 | [diff] [blame] | 96 | .testEquals(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests that objects are created properly. |
| 101 | */ |
| 102 | @Test |
| 103 | public void checkConstruction() { |
| 104 | assertThat(event1.type(), is(ClusterEvent.Type.INSTANCE_ADDED)); |
| 105 | assertThat(event1.subject(), is(cNode1)); |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame^] | 106 | assertThat(event1.instanceType(), is(ClusterEvent.InstanceType.UNKNOWN)); |
Jon Hall | b471f2c | 2017-04-05 09:07:39 -0700 | [diff] [blame] | 107 | |
| 108 | assertThat(event7.time(), is(time)); |
| 109 | assertThat(event7.type(), is(ClusterEvent.Type.INSTANCE_READY)); |
| 110 | assertThat(event7.subject(), is(cNode2)); |
pierventre | 55a5f39 | 2021-11-05 15:37:32 +0100 | [diff] [blame^] | 111 | assertThat(event7.instanceType(), is(ClusterEvent.InstanceType.UNKNOWN)); |
| 112 | |
| 113 | assertThat(event8.type(), is(ClusterEvent.Type.INSTANCE_ADDED)); |
| 114 | assertThat(event8.subject(), is(cNode2)); |
| 115 | assertThat(event8.instanceType(), is(ClusterEvent.InstanceType.ONOS)); |
| 116 | |
| 117 | assertThat(event9.type(), is(ClusterEvent.Type.INSTANCE_ADDED)); |
| 118 | assertThat(event9.subject(), is(cNode2)); |
| 119 | assertThat(event9.instanceType(), is(ClusterEvent.InstanceType.STORAGE)); |
| 120 | |
| 121 | assertThat(event10.type(), is(ClusterEvent.Type.INSTANCE_REMOVED)); |
| 122 | assertThat(event10.subject(), is(cNode2)); |
| 123 | assertThat(event10.instanceType(), is(ClusterEvent.InstanceType.ONOS)); |
| 124 | |
| 125 | assertThat(event11.type(), is(ClusterEvent.Type.INSTANCE_REMOVED)); |
| 126 | assertThat(event11.subject(), is(cNode2)); |
| 127 | assertThat(event11.instanceType(), is(ClusterEvent.InstanceType.STORAGE)); |
| 128 | |
| 129 | assertThat(event12.type(), is(ClusterEvent.Type.INSTANCE_ACTIVATED)); |
| 130 | assertThat(event12.subject(), is(cNode1)); |
| 131 | assertThat(event12.instanceType(), is(ClusterEvent.InstanceType.ONOS)); |
| 132 | |
| 133 | assertThat(event13.type(), is(ClusterEvent.Type.INSTANCE_ACTIVATED)); |
| 134 | assertThat(event13.subject(), is(cNode1)); |
| 135 | assertThat(event13.instanceType(), is(ClusterEvent.InstanceType.STORAGE)); |
| 136 | |
| 137 | assertThat(event14.type(), is(ClusterEvent.Type.INSTANCE_READY)); |
| 138 | assertThat(event14.subject(), is(cNode1)); |
| 139 | assertThat(event14.instanceType(), is(ClusterEvent.InstanceType.ONOS)); |
| 140 | |
| 141 | assertThat(event15.type(), is(ClusterEvent.Type.INSTANCE_READY)); |
| 142 | assertThat(event15.subject(), is(cNode1)); |
| 143 | assertThat(event15.instanceType(), is(ClusterEvent.InstanceType.STORAGE)); |
| 144 | |
| 145 | assertThat(event16.type(), is(ClusterEvent.Type.INSTANCE_DEACTIVATED)); |
| 146 | assertThat(event16.subject(), is(cNode1)); |
| 147 | assertThat(event16.instanceType(), is(ClusterEvent.InstanceType.ONOS)); |
| 148 | |
| 149 | assertThat(event17.type(), is(ClusterEvent.Type.INSTANCE_DEACTIVATED)); |
| 150 | assertThat(event17.subject(), is(cNode1)); |
| 151 | assertThat(event17.instanceType(), is(ClusterEvent.InstanceType.STORAGE)); |
Jon Hall | b471f2c | 2017-04-05 09:07:39 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } |