Skip to content

Commit 5280666

Browse files
committed
add more tests
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 534a56d commit 5280666

17 files changed

+2179
-1
lines changed

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/api/DeleteExtensionCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void execute() throws ServerApiException, ConcurrentOperationException {
8383
response.setSuccess(result);
8484
setResponseObject(response);
8585
} else {
86-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete userdata");
86+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete extension");
8787
}
8888
}
8989

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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.framework.extensions.api;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.mockito.Mockito.any;
25+
import static org.mockito.Mockito.doNothing;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.spy;
28+
import static org.mockito.Mockito.verify;
29+
import static org.mockito.Mockito.when;
30+
31+
import java.util.Arrays;
32+
import java.util.Collections;
33+
import java.util.HashMap;
34+
import java.util.List;
35+
import java.util.Map;
36+
37+
import org.apache.cloudstack.acl.RoleType;
38+
import org.apache.cloudstack.api.ApiCommandResourceType;
39+
import org.apache.cloudstack.api.response.ExtensionCustomActionResponse;
40+
import org.apache.cloudstack.extension.ExtensionCustomAction;
41+
import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager;
42+
import org.apache.commons.collections.MapUtils;
43+
import org.junit.Before;
44+
import org.junit.Test;
45+
import org.junit.runner.RunWith;
46+
import org.mockito.junit.MockitoJUnitRunner;
47+
import org.springframework.test.util.ReflectionTestUtils;
48+
49+
import com.cloud.user.Account;
50+
51+
@RunWith(MockitoJUnitRunner.class)
52+
public class AddCustomActionCmdTest {
53+
54+
private AddCustomActionCmd cmd;
55+
private ExtensionsManager extensionsManager;
56+
57+
@Before
58+
public void setUp() throws Exception {
59+
cmd = new AddCustomActionCmd();
60+
extensionsManager = mock(ExtensionsManager.class);
61+
cmd.extensionsManager = extensionsManager;
62+
}
63+
64+
private void setField(String fieldName, Object value) {
65+
ReflectionTestUtils.setField(cmd, fieldName, value);
66+
}
67+
68+
@Test
69+
public void testGetters() {
70+
Long extensionId = 42L;
71+
String name = "actionName";
72+
String description = "desc";
73+
String resourceType = "VM";
74+
List<String> allowedRoleTypes = Arrays.asList(RoleType.Admin.name(), RoleType.User.name());
75+
Map<String, Object> parameters = new HashMap<>();
76+
parameters.put("name", "param1");
77+
String successMessage = "Success!";
78+
String errorMessage = "Error!";
79+
Integer timeout = 10;
80+
Boolean enabled = true;
81+
Map<String, Map<String, String>> details = new HashMap<>();
82+
Map<String, String> inner = new HashMap<>();
83+
inner.put("vendor", "acme");
84+
details.put("details", inner);
85+
setField("details", details);
86+
87+
setField("extensionId", extensionId);
88+
setField("name", name);
89+
setField("description", description);
90+
setField("resourceType", resourceType);
91+
setField("allowedRoleTypes", allowedRoleTypes);
92+
setField("parameters", parameters);
93+
setField("successMessage", successMessage);
94+
setField("errorMessage", errorMessage);
95+
setField("timeout", timeout);
96+
setField("enabled", enabled);
97+
setField("details", details);
98+
99+
assertEquals(extensionId, cmd.getExtensionId());
100+
assertEquals(name, cmd.getName());
101+
assertEquals(description, cmd.getDescription());
102+
assertEquals(resourceType, cmd.getResourceType());
103+
assertEquals(allowedRoleTypes, cmd.getAllowedRoleTypes());
104+
assertEquals(parameters, cmd.getParametersMap());
105+
assertEquals(successMessage, cmd.getSuccessMessage());
106+
assertEquals(errorMessage, cmd.getErrorMessage());
107+
assertEquals(timeout, cmd.getTimeout());
108+
assertTrue(cmd.isEnabled());
109+
assertTrue(MapUtils.isNotEmpty(cmd.getDetails()));
110+
}
111+
112+
@Test
113+
public void testIsEnabledReturnsFalseWhenNull() {
114+
setField("enabled", null);
115+
assertFalse(cmd.isEnabled());
116+
}
117+
118+
@Test
119+
public void testIsEnabledReturnsFalseWhenFalse() {
120+
setField("enabled", Boolean.FALSE);
121+
assertFalse(cmd.isEnabled());
122+
}
123+
124+
@Test
125+
public void testIsEnabledReturnsTrueWhenTrue() {
126+
setField("enabled", Boolean.TRUE);
127+
assertTrue(cmd.isEnabled());
128+
}
129+
130+
@Test
131+
public void testGetAllowedRoleTypesReturnsNullWhenUnset() {
132+
setField("allowedRoleTypes", null);
133+
assertNull(cmd.getAllowedRoleTypes());
134+
}
135+
136+
@Test
137+
public void testGetAllowedRoleTypesReturnsEmptyList() {
138+
setField("allowedRoleTypes", Collections.emptyList());
139+
assertEquals(0, cmd.getAllowedRoleTypes().size());
140+
}
141+
142+
@Test
143+
public void testGetParametersMapReturnsNullWhenUnset() {
144+
setField("parameters", null);
145+
assertNull(cmd.getParametersMap());
146+
}
147+
148+
@Test
149+
public void testGetParametersMapReturnsMap() {
150+
Map<String, Object> parameters = new HashMap<>();
151+
parameters.put("foo", "bar");
152+
setField("parameters", parameters);
153+
assertEquals(parameters, cmd.getParametersMap());
154+
}
155+
156+
@Test
157+
public void testGetDetailsReturnsNullWhenUnset() {
158+
setField("details", null);
159+
assertTrue(MapUtils.isEmpty(cmd.getDetails()));
160+
}
161+
162+
@Test
163+
public void testGetDetailsReturnsMap() {
164+
Map<String, Map<String, String>> details = new HashMap<>();
165+
Map<String, String> inner = new HashMap<>();
166+
inner.put("key", "value");
167+
details.put("details", inner);
168+
setField("details", details);
169+
assertTrue(MapUtils.isNotEmpty(cmd.getDetails()));
170+
}
171+
172+
@Test
173+
public void testGetDescriptionReturnsNullWhenUnset() {
174+
setField("description", null);
175+
assertNull(cmd.getDescription());
176+
}
177+
178+
@Test
179+
public void testGetSuccessMessageReturnsNullWhenUnset() {
180+
setField("successMessage", null);
181+
assertNull(cmd.getSuccessMessage());
182+
}
183+
184+
@Test
185+
public void testGetErrorMessageReturnsNullWhenUnset() {
186+
setField("errorMessage", null);
187+
assertNull(cmd.getErrorMessage());
188+
}
189+
190+
@Test
191+
public void testGetTimeoutReturnsNullWhenUnset() {
192+
setField("timeout", null);
193+
assertNull(cmd.getTimeout());
194+
}
195+
196+
@Test
197+
public void testExecuteCallsExtensionsManagerAndSetsResponse() {
198+
ExtensionCustomAction extensionCustomAction = mock(ExtensionCustomAction.class);
199+
ExtensionCustomActionResponse response = mock(ExtensionCustomActionResponse.class);
200+
201+
when(extensionsManager.addCustomAction(any(AddCustomActionCmd.class))).thenReturn(extensionCustomAction);
202+
when(extensionsManager.createCustomActionResponse(extensionCustomAction)).thenReturn(response);
203+
204+
AddCustomActionCmd spyCmd = spy(cmd);
205+
doNothing().when(spyCmd).setResponseObject(any());
206+
207+
spyCmd.execute();
208+
209+
verify(extensionsManager).addCustomAction(spyCmd);
210+
verify(extensionsManager).createCustomActionResponse(extensionCustomAction);
211+
verify(response).setResponseName(spyCmd.getCommandName());
212+
verify(spyCmd).setResponseObject(response);
213+
}
214+
215+
@Test
216+
public void testGetEntityOwnerIdReturnsSystemAccount() {
217+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
218+
}
219+
220+
@Test
221+
public void testGetApiResourceTypeReturnsExtensionCustomAction() {
222+
assertEquals(ApiCommandResourceType.ExtensionCustomAction, cmd.getApiResourceType());
223+
}
224+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.framework.extensions.api;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.springframework.test.util.ReflectionTestUtils.setField;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import org.apache.commons.collections.MapUtils;
30+
import org.junit.Test;
31+
32+
public class CreateExtensionCmdTest {
33+
CreateExtensionCmd cmd = new CreateExtensionCmd();
34+
35+
@Test
36+
public void testGetNameReturnsNullWhenUnset() {
37+
assertNull(cmd.getName());
38+
}
39+
40+
@Test
41+
public void testGetNameReturnsValueWhenSet() {
42+
String name = "name";
43+
setField(cmd, "name", name);
44+
assertEquals(name, cmd.getName());
45+
}
46+
47+
@Test
48+
public void testGetTypeReturnsNullWhenUnset() {
49+
setField(cmd, "type", null);
50+
assertNull(cmd.getType());
51+
}
52+
53+
@Test
54+
public void testGetDescriptionReturnsValueWhenSet() {
55+
String description = "description";
56+
setField(cmd, "description", description);
57+
assertEquals(description, cmd.getDescription());
58+
}
59+
60+
@Test
61+
public void testGetEntryPointReturnsValueWhenSet() {
62+
String entryPoint = "/entry";
63+
setField(cmd, "entryPoint", entryPoint);
64+
assertEquals(entryPoint, cmd.getEntryPoint());
65+
}
66+
67+
@Test
68+
public void testGetStateReturnsNullWhenUnset() {
69+
setField(cmd, "state", null);
70+
assertNull(cmd.getState());
71+
}
72+
73+
@Test
74+
public void testIsOrchestratorRequiresPrepareVm() {
75+
assertNull(cmd.isOrchestratorRequiresPrepareVm());
76+
setField(cmd, "orchestratorRequiresPrepareVm", true);
77+
assertTrue(cmd.isOrchestratorRequiresPrepareVm());
78+
setField(cmd, "orchestratorRequiresPrepareVm", false);
79+
assertFalse(cmd.isOrchestratorRequiresPrepareVm());
80+
}
81+
82+
@Test
83+
public void testGetDetailsReturnsNullWhenUnset() {
84+
setField(cmd, "details", null);
85+
assertTrue(MapUtils.isEmpty(cmd.getDetails()));
86+
}
87+
88+
@Test
89+
public void testGetDetailsReturnsMap() {
90+
Map<String, Map<String, String>> details = new HashMap<>();
91+
Map<String, String> inner = new HashMap<>();
92+
inner.put("key", "value");
93+
details.put("details", inner);
94+
setField(cmd, "details", details);
95+
assertTrue(MapUtils.isNotEmpty(cmd.getDetails()));
96+
}
97+
}

0 commit comments

Comments
 (0)