Skip to content

Commit a71c985

Browse files
committed
findbugs: added test for getServiceProvider of CreateVpcOffering api
Signed-off-by: Daan Hoogland <daan.hoogland@gmail.com> This closes apache#538
1 parent e34389a commit a71c985

3 files changed

Lines changed: 71 additions & 19 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.apache.cloudstack.api;
2+
3+
import java.lang.reflect.Field;
4+
5+
public class ApiCmdTestUtil {
6+
public static void set(BaseCmd cmd, String fieldName, Object value)
7+
throws IllegalArgumentException, IllegalAccessException {
8+
for (Field field : cmd.getClass().getDeclaredFields()) {
9+
Parameter parameter = field.getAnnotation(Parameter.class);
10+
if (parameter != null && fieldName.equals(parameter.name())) {
11+
field.setAccessible(true);
12+
field.set(cmd, value);
13+
}
14+
}
15+
}
16+
17+
}

‎api/test/org/apache/cloudstack/api/command/admin/storage/CreateSecondaryStagingStoreCmdTest.java‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,15 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.admin.storage;
1818

19-
import java.lang.reflect.Field;
2019
import java.util.HashMap;
2120
import java.util.Map;
2221

23-
import junit.framework.Assert;
24-
25-
import org.apache.cloudstack.api.BaseCmd;
26-
import org.apache.cloudstack.api.Parameter;
22+
import org.junit.Assert;
2723
import org.junit.Test;
2824

29-
public class CreateSecondaryStagingStoreCmdTest {
25+
import org.apache.cloudstack.api.ApiCmdTestUtil;
3026

31-
static void set(BaseCmd cmd, String fieldName, Object value)
32-
throws IllegalArgumentException, IllegalAccessException {
33-
for (Field field : cmd.getClass().getDeclaredFields()) {
34-
Parameter parameter = field.getAnnotation(Parameter.class);
35-
if (parameter != null && fieldName.equals(parameter.name())) {
36-
field.setAccessible(true);
37-
field.set(cmd, value);
38-
}
39-
}
40-
}
27+
public class CreateSecondaryStagingStoreCmdTest {
4128

4229
@Test
4330
public void getDetails() throws IllegalArgumentException,
@@ -48,7 +35,7 @@ public void getDetails() throws IllegalArgumentException,
4835
kv.put("key", "TEST-KEY");
4936
kv.put("value", "TEST-VALUE");
5037
details.put("does not matter", kv);
51-
set(cmd, "details", details);
38+
ApiCmdTestUtil.set(cmd, "details", details);
5239
Map<String, String> detailsMap = cmd.getDetails();
5340
Assert.assertNotNull(detailsMap);
5441
Assert.assertEquals(1, detailsMap.size());
@@ -60,15 +47,15 @@ public void getDetails() throws IllegalArgumentException,
6047
public void getDetailsEmpty() throws IllegalArgumentException,
6148
IllegalAccessException {
6249
CreateSecondaryStagingStoreCmd cmd = new CreateSecondaryStagingStoreCmd();
63-
set(cmd, "details", new HashMap<String, Map<String, String>>());
50+
ApiCmdTestUtil.set(cmd, "details", new HashMap<String, Map<String, String>>());
6451
Assert.assertNull(cmd.getDetails());
6552
}
6653

6754
@Test
6855
public void getDetailsNull() throws IllegalArgumentException,
6956
IllegalAccessException {
7057
CreateSecondaryStagingStoreCmd cmd = new CreateSecondaryStagingStoreCmd();
71-
set(cmd, "details", null);
58+
ApiCmdTestUtil.set(cmd, "details", null);
7259
Assert.assertNull(cmd.getDetails());
7360
}
7461

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.apache.cloudstack.api.command.admin.vpc;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import org.apache.cloudstack.api.ApiCmdTestUtil;
11+
import org.apache.cloudstack.api.ApiConstants;
12+
13+
public class CreateVPCOfferingCmdTest {
14+
15+
@Test
16+
public void testServiceProviders() throws IllegalArgumentException,
17+
IllegalAccessException {
18+
CreateVPCOfferingCmd cmd = new CreateVPCOfferingCmd();
19+
HashMap<String, Map<String, String>> providers = new HashMap<String, Map<String, String>>();
20+
HashMap<String, String> kv = new HashMap<String, String>();
21+
kv.put("service", "TEST-SERVICE");
22+
kv.put("provider", "TEST-PROVIDER");
23+
providers.put("does not matter", kv);
24+
ApiCmdTestUtil.set(cmd, ApiConstants.SERVICE_PROVIDER_LIST, providers);
25+
Map<String, List<String>> providerMap = cmd.getServiceProviders();
26+
Assert.assertNotNull(providerMap);
27+
Assert.assertEquals(1, providerMap.size());
28+
Assert.assertTrue(providerMap.containsKey("TEST-SERVICE"));
29+
Assert.assertTrue(providerMap.get("TEST-SERVICE").contains("TEST-PROVIDER"));
30+
}
31+
32+
@Test
33+
public void testServiceProvidersEmpty() throws IllegalArgumentException,
34+
IllegalAccessException {
35+
CreateVPCOfferingCmd cmd = new CreateVPCOfferingCmd();
36+
ApiCmdTestUtil.set(cmd, ApiConstants.SERVICE_PROVIDER_LIST, new HashMap<String, Map<String, String>>());
37+
Assert.assertNull(cmd.getServiceProviders());
38+
}
39+
40+
@Test
41+
public void getDetailsNull() throws IllegalArgumentException,
42+
IllegalAccessException {
43+
CreateVPCOfferingCmd cmd = new CreateVPCOfferingCmd();
44+
ApiCmdTestUtil.set(cmd, ApiConstants.SERVICE_PROVIDER_LIST, null);
45+
Assert.assertNull(cmd.getServiceProviders());
46+
}
47+
48+
}

0 commit comments

Comments
 (0)