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.net.URI;
import java.util.Set; 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.Account;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.date.internal.SimpleDateFormatDateService;
@ -39,7 +41,7 @@ import com.google.common.collect.ImmutableSet;
* @author Andrei Savu * @author Andrei Savu
*/ */
@Test(groups = "unit", testName = "AccountClientExpectTest") @Test(groups = "unit", testName = "AccountClientExpectTest")
public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest { public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest<AccountClient> {
public void testListAccountsWhenResponseIs2xx() { public void testListAccountsWhenResponseIs2xx() {
@ -57,8 +59,7 @@ public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/listaccountsresponse.json")) .payload(payloadFromResource("/listaccountsresponse.json"))
.build()) .build());
.getAccountClient();
Set<User> users = ImmutableSet.of( Set<User> users = ImmutableSet.of(
User.builder() User.builder()
@ -106,4 +107,9 @@ public class AccountClientExpectTest extends BaseCloudStackRestClientExpectTest
.state(Account.State.ENABLED) .state(Account.State.ENABLED)
.users(users).build())); .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; 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 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 * Base class for writing CloudStack Rest Client Expect tests
* *
* @author Andrei Savu * @author Andrei Savu
*/ */
public class BaseCloudStackRestClientExpectTest extends BaseRestClientExpectTest<CloudStackClient> { public abstract class BaseCloudStackRestClientExpectTest<S> extends BaseRestClientExpectTest<S> {
public BaseCloudStackRestClientExpectTest() { public BaseCloudStackRestClientExpectTest() {
provider = "cloudstack"; 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; package org.jclouds.cloudstack.features;
import com.google.common.collect.ImmutableMultimap; import static org.testng.Assert.assertEquals;
import com.google.common.collect.ImmutableSet; import static org.testng.Assert.assertNull;
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 java.net.URI; import java.net.URI;
import java.util.Set; import java.util.Set;
import static org.testng.Assert.assertEquals; import org.jclouds.cloudstack.CloudStackClient;
import static org.testng.Assert.assertNull; 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 * Test the CloudStack FirewallClient
@ -42,7 +42,7 @@ import static org.testng.Assert.assertNull;
* @author Andrei Savu * @author Andrei Savu
*/ */
@Test(groups = "unit", testName = "FirewallClientExpectTest") @Test(groups = "unit", testName = "FirewallClientExpectTest")
public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest { public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest<FirewallClient> {
public void testListFirewallRulesWhenResponseIs2xx() { public void testListFirewallRulesWhenResponseIs2xx() {
FirewallClient client = requestSendsResponse( FirewallClient client = requestSendsResponse(
@ -59,8 +59,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/listfirewallrulesresponse.json")) .payload(payloadFromResource("/listfirewallrulesresponse.json"))
.build()) .build());
.getFirewallClient();
Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0"); Set<String> CIDRs = ImmutableSet.of("0.0.0.0/0");
assertEquals(client.listFirewallRules(), assertEquals(client.listFirewallRules(),
@ -91,8 +90,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build(), .build(),
HttpResponse.builder() HttpResponse.builder()
.statusCode(404) .statusCode(404)
.build()) .build());
.getFirewallClient();
assertEquals(client.listFirewallRules(), ImmutableSet.of()); assertEquals(client.listFirewallRules(), ImmutableSet.of());
} }
@ -112,8 +110,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/getfirewallrulesresponse.json")) .payload(payloadFromResource("/getfirewallrulesresponse.json"))
.build()) .build());
.getFirewallClient();
assertEquals(client.getFirewallRule(2017), assertEquals(client.getFirewallRule(2017),
FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30) FirewallRule.builder().id(2017).protocol(FirewallRule.Protocol.TCP).startPort(30)
@ -136,8 +133,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
.build(), .build(),
HttpResponse.builder() HttpResponse.builder()
.statusCode(404) .statusCode(404)
.build()) .build());
.getFirewallClient();
assertNull(client.getFirewallRule(4)); assertNull(client.getFirewallRule(4));
} }
@ -157,8 +153,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/createfirewallrulesresponse.json")) .payload(payloadFromResource("/createfirewallrulesresponse.json"))
.build()) .build());
.getFirewallClient();
AsyncCreateResponse response = client.createFirewallRuleForIpAndProtocol(2, FirewallRule.Protocol.TCP); AsyncCreateResponse response = client.createFirewallRuleForIpAndProtocol(2, FirewallRule.Protocol.TCP);
assertEquals(response.getJobId(), 2036); assertEquals(response.getJobId(), 2036);
@ -176,8 +171,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/deletefirewallrulesresponse.json")) .payload(payloadFromResource("/deletefirewallrulesresponse.json"))
.build()) .build());
.getFirewallClient();
client.deleteFirewallRule(2015); client.deleteFirewallRule(2015);
} }
@ -197,8 +191,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/listportforwardingrulesresponse.json")) .payload(payloadFromResource("/listportforwardingrulesresponse.json"))
.build()) .build());
.getFirewallClient();
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); 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(), .build(),
HttpResponse.builder() HttpResponse.builder()
.statusCode(404) .statusCode(404)
.build()) .build());
.getFirewallClient();
assertEquals(client.listPortForwardingRules(), ImmutableSet.of()); assertEquals(client.listPortForwardingRules(), ImmutableSet.of());
} }
@ -248,8 +240,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/getportforwardingrulesresponse.json")) .payload(payloadFromResource("/getportforwardingrulesresponse.json"))
.build()) .build());
.getFirewallClient();
Set<String> cidrs = ImmutableSet.of("0.0.0.0/1", "128.0.0.0/1"); 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(), .build(),
HttpResponse.builder() HttpResponse.builder()
.statusCode(404) .statusCode(404)
.build()) .build());
.getFirewallClient();
assertNull(client.getPortForwardingRule(4)); assertNull(client.getPortForwardingRule(4));
} }
@ -295,8 +285,7 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/createportforwardingrulesresponse.json")) .payload(payloadFromResource("/createportforwardingrulesresponse.json"))
.build()) .build());
.getFirewallClient();
AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine( AsyncCreateResponse response = client.createPortForwardingRuleForVirtualMachine(
2, PortForwardingRule.Protocol.TCP, 22, 1234, 22); 2, PortForwardingRule.Protocol.TCP, 22, 1234, 22);
@ -315,9 +304,13 @@ public class FirewallClientExpectTest extends BaseCloudStackRestClientExpectTest
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.payload(payloadFromResource("/deleteportforwardingrulesresponse.json")) .payload(payloadFromResource("/deleteportforwardingrulesresponse.json"))
.build()) .build());
.getFirewallClient();
client.deletePortForwardingRule(2015); client.deletePortForwardingRule(2015);
} }
@Override
protected FirewallClient clientFrom(CloudStackContext context) {
return CloudStackClient.class.cast(context.getProviderSpecificContext().getApi()).getFirewallClient();
}
} }