diff --git a/api/test/org/apache/cloudstack/api/ApiCmdTestUtil.java b/api/test/org/apache/cloudstack/api/ApiCmdTestUtil.java new file mode 100644 index 00000000000..ca4510dd37d --- /dev/null +++ b/api/test/org/apache/cloudstack/api/ApiCmdTestUtil.java @@ -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); + } + } + } + +} diff --git a/api/test/org/apache/cloudstack/api/command/admin/storage/CreateSecondaryStagingStoreCmdTest.java b/api/test/org/apache/cloudstack/api/command/admin/storage/CreateSecondaryStagingStoreCmdTest.java index 901e9761a2e..e9358429586 100644 --- a/api/test/org/apache/cloudstack/api/command/admin/storage/CreateSecondaryStagingStoreCmdTest.java +++ b/api/test/org/apache/cloudstack/api/command/admin/storage/CreateSecondaryStagingStoreCmdTest.java @@ -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 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>()); + ApiCmdTestUtil.set(cmd, "details", new HashMap>()); 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()); } diff --git a/api/test/org/apache/cloudstack/api/command/admin/vpc/CreateVPCOfferingCmdTest.java b/api/test/org/apache/cloudstack/api/command/admin/vpc/CreateVPCOfferingCmdTest.java new file mode 100644 index 00000000000..99433ed60f6 --- /dev/null +++ b/api/test/org/apache/cloudstack/api/command/admin/vpc/CreateVPCOfferingCmdTest.java @@ -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> providers = new HashMap>(); + HashMap kv = new HashMap(); + 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> 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>()); + 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()); + } + +}