Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +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.openstacktelemetry.impl; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 24 | import org.onlab.packet.TpPort; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.net.DeviceId; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
| 28 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 32 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unit tests for DefaultFlowInfo class. |
| 36 | */ |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 37 | public final class DefaultFlowInfoTest { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 38 | |
| 39 | private static final String IP_ADDRESS_1 = "10.10.10.1"; |
| 40 | private static final String IP_ADDRESS_2 = "20.20.20.1"; |
| 41 | |
| 42 | private static final String MAC_ADDRESS_1 = "AA:BB:CC:DD:EE:FF"; |
| 43 | private static final String MAC_ADDRESS_2 = "FF:EE:DD:CC:BB:AA"; |
| 44 | |
| 45 | private static final int IP_PREFIX_LENGTH_1 = 10; |
| 46 | private static final int IP_PREFIX_LENGTH_2 = 20; |
| 47 | |
| 48 | private static final int PORT_1 = 1000; |
| 49 | private static final int PORT_2 = 2000; |
| 50 | |
| 51 | private static final int STATIC_INTEGER_1 = 1; |
| 52 | private static final int STATIC_INTEGER_2 = 2; |
| 53 | |
| 54 | private static final String STATIC_STRING_1 = "1"; |
| 55 | private static final String STATIC_STRING_2 = "2"; |
| 56 | |
| 57 | |
| 58 | private FlowInfo info1; |
| 59 | private FlowInfo sameAsInfo1; |
| 60 | private FlowInfo info2; |
| 61 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 62 | /** |
| 63 | * Initial setup for this unit test. |
| 64 | */ |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 65 | @Before |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 66 | public void setUp() { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 67 | |
| 68 | FlowInfo.Builder builder1 = new DefaultFlowInfo.DefaultBuilder(); |
| 69 | FlowInfo.Builder builder2 = new DefaultFlowInfo.DefaultBuilder(); |
| 70 | FlowInfo.Builder builder3 = new DefaultFlowInfo.DefaultBuilder(); |
| 71 | |
| 72 | StatsInfo statsInfo = new DefaultStatsInfo.DefaultBuilder().build(); |
| 73 | |
| 74 | info1 = builder1 |
| 75 | .withFlowType((byte) STATIC_INTEGER_1) |
| 76 | .withInputInterfaceId(STATIC_INTEGER_1) |
| 77 | .withOutputInterfaceId(STATIC_INTEGER_1) |
| 78 | .withDeviceId(DeviceId.deviceId(STATIC_STRING_1)) |
| 79 | .withSrcIp(IpPrefix.valueOf( |
| 80 | IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1)) |
| 81 | .withDstIp(IpPrefix.valueOf( |
| 82 | IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1)) |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 83 | .withSrcPort(TpPort.tpPort(PORT_1)) |
| 84 | .withDstPort(TpPort.tpPort(PORT_1)) |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 85 | .withProtocol((byte) STATIC_INTEGER_1) |
| 86 | .withVlanId(VlanId.vlanId(STATIC_STRING_1)) |
| 87 | .withSrcMac(MacAddress.valueOf(MAC_ADDRESS_1)) |
| 88 | .withDstMac(MacAddress.valueOf(MAC_ADDRESS_1)) |
| 89 | .withStatsInfo(statsInfo) |
| 90 | .build(); |
| 91 | |
| 92 | sameAsInfo1 = builder2 |
| 93 | .withFlowType((byte) STATIC_INTEGER_1) |
| 94 | .withInputInterfaceId(STATIC_INTEGER_1) |
| 95 | .withOutputInterfaceId(STATIC_INTEGER_1) |
| 96 | .withDeviceId(DeviceId.deviceId(STATIC_STRING_1)) |
| 97 | .withSrcIp(IpPrefix.valueOf( |
| 98 | IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1)) |
| 99 | .withDstIp(IpPrefix.valueOf( |
| 100 | IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1)) |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 101 | .withSrcPort(TpPort.tpPort(PORT_1)) |
| 102 | .withDstPort(TpPort.tpPort(PORT_1)) |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 103 | .withProtocol((byte) STATIC_INTEGER_1) |
| 104 | .withVlanId(VlanId.vlanId(STATIC_STRING_1)) |
| 105 | .withSrcMac(MacAddress.valueOf(MAC_ADDRESS_1)) |
| 106 | .withDstMac(MacAddress.valueOf(MAC_ADDRESS_1)) |
| 107 | .withStatsInfo(statsInfo) |
| 108 | .build(); |
| 109 | |
| 110 | info2 = builder3 |
| 111 | .withFlowType((byte) STATIC_INTEGER_2) |
| 112 | .withInputInterfaceId(STATIC_INTEGER_2) |
| 113 | .withOutputInterfaceId(STATIC_INTEGER_2) |
| 114 | .withDeviceId(DeviceId.deviceId(STATIC_STRING_2)) |
| 115 | .withSrcIp(IpPrefix.valueOf( |
| 116 | IpAddress.valueOf(IP_ADDRESS_2), IP_PREFIX_LENGTH_2)) |
| 117 | .withDstIp(IpPrefix.valueOf( |
| 118 | IpAddress.valueOf(IP_ADDRESS_2), IP_PREFIX_LENGTH_2)) |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 119 | .withSrcPort(TpPort.tpPort(PORT_2)) |
| 120 | .withDstPort(TpPort.tpPort(PORT_2)) |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 121 | .withProtocol((byte) STATIC_INTEGER_2) |
| 122 | .withVlanId(VlanId.vlanId(STATIC_STRING_2)) |
| 123 | .withSrcMac(MacAddress.valueOf(MAC_ADDRESS_2)) |
| 124 | .withDstMac(MacAddress.valueOf(MAC_ADDRESS_2)) |
| 125 | .withStatsInfo(statsInfo) |
| 126 | .build(); |
| 127 | } |
| 128 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 129 | /** |
| 130 | * Tests class immutability. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testImmutability() { |
| 134 | assertThatClassIsImmutable(DefaultFlowInfo.class); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Tests object equality. |
| 139 | */ |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 140 | @Test |
| 141 | public void testEquality() { |
| 142 | new EqualsTester() |
| 143 | .addEqualityGroup(info1, sameAsInfo1) |
| 144 | .addEqualityGroup(info2).testEquals(); |
| 145 | } |
| 146 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 147 | /** |
| 148 | * Tests object construction. |
| 149 | */ |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 150 | @Test |
| 151 | public void testConstruction() { |
| 152 | FlowInfo info = info1; |
| 153 | |
| 154 | assertThat(info.flowType(), is((byte) STATIC_INTEGER_1)); |
| 155 | assertThat(info.inputInterfaceId(), is(STATIC_INTEGER_1)); |
| 156 | assertThat(info.outputInterfaceId(), is(STATIC_INTEGER_1)); |
| 157 | assertThat(info.deviceId(), is(DeviceId.deviceId(STATIC_STRING_1))); |
| 158 | assertThat(info.srcIp(), is(IpPrefix.valueOf( |
| 159 | IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1))); |
| 160 | assertThat(info.dstIp(), is(IpPrefix.valueOf( |
| 161 | IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1))); |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 162 | assertThat(info.srcPort(), is(TpPort.tpPort(PORT_1))); |
| 163 | assertThat(info.dstPort(), is(TpPort.tpPort(PORT_1))); |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 164 | assertThat(info.protocol(), is((byte) STATIC_INTEGER_1)); |
| 165 | assertThat(info.vlanId(), is(VlanId.vlanId(STATIC_STRING_1))); |
| 166 | assertThat(info.srcMac(), is(MacAddress.valueOf(MAC_ADDRESS_1))); |
| 167 | assertThat(info.dstMac(), is(MacAddress.valueOf(MAC_ADDRESS_1))); |
| 168 | } |
| 169 | } |