Jian Li | ae3fcff | 2018-07-30 11:55:44 +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.TpPort; |
| 24 | import org.onosproject.openstacktelemetry.api.StatsFlowRule; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for DefaultStatsFlowRule class. |
| 31 | */ |
| 32 | public final class DefaultStatsFlowRuleTest { |
| 33 | |
| 34 | private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("10.10.10.1"); |
| 35 | private static final IpAddress IP_ADDRESS_2 = IpAddress.valueOf("20.20.20.1"); |
| 36 | |
| 37 | private static final int IP_PREFIX_LENGTH_1 = 10; |
| 38 | private static final int IP_PREFIX_LENGTH_2 = 20; |
| 39 | |
| 40 | private static final int PORT_1 = 1000; |
| 41 | private static final int PORT_2 = 2000; |
| 42 | |
| 43 | private static final byte PROTOCOL_1 = 1; |
| 44 | private static final byte PROTOCOL_2 = 2; |
| 45 | |
| 46 | private StatsFlowRule rule1; |
| 47 | private StatsFlowRule sameAsRule1; |
| 48 | private StatsFlowRule rule2; |
| 49 | |
| 50 | /** |
| 51 | * Initial setup for this unit test. |
| 52 | */ |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | |
| 56 | rule1 = DefaultStatsFlowRule.builder() |
| 57 | .srcIpPrefix(IpPrefix.valueOf(IP_ADDRESS_1, IP_PREFIX_LENGTH_1)) |
| 58 | .dstIpPrefix(IpPrefix.valueOf(IP_ADDRESS_2, IP_PREFIX_LENGTH_2)) |
| 59 | .srcTpPort(TpPort.tpPort(PORT_1)) |
| 60 | .dstTpPort(TpPort.tpPort(PORT_2)) |
| 61 | .ipProtocol(PROTOCOL_1) |
| 62 | .build(); |
| 63 | |
| 64 | sameAsRule1 = DefaultStatsFlowRule.builder() |
| 65 | .srcIpPrefix(IpPrefix.valueOf(IP_ADDRESS_1, IP_PREFIX_LENGTH_1)) |
| 66 | .dstIpPrefix(IpPrefix.valueOf(IP_ADDRESS_2, IP_PREFIX_LENGTH_2)) |
| 67 | .srcTpPort(TpPort.tpPort(PORT_1)) |
| 68 | .dstTpPort(TpPort.tpPort(PORT_2)) |
| 69 | .ipProtocol(PROTOCOL_1) |
| 70 | .build(); |
| 71 | |
| 72 | rule2 = DefaultStatsFlowRule.builder() |
| 73 | .srcIpPrefix(IpPrefix.valueOf(IP_ADDRESS_2, IP_PREFIX_LENGTH_2)) |
| 74 | .dstIpPrefix(IpPrefix.valueOf(IP_ADDRESS_1, IP_PREFIX_LENGTH_1)) |
| 75 | .srcTpPort(TpPort.tpPort(PORT_2)) |
| 76 | .dstTpPort(TpPort.tpPort(PORT_1)) |
| 77 | .ipProtocol(PROTOCOL_2) |
| 78 | .build(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests class immutability. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testImmutability() { |
| 86 | assertThatClassIsImmutable(DefaultStatsFlowRule.class); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests object equality. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testEquality() { |
| 94 | new EqualsTester() |
| 95 | .addEqualityGroup(rule1, sameAsRule1) |
| 96 | .addEqualityGroup(rule2).testEquals(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests object construction. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testConstruction() { |
| 104 | StatsFlowRule rule = rule1; |
| 105 | |
| 106 | assertEquals(IpPrefix.valueOf(IP_ADDRESS_1, IP_PREFIX_LENGTH_1), rule.srcIpPrefix()); |
| 107 | assertEquals(IpPrefix.valueOf(IP_ADDRESS_2, IP_PREFIX_LENGTH_2), rule.dstIpPrefix()); |
| 108 | assertEquals(TpPort.tpPort(PORT_1), rule.srcTpPort()); |
| 109 | assertEquals(TpPort.tpPort(PORT_2), rule.dstTpPort()); |
| 110 | assertEquals(PROTOCOL_1, rule.ipProtocol()); |
| 111 | } |
| 112 | } |