Fix vCloud test usage of new ContextBuilder

This commit is contained in:
Andrew Donald Kennedy 2012-04-13 12:23:45 +01:00
parent c56c2d2f72
commit c0d781a11d
4 changed files with 113 additions and 117 deletions

View File

@ -133,7 +133,6 @@ public class ContextBuilder<S, A, C extends Closeable, M extends ApiMetadata<S,
}
}
@SuppressWarnings("unchecked")
public static <S, A, C extends Closeable, M extends ApiMetadata<S, A, C, M>> ContextBuilder<S, A, C, M> newBuilder(
ApiMetadata<S, A, C, M> apiMetadata) {
try {
@ -149,7 +148,6 @@ public class ContextBuilder<S, A, C extends Closeable, M extends ApiMetadata<S,
}
}
@SuppressWarnings("unchecked")
public static <S, A, C extends Closeable, M extends ApiMetadata<S, A, C, M>> ContextBuilder<S, A, C, M> newBuilder(
ProviderMetadata<S, A, C, M> providerMetadata) {
try {
@ -232,10 +230,8 @@ public class ContextBuilder<S, A, C extends Closeable, M extends ApiMetadata<S,
}
/**
*
* @throws NoSuchElementException
*/
public static String searchPropertiesForProviderScopedProperty(Properties overrides, String prov, String key,
String defaultVal) {
try {

View File

@ -49,13 +49,17 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
protected volatile C context;
protected String identity;
protected String credential;
protected String endpoint;
protected Properties setupProperties() {
Properties overrides = new Properties();
overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
setIfTestSystemPropertyPresent(overrides, provider + ".identity");
setIfTestSystemPropertyPresent(overrides, provider + ".credential");
setIfTestSystemPropertyPresent(overrides, provider + ".endpoint");
identity = setIfTestSystemPropertyPresent(overrides, provider + ".identity");
credential = setIfTestSystemPropertyPresent(overrides, provider + ".credential");
endpoint = setIfTestSystemPropertyPresent(overrides, provider + ".endpoint");
setIfTestSystemPropertyPresent(overrides, provider + ".api-version");
setIfTestSystemPropertyPresent(overrides, provider + ".build-version");
return overrides;
@ -65,6 +69,7 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
if (System.getProperties().containsKey("test." + key)) {
String val = System.getProperty("test." + key);
overrides.setProperty(key, val);
return val;
}
return null;
}
@ -91,7 +96,6 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
/**
* @see org.jclouds.providers.Providers#withId
*/
@SuppressWarnings("unchecked")
protected ProviderMetadata<?, ?, C, ?> createProviderMetadata() {
try {
return (ProviderMetadata<?, ?, C, ?>) Providers.withId(provider);
@ -103,7 +107,6 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
/**
* @see org.jclouds.apis.Apis#withId
*/
@SuppressWarnings("unchecked")
protected ApiMetadata<?, ?, C, ?> createApiMetadata() {
try {
return (ApiMetadata<?, ?, C, ?>) Apis.withId(provider);

View File

@ -51,6 +51,13 @@ import org.jclouds.vcloud.director.v1_5.domain.Link;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.ResourceEntity.Status;
import org.jclouds.vcloud.director.v1_5.domain.Role.DefaultRoles;
import org.jclouds.vcloud.director.v1_5.domain.RoleReferences;
import org.jclouds.vcloud.director.v1_5.domain.Session;
import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.User;
import org.jclouds.vcloud.director.v1_5.domain.VApp;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.domain.Vdc;
import org.jclouds.vcloud.director.v1_5.domain.network.Network;
import org.jclouds.vcloud.director.v1_5.domain.network.NetworkConfiguration;
import org.jclouds.vcloud.director.v1_5.domain.network.VAppNetworkConfiguration;
@ -59,13 +66,6 @@ import org.jclouds.vcloud.director.v1_5.domain.params.InstantiateVAppTemplatePar
import org.jclouds.vcloud.director.v1_5.domain.params.InstantiationParams;
import org.jclouds.vcloud.director.v1_5.domain.params.UndeployVAppParams;
import org.jclouds.vcloud.director.v1_5.domain.section.NetworkConfigSection;
import org.jclouds.vcloud.director.v1_5.domain.RoleReferences;
import org.jclouds.vcloud.director.v1_5.domain.Session;
import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.User;
import org.jclouds.vcloud.director.v1_5.domain.VApp;
import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate;
import org.jclouds.vcloud.director.v1_5.domain.Vdc;
import org.jclouds.vcloud.director.v1_5.features.TaskClient;
import org.jclouds.vcloud.director.v1_5.features.VAppClient;
import org.jclouds.vcloud.director.v1_5.features.VAppTemplateClient;
@ -75,7 +75,7 @@ import org.jclouds.vcloud.director.v1_5.predicates.TaskStatusEquals;
import org.jclouds.vcloud.director.v1_5.predicates.TaskSuccess;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@ -89,6 +89,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.Closeables;
import com.google.inject.Guice;
/**
@ -138,9 +139,9 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
protected DateService dateService;
private static VCloudDirectorTestSession testSession;
protected VCloudDirectorTestSession testSession;
private static String testStamp;
protected static String testStamp;
@BeforeClass(alwaysRun = true)
protected void setupDateService() {
@ -148,7 +149,7 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
assertNotNull(dateService);
}
// NOTE Implement as required to populate xxxClient fields, or NOP
/** Implement as required to populate xxxClient fields, or NOP */
protected abstract void setupRequiredClients();
@Inject
@ -160,33 +161,24 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
protected void initTaskSuccessLong(TaskSuccess taskSuccess) {
retryTaskSuccessLong = new RetryablePredicate<Task>(taskSuccess, LONG_TASK_TIMEOUT_SECONDS * 1000L);
}
//temporary until we marry up the test fixtures
protected String identity;
protected String credential;
protected String endpoint;
@Override
protected Properties setupProperties() {
Properties overrides = new Properties();
overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
identity = setIfTestSystemPropertyPresent(overrides, provider + ".identity");
credential = setIfTestSystemPropertyPresent(overrides, provider + ".credential");
endpoint = setIfTestSystemPropertyPresent(overrides, provider + ".endpoint");
setIfTestSystemPropertyPresent(overrides, provider + ".api-version");
setIfTestSystemPropertyPresent(overrides, provider + ".build-version");
return overrides;
}
@AfterSuite(alwaysRun = true)
@AfterClass(alwaysRun = true)
protected void tearDownTestSession() {
testSession.close();
Closeables.closeQuietly(testSession);
}
@BeforeClass(alwaysRun = true)
@Override
public void setupContext(){
protected void initializeContext() {
Properties overrides = setupProperties();
testSession = VCloudDirectorTestSession.builder()
.provider(provider)
.identity(identity)
.credential(credential)
.endpoint(endpoint)
.overrides(overrides)
.build();
System.err.println("*** " + endpoint + " ***");
context = testSession.getUserContext();
adminContext = testSession.getAdminContext();

View File

@ -5,12 +5,13 @@ import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.R
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.io.Closeable;
import java.net.URI;
import java.util.Properties;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.RestContext;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.rest.internal.ContextBuilder;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncClient;
@ -25,9 +26,10 @@ import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.Closeables;
import com.google.inject.Module;
public class VCloudDirectorTestSession {
public class VCloudDirectorTestSession implements Closeable {
public static Builder builder() {
return new Builder();
@ -78,9 +80,12 @@ public class VCloudDirectorTestSession {
private User createdUser;
private VCloudDirectorTestSession(String provider, String identity, String credential, Properties overrides, String endpoint) {
VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast(
new RestContextFactory().createContext(provider, identity, credential, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides));
ContextBuilder<?, ?, ?, ?> builder = ContextBuilder.newBuilder(provider)
.credentials(identity, credential)
.endpoint(endpoint)
.modules(ImmutableSet.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule()))
.overrides(overrides);
VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast(builder.build());
if (rootContext.getApi().getCurrentSession().getLinks().contains(Link.builder()
.rel("down")
@ -113,10 +118,11 @@ public class VCloudDirectorTestSession {
.isEnabled(true)
.build());
rootContext.close(); rootContext = null;
Closeables.closeQuietly(rootContext);
builder.credentials(adminIdentity, adminCredential);
adminContext = VCloudDirectorContext.class.cast(builder.build()).getAdminContext();
adminContext = VCloudDirectorContext.class.cast(new RestContextFactory().createContext(provider, adminIdentity, adminCredential, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides)).getAdminContext();
String userIdentity = "test"+BaseVCloudDirectorClientLiveTest.getTestDateTimeStamp();
String userCredential = "testPassword";
@ -129,25 +135,24 @@ public class VCloudDirectorTestSession {
.isEnabled(true)
.build());
userContext = new RestContextFactory().createContext(provider, userIdentity, userCredential, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides);
builder.credentials(userIdentity, userCredential);
userContext = VCloudDirectorContext.class.cast(builder.build());
} else {
userContext = rootContext;
}
}
@Override
public void close() {
if (createdUser != null) {
adminContext.getApi().getUserClient().deleteUser(createdUser.getHref());
}
if (userContext != null)
userContext.close();
if (userContext != null) userContext.close();
if (createdAdminUser != null) {
// TODO: may have to preserve root context if we can't delete the user for it's own context here
adminContext.getApi().getUserClient().deleteUser(createdAdminUser.getHref());
}
if (adminContext != null)
adminContext.close();
if (adminContext != null) adminContext.close();
}
public RestContext<VCloudDirectorClient, VCloudDirectorAsyncClient> getUserContext() {