CLOUDSTACK-763: Added unit tests and integration test
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/e335d8b5 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/e335d8b5 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/e335d8b5 Branch: refs/heads/network_acl Commit: e335d8b5b505a34096dc85feb46306a8e9e96135 Parents: 4af8682 Author: Kishan Kavala <kis...@cloud.com> Authored: Tue May 7 19:43:28 2013 +0530 Committer: Kishan Kavala <kis...@cloud.com> Committed: Tue May 7 19:44:40 2013 +0530 ---------------------------------------------------------------------- .../cloud/network/firewall/NetworkACLService.java | 118 -------- .../com/cloud/network/vpc/NetworkACLService.java | 118 ++++++++ api/src/org/apache/cloudstack/api/BaseCmd.java | 2 +- .../command/user/network/CreateNetworkACLCmd.java | 4 +- .../com/cloud/network/vpc/NetworkACLItemVO.java | 2 +- .../com/cloud/network/vpc/NetworkACLManager.java | 1 - .../cloud/network/vpc/NetworkACLManagerImpl.java | 9 +- .../cloud/network/vpc/NetworkACLServiceImpl.java | 1 - .../test/com/cloud/vpc/NetworkACLManagerTest.java | 195 +++++++++++++ .../test/com/cloud/vpc/NetworkACLServiceTest.java | 219 +++++++++++++++ 10 files changed, 543 insertions(+), 126 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/api/src/com/cloud/network/firewall/NetworkACLService.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/network/firewall/NetworkACLService.java b/api/src/com/cloud/network/firewall/NetworkACLService.java deleted file mode 100644 index cd25da3..0000000 --- a/api/src/com/cloud/network/firewall/NetworkACLService.java +++ /dev/null @@ -1,118 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you 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 com.cloud.network.firewall; - - -import java.util.List; - -import com.cloud.network.vpc.NetworkACL; -import com.cloud.network.vpc.NetworkACLItem; -import org.apache.cloudstack.api.command.user.network.CreateNetworkACLCmd; -import org.apache.cloudstack.api.command.user.network.CreateNetworkACLListCmd; -import org.apache.cloudstack.api.command.user.network.ListNetworkACLListsCmd; -import org.apache.cloudstack.api.command.user.network.ListNetworkACLsCmd; - -import com.cloud.exception.NetworkRuleConflictException; -import com.cloud.exception.ResourceUnavailableException; -import com.cloud.user.Account; -import com.cloud.utils.Pair; - -public interface NetworkACLService { - /** - * Creates Network ACL for the specified VPC - * @param name - * @param description - * @param vpcId - * @return - */ - NetworkACL createNetworkACL(String name, String description, long vpcId); - - /** - * Get Network ACL with specified Id - * @param id - * @return - */ - NetworkACL getNetworkACL(long id); - - /** - * List NeetworkACLs by Id/Name/Network or Vpc it belongs to - * @param id - * @param name - * @param networkId - * @param vpcId - * @return - */ - Pair<List<? extends NetworkACL>,Integer> listNetworkACLs(Long id, String name, Long networkId, Long vpcId); - - /** - * Delete specified network ACL. Deletion fails if the list is not empty - * @param id - * @return - */ - boolean deleteNetworkACL(long id); - - /** - * Associates ACL with specified Network - * @param aclId - * @param networkId - * @return - * @throws ResourceUnavailableException - */ - boolean replaceNetworkACL(long aclId, long networkId) throws ResourceUnavailableException; - - /** - * Applied ACL to associated networks - * @param aclId - * @return - * @throws ResourceUnavailableException - */ - boolean applyNetworkACL(long aclId) throws ResourceUnavailableException; - - /** - * Creates a Network ACL Item within an ACL and applies the ACL to associated networks - * @param createNetworkACLCmd - * @return - */ - NetworkACLItem createNetworkACLItem(CreateNetworkACLCmd aclItemCmd); - - /** - * Return ACL item with specified Id - * @param ruleId - * @return - */ - NetworkACLItem getNetworkACLItem(long ruleId); - - /** - * Lists Network ACL Items by Id, Network, ACLId, Traffic Type, protocol - * @param listNetworkACLsCmd - * @return - */ - Pair<List<? extends NetworkACLItem>, Integer> listNetworkACLItems(ListNetworkACLsCmd cmd); - - /** - * Revoked ACL Item with specified Id - * @param ruleId - * @param apply - * @return - */ - boolean revokeNetworkACLItem(long ruleId); - - - NetworkACLItem updateNetworkACLItem(Long id, String protocol, List<String> sourceCidrList, NetworkACLItem.TrafficType trafficType, - String action, Integer number, Integer sourcePortStart, Integer sourcePortEnd, - Integer icmpCode, Integer icmpType) throws ResourceUnavailableException; -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/api/src/com/cloud/network/vpc/NetworkACLService.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/network/vpc/NetworkACLService.java b/api/src/com/cloud/network/vpc/NetworkACLService.java new file mode 100644 index 0000000..9fc476f --- /dev/null +++ b/api/src/com/cloud/network/vpc/NetworkACLService.java @@ -0,0 +1,118 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 com.cloud.network.vpc; + + +import java.util.List; + +import com.cloud.network.vpc.NetworkACL; +import com.cloud.network.vpc.NetworkACLItem; +import org.apache.cloudstack.api.command.user.network.CreateNetworkACLCmd; +import org.apache.cloudstack.api.command.user.network.CreateNetworkACLListCmd; +import org.apache.cloudstack.api.command.user.network.ListNetworkACLListsCmd; +import org.apache.cloudstack.api.command.user.network.ListNetworkACLsCmd; + +import com.cloud.exception.NetworkRuleConflictException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.user.Account; +import com.cloud.utils.Pair; + +public interface NetworkACLService { + /** + * Creates Network ACL for the specified VPC + * @param name + * @param description + * @param vpcId + * @return + */ + NetworkACL createNetworkACL(String name, String description, long vpcId); + + /** + * Get Network ACL with specified Id + * @param id + * @return + */ + NetworkACL getNetworkACL(long id); + + /** + * List NeetworkACLs by Id/Name/Network or Vpc it belongs to + * @param id + * @param name + * @param networkId + * @param vpcId + * @return + */ + Pair<List<? extends NetworkACL>,Integer> listNetworkACLs(Long id, String name, Long networkId, Long vpcId); + + /** + * Delete specified network ACL. Deletion fails if the list is not empty + * @param id + * @return + */ + boolean deleteNetworkACL(long id); + + /** + * Associates ACL with specified Network + * @param aclId + * @param networkId + * @return + * @throws ResourceUnavailableException + */ + boolean replaceNetworkACL(long aclId, long networkId) throws ResourceUnavailableException; + + /** + * Applied ACL to associated networks + * @param aclId + * @return + * @throws ResourceUnavailableException + */ + boolean applyNetworkACL(long aclId) throws ResourceUnavailableException; + + /** + * Creates a Network ACL Item within an ACL and applies the ACL to associated networks + * @param createNetworkACLCmd + * @return + */ + NetworkACLItem createNetworkACLItem(CreateNetworkACLCmd aclItemCmd); + + /** + * Return ACL item with specified Id + * @param ruleId + * @return + */ + NetworkACLItem getNetworkACLItem(long ruleId); + + /** + * Lists Network ACL Items by Id, Network, ACLId, Traffic Type, protocol + * @param listNetworkACLsCmd + * @return + */ + Pair<List<? extends NetworkACLItem>, Integer> listNetworkACLItems(ListNetworkACLsCmd cmd); + + /** + * Revoked ACL Item with specified Id + * @param ruleId + * @param apply + * @return + */ + boolean revokeNetworkACLItem(long ruleId); + + + NetworkACLItem updateNetworkACLItem(Long id, String protocol, List<String> sourceCidrList, NetworkACLItem.TrafficType trafficType, + String action, Integer number, Integer sourcePortStart, Integer sourcePortEnd, + Integer icmpCode, Integer icmpType) throws ResourceUnavailableException; +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/api/src/org/apache/cloudstack/api/BaseCmd.java ---------------------------------------------------------------------- diff --git a/api/src/org/apache/cloudstack/api/BaseCmd.java b/api/src/org/apache/cloudstack/api/BaseCmd.java index 48d18d0..98e5a64 100644 --- a/api/src/org/apache/cloudstack/api/BaseCmd.java +++ b/api/src/org/apache/cloudstack/api/BaseCmd.java @@ -49,7 +49,7 @@ import com.cloud.network.StorageNetworkService; import com.cloud.network.VpcVirtualNetworkApplianceService; import com.cloud.network.as.AutoScaleService; import com.cloud.network.firewall.FirewallService; -import com.cloud.network.firewall.NetworkACLService; +import com.cloud.network.vpc.NetworkACLService; import com.cloud.network.lb.LoadBalancingRulesService; import com.cloud.network.rules.RulesService; import com.cloud.network.security.SecurityGroupService; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java ---------------------------------------------------------------------- diff --git a/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java index 1ed6445..275fa18 100644 --- a/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java @@ -179,7 +179,7 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd { public Integer getIcmpCode() { if (icmpCode != null) { return icmpCode; - } else if (protocol.equalsIgnoreCase(NetUtils.ICMP_PROTO)) { + } else if (getProtocol().equalsIgnoreCase(NetUtils.ICMP_PROTO)) { return -1; } return null; @@ -188,7 +188,7 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd { public Integer getIcmpType() { if (icmpType != null) { return icmpType; - } else if (protocol.equalsIgnoreCase(NetUtils.ICMP_PROTO)) { + } else if (getProtocol().equalsIgnoreCase(NetUtils.ICMP_PROTO)) { return -1; } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/server/src/com/cloud/network/vpc/NetworkACLItemVO.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/network/vpc/NetworkACLItemVO.java b/server/src/com/cloud/network/vpc/NetworkACLItemVO.java index 74d0445..46f84c9 100644 --- a/server/src/com/cloud/network/vpc/NetworkACLItemVO.java +++ b/server/src/com/cloud/network/vpc/NetworkACLItemVO.java @@ -74,7 +74,7 @@ public class NetworkACLItemVO implements NetworkACLItem { @Enumerated(value=EnumType.STRING) Action action; - protected NetworkACLItemVO() { + public NetworkACLItemVO() { this.uuid = UUID.randomUUID().toString(); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/server/src/com/cloud/network/vpc/NetworkACLManager.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/network/vpc/NetworkACLManager.java b/server/src/com/cloud/network/vpc/NetworkACLManager.java index fc2b1a8..58c26e3 100644 --- a/server/src/com/cloud/network/vpc/NetworkACLManager.java +++ b/server/src/com/cloud/network/vpc/NetworkACLManager.java @@ -20,7 +20,6 @@ import java.util.List; import com.cloud.exception.ResourceUnavailableException; import com.cloud.network.dao.NetworkVO; -import com.cloud.network.firewall.NetworkACLService; import com.cloud.network.rules.FirewallRule; import com.cloud.user.Account; import com.cloud.utils.db.DB; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java b/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java index 28f5af2..430e55d 100644 --- a/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java +++ b/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java @@ -71,8 +71,7 @@ public class NetworkACLManagerImpl extends ManagerBase implements NetworkACLMana @Override public NetworkACL createNetworkACL(String name, String description, long vpcId) { NetworkACLVO acl = new NetworkACLVO(name, description, vpcId); - _networkACLDao.persist(acl); - return acl; + return _networkACLDao.persist(acl); } @Override @@ -195,6 +194,9 @@ public class NetworkACLManagerImpl extends ManagerBase implements NetworkACLMana @Override public boolean revokeACLItemsForNetwork(long networkId, long userId, Account caller) throws ResourceUnavailableException { Network network = _networkDao.findById(networkId); + if(network.getNetworkACLId() == null){ + return true; + } List<NetworkACLItemVO> aclItems = _networkACLItemDao.listByACL(network.getNetworkACLId()); if (aclItems.isEmpty()) { s_logger.debug("Found no network ACL Items for network id=" + networkId); @@ -236,6 +238,9 @@ public class NetworkACLManagerImpl extends ManagerBase implements NetworkACLMana @Override public boolean applyACLToNetwork(long networkId) throws ResourceUnavailableException { Network network = _networkDao.findById(networkId); + if(network.getNetworkACLId() == null){ + return true; + } List<NetworkACLItemVO> rules = _networkACLItemDao.listByACL(network.getNetworkACLId()); return applyACLItemsToNetwork(networkId, rules); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/server/src/com/cloud/network/vpc/NetworkACLServiceImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/network/vpc/NetworkACLServiceImpl.java b/server/src/com/cloud/network/vpc/NetworkACLServiceImpl.java index 570ba2b..94be0c7 100644 --- a/server/src/com/cloud/network/vpc/NetworkACLServiceImpl.java +++ b/server/src/com/cloud/network/vpc/NetworkACLServiceImpl.java @@ -24,7 +24,6 @@ import com.cloud.network.Networks; import com.cloud.network.dao.NetworkDao; import com.cloud.network.dao.NetworkVO; import com.cloud.network.element.NetworkACLServiceProvider; -import com.cloud.network.firewall.NetworkACLService; import com.cloud.network.vpc.dao.NetworkACLDao; import com.cloud.projects.Project.ListProjectResourcesCriteria; import com.cloud.server.ResourceTag.TaggedResourceType; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/server/test/com/cloud/vpc/NetworkACLManagerTest.java ---------------------------------------------------------------------- diff --git a/server/test/com/cloud/vpc/NetworkACLManagerTest.java b/server/test/com/cloud/vpc/NetworkACLManagerTest.java new file mode 100644 index 0000000..dc53b2b --- /dev/null +++ b/server/test/com/cloud/vpc/NetworkACLManagerTest.java @@ -0,0 +1,195 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 com.cloud.vpc; + +import com.cloud.network.Network; +import com.cloud.network.NetworkManager; +import com.cloud.network.NetworkModel; +import com.cloud.network.dao.NetworkDao; +import com.cloud.network.dao.NetworkVO; +import com.cloud.network.element.NetworkACLServiceProvider; +import com.cloud.network.vpc.NetworkACLItem; +import com.cloud.network.vpc.NetworkACLItemDao; +import com.cloud.network.vpc.NetworkACLItemVO; +import com.cloud.network.vpc.NetworkACLManager; +import com.cloud.network.vpc.NetworkACLManagerImpl; +import com.cloud.network.vpc.NetworkACLVO; +import com.cloud.network.vpc.VpcManager; +import com.cloud.network.vpc.dao.NetworkACLDao; +import com.cloud.tags.dao.ResourceTagDao; +import com.cloud.user.Account; +import com.cloud.user.AccountManager; +import com.cloud.user.AccountVO; +import com.cloud.user.UserContext; +import com.cloud.utils.component.ComponentContext; +import com.cloud.utils.exception.CloudRuntimeException; +import junit.framework.TestCase; +import org.apache.cloudstack.test.utils.SpringUtils; +import org.apache.log4j.Logger; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.TypeFilter; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) +public class NetworkACLManagerTest extends TestCase{ + @Inject + NetworkACLManager _aclMgr; + + @Inject + AccountManager _accountMgr; + @Inject + VpcManager _vpcMgr; + @Inject + NetworkACLDao _networkACLDao; + @Inject + NetworkACLItemDao _networkACLItemDao; + @Inject + NetworkDao _networkDao; + @Inject + NetworkModel _networkModel; + @Inject + List<NetworkACLServiceProvider> _networkAclElements; + + private NetworkACLVO acl; + private NetworkACLItemVO aclItem; + + private static final Logger s_logger = Logger.getLogger( NetworkACLManagerTest.class); + + @Before + public void setUp() { + ComponentContext.initComponentsLifeCycle(); + Account account = new AccountVO("testaccount", 1, "testdomain", (short) 0, UUID.randomUUID().toString()); + UserContext.registerContext(1, account, null, true); + acl = Mockito.mock(NetworkACLVO.class); + aclItem = Mockito.mock(NetworkACLItemVO.class); + } + + @Test + public void testCreateACL() throws Exception { + Mockito.when(_networkACLDao.persist(Mockito.any(NetworkACLVO.class))).thenReturn(acl); + assertNotNull(_aclMgr.createNetworkACL("acl_new", "acl desc", 1L)); + } + + @Test + public void testApplyACL() throws Exception { + NetworkVO network = Mockito.mock(NetworkVO.class); + Mockito.when(_networkDao.findById(Mockito.anyLong())).thenReturn(network); + Mockito.when(_networkModel.isProviderSupportServiceInNetwork(Mockito.anyLong(), Mockito.any(Network.Service.class), Mockito.any(Network.Provider.class))).thenReturn(true); + Mockito.when(_networkAclElements.get(0).applyNetworkACLs(Mockito.any(Network.class), Mockito.anyList())).thenReturn(true); + assertTrue(_aclMgr.applyACLToNetwork(1L)); + } + + @Test + public void testRevokeACLItem() throws Exception { + Mockito.when(_networkACLItemDao.findById(Mockito.anyLong())).thenReturn(aclItem); + assertTrue(_aclMgr.revokeNetworkACLItem(1L)); + } + + @Test + public void testUpdateACLItem() throws Exception { + Mockito.when(_networkACLItemDao.findById(Mockito.anyLong())).thenReturn(aclItem); + Mockito.when(_networkACLItemDao.update(Mockito.anyLong(), Mockito.any(NetworkACLItemVO.class))).thenReturn(true); + assertNotNull(_aclMgr.updateNetworkACLItem(1L, "UDP", null, NetworkACLItem.TrafficType.Ingress, "Deny", 10, 22, 32, null, null)); + } + + @Test(expected = CloudRuntimeException.class) + public void deleteNonEmptyACL() throws Exception { + List<NetworkACLItemVO> aclItems = new ArrayList<NetworkACLItemVO>(); + aclItems.add(aclItem); + Mockito.when(_networkACLItemDao.listByACL(Mockito.anyLong())).thenReturn(aclItems); + _aclMgr.deleteNetworkACL(acl); + } + + @Configuration + @ComponentScan(basePackageClasses={NetworkACLManagerImpl.class}, + includeFilters={@ComponentScan.Filter(value=NetworkACLTestConfiguration.Library.class, type= FilterType.CUSTOM)}, + useDefaultFilters=false) + public static class NetworkACLTestConfiguration extends SpringUtils.CloudStackTestConfiguration{ + + @Bean + public AccountManager accountManager() { + return Mockito.mock(AccountManager.class); + } + + @Bean + public NetworkManager networkManager() { + return Mockito.mock(NetworkManager.class); + } + + @Bean + public NetworkModel networkModel() { + return Mockito.mock(NetworkModel.class); + } + + @Bean + public VpcManager vpcManager() { + return Mockito.mock(VpcManager.class); + } + + @Bean + public ResourceTagDao resourceTagDao() { + return Mockito.mock(ResourceTagDao.class); + } + + @Bean + public NetworkACLDao networkACLDao() { + return Mockito.mock(NetworkACLDao.class); + } + + @Bean + public NetworkACLItemDao networkACLItemDao() { + return Mockito.mock(NetworkACLItemDao.class); + } + + @Bean + public NetworkDao networkDao() { + return Mockito.mock(NetworkDao.class); + } + + @Bean + public NetworkACLServiceProvider networkElements() { + return Mockito.mock(NetworkACLServiceProvider.class); + } + + public static class Library implements TypeFilter { + @Override + public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { + mdr.getClassMetadata().getClassName(); + ComponentScan cs = NetworkACLTestConfiguration.class.getAnnotation(ComponentScan.class); + return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); + } + } + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e335d8b5/server/test/com/cloud/vpc/NetworkACLServiceTest.java ---------------------------------------------------------------------- diff --git a/server/test/com/cloud/vpc/NetworkACLServiceTest.java b/server/test/com/cloud/vpc/NetworkACLServiceTest.java new file mode 100644 index 0000000..145efb4 --- /dev/null +++ b/server/test/com/cloud/vpc/NetworkACLServiceTest.java @@ -0,0 +1,219 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 com.cloud.vpc; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.network.NetworkManager; +import com.cloud.network.NetworkModel; +import com.cloud.network.dao.NetworkDao; +import com.cloud.network.vpc.*; +import com.cloud.network.vpc.dao.NetworkACLDao; +import com.cloud.tags.dao.ResourceTagDao; +import com.cloud.user.Account; +import com.cloud.user.AccountManager; +import com.cloud.user.AccountVO; +import com.cloud.user.UserContext; +import com.cloud.utils.component.ComponentContext; +import junit.framework.TestCase; +import org.apache.cloudstack.api.command.user.network.CreateNetworkACLCmd; +import org.apache.cloudstack.test.utils.SpringUtils; +import org.apache.log4j.Logger; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.TypeFilter; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.UUID; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) +public class NetworkACLServiceTest extends TestCase{ + @Inject + NetworkACLService _aclService; + + @Inject + AccountManager _accountMgr; + @Inject + VpcManager _vpcMgr; + @Inject + NetworkACLManager _networkAclMgr; + @Inject + NetworkACLDao _networkACLDao; + @Inject + NetworkACLItemDao _networkACLItemDao; + + private CreateNetworkACLCmd createACLItemCmd; + private NetworkACLVO acl; + private NetworkACLItemVO aclItem; + + private static final Logger s_logger = Logger.getLogger( NetworkACLServiceTest.class); + + @Before + public void setUp() { + ComponentContext.initComponentsLifeCycle(); + Account account = new AccountVO("testaccount", 1, "testdomain", (short) 0, UUID.randomUUID().toString()); + UserContext.registerContext(1, account, null, true); + + createACLItemCmd = new CreateNetworkACLCmd(){ + @Override + public Long getACLId(){ + return 1L; + } + + @Override + public Integer getNumber(){ + return 1; + } + + @Override + public String getProtocol(){ + return "TCP"; + } + }; + + acl = new NetworkACLVO(){ + @Override + public Long getVpcId(){ + return 1L; + } + + @Override + public long getId(){ + return 1L; + } + + }; + + aclItem = new NetworkACLItemVO(){ + @Override + public long getAclId(){ + return 4L; + } + }; + } + + @Test + public void testCreateACL() throws Exception { + Mockito.when(_vpcMgr.getVpc(Mockito.anyLong())).thenReturn(new VpcVO()); + Mockito.when(_networkAclMgr.createNetworkACL("acl_new", "acl desc", 1L)).thenReturn(acl); + assertNotNull(_aclService.createNetworkACL("acl_new", "acl desc", 1L)); + } + + @Test(expected = InvalidParameterValueException.class) + public void testDeleteDefaultACL() throws Exception { + Mockito.when(_networkACLDao.findById(Mockito.anyLong())).thenReturn(acl); + Mockito.when(_networkAclMgr.deleteNetworkACL(acl)).thenReturn(true); + _aclService.deleteNetworkACL(1L); + } + + @Test + public void testCreateACLItem() throws Exception { + Mockito.when(_vpcMgr.getVpc(Mockito.anyLong())).thenReturn(new VpcVO()); + Mockito.when(_networkAclMgr.getNetworkACL(Mockito.anyLong())).thenReturn(acl); + Mockito.when(_networkAclMgr.createNetworkACLItem(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyList(), Mockito.anyInt(), Mockito.anyInt(), + Mockito.any(NetworkACLItem.TrafficType.class), Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt())).thenReturn(new NetworkACLItemVO()); + assertNotNull(_aclService.createNetworkACLItem(createACLItemCmd)); + } + + @Test(expected = InvalidParameterValueException.class) + public void testCreateACLItemDuplicateNumber() throws Exception { + Mockito.when(_vpcMgr.getVpc(Mockito.anyLong())).thenReturn(new VpcVO()); + Mockito.when(_networkAclMgr.getNetworkACL(Mockito.anyLong())).thenReturn(acl); + Mockito.when(_networkACLItemDao.findByAclAndNumber(Mockito.anyLong(), Mockito.anyInt())).thenReturn(new NetworkACLItemVO()); + _aclService.createNetworkACLItem(createACLItemCmd); + } + + @Test + public void testDeleteACLItem() throws Exception { + Mockito.when(_networkACLItemDao.findById(Mockito.anyLong())).thenReturn(aclItem); + Mockito.when(_networkAclMgr.revokeNetworkACLItem(Mockito.anyLong())).thenReturn(true); + assertTrue(_aclService.revokeNetworkACLItem(1L)); + } + + @Configuration + @ComponentScan(basePackageClasses={NetworkACLServiceImpl.class}, + includeFilters={@ComponentScan.Filter(value=NetworkACLTestConfiguration.Library.class, type= FilterType.CUSTOM)}, + useDefaultFilters=false) + public static class NetworkACLTestConfiguration extends SpringUtils.CloudStackTestConfiguration{ + + @Bean + public AccountManager accountManager() { + return Mockito.mock(AccountManager.class); + } + + @Bean + public NetworkManager networkManager() { + return Mockito.mock(NetworkManager.class); + } + + @Bean + public NetworkModel networkModel() { + return Mockito.mock(NetworkModel.class); + } + + @Bean + public VpcManager vpcManager() { + return Mockito.mock(VpcManager.class); + } + + @Bean + public ResourceTagDao resourceTagDao() { + return Mockito.mock(ResourceTagDao.class); + } + + @Bean + public NetworkACLDao networkACLDao() { + return Mockito.mock(NetworkACLDao.class); + } + + @Bean + public NetworkACLItemDao networkACLItemDao() { + return Mockito.mock(NetworkACLItemDao.class); + } + + @Bean + public NetworkDao networkDao() { + return Mockito.mock(NetworkDao.class); + } + + @Bean + public NetworkACLManager networkACLManager() { + return Mockito.mock(NetworkACLManager.class); + } + + public static class Library implements TypeFilter { + @Override + public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { + mdr.getClassMetadata().getClassName(); + ComponentScan cs = NetworkACLTestConfiguration.class.getAnnotation(ComponentScan.class); + return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); + } + } + } + +}