blob: 60fc9809b704fb9345a4b90f7d554b7313e951a9 [file] [log] [blame]
Jian Li47e7af72021-03-05 01:32:04 +09001/*
2 * Copyright 2021-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 */
16package org.onosproject.kubevirtnetworking.api;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpPrefix;
22
23import static junit.framework.TestCase.assertEquals;
24import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
25
26/**
27 * Unit tests for the default kubevirt security group rule class.
28 */
29public class DefaultKubevirtSecurityGroupRuleTest {
30 private static final String ID_1 = "id-1";
31 private static final String ID_2 = "id-2";
32 private static final String SECURITY_GROUP_ID_1 = "sg-id-1";
33 private static final String SECURITY_GROUP_ID_2 = "sg-id-2";
34 private static final String DIRECTION_1 = "ingress";
35 private static final String DIRECTION_2 = "egress";
36 private static final String ETHER_TYPE_1 = "IPv4";
37 private static final String ETHER_TYPE_2 = "IPv4";
38 private static final Integer PORT_RANGE_MAX_1 = 80;
39 private static final Integer PORT_RANGE_MAX_2 = 8080;
40 private static final Integer PORT_RANGE_MIN_1 = 80;
41 private static final Integer PORT_RANGE_MIN_2 = 8080;
42 private static final String PROTOCOL_1 = "tcp";
43 private static final String PROTOCOL_2 = "udp";
44 private static final IpPrefix REMOTE_IP_PREFIX_1 = IpPrefix.valueOf("10.10.10.0/24");
45 private static final IpPrefix REMOTE_IP_PREFIX_2 = IpPrefix.valueOf("20.20.20.0/24");
46 private static final String REMOTE_GROUP_ID_1 = "rid-1";
47 private static final String REMOTE_GROUP_ID_2 = "rid-2";
48
49 private KubevirtSecurityGroupRule rule1;
50 private KubevirtSecurityGroupRule sameAsRule1;
51 private KubevirtSecurityGroupRule rule2;
52
53 /**
54 * Tests class immutability.
55 */
56 @Test
57 public void testImmutability() {
58 assertThatClassIsImmutable(DefaultKubevirtSecurityGroupRule.class);
59 }
60
61 /**
62 * Initial setup for this unit test.
63 */
64 @Before
65 public void setUp() {
66 rule1 = DefaultKubevirtSecurityGroupRule.builder()
67 .id(ID_1)
68 .securityGroupId(SECURITY_GROUP_ID_1)
69 .direction(DIRECTION_1)
70 .etherType(ETHER_TYPE_1)
71 .portRangeMax(PORT_RANGE_MAX_1)
72 .portRangeMin(PORT_RANGE_MIN_1)
73 .protocol(PROTOCOL_1)
74 .remoteIpPrefix(REMOTE_IP_PREFIX_1)
75 .remoteGroupId(REMOTE_GROUP_ID_1)
76 .build();
77
78 sameAsRule1 = DefaultKubevirtSecurityGroupRule.builder()
79 .id(ID_1)
80 .securityGroupId(SECURITY_GROUP_ID_1)
81 .direction(DIRECTION_1)
82 .etherType(ETHER_TYPE_1)
83 .portRangeMax(PORT_RANGE_MAX_1)
84 .portRangeMin(PORT_RANGE_MIN_1)
85 .protocol(PROTOCOL_1)
86 .remoteIpPrefix(REMOTE_IP_PREFIX_1)
87 .remoteGroupId(REMOTE_GROUP_ID_1)
88 .build();
89
90 rule2 = DefaultKubevirtSecurityGroupRule.builder()
91 .id(ID_2)
92 .securityGroupId(SECURITY_GROUP_ID_2)
93 .direction(DIRECTION_2)
94 .etherType(ETHER_TYPE_2)
95 .portRangeMax(PORT_RANGE_MAX_2)
96 .portRangeMin(PORT_RANGE_MIN_2)
97 .protocol(PROTOCOL_2)
98 .remoteIpPrefix(REMOTE_IP_PREFIX_2)
99 .remoteGroupId(REMOTE_GROUP_ID_2)
100 .build();
101 }
102
103 /**
104 * Tests object equality.
105 */
106 @Test
107 public void testEquality() {
108 new EqualsTester().addEqualityGroup(rule1, sameAsRule1)
109 .addEqualityGroup(rule2)
110 .testEquals();
111 }
112
113 /**
114 * Test object construction.
115 */
116 @Test
117 public void testConstruction() {
118 KubevirtSecurityGroupRule rule = rule1;
119
120 assertEquals(ID_1, rule.id());
121 assertEquals(SECURITY_GROUP_ID_1, rule.securityGroupId());
122 assertEquals(DIRECTION_1, rule.direction());
123 assertEquals(ETHER_TYPE_1, rule.etherType());
124 assertEquals(PORT_RANGE_MAX_1, rule.portRangeMax());
125 assertEquals(PORT_RANGE_MIN_1, rule.portRangeMin());
126 assertEquals(PROTOCOL_1, rule.protocol());
127 assertEquals(REMOTE_IP_PREFIX_1, rule.remoteIpPrefix());
128 assertEquals(REMOTE_GROUP_ID_1, rule.remoteGroupId());
129 }
130}