Skip to content

Commit e734131

Browse files
author
Kishan Kavala
committed
Added unit tests
1 parent c17c700 commit e734131

5 files changed

Lines changed: 184 additions & 8 deletions

File tree

api/src/org/apache/cloudstack/api/command/admin/domain/DeleteDomainCmd.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class DeleteDomainCmd extends BaseAsyncCmd {
4444
private Boolean cleanup;
4545

4646
@Parameter(name=ApiConstants.IS_PROPAGATE, type=CommandType.BOOLEAN, description="True if command is sent from another Region")
47-
private Boolean isPropagate;
47+
private Boolean propagate;
4848

4949
/////////////////////////////////////////////////////
5050
/////////////////// Accessors ///////////////////////
@@ -58,8 +58,8 @@ public Boolean getCleanup() {
5858
return cleanup;
5959
}
6060

61-
public Boolean getIsPropagate() {
62-
return isPropagate;
61+
public Boolean isPropagate() {
62+
return propagate;
6363
}
6464

6565
/////////////////////////////////////////////////////
@@ -94,7 +94,6 @@ public String getEventDescription() {
9494
@Override
9595
public void execute(){
9696
UserContext.current().setEventDetails("Domain Id: "+getId());
97-
boolean isPopagate = (getIsPropagate() != null ) ? getIsPropagate() : false;
9897
boolean result = _regionService.deleteDomain(this);
9998
if (result) {
10099
SuccessResponse response = new SuccessResponse(getCommandName());
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.test;
18+
19+
import junit.framework.Assert;
20+
import junit.framework.TestCase;
21+
22+
import org.apache.cloudstack.api.ResponseGenerator;
23+
import org.apache.cloudstack.api.ServerApiException;
24+
import org.apache.cloudstack.api.command.admin.region.AddRegionCmd;
25+
import org.apache.cloudstack.api.response.RegionResponse;
26+
import org.apache.cloudstack.region.Region;
27+
import org.apache.cloudstack.region.RegionService;
28+
import org.junit.Before;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.ExpectedException;
32+
import org.mockito.Mockito;
33+
34+
public class RegionCmdTest extends TestCase {
35+
36+
private AddRegionCmd addRegionCmd;
37+
private ResponseGenerator responseGenerator;
38+
39+
@Rule
40+
public ExpectedException expectedException = ExpectedException.none();
41+
42+
@Before
43+
public void setUp() {
44+
45+
addRegionCmd = new AddRegionCmd() {
46+
47+
@Override
48+
public Integer getId() {
49+
return 2;
50+
}
51+
52+
@Override
53+
public String getRegionName() {
54+
return "APAC";
55+
}
56+
57+
};
58+
}
59+
60+
@Test
61+
public void testCreateSuccess() {
62+
63+
RegionService regionService = Mockito.mock(RegionService.class);
64+
65+
Region region = Mockito.mock(Region.class);
66+
Mockito.when(
67+
regionService.addRegion(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
68+
.thenReturn(region);
69+
70+
addRegionCmd._regionService = regionService;
71+
responseGenerator = Mockito.mock(ResponseGenerator.class);
72+
73+
RegionResponse regionResponse = Mockito.mock(RegionResponse.class);
74+
75+
Mockito.when(responseGenerator.createRegionResponse(region)).thenReturn(
76+
regionResponse);
77+
78+
addRegionCmd._responseGenerator = responseGenerator;
79+
addRegionCmd.execute();
80+
81+
}
82+
83+
@Test
84+
public void testCreateFailure() {
85+
86+
RegionService regionService = Mockito.mock(RegionService.class);
87+
88+
Region region = Mockito.mock(Region.class);
89+
Mockito.when(
90+
regionService.addRegion(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
91+
.thenReturn(null);
92+
93+
addRegionCmd._regionService = regionService;
94+
95+
try {
96+
addRegionCmd.execute();
97+
} catch (ServerApiException exception) {
98+
Assert.assertEquals("Failed to add Region",
99+
exception.getDescription());
100+
}
101+
102+
}
103+
104+
}

server/src/org/apache/cloudstack/region/RegionManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public class RegionManagerImpl implements RegionManager, Manager{
5858
public static final Logger s_logger = Logger.getLogger(RegionManagerImpl.class);
5959

6060
@Inject
61-
private RegionDao _regionDao;
61+
RegionDao _regionDao;
6262
@Inject
63-
private AccountDao _accountDao;
63+
AccountDao _accountDao;
6464
@Inject
6565
private AccountManager _accountMgr;
6666
@Inject

server/src/org/apache/cloudstack/region/RegionServiceImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.cloud.domain.Domain;
4040
import com.cloud.domain.dao.DomainDao;
4141
import com.cloud.exception.ConcurrentOperationException;
42-
import com.cloud.exception.InvalidParameterValueException;
4342
import com.cloud.exception.PermissionDeniedException;
4443
import com.cloud.exception.ResourceUnavailableException;
4544
import com.cloud.user.Account;
@@ -224,7 +223,7 @@ public Domain updateDomain(UpdateDomainCmd cmd) {
224223
@Override
225224
public boolean deleteDomain(DeleteDomainCmd cmd) {
226225
boolean result = false;
227-
if(checkIsPropagate(cmd.getIsPropagate())){
226+
if(checkIsPropagate(cmd.isPropagate())){
228227
result = _domainMgr.deleteDomain(cmd.getId(), cmd.getCleanup());
229228
} else {
230229
result = _regionMgr.deleteDomain(cmd.getId(), cmd.getCleanup());
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
18+
package org.apache.cloudstack.region;
19+
20+
21+
import junit.framework.Assert;
22+
import junit.framework.TestCase;
23+
24+
import org.apache.cloudstack.api.command.admin.domain.DeleteDomainCmd;
25+
import org.apache.cloudstack.region.dao.RegionDao;
26+
import org.apache.log4j.Logger;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.mockito.Mockito;
30+
31+
import com.cloud.exception.InvalidParameterValueException;
32+
import com.cloud.user.Account;
33+
import com.cloud.user.UserContext;
34+
import com.cloud.user.dao.AccountDao;
35+
36+
37+
38+
public class RegionManagerTest extends TestCase {
39+
private static final Logger s_logger = Logger.getLogger(RegionManagerTest.class);
40+
41+
@Before
42+
@Override
43+
protected void setUp() {
44+
45+
}
46+
47+
@Test
48+
public void testUniqueName() {
49+
RegionManagerImpl regionMgr = new RegionManagerImpl();
50+
RegionDao regionDao = Mockito.mock(RegionDao.class);
51+
RegionVO region = new RegionVO(2, "APAC", "", null, null);
52+
Mockito.when(regionDao.findByName(Mockito.anyString())).thenReturn(region);
53+
regionMgr._regionDao = regionDao;
54+
try {
55+
regionMgr.addRegion(2, "APAC", "", null, null);
56+
} catch (InvalidParameterValueException e){
57+
Assert.assertEquals("Region with name: APAC already exists", e.getMessage());
58+
}
59+
}
60+
61+
@Test
62+
public void testUserDelete() {
63+
RegionManagerImpl regionMgr = new RegionManagerImpl();
64+
AccountDao accountDao = Mockito.mock(AccountDao.class);
65+
Mockito.when(accountDao.findById(Mockito.anyLong())).thenReturn(null);
66+
regionMgr._accountDao = accountDao;
67+
try {
68+
regionMgr.deleteUserAccount(5);
69+
} catch (InvalidParameterValueException e){
70+
Assert.assertEquals("The specified account does not exist in the system", e.getMessage());
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)