Updated Branches: refs/heads/master 66ca4323b -> b1af51071
Adding new unit tests for a set of API commands Signed-off-by: Chip Childers <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/b1af5107 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/b1af5107 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/b1af5107 Branch: refs/heads/master Commit: b1af5107159c0acd9825388bf46660c6045f6034 Parents: b7c74ee Author: Meghna Kale <[email protected]> Authored: Tue Nov 20 12:28:55 2012 -0500 Committer: Chip Childers <[email protected]> Committed: Tue Nov 20 12:28:55 2012 -0500 ---------------------------------------------------------------------- .../com/cloud/api/commands/ActivateProjectCmd.java | 3 +- api/src/com/cloud/api/commands/AddClusterCmd.java | 2 +- .../cloud/api/commands/ActivateProjectCmdTest.java | 7 +- .../com/cloud/api/commands/AddClusterCmdTest.java | 50 ++--- .../com/cloud/api/commands/AddHostCmdTest.java | 169 +++++++++++++++ .../commands/AddNetworkServiceProviderCmdTest.java | 118 ++++++++++ .../api/commands/AddSecondaryStorageCmdTest.java | 120 ++++++++++ .../com/cloud/api/commands/AddSwiftCmdTest.java | 92 ++++++++ .../com/cloud/api/commands/AddVpnUserCmdTest.java | 143 ++++++++++++ 9 files changed, 672 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/src/com/cloud/api/commands/ActivateProjectCmd.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/api/commands/ActivateProjectCmd.java b/api/src/com/cloud/api/commands/ActivateProjectCmd.java index 7247767..b8c4721 100644 --- a/api/src/com/cloud/api/commands/ActivateProjectCmd.java +++ b/api/src/com/cloud/api/commands/ActivateProjectCmd.java @@ -59,6 +59,7 @@ public class ActivateProjectCmd extends BaseAsyncCmd { return s_name; } + @Override public long getEntityOwnerId() { Project project= _projectService.getProject(getId()); @@ -78,7 +79,7 @@ public class ActivateProjectCmd extends BaseAsyncCmd { @Override public void execute(){ UserContext.current().setEventDetails("Project id: "+ getId()); - Project project = _projectService.activateProject(id); + Project project = _projectService.activateProject(getId()); if (project != null) { ProjectResponse response = _responseGenerator.createProjectResponse(project); response.setResponseName(getCommandName()); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/src/com/cloud/api/commands/AddClusterCmd.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/api/commands/AddClusterCmd.java b/api/src/com/cloud/api/commands/AddClusterCmd.java index 0ff77e0..0e10f23 100755 --- a/api/src/com/cloud/api/commands/AddClusterCmd.java +++ b/api/src/com/cloud/api/commands/AddClusterCmd.java @@ -153,7 +153,7 @@ public class AddClusterCmd extends BaseCmd { List<? extends Cluster> result = _resourceService.discoverCluster(this); ListResponse<ClusterResponse> response = new ListResponse<ClusterResponse>(); List<ClusterResponse> clusterResponses = new ArrayList<ClusterResponse>(); - if (result != null) { + if (result != null && result.size() > 0) { for (Cluster cluster : result) { ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false); clusterResponses.add(clusterResponse); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java b/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java index 1b06c5d..9957851 100644 --- a/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java +++ b/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java @@ -25,10 +25,13 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; +import com.cloud.api.ResponseGenerator; +import com.cloud.api.response.ProjectResponse; import com.cloud.exception.InvalidParameterValueException; import com.cloud.projects.Project; import com.cloud.projects.ProjectService; import com.cloud.user.Account; +import com.cloud.user.UserContext; public class ActivateProjectCmdTest extends TestCase { @@ -39,15 +42,18 @@ public class ActivateProjectCmdTest extends TestCase { @Before public void setUp() { + activateProjectCmd = new ActivateProjectCmd(){ @Override public Long getId() { return 2L; } + }; } + @Test public void testGetEntityOwnerIdForNullProject() { ProjectService projectService = Mockito.mock(ProjectService.class); @@ -77,5 +83,4 @@ public class ActivateProjectCmdTest extends TestCase { } - } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/AddClusterCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/AddClusterCmdTest.java b/api/test/com/cloud/api/commands/AddClusterCmdTest.java index 6473531..bba1b3a 100644 --- a/api/test/com/cloud/api/commands/AddClusterCmdTest.java +++ b/api/test/com/cloud/api/commands/AddClusterCmdTest.java @@ -16,9 +16,6 @@ // under the License. package com.cloud.api.commands; -import java.util.ArrayList; -import java.util.List; - import junit.framework.Assert; import junit.framework.TestCase; @@ -28,22 +25,31 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; +import com.cloud.api.ResponseGenerator; import com.cloud.api.ServerApiException; +import com.cloud.api.response.ClusterResponse; import com.cloud.exception.DiscoveryException; import com.cloud.exception.ResourceInUseException; import com.cloud.org.Cluster; import com.cloud.resource.ResourceService; +import edu.emory.mathcs.backport.java.util.Arrays; + public class AddClusterCmdTest extends TestCase { private AddClusterCmd addClusterCmd; + private ResourceService resourceService; + private ResponseGenerator responseGenerator; + @Rule public ExpectedException expectedException = ExpectedException.none(); @Before public void setUp() { - addClusterCmd = new AddClusterCmd(){ +/* resourceService = Mockito.mock(ResourceService.class); + responseGenerator = Mockito.mock(ResponseGenerator.class); +*/ addClusterCmd = new AddClusterCmd(){ }; } @@ -92,40 +98,26 @@ public class AddClusterCmdTest extends TestCase { } - - + @Test - public void testExecuteForResult() { + public void testExecuteForResult() throws Exception { + + resourceService = Mockito.mock(ResourceService.class); + responseGenerator = Mockito.mock(ResponseGenerator.class); - ResourceService resourceService = Mockito.mock(ResourceService.class); + addClusterCmd._resourceService = resourceService; + addClusterCmd._responseGenerator = responseGenerator; - List<? extends Cluster> result = new ArrayList<Cluster>(); Cluster cluster = Mockito.mock(Cluster.class); -// result.add(cluster); + Cluster[] clusterArray = new Cluster[]{cluster}; - /*try { - Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(Arrays.asList(result)); - } catch (ResourceInUseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (DiscoveryException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - }*/ + Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(Arrays.asList(clusterArray)); - addClusterCmd._resourceService = resourceService; + addClusterCmd.execute(); - try { - addClusterCmd.execute(); - } catch (ServerApiException exception) { - Assert.assertEquals("Failed to add cluster", exception.getDescription()); - } } - + } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/AddHostCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/AddHostCmdTest.java b/api/test/com/cloud/api/commands/AddHostCmdTest.java new file mode 100644 index 0000000..85cac0c --- /dev/null +++ b/api/test/com/cloud/api/commands/AddHostCmdTest.java @@ -0,0 +1,169 @@ +// 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.api.commands; + +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import com.cloud.api.ResponseGenerator; +import com.cloud.api.ServerApiException; +import com.cloud.api.response.HostResponse; +import com.cloud.api.response.ListResponse; +import com.cloud.exception.DiscoveryException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.host.Host; +import com.cloud.host.Status; +import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.resource.ResourceService; +import com.cloud.resource.ResourceState; + +import edu.emory.mathcs.backport.java.util.Arrays; + +public class AddHostCmdTest extends TestCase { + + private AddHostCmd addHostCmd; + private ResourceService resourceService; + private ResponseGenerator responseGenerator; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + resourceService = Mockito.mock(ResourceService.class); + responseGenerator = Mockito.mock(ResponseGenerator.class); + addHostCmd = new AddHostCmd(){ + }; + } + + @Test + public void testExecuteForEmptyResult() { + addHostCmd._resourceService = resourceService; + + try { + addHostCmd.execute(); + } catch(ServerApiException exception) { + Assert.assertEquals("Failed to add host", exception.getDescription()); + } + + } + + + @Test + public void testExecuteForNullResult() { + + ResourceService resourceService = Mockito.mock(ResourceService.class); + addHostCmd._resourceService = resourceService; + + try { + Mockito.when(resourceService.discoverHosts(addHostCmd)).thenReturn(null); + } catch (InvalidParameterValueException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (DiscoveryException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + addHostCmd.execute(); + } catch(ServerApiException exception) { + Assert.assertEquals("Failed to add host", exception.getDescription()); + } + + } + + +/* @Test + public void testExecuteForResult() throws Exception { + + addHostCmd._resourceService = resourceService; + addHostCmd._responseGenerator = responseGenerator; + MockHost mockInstance = new MockHost(); + MockHost[] mockArray = new MockHost[]{mockInstance}; + HostResponse responseHost = new HostResponse(); + responseHost.setName("Test"); + Mockito.when(resourceService.discoverHosts(addHostCmd)).thenReturn(Arrays.asList(mockArray)); + Mockito.when(responseGenerator.createHostResponse(mockInstance)).thenReturn(responseHost); + addHostCmd.execute(); + Mockito.verify(responseGenerator).createHostResponse(mockInstance); + ListResponse<HostResponse> actualResponse = ((ListResponse<HostResponse>)addHostCmd.getResponseObject()); + Assert.assertEquals(responseHost, actualResponse.getResponses().get(0)); + Assert.assertEquals("addhostresponse", actualResponse.getResponseName()); + } +*/ + @Test + public void testExecuteForResult() throws Exception { + + addHostCmd._resourceService = resourceService; + addHostCmd._responseGenerator = responseGenerator; + Host host = Mockito.mock(Host.class); + Host[] mockArray = new Host[]{host}; + + HostResponse responseHost = new HostResponse(); + responseHost.setName("Test"); + Mockito.when(resourceService.discoverHosts(addHostCmd)).thenReturn(Arrays.asList(mockArray)); + Mockito.when(responseGenerator.createHostResponse(host)).thenReturn(responseHost); + addHostCmd.execute(); + Mockito.verify(responseGenerator).createHostResponse(host); + ListResponse<HostResponse> actualResponse = ((ListResponse<HostResponse>)addHostCmd.getResponseObject()); + Assert.assertEquals(responseHost, actualResponse.getResponses().get(0)); + Assert.assertEquals("addhostresponse", actualResponse.getResponseName()); + + } + + + @Test + public void testExecuteForDiscoveryException() { + + addHostCmd._resourceService = resourceService; + + try { + Mockito.when(resourceService.discoverHosts(addHostCmd)).thenThrow(DiscoveryException.class); + } catch (InvalidParameterValueException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (DiscoveryException e) { + e.printStackTrace(); + } + + try { + addHostCmd.execute(); + } catch(ServerApiException exception) { + Assert.assertNull(exception.getDescription()); + } + + } + + + +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/AddNetworkServiceProviderCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/AddNetworkServiceProviderCmdTest.java b/api/test/com/cloud/api/commands/AddNetworkServiceProviderCmdTest.java new file mode 100644 index 0000000..387680d --- /dev/null +++ b/api/test/com/cloud/api/commands/AddNetworkServiceProviderCmdTest.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.api.commands; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import com.cloud.api.ServerApiException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.network.NetworkService; +import com.cloud.network.PhysicalNetworkServiceProvider; + +public class AddNetworkServiceProviderCmdTest extends TestCase { + + private AddNetworkServiceProviderCmd addNetworkServiceProviderCmd; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + addNetworkServiceProviderCmd = new AddNetworkServiceProviderCmd() { + + @Override + public Long getPhysicalNetworkId() { + return 2L; + } + + @Override + public String getProviderName() { + return "ProviderName"; + } + + @Override + public Long getDestinationPhysicalNetworkId() { + return 2L; + } + + @Override + public List<String> getEnabledServices() { + List<String> lOfEnabledServices = new ArrayList<String>(); + lOfEnabledServices.add("Enabled Services"); + return lOfEnabledServices; + } + + public Long getEntityId() { + return 2L; + } + + }; + + } + + @Test + public void testCreateProviderToPhysicalNetworkSuccess() { + + NetworkService networkService = Mockito.mock(NetworkService.class); + addNetworkServiceProviderCmd._networkService = networkService; + + PhysicalNetworkServiceProvider physicalNetworkServiceProvider = Mockito.mock(PhysicalNetworkServiceProvider.class); + Mockito.when(networkService.addProviderToPhysicalNetwork(Mockito.anyLong(), + Mockito.anyString(), Mockito.anyLong(), + Mockito.anyList())).thenReturn( + physicalNetworkServiceProvider); + + try { + addNetworkServiceProviderCmd.create(); + } catch (ResourceAllocationException e) { + e.printStackTrace(); + } + + } + + + @Test + public void testCreateProviderToPhysicalNetworkFailure() throws ResourceAllocationException { + + NetworkService networkService = Mockito.mock(NetworkService.class); + addNetworkServiceProviderCmd._networkService = networkService; + + Mockito.when(networkService.addProviderToPhysicalNetwork(Mockito.anyLong(), + Mockito.anyString(), Mockito.anyLong(), + Mockito.anyList())).thenReturn(null); + + try { + addNetworkServiceProviderCmd.create(); + } catch (ServerApiException exception) { + Assert.assertEquals("Failed to add service provider entity to physical network", exception.getDescription()); + } + + + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/AddSecondaryStorageCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/AddSecondaryStorageCmdTest.java b/api/test/com/cloud/api/commands/AddSecondaryStorageCmdTest.java new file mode 100644 index 0000000..c20415c --- /dev/null +++ b/api/test/com/cloud/api/commands/AddSecondaryStorageCmdTest.java @@ -0,0 +1,120 @@ +// 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.api.commands; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import com.cloud.api.ResponseGenerator; +import com.cloud.api.ServerApiException; +import com.cloud.api.response.HostResponse; +import com.cloud.api.response.ListResponse; +import com.cloud.exception.DiscoveryException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.host.Host; +import com.cloud.resource.ResourceService; + +import edu.emory.mathcs.backport.java.util.Arrays; + +public class AddSecondaryStorageCmdTest extends TestCase { + + private AddSecondaryStorageCmd addSecondaryStorageCmd; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + addSecondaryStorageCmd = new AddSecondaryStorageCmd() { + }; + + } + + + @Test + public void testExecuteForResult() throws Exception { + + ResourceService resourceService = Mockito.mock(ResourceService.class); + addSecondaryStorageCmd._resourceService = resourceService; + + Host host = Mockito.mock(Host.class); + Host[] mockHosts = new Host[] {host}; + + Mockito.when(resourceService.discoverHosts(addSecondaryStorageCmd)).thenReturn(Arrays.asList(mockHosts)); + + ResponseGenerator responseGenerator = Mockito.mock(ResponseGenerator.class); + addSecondaryStorageCmd._responseGenerator = responseGenerator; + + HostResponse responseHost = new HostResponse(); + responseHost.setName("Test"); + + Mockito.when(responseGenerator.createHostResponse(host)).thenReturn(responseHost); + + addSecondaryStorageCmd.execute(); + + Mockito.verify(responseGenerator).createHostResponse(host); + + HostResponse actualResponse = (HostResponse)addSecondaryStorageCmd.getResponseObject(); + + Assert.assertEquals(responseHost, actualResponse); + Assert.assertEquals("addsecondarystorageresponse", actualResponse.getResponseName()); + + + } + + @Test + public void testExecuteForEmptyResult() throws Exception { + + ResourceService resourceService = Mockito.mock(ResourceService.class); + addSecondaryStorageCmd._resourceService = resourceService; + + Host[] mockHosts = new Host[] {}; + + Mockito.when(resourceService.discoverHosts(addSecondaryStorageCmd)).thenReturn(Arrays.asList(mockHosts)); + + try { + addSecondaryStorageCmd.execute(); + } catch(ServerApiException exception) { + Assert.assertEquals("Failed to add secondary storage", exception.getDescription()); + } + + } + + @Test + public void testExecuteForNullResult() throws Exception { + + ResourceService resourceService = Mockito.mock(ResourceService.class); + addSecondaryStorageCmd._resourceService = resourceService; + + Mockito.when(resourceService.discoverHosts(addSecondaryStorageCmd)).thenReturn(null); + + try { + addSecondaryStorageCmd.execute(); + } catch(ServerApiException exception) { + Assert.assertEquals("Failed to add secondary storage", exception.getDescription()); + } + + + } + +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/AddSwiftCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/AddSwiftCmdTest.java b/api/test/com/cloud/api/commands/AddSwiftCmdTest.java new file mode 100644 index 0000000..0402135 --- /dev/null +++ b/api/test/com/cloud/api/commands/AddSwiftCmdTest.java @@ -0,0 +1,92 @@ +// 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.api.commands; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import com.cloud.api.ResponseGenerator; +import com.cloud.api.ServerApiException; +import com.cloud.api.response.SwiftResponse; +import com.cloud.exception.DiscoveryException; +import com.cloud.resource.ResourceService; +import com.cloud.storage.Swift; + +import junit.framework.Assert; +import junit.framework.TestCase; + +public class AddSwiftCmdTest extends TestCase { + + private AddSwiftCmd addSwiftCmd; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + addSwiftCmd = new AddSwiftCmd(); + } + + @Test + public void testExecuteSuccess() { + + ResourceService resourceService = Mockito.mock(ResourceService.class); + addSwiftCmd._resourceService = resourceService; + + Swift swift = Mockito.mock(Swift.class); + + try { + Mockito.when(resourceService.discoverSwift(addSwiftCmd)).thenReturn(swift); + } catch (DiscoveryException e) { + e.printStackTrace(); + } + + ResponseGenerator responseGenerator = Mockito.mock(ResponseGenerator.class); + addSwiftCmd._responseGenerator = responseGenerator; + + SwiftResponse swiftResponse = Mockito.mock(SwiftResponse.class); + + Mockito.when(responseGenerator.createSwiftResponse(swift)).thenReturn(swiftResponse); + + addSwiftCmd.execute(); + + } + + + @Test + public void testExecuteFailure() { + + ResourceService resourceService = Mockito.mock(ResourceService.class); + addSwiftCmd._resourceService = resourceService; + try { + Mockito.when(resourceService.discoverSwift(addSwiftCmd)).thenReturn(null); + } catch (DiscoveryException e) { + e.printStackTrace(); + } + + try { + addSwiftCmd.execute(); + } catch(ServerApiException exception) { + Assert.assertEquals("Failed to add Swift", exception.getDescription()); + } + + } + +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b1af5107/api/test/com/cloud/api/commands/AddVpnUserCmdTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/api/commands/AddVpnUserCmdTest.java b/api/test/com/cloud/api/commands/AddVpnUserCmdTest.java new file mode 100644 index 0000000..3e2b94a --- /dev/null +++ b/api/test/com/cloud/api/commands/AddVpnUserCmdTest.java @@ -0,0 +1,143 @@ +// 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.api.commands; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; + +import com.cloud.api.ServerApiException; +import com.cloud.network.VpnUser; +import com.cloud.network.vpn.RemoteAccessVpnService; +import com.cloud.user.Account; +import com.cloud.user.AccountService; + +public class AddVpnUserCmdTest extends TestCase { + + + private AddVpnUserCmd addVpnUserCmd; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + + addVpnUserCmd = new AddVpnUserCmd() { + + @Override + public Long getEntityId() { + return 2L; + } + + @Override + public long getEntityOwnerId() { + return 2L; + } + + @Override + public String getUserName() { + return "User Name"; + } + + @Override + public String getPassword() { + return "password"; + } + + }; + } + + /*@Test + public void testExecuteVpnUserNotFound() { + + EntityManager entityManager = Mockito.mock(EntityManager.class); + + Mockito.when(entityManager.findById(VpnUser.class, Mockito.anyLong())).thenReturn(null); + + addVpnUserCmd._entityMgr = entityManager; + try { + addVpnUserCmd.execute(); + } catch (Exception e) { + } + + } + + + @Test + public void testExecuteVpnUserFound() { + + EntityManager entityManager = Mockito.mock(EntityManager.class); + addVpnUserCmd._entityMgr = entityManager; + + VpnUser vpnUser = Mockito.mock(VpnUser.class); + Mockito.when(entityManager.findById(VpnUser.class, Mockito.anyLong())).thenReturn(vpnUser); + addVpnUserCmd.execute(); + + }*/ + + @Test + public void testCreateSuccess() { + + + AccountService accountService = Mockito.mock(AccountService.class); + + Account account = Mockito.mock(Account.class); + Mockito.when(accountService.getAccount(Mockito.anyLong())).thenReturn(account); + + addVpnUserCmd._accountService = accountService; + + RemoteAccessVpnService ravService = Mockito.mock(RemoteAccessVpnService.class); + + VpnUser vpnUser = Mockito.mock(VpnUser.class); + Mockito.when(ravService.addVpnUser(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString())).thenReturn(vpnUser); + + addVpnUserCmd._ravService = ravService; + + addVpnUserCmd.create(); + + } + + + @Test + public void testCreateFailure() { + + AccountService accountService = Mockito.mock(AccountService.class); + Account account = Mockito.mock(Account.class); + Mockito.when(accountService.getAccount(Mockito.anyLong())).thenReturn(account); + + addVpnUserCmd._accountService = accountService; + + RemoteAccessVpnService ravService = Mockito.mock(RemoteAccessVpnService.class); + Mockito.when(ravService.addVpnUser(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString())).thenReturn(null); + + addVpnUserCmd._ravService = ravService; + + try { + addVpnUserCmd.create(); + } catch (ServerApiException exception) { + Assert.assertEquals("Failed to add vpn user", exception.getDescription()); + } + + } + +}
