Skip to content

Instantly share code, notes, and snippets.

Created July 12, 2013 12:29
Show Gist options
  • Save anonymous/5984101 to your computer and use it in GitHub Desktop.
Save anonymous/5984101 to your computer and use it in GitHub Desktop.
jclouds test for checking whether getBlob correctly throws an exception.
package com.ibm.icstore.test.stress;
import java.io.IOException;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.options.CreateContainerOptions;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
public class ContainerExistTest
{
private static final Logger LOG = LoggerFactory.getLogger(ContainerExistTest.class);
public static final String CONTAINER_NAME = "test-container-1";
public static final String BLOB_NAME = "uploadedImage";
private final BlobStoreContext ctx;
private final BlobStore store;
@Parameters(
{ "provider", "identity", "credentials" })
public ContainerExistTest(String provider, String identity, String credential)
{
ctx = ContextBuilder.newBuilder(provider)
.credentials(identity, credential)
.modules(ImmutableSet.of(new SLF4JLoggingModule()))
.buildView(BlobStoreContext.class);
System.out.format("Provider '%s' uses %s consistency%n", provider, ctx.getConsistencyModel());
store = ctx.getBlobStore();
}
public void createContainer(String containerName)
{
LOG.info("Creating public container '{}'", containerName);
store.createContainerInLocation(null, containerName, CreateContainerOptions.Builder.publicRead());
}
public boolean containerExists(String containerName)
{
LOG.info("Checking if '{}' exists", containerName);
return store.containerExists(containerName);
}
public Blob getBlob(String containerName, String blobName)
{
LOG.info("Getting blob '{}' from '{}'", blobName, containerName);
Blob blob = store.getBlob(containerName, blobName);
LOG.info("Got blob '{}'", blob);
return blob;
}
public void tryDeleteContainer(String containerName)
{
LOG.warn("Deleting public container '{}'", containerName);
try
{
store.deleteContainer(containerName);
}
catch (RuntimeException exception)
{
LOG.warn("Unable to delete container due to: ", exception.getMessage());
}
}
public void cleanup()
{
ctx.close();
}
@Test
public void verifyOperations()
{
try
{
createContainer(CONTAINER_NAME);
assert (containerExists(CONTAINER_NAME));
assert (!containerExists("wrong" + CONTAINER_NAME));
assert (getBlob(CONTAINER_NAME, BLOB_NAME) == null);
boolean exc = false;
try
{
Blob blob = getBlob("wrong" + CONTAINER_NAME, BLOB_NAME);
LOG.error("Got this blob!? {}", blob);
assert (false);
}
catch (RuntimeException e)
{
LOG.info("Caught exception correctly", e);
exc = true;
}
assert (exc == true);
}
finally
{
tryDeleteContainer(CONTAINER_NAME);
cleanup();
}
}
public static void main(String[] args) throws IOException
{
if (args.length < 3)
{
System.out.format("%nUsage: %s <provider> <identity> <credential>%n",
ContainerExistTest.class.getSimpleName());
System.exit(1);
}
ContainerExistTest tester = new ContainerExistTest(args[0], args[1], args[2]);
tester.verifyOperations();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment