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( 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) { ApiMetadata<S, A, C, M> apiMetadata) {
try { 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( 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) { ProviderMetadata<S, A, C, M> providerMetadata) {
try { try {
@ -232,10 +230,8 @@ public class ContextBuilder<S, A, C extends Closeable, M extends ApiMetadata<S,
} }
/** /**
*
* @throws NoSuchElementException * @throws NoSuchElementException
*/ */
public static String searchPropertiesForProviderScopedProperty(Properties overrides, String prov, String key, public static String searchPropertiesForProviderScopedProperty(Properties overrides, String prov, String key,
String defaultVal) { String defaultVal) {
try { try {

View File

@ -49,13 +49,17 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
protected volatile C context; protected volatile C context;
protected String identity;
protected String credential;
protected String endpoint;
protected Properties setupProperties() { protected Properties setupProperties() {
Properties overrides = new Properties(); Properties overrides = new Properties();
overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true"); overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true"); overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
setIfTestSystemPropertyPresent(overrides, provider + ".identity"); identity = setIfTestSystemPropertyPresent(overrides, provider + ".identity");
setIfTestSystemPropertyPresent(overrides, provider + ".credential"); credential = setIfTestSystemPropertyPresent(overrides, provider + ".credential");
setIfTestSystemPropertyPresent(overrides, provider + ".endpoint"); endpoint = setIfTestSystemPropertyPresent(overrides, provider + ".endpoint");
setIfTestSystemPropertyPresent(overrides, provider + ".api-version"); setIfTestSystemPropertyPresent(overrides, provider + ".api-version");
setIfTestSystemPropertyPresent(overrides, provider + ".build-version"); setIfTestSystemPropertyPresent(overrides, provider + ".build-version");
return overrides; return overrides;
@ -65,6 +69,7 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
if (System.getProperties().containsKey("test." + key)) { if (System.getProperties().containsKey("test." + key)) {
String val = System.getProperty("test." + key); String val = System.getProperty("test." + key);
overrides.setProperty(key, val); overrides.setProperty(key, val);
return val;
} }
return null; return null;
} }
@ -91,7 +96,6 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
/** /**
* @see org.jclouds.providers.Providers#withId * @see org.jclouds.providers.Providers#withId
*/ */
@SuppressWarnings("unchecked")
protected ProviderMetadata<?, ?, C, ?> createProviderMetadata() { protected ProviderMetadata<?, ?, C, ?> createProviderMetadata() {
try { try {
return (ProviderMetadata<?, ?, C, ?>) Providers.withId(provider); return (ProviderMetadata<?, ?, C, ?>) Providers.withId(provider);
@ -103,7 +107,6 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
/** /**
* @see org.jclouds.apis.Apis#withId * @see org.jclouds.apis.Apis#withId
*/ */
@SuppressWarnings("unchecked")
protected ApiMetadata<?, ?, C, ?> createApiMetadata() { protected ApiMetadata<?, ?, C, ?> createApiMetadata() {
try { try {
return (ApiMetadata<?, ?, C, ?>) Apis.withId(provider); 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.Reference;
import org.jclouds.vcloud.director.v1_5.domain.ResourceEntity.Status; 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.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.Network;
import org.jclouds.vcloud.director.v1_5.domain.network.NetworkConfiguration; import org.jclouds.vcloud.director.v1_5.domain.network.NetworkConfiguration;
import org.jclouds.vcloud.director.v1_5.domain.network.VAppNetworkConfiguration; 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.InstantiationParams;
import org.jclouds.vcloud.director.v1_5.domain.params.UndeployVAppParams; 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.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.TaskClient;
import org.jclouds.vcloud.director.v1_5.features.VAppClient; import org.jclouds.vcloud.director.v1_5.features.VAppClient;
import org.jclouds.vcloud.director.v1_5.features.VAppTemplateClient; 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.predicates.TaskSuccess;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient; import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient; 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.BeforeClass;
import org.testng.annotations.Listeners; import org.testng.annotations.Listeners;
import org.testng.annotations.Test; 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.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.io.Closeables;
import com.google.inject.Guice; import com.google.inject.Guice;
/** /**
@ -138,9 +139,9 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
protected DateService dateService; protected DateService dateService;
private static VCloudDirectorTestSession testSession; protected VCloudDirectorTestSession testSession;
private static String testStamp; protected static String testStamp;
@BeforeClass(alwaysRun = true) @BeforeClass(alwaysRun = true)
protected void setupDateService() { protected void setupDateService() {
@ -148,7 +149,7 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
assertNotNull(dateService); 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(); protected abstract void setupRequiredClients();
@Inject @Inject
@ -160,38 +161,29 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
protected void initTaskSuccessLong(TaskSuccess taskSuccess) { protected void initTaskSuccessLong(TaskSuccess taskSuccess) {
retryTaskSuccessLong = new RetryablePredicate<Task>(taskSuccess, LONG_TASK_TIMEOUT_SECONDS * 1000L); retryTaskSuccessLong = new RetryablePredicate<Task>(taskSuccess, LONG_TASK_TIMEOUT_SECONDS * 1000L);
} }
//temporary until we marry up the test fixtures
protected String identity; @AfterClass(alwaysRun = true)
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)
protected void tearDownTestSession() { protected void tearDownTestSession() {
testSession.close(); Closeables.closeQuietly(testSession);
} }
@BeforeClass(alwaysRun = true)
@Override @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(); context = testSession.getUserContext();
adminContext = testSession.getAdminContext(); adminContext = testSession.getAdminContext();
if(adminContext != null) { if (adminContext != null) {
adminSession = adminContext.getApi().getCurrentSession(); adminSession = adminContext.getApi().getCurrentSession();
adminContext.utils().injector().injectMembers(this); adminContext.utils().injector().injectMembers(this);
} }

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