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