summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfaces/acl/common/PortPairTest.java
blob: 2127fb0fa132f702f2d2e2f6ea785fcc9d9ccb37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.translate.v3po.interfaces.acl.common;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;

import java.util.List;
import org.junit.Test;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRange;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRangeBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRange;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRangeBuilder;

public class PortPairTest {

    @Test
    public void testSingleSrc() {
        final SourcePortRange src = new SourcePortRangeBuilder().setLowerPort(new PortNumber(123)).build();
        final List<PortPair> portPairs = PortPair.fromRange(src, null);
        assertThat(portPairs, hasSize(1));
        assertThat(portPairs, contains(new PortPair(123, null)));
    }

    @Test
    public void testSrcRange() {
        final SourcePortRange src = new SourcePortRangeBuilder()
            .setLowerPort(new PortNumber(123))
            .setUpperPort(new PortNumber(125)).build();
        final List<PortPair> portPairs = PortPair.fromRange(src, null);
        assertThat(portPairs, hasSize(3));
        assertThat(portPairs, contains(new PortPair(123, null), new PortPair(124, null), new PortPair(125, null)));
    }

    @Test
    public void testSrcRangeWithDst() {
        final SourcePortRange src = new SourcePortRangeBuilder()
            .setLowerPort(new PortNumber(123))
            .setUpperPort(new PortNumber(125)).build();
        final DestinationPortRange dst = new DestinationPortRangeBuilder().setLowerPort(new PortNumber(111)).build();
        final List<PortPair> portPairs = PortPair.fromRange(src, dst);
        assertThat(portPairs, hasSize(3));
        assertThat(portPairs, contains(new PortPair(123, 111), new PortPair(124, 111), new PortPair(125, 111)));
    }

    @Test
    public void testSingleDst() {
        final DestinationPortRange dst = new DestinationPortRangeBuilder().setLowerPort(new PortNumber(123)).build();
        final List<PortPair> portPairs = PortPair.fromRange(null, dst);
        assertThat(portPairs, hasSize(1));
        assertThat(portPairs, contains(new PortPair(null, 123)));
    }

    @Test
    public void testDstRange() {
        final DestinationPortRange dst = new DestinationPortRangeBuilder()
            .setLowerPort(new PortNumber(10))
            .setUpperPort(new PortNumber(11)).build();
        final List<PortPair> portPairs = PortPair.fromRange(null, dst);
        assertThat(portPairs, hasSize(2));
        assertThat(portPairs, contains(new PortPair(null, 10), new PortPair(null, 11)));
    }

    @Test
    public void testDstRangeWithSrc() {
        final SourcePortRange src = new SourcePortRangeBuilder().setLowerPort(new PortNumber(111)).build();
        final DestinationPortRange dst = new DestinationPortRangeBuilder()
            .setLowerPort(new PortNumber(10))
            .setUpperPort(new PortNumber(11)).build();
        final List<PortPair> portPairs = PortPair.fromRange(src, dst);
        assertThat(portPairs, hasSize(2));
        assertThat(portPairs, contains(new PortPair(111, 10), new PortPair(111, 11)));
    }

    @Test
    public void testSinglePair() {
        final SourcePortRange src = new SourcePortRangeBuilder().setLowerPort(new PortNumber(123)).build();
        final DestinationPortRange dst = new DestinationPortRangeBuilder().setLowerPort(new PortNumber(321)).build();
        final List<PortPair> portPairs = PortPair.fromRange(src, dst);
        assertThat(portPairs, hasSize(1));
        assertThat(portPairs, contains(new PortPair(123, 321)));
    }

    @Test
    public void testCartesianProduct() {
        final SourcePortRange src = new SourcePortRangeBuilder()
            .setLowerPort(new PortNumber(1))
            .setUpperPort(new PortNumber(2)).build();
        final DestinationPortRange dst = new DestinationPortRangeBuilder()
            .setLowerPort(new PortNumber(1))
            .setUpperPort(new PortNumber(3)).build();
        final List<PortPair> portPairs = PortPair.fromRange(src, dst);
        assertThat(portPairs, hasSize(6));
        assertThat(portPairs,
            contains(new PortPair(1, 1), new PortPair(1, 2), new PortPair(1, 3), new PortPair(2, 1), new PortPair(2, 2),
                new PortPair(2, 3)));
    }
}