|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.user.firewall; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.apache.commons.collections.CollectionUtils; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.mockito.junit.MockitoJUnitRunner; |
| 29 | +import org.springframework.test.util.ReflectionTestUtils; |
| 30 | + |
| 31 | +import com.cloud.utils.net.NetUtils; |
| 32 | + |
| 33 | +@RunWith(MockitoJUnitRunner.class) |
| 34 | +public class CreateFirewallRuleCmdTest { |
| 35 | + |
| 36 | + private void validateAllIp4Cidr(final CreateFirewallRuleCmd cmd) { |
| 37 | + Assert.assertTrue(CollectionUtils.isNotEmpty(cmd.getSourceCidrList())); |
| 38 | + Assert.assertEquals(1, cmd.getSourceCidrList().size()); |
| 39 | + Assert.assertEquals(NetUtils.ALL_IP4_CIDRS, cmd.getSourceCidrList().get(0)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testGetSourceCidrList_Null() { |
| 44 | + final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd(); |
| 45 | + ReflectionTestUtils.setField(cmd, "cidrlist", null); |
| 46 | + validateAllIp4Cidr(cmd); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testGetSourceCidrList_Empty() { |
| 51 | + final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd(); |
| 52 | + ReflectionTestUtils.setField(cmd, "cidrlist", new ArrayList<>()); |
| 53 | + validateAllIp4Cidr(cmd); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testGetSourceCidrList_NullFirstElement() { |
| 58 | + final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd(); |
| 59 | + List<String> list = new ArrayList<>(); |
| 60 | + list.add(null); |
| 61 | + ReflectionTestUtils.setField(cmd, "cidrlist", list); |
| 62 | + validateAllIp4Cidr(cmd); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testGetSourceCidrList_EmptyFirstElement() { |
| 67 | + final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd(); |
| 68 | + ReflectionTestUtils.setField(cmd, "cidrlist", Collections.singletonList(" ")); |
| 69 | + validateAllIp4Cidr(cmd); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGetSourceCidrList_Valid() { |
| 74 | + final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd(); |
| 75 | + String cidr = "10.1.1.1/22"; |
| 76 | + ReflectionTestUtils.setField(cmd, "cidrlist", Collections.singletonList(cidr)); |
| 77 | + Assert.assertTrue(CollectionUtils.isNotEmpty(cmd.getSourceCidrList())); |
| 78 | + Assert.assertEquals(1, cmd.getSourceCidrList().size()); |
| 79 | + Assert.assertEquals(cidr, cmd.getSourceCidrList().get(0)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetSourceCidrList_EmptyFirstElementButMore() { |
| 84 | + final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd(); |
| 85 | + String cidr = "10.1.1.1/22"; |
| 86 | + ReflectionTestUtils.setField(cmd, "cidrlist", Arrays.asList(" ", cidr)); |
| 87 | + Assert.assertTrue(CollectionUtils.isNotEmpty(cmd.getSourceCidrList())); |
| 88 | + Assert.assertEquals(2, cmd.getSourceCidrList().size()); |
| 89 | + Assert.assertEquals(cidr, cmd.getSourceCidrList().get(1)); |
| 90 | + } |
| 91 | +} |
0 commit comments