Jian Li | d18f2b0 | 2018-07-04 02:26:45 +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.openstackvtap.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.openstackvtap.api.OpenstackVtapCriterion; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for DefaultOpenstackVtapCriterion. |
| 31 | */ |
| 32 | public class DefaultOpenstackVtapCriterionTest { |
| 33 | |
| 34 | private static final IpPrefix SRC_IP_PREFIX_1 = |
| 35 | IpPrefix.valueOf(IpAddress.valueOf("10.10.10.1"), 32); |
| 36 | private static final IpPrefix SRC_IP_PREFIX_2 = |
| 37 | IpPrefix.valueOf(IpAddress.valueOf("20.20.20.1"), 32); |
| 38 | private static final IpPrefix DST_IP_PREFIX_1 = |
| 39 | IpPrefix.valueOf(IpAddress.valueOf("10.10.10.2"), 32); |
| 40 | private static final IpPrefix DST_IP_PREFIX_2 = |
| 41 | IpPrefix.valueOf(IpAddress.valueOf("20.20.20.2"), 32); |
| 42 | |
| 43 | private static final TpPort SRC_PORT_1 = TpPort.tpPort(10); |
| 44 | private static final TpPort SRC_PORT_2 = TpPort.tpPort(20); |
| 45 | private static final TpPort DST_PORT_1 = TpPort.tpPort(30); |
| 46 | private static final TpPort DST_PORT_2 = TpPort.tpPort(40); |
| 47 | |
| 48 | private static final byte IP_PROTOCOL_1 = (byte) 1; |
| 49 | private static final byte IP_PROTOCOL_2 = (byte) 2; |
| 50 | |
| 51 | private OpenstackVtapCriterion criterion1; |
| 52 | private OpenstackVtapCriterion sameAsCriterion1; |
| 53 | private OpenstackVtapCriterion criterion2; |
| 54 | |
| 55 | @Before |
| 56 | public void setup() { |
| 57 | |
| 58 | OpenstackVtapCriterion.Builder builder1 = DefaultOpenstackVtapCriterion.builder(); |
| 59 | |
| 60 | criterion1 = builder1 |
| 61 | .srcIpPrefix(SRC_IP_PREFIX_1) |
| 62 | .dstIpPrefix(DST_IP_PREFIX_1) |
| 63 | .srcTpPort(SRC_PORT_1) |
| 64 | .dstTpPort(DST_PORT_1) |
| 65 | .ipProtocol(IP_PROTOCOL_1) |
| 66 | .build(); |
| 67 | |
| 68 | OpenstackVtapCriterion.Builder builder2 = DefaultOpenstackVtapCriterion.builder(); |
| 69 | |
| 70 | sameAsCriterion1 = builder2 |
| 71 | .srcIpPrefix(SRC_IP_PREFIX_1) |
| 72 | .dstIpPrefix(DST_IP_PREFIX_1) |
| 73 | .srcTpPort(SRC_PORT_1) |
| 74 | .dstTpPort(DST_PORT_1) |
| 75 | .ipProtocol(IP_PROTOCOL_1) |
| 76 | .build(); |
| 77 | |
| 78 | OpenstackVtapCriterion.Builder builder3 = DefaultOpenstackVtapCriterion.builder(); |
| 79 | |
| 80 | criterion2 = builder3 |
| 81 | .srcIpPrefix(SRC_IP_PREFIX_2) |
| 82 | .dstIpPrefix(DST_IP_PREFIX_2) |
| 83 | .srcTpPort(SRC_PORT_2) |
| 84 | .dstTpPort(DST_PORT_2) |
| 85 | .ipProtocol(IP_PROTOCOL_2) |
| 86 | .build(); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testEquality() { |
| 91 | new EqualsTester() |
| 92 | .addEqualityGroup(criterion1, sameAsCriterion1) |
| 93 | .addEqualityGroup(criterion2).testEquals(); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testConstruction() { |
| 98 | DefaultOpenstackVtapCriterion criterion = |
| 99 | (DefaultOpenstackVtapCriterion) criterion1; |
| 100 | |
| 101 | assertThat(criterion.srcIpPrefix(), is(SRC_IP_PREFIX_1)); |
| 102 | assertThat(criterion.dstIpPrefix(), is(DST_IP_PREFIX_1)); |
| 103 | assertThat(criterion.srcTpPort(), is(SRC_PORT_1)); |
| 104 | assertThat(criterion.dstTpPort(), is(DST_PORT_1)); |
| 105 | assertThat(criterion.ipProtocol(), is(IP_PROTOCOL_1)); |
| 106 | } |
| 107 | } |