mirror of https://github.com/apache/cloudstack.git
findbugs: added test for getServiceProvider of CreateVpcOffering api
Signed-off-by: Daan Hoogland <daan.hoogland@gmail.com> This closes #538
This commit is contained in:
parent
e34389a612
commit
a71c985223
|
|
@ -0,0 +1,17 @@
|
|||
package org.apache.cloudstack.api;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class ApiCmdTestUtil {
|
||||
public static void set(BaseCmd cmd, String fieldName, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
for (Field field : cmd.getClass().getDeclaredFields()) {
|
||||
Parameter parameter = field.getAnnotation(Parameter.class);
|
||||
if (parameter != null && fieldName.equals(parameter.name())) {
|
||||
field.setAccessible(true);
|
||||
field.set(cmd, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,28 +16,15 @@
|
|||
// under the License.
|
||||
package org.apache.cloudstack.api.command.admin.storage;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.cloudstack.api.BaseCmd;
|
||||
import org.apache.cloudstack.api.Parameter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CreateSecondaryStagingStoreCmdTest {
|
||||
import org.apache.cloudstack.api.ApiCmdTestUtil;
|
||||
|
||||
static void set(BaseCmd cmd, String fieldName, Object value)
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
for (Field field : cmd.getClass().getDeclaredFields()) {
|
||||
Parameter parameter = field.getAnnotation(Parameter.class);
|
||||
if (parameter != null && fieldName.equals(parameter.name())) {
|
||||
field.setAccessible(true);
|
||||
field.set(cmd, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
public class CreateSecondaryStagingStoreCmdTest {
|
||||
|
||||
@Test
|
||||
public void getDetails() throws IllegalArgumentException,
|
||||
|
|
@ -48,7 +35,7 @@ public class CreateSecondaryStagingStoreCmdTest {
|
|||
kv.put("key", "TEST-KEY");
|
||||
kv.put("value", "TEST-VALUE");
|
||||
details.put("does not matter", kv);
|
||||
set(cmd, "details", details);
|
||||
ApiCmdTestUtil.set(cmd, "details", details);
|
||||
Map<String, String> detailsMap = cmd.getDetails();
|
||||
Assert.assertNotNull(detailsMap);
|
||||
Assert.assertEquals(1, detailsMap.size());
|
||||
|
|
@ -60,7 +47,7 @@ public class CreateSecondaryStagingStoreCmdTest {
|
|||
public void getDetailsEmpty() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
CreateSecondaryStagingStoreCmd cmd = new CreateSecondaryStagingStoreCmd();
|
||||
set(cmd, "details", new HashMap<String, Map<String, String>>());
|
||||
ApiCmdTestUtil.set(cmd, "details", new HashMap<String, Map<String, String>>());
|
||||
Assert.assertNull(cmd.getDetails());
|
||||
}
|
||||
|
||||
|
|
@ -68,7 +55,7 @@ public class CreateSecondaryStagingStoreCmdTest {
|
|||
public void getDetailsNull() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
CreateSecondaryStagingStoreCmd cmd = new CreateSecondaryStagingStoreCmd();
|
||||
set(cmd, "details", null);
|
||||
ApiCmdTestUtil.set(cmd, "details", null);
|
||||
Assert.assertNull(cmd.getDetails());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
package org.apache.cloudstack.api.command.admin.vpc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.cloudstack.api.ApiCmdTestUtil;
|
||||
import org.apache.cloudstack.api.ApiConstants;
|
||||
|
||||
public class CreateVPCOfferingCmdTest {
|
||||
|
||||
@Test
|
||||
public void testServiceProviders() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
CreateVPCOfferingCmd cmd = new CreateVPCOfferingCmd();
|
||||
HashMap<String, Map<String, String>> providers = new HashMap<String, Map<String, String>>();
|
||||
HashMap<String, String> kv = new HashMap<String, String>();
|
||||
kv.put("service", "TEST-SERVICE");
|
||||
kv.put("provider", "TEST-PROVIDER");
|
||||
providers.put("does not matter", kv);
|
||||
ApiCmdTestUtil.set(cmd, ApiConstants.SERVICE_PROVIDER_LIST, providers);
|
||||
Map<String, List<String>> providerMap = cmd.getServiceProviders();
|
||||
Assert.assertNotNull(providerMap);
|
||||
Assert.assertEquals(1, providerMap.size());
|
||||
Assert.assertTrue(providerMap.containsKey("TEST-SERVICE"));
|
||||
Assert.assertTrue(providerMap.get("TEST-SERVICE").contains("TEST-PROVIDER"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceProvidersEmpty() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
CreateVPCOfferingCmd cmd = new CreateVPCOfferingCmd();
|
||||
ApiCmdTestUtil.set(cmd, ApiConstants.SERVICE_PROVIDER_LIST, new HashMap<String, Map<String, String>>());
|
||||
Assert.assertNull(cmd.getServiceProviders());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDetailsNull() throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
CreateVPCOfferingCmd cmd = new CreateVPCOfferingCmd();
|
||||
ApiCmdTestUtil.set(cmd, ApiConstants.SERVICE_PROVIDER_LIST, null);
|
||||
Assert.assertNull(cmd.getServiceProviders());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue