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