You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ki...@apache.org on 2013/02/01 07:32:03 UTC
[36/40] git commit: refs/heads/master - Added unit tests
Added unit tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/e7341313
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/e7341313
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/e7341313
Branch: refs/heads/master
Commit: e7341313e999a64f6b221453be82f1561bce082d
Parents: c17c700
Author: Kishan Kavala <ki...@cloud.com>
Authored: Thu Jan 31 22:28:09 2013 +0530
Committer: Kishan Kavala <ki...@cloud.com>
Committed: Thu Jan 31 22:28:09 2013 +0530
----------------------------------------------------------------------
.../api/command/admin/domain/DeleteDomainCmd.java | 7 +-
.../cloudstack/api/command/test/RegionCmdTest.java | 104 +++++++++++++++
.../cloudstack/region/RegionManagerImpl.java | 4 +-
.../cloudstack/region/RegionServiceImpl.java | 3 +-
.../cloudstack/region/RegionManagerTest.java | 74 ++++++++++
5 files changed, 184 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e7341313/api/src/org/apache/cloudstack/api/command/admin/domain/DeleteDomainCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/admin/domain/DeleteDomainCmd.java b/api/src/org/apache/cloudstack/api/command/admin/domain/DeleteDomainCmd.java
index b013391..39250fd 100644
--- a/api/src/org/apache/cloudstack/api/command/admin/domain/DeleteDomainCmd.java
+++ b/api/src/org/apache/cloudstack/api/command/admin/domain/DeleteDomainCmd.java
@@ -44,7 +44,7 @@ public class DeleteDomainCmd extends BaseAsyncCmd {
private Boolean cleanup;
@Parameter(name=ApiConstants.IS_PROPAGATE, type=CommandType.BOOLEAN, description="True if command is sent from another Region")
- private Boolean isPropagate;
+ private Boolean propagate;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
@@ -58,8 +58,8 @@ public class DeleteDomainCmd extends BaseAsyncCmd {
return cleanup;
}
- public Boolean getIsPropagate() {
- return isPropagate;
+ public Boolean isPropagate() {
+ return propagate;
}
/////////////////////////////////////////////////////
@@ -94,7 +94,6 @@ public class DeleteDomainCmd extends BaseAsyncCmd {
@Override
public void execute(){
UserContext.current().setEventDetails("Domain Id: "+getId());
- boolean isPopagate = (getIsPropagate() != null ) ? getIsPropagate() : false;
boolean result = _regionService.deleteDomain(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e7341313/api/test/org/apache/cloudstack/api/command/test/RegionCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/org/apache/cloudstack/api/command/test/RegionCmdTest.java b/api/test/org/apache/cloudstack/api/command/test/RegionCmdTest.java
new file mode 100644
index 0000000..01cd33b
--- /dev/null
+++ b/api/test/org/apache/cloudstack/api/command/test/RegionCmdTest.java
@@ -0,0 +1,104 @@
+// 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 org.apache.cloudstack.api.command.test;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.cloudstack.api.ResponseGenerator;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.command.admin.region.AddRegionCmd;
+import org.apache.cloudstack.api.response.RegionResponse;
+import org.apache.cloudstack.region.Region;
+import org.apache.cloudstack.region.RegionService;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+public class RegionCmdTest extends TestCase {
+
+ private AddRegionCmd addRegionCmd;
+ private ResponseGenerator responseGenerator;
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Before
+ public void setUp() {
+
+ addRegionCmd = new AddRegionCmd() {
+
+ @Override
+ public Integer getId() {
+ return 2;
+ }
+
+ @Override
+ public String getRegionName() {
+ return "APAC";
+ }
+
+ };
+ }
+
+ @Test
+ public void testCreateSuccess() {
+
+ RegionService regionService = Mockito.mock(RegionService.class);
+
+ Region region = Mockito.mock(Region.class);
+ Mockito.when(
+ regionService.addRegion(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
+ .thenReturn(region);
+
+ addRegionCmd._regionService = regionService;
+ responseGenerator = Mockito.mock(ResponseGenerator.class);
+
+ RegionResponse regionResponse = Mockito.mock(RegionResponse.class);
+
+ Mockito.when(responseGenerator.createRegionResponse(region)).thenReturn(
+ regionResponse);
+
+ addRegionCmd._responseGenerator = responseGenerator;
+ addRegionCmd.execute();
+
+ }
+
+ @Test
+ public void testCreateFailure() {
+
+ RegionService regionService = Mockito.mock(RegionService.class);
+
+ Region region = Mockito.mock(Region.class);
+ Mockito.when(
+ regionService.addRegion(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
+ .thenReturn(null);
+
+ addRegionCmd._regionService = regionService;
+
+ try {
+ addRegionCmd.execute();
+ } catch (ServerApiException exception) {
+ Assert.assertEquals("Failed to add Region",
+ exception.getDescription());
+ }
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e7341313/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/region/RegionManagerImpl.java b/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
index d93ab10..bef2325 100755
--- a/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
+++ b/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
@@ -58,9 +58,9 @@ public class RegionManagerImpl implements RegionManager, Manager{
public static final Logger s_logger = Logger.getLogger(RegionManagerImpl.class);
@Inject
- private RegionDao _regionDao;
+ RegionDao _regionDao;
@Inject
- private AccountDao _accountDao;
+ AccountDao _accountDao;
@Inject
private AccountManager _accountMgr;
@Inject
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e7341313/server/src/org/apache/cloudstack/region/RegionServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/region/RegionServiceImpl.java b/server/src/org/apache/cloudstack/region/RegionServiceImpl.java
index db592ad..f5a0a80 100755
--- a/server/src/org/apache/cloudstack/region/RegionServiceImpl.java
+++ b/server/src/org/apache/cloudstack/region/RegionServiceImpl.java
@@ -39,7 +39,6 @@ import org.apache.log4j.Logger;
import com.cloud.domain.Domain;
import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.ConcurrentOperationException;
-import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
@@ -224,7 +223,7 @@ public class RegionServiceImpl implements RegionService, Manager {
@Override
public boolean deleteDomain(DeleteDomainCmd cmd) {
boolean result = false;
- if(checkIsPropagate(cmd.getIsPropagate())){
+ if(checkIsPropagate(cmd.isPropagate())){
result = _domainMgr.deleteDomain(cmd.getId(), cmd.getCleanup());
} else {
result = _regionMgr.deleteDomain(cmd.getId(), cmd.getCleanup());
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e7341313/server/test/org/apache/cloudstack/region/RegionManagerTest.java
----------------------------------------------------------------------
diff --git a/server/test/org/apache/cloudstack/region/RegionManagerTest.java b/server/test/org/apache/cloudstack/region/RegionManagerTest.java
new file mode 100644
index 0000000..330f0b4
--- /dev/null
+++ b/server/test/org/apache/cloudstack/region/RegionManagerTest.java
@@ -0,0 +1,74 @@
+// 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 org.apache.cloudstack.region;
+
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.cloudstack.api.command.admin.domain.DeleteDomainCmd;
+import org.apache.cloudstack.region.dao.RegionDao;
+import org.apache.log4j.Logger;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+import com.cloud.user.dao.AccountDao;
+
+
+
+public class RegionManagerTest extends TestCase {
+ private static final Logger s_logger = Logger.getLogger(RegionManagerTest.class);
+
+ @Before
+ @Override
+ protected void setUp() {
+
+ }
+
+ @Test
+ public void testUniqueName() {
+ RegionManagerImpl regionMgr = new RegionManagerImpl();
+ RegionDao regionDao = Mockito.mock(RegionDao.class);
+ RegionVO region = new RegionVO(2, "APAC", "", null, null);
+ Mockito.when(regionDao.findByName(Mockito.anyString())).thenReturn(region);
+ regionMgr._regionDao = regionDao;
+ try {
+ regionMgr.addRegion(2, "APAC", "", null, null);
+ } catch (InvalidParameterValueException e){
+ Assert.assertEquals("Region with name: APAC already exists", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testUserDelete() {
+ RegionManagerImpl regionMgr = new RegionManagerImpl();
+ AccountDao accountDao = Mockito.mock(AccountDao.class);
+ Mockito.when(accountDao.findById(Mockito.anyLong())).thenReturn(null);
+ regionMgr._accountDao = accountDao;
+ try {
+ regionMgr.deleteUserAccount(5);
+ } catch (InvalidParameterValueException e){
+ Assert.assertEquals("The specified account does not exist in the system", e.getMessage());
+ }
+ }
+
+}