HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / PortPairTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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
17 package io.fd.honeycomb.translate.v3po.interfaces.acl.common;
18
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.contains;
21 import static org.hamcrest.Matchers.hasSize;
22
23 import java.util.List;
24 import org.junit.Test;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRange;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRangeBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRange;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRangeBuilder;
30
31 public class PortPairTest {
32
33     @Test
34     public void testSingleSrc() {
35         final SourcePortRange src = new SourcePortRangeBuilder().setLowerPort(new PortNumber(123)).build();
36         final List<PortPair> portPairs = PortPair.fromRange(src, null);
37         assertThat(portPairs, hasSize(1));
38         assertThat(portPairs, contains(new PortPair(123, null)));
39     }
40
41     @Test
42     public void testSrcRange() {
43         final SourcePortRange src = new SourcePortRangeBuilder()
44             .setLowerPort(new PortNumber(123))
45             .setUpperPort(new PortNumber(125)).build();
46         final List<PortPair> portPairs = PortPair.fromRange(src, null);
47         assertThat(portPairs, hasSize(3));
48         assertThat(portPairs, contains(new PortPair(123, null), new PortPair(124, null), new PortPair(125, null)));
49     }
50
51     @Test
52     public void testSrcRangeWithDst() {
53         final SourcePortRange src = new SourcePortRangeBuilder()
54             .setLowerPort(new PortNumber(123))
55             .setUpperPort(new PortNumber(125)).build();
56         final DestinationPortRange dst = new DestinationPortRangeBuilder().setLowerPort(new PortNumber(111)).build();
57         final List<PortPair> portPairs = PortPair.fromRange(src, dst);
58         assertThat(portPairs, hasSize(3));
59         assertThat(portPairs, contains(new PortPair(123, 111), new PortPair(124, 111), new PortPair(125, 111)));
60     }
61
62     @Test
63     public void testSingleDst() {
64         final DestinationPortRange dst = new DestinationPortRangeBuilder().setLowerPort(new PortNumber(123)).build();
65         final List<PortPair> portPairs = PortPair.fromRange(null, dst);
66         assertThat(portPairs, hasSize(1));
67         assertThat(portPairs, contains(new PortPair(null, 123)));
68     }
69
70     @Test
71     public void testDstRange() {
72         final DestinationPortRange dst = new DestinationPortRangeBuilder()
73             .setLowerPort(new PortNumber(10))
74             .setUpperPort(new PortNumber(11)).build();
75         final List<PortPair> portPairs = PortPair.fromRange(null, dst);
76         assertThat(portPairs, hasSize(2));
77         assertThat(portPairs, contains(new PortPair(null, 10), new PortPair(null, 11)));
78     }
79
80     @Test
81     public void testDstRangeWithSrc() {
82         final SourcePortRange src = new SourcePortRangeBuilder().setLowerPort(new PortNumber(111)).build();
83         final DestinationPortRange dst = new DestinationPortRangeBuilder()
84             .setLowerPort(new PortNumber(10))
85             .setUpperPort(new PortNumber(11)).build();
86         final List<PortPair> portPairs = PortPair.fromRange(src, dst);
87         assertThat(portPairs, hasSize(2));
88         assertThat(portPairs, contains(new PortPair(111, 10), new PortPair(111, 11)));
89     }
90
91     @Test
92     public void testSinglePair() {
93         final SourcePortRange src = new SourcePortRangeBuilder().setLowerPort(new PortNumber(123)).build();
94         final DestinationPortRange dst = new DestinationPortRangeBuilder().setLowerPort(new PortNumber(321)).build();
95         final List<PortPair> portPairs = PortPair.fromRange(src, dst);
96         assertThat(portPairs, hasSize(1));
97         assertThat(portPairs, contains(new PortPair(123, 321)));
98     }
99
100     @Test
101     public void testCartesianProduct() {
102         final SourcePortRange src = new SourcePortRangeBuilder()
103             .setLowerPort(new PortNumber(1))
104             .setUpperPort(new PortNumber(2)).build();
105         final DestinationPortRange dst = new DestinationPortRangeBuilder()
106             .setLowerPort(new PortNumber(1))
107             .setUpperPort(new PortNumber(3)).build();
108         final List<PortPair> portPairs = PortPair.fromRange(src, dst);
109         assertThat(portPairs, hasSize(6));
110         assertThat(portPairs,
111             contains(new PortPair(1, 1), new PortPair(1, 2), new PortPair(1, 3), new PortPair(2, 1), new PortPair(2, 2),
112                 new PortPair(2, 3)));
113     }
114 }