Jian Li | fae7b38 | 2018-07-12 22:27:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstacknetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
| 26 | import org.onosproject.net.DefaultHost; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.HostLocation; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.provider.ProviderId; |
| 33 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 34 | |
| 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 38 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_CREATE_TIME; |
Jian Li | fae7b38 | 2018-07-12 22:27:47 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
Jian Li | fae7b38 | 2018-07-12 22:27:47 +0900 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Unit tests for the default instance port class. |
| 43 | */ |
| 44 | public class DefaultInstancePortTest { |
| 45 | |
| 46 | private static final String ANNOTATION_NETWORK_ID = "networkId"; |
| 47 | private static final String ANNOTATION_PORT_ID = "portId"; |
| 48 | |
| 49 | private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("1.2.3.4"); |
| 50 | private static final IpAddress IP_ADDRESS_2 = IpAddress.valueOf("5.6.7.8"); |
| 51 | |
| 52 | private static final MacAddress MAC_ADDRESS_1 = MacAddress.valueOf("11:22:33:44:55:66"); |
| 53 | private static final MacAddress MAC_ADDRESS_2 = MacAddress.valueOf("77:88:99:AA:BB:CC"); |
| 54 | |
| 55 | private static final DeviceId DEV_ID_1 = DeviceId.deviceId("of:0000000000000001"); |
| 56 | private static final DeviceId DEV_ID_2 = DeviceId.deviceId("of:0000000000000002"); |
| 57 | private static final PortNumber PORT_NUM_1 = PortNumber.portNumber(1L); |
| 58 | private static final PortNumber PORT_NUM_2 = PortNumber.portNumber(2L); |
| 59 | |
| 60 | private static final VlanId VLAN_ID = VlanId.vlanId(); |
| 61 | private static final ProviderId PROVIDER_ID = ProviderId.NONE; |
| 62 | private static final HostId HOST_ID_1 = HostId.hostId("00:00:11:00:00:01/1"); |
| 63 | private static final HostId HOST_ID_2 = HostId.hostId("00:00:11:00:00:02/1"); |
| 64 | |
| 65 | private static final String NETWORK_ID_1 = "net-id-1"; |
| 66 | private static final String NETWORK_ID_2 = "net-id-2"; |
| 67 | private static final String PORT_ID_1 = "port-id-1"; |
| 68 | private static final String PORT_ID_2 = "port-id-2"; |
| 69 | |
| 70 | private static final long TIME_1 = 1L; |
| 71 | private static final long TIME_2 = 2L; |
| 72 | |
| 73 | private InstancePort instancePort1; |
| 74 | private InstancePort sameAsInstancePort1; |
| 75 | private InstancePort instancePort2; |
| 76 | |
| 77 | /** |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 78 | * Tests class immutability. |
Jian Li | fae7b38 | 2018-07-12 22:27:47 +0900 | [diff] [blame] | 79 | */ |
| 80 | @Test |
| 81 | public void testImmutability() { |
| 82 | assertThatClassIsImmutable(DefaultInstancePort.class); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Initial setup for this unit test. |
| 87 | */ |
| 88 | @Before |
| 89 | public void setUp() { |
| 90 | |
| 91 | HostLocation location1 = new HostLocation(DEV_ID_1, PORT_NUM_1, TIME_1); |
| 92 | HostLocation location2 = new HostLocation(DEV_ID_2, PORT_NUM_2, TIME_2); |
| 93 | |
| 94 | DefaultAnnotations.Builder annotations1 = DefaultAnnotations.builder() |
| 95 | .set(ANNOTATION_NETWORK_ID, NETWORK_ID_1) |
| 96 | .set(ANNOTATION_PORT_ID, PORT_ID_1) |
| 97 | .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_1)); |
| 98 | |
| 99 | DefaultAnnotations.Builder annotations2 = DefaultAnnotations.builder() |
| 100 | .set(ANNOTATION_NETWORK_ID, NETWORK_ID_2) |
| 101 | .set(ANNOTATION_PORT_ID, PORT_ID_2) |
| 102 | .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_2)); |
| 103 | |
| 104 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_1, MAC_ADDRESS_1, |
| 105 | VLAN_ID, location1, ImmutableSet.of(IP_ADDRESS_1), |
| 106 | annotations1.build()); |
| 107 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_2, MAC_ADDRESS_2, |
| 108 | VLAN_ID, location2, ImmutableSet.of(IP_ADDRESS_2), |
| 109 | annotations2.build()); |
| 110 | |
| 111 | instancePort1 = DefaultInstancePort.from(host1, ACTIVE); |
| 112 | sameAsInstancePort1 = DefaultInstancePort.from(host1, ACTIVE); |
| 113 | instancePort2 = DefaultInstancePort.from(host2, ACTIVE); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests object equality. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testEquality() { |
| 121 | new EqualsTester().addEqualityGroup(instancePort1, sameAsInstancePort1) |
| 122 | .addEqualityGroup(instancePort2) |
| 123 | .testEquals(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Test object construction. |
| 128 | */ |
| 129 | @Test |
| 130 | public void testConstruction() { |
| 131 | InstancePort instancePort = instancePort1; |
| 132 | |
| 133 | assertThat(instancePort.portId(), is(PORT_ID_1)); |
| 134 | assertThat(instancePort.networkId(), is(NETWORK_ID_1)); |
| 135 | assertThat(instancePort.macAddress(), is(MAC_ADDRESS_1)); |
| 136 | assertThat(instancePort.ipAddress(), is(IP_ADDRESS_1)); |
| 137 | assertThat(instancePort.portNumber(), is(PORT_NUM_1)); |
| 138 | assertThat(instancePort.deviceId(), is(DEV_ID_1)); |
| 139 | assertThat(instancePort.state(), is(ACTIVE)); |
| 140 | } |
| 141 | } |