exposed CloudStackContext for tests

This commit is contained in:
Adrian Cole 2012-01-10 17:07:08 -08:00
parent 018771d3a4
commit 3db62b6f8b
3 changed files with 62 additions and 44 deletions

View File

@ -23,6 +23,8 @@ import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Set;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.User;
import org.jclouds.date.internal.SimpleDateFormatDateService;
@ -39,7 +41,7 @@ import com.google.common.collect.ImmutableSet;
* @author Andrei Savu
*/
@Test(groups = "unit", testName = "AccountClientExpectTest")
public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest {
public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest<AccountClient> {
public void testListAccountsWhenResponseIs2xx() {
@ -57,8 +59,7 @@ public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/listaccountsresponse.json"))
.build())
.getAccountClient();
.build());
Set<User> users = ImmutableSet.of(
User.builder()
@ -106,4 +107,9 @@ public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest
.state(Account.State.ENABLED)
.users(users).build()));
}
@Override
protected AccountClient clientFrom(CloudStackContext context) {
return CloudStackClient.class.cast(context.getProviderSpecificContext().getApi()).getAccountClient();
}
}

View File

@ -18,18 +18,37 @@
*/
package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.CloudStackClient;
import java.util.Properties;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.BaseRestClientExpectTest;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
/**
* Base class for writing CloudStack Rest Client Expect tests
*
*
* @author Andrei Savu
*/
public class BaseCloudStackRestClientExpectTest extends BaseRestClientExpectTest<CloudStackClient> {
public abstract class BaseCloudStackRestClientExpectTest<S> extends BaseRestClientExpectTest<S> {
public BaseCloudStackRestClientExpectTest() {
provider = "cloudstack";
}
@Override
public S createClient(Function<HttpRequest, HttpResponse> fn, Module module, Properties props) {
return clientFrom(CloudStackContext.class.cast(new ComputeServiceContextFactory(setupRestProperties())
.createContext(provider, "identity", "credential", ImmutableSet.<Module> of(new ExpectModule(fn),
new NullLoggingModule(), module), props)));
}
protected abstract S clientFrom(CloudStackContext context);
}

View File

@ -18,23 +18,23 @@
*/
package org.jclouds.cloudstack.features;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.cloudstack.domain.User;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import java.net.URI;
import java.util.Set;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.FirewallRule;
import org.jclouds.cloudstack.domain.PortForwardingRule;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
/**
* Test the CloudStack FirewallClient
@ -42,7 +42,7 @@ import static org.testng.Assert.assertNull;
* @author Andrei Savu
*/
@Test(groups = "unit", testName = "FirewallClientExpectTest")
public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest {
public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest<FirewallClient> {
public void testListFirewallRulesWhenResponseIs2xx() {
FirewallClient client = requestSendsResponse(
@ -59,8 +59,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/listfirewallrulesresponse.json"))
.build())
.getFirewallClient();
.build());
Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0");
assertEquals(client.listFirewallRules(),
@ -91,8 +90,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build(),
HttpResponse.builder()
.statusCode(404)
.build())
.getFirewallClient();
.build());
assertEquals(client.listFirewallRules(), ImmutableSet.of());
}
@ -112,8 +110,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/getfirewallrulesresponse.json"))
.build())
.getFirewallClient();
.build());
assertEquals(client.getFirewallRule(2017),
FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30)
@ -136,8 +133,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build(),
HttpResponse.builder()
.statusCode(404)
.build())
.getFirewallClient();
.build());
assertNull(client.getFirewallRule(4));
}
@ -157,8 +153,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/createfirewallrulesresponse.json"))
.build())
.getFirewallClient();
.build());
AsyncCreateResponse response = client.createFirewallRuleForIpAndProtocol(2, FirewallRule.Protocol.TCP);
assertEquals(response.getJobId(), 2036);
@ -176,8 +171,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/deletefirewallrulesresponse.json"))
.build())
.getFirewallClient();
.build());
client.deleteFirewallRule(2015);
}
@ -197,8 +191,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/listportforwardingrulesresponse.json"))
.build())
.getFirewallClient();
.build());
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1");
@ -227,8 +220,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build(),
HttpResponse.builder()
.statusCode(404)
.build())
.getFirewallClient();
.build());
assertEquals(client.listPortForwardingRules(), ImmutableSet.of());
}
@ -248,8 +240,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/getportforwardingrulesresponse.json"))
.build())
.getFirewallClient();
.build());
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1");
@ -273,8 +264,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build(),
HttpResponse.builder()
.statusCode(404)
.build())
.getFirewallClient();
.build());
assertNull(client.getPortForwardingRule(4));
}
@ -295,8 +285,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/createportforwardingrulesresponse.json"))
.build())
.getFirewallClient();
.build());
AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine(
2, PortForwardingRule.Protocol.TCP, 22, 1234, 22);
@ -315,9 +304,13 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/deleteportforwardingrulesresponse.json"))
.build())
.getFirewallClient();
.build());
client.deletePortForwardingRule(2015);
}
@Override
protected FirewallClient clientFrom(CloudStackContext context) {
return CloudStackClient.class.cast(context.getProviderSpecificContext().getApi()).getFirewallClient();
}
}