Issue 100: updated test cases and typed metadata

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2034 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-10-31 19:50:18 +00:00
parent 67612d0f0c
commit 19e315042f
3 changed files with 118 additions and 102 deletions

View File

@ -39,6 +39,7 @@ import org.jclouds.nirvanix.sdn.binders.BindMetadataToQueryParams;
import org.jclouds.nirvanix.sdn.domain.UploadInfo;
import org.jclouds.nirvanix.sdn.filters.AddSessionTokenToRequest;
import org.jclouds.nirvanix.sdn.filters.InsertUserContextIntoPath;
import org.jclouds.nirvanix.sdn.functions.ParseMetadataFromJsonResponse;
import org.jclouds.nirvanix.sdn.functions.ParseUploadInfoFromJsonResponse;
import org.jclouds.nirvanix.sdn.reference.SDNQueryParams;
import org.jclouds.rest.annotations.BinderParam;
@ -63,7 +64,7 @@ import org.jclouds.rest.annotations.SkipEncoding;
public interface SDNClient {
public Blob newBlob();
/**
* The GetStorageNode method is used to determine which storage node a file should be uploaded
* to. It returns the host to upload to and an Upload Token that will be used to authenticate.
@ -95,8 +96,9 @@ public interface SDNClient {
*/
@GET
@Path("/ws/Metadata/GetMetadata.ashx")
@ResponseParser(ParseMetadataFromJsonResponse.class)
@QueryParams(keys = SDNQueryParams.PATH, values = "{path}")
Future<String> getMetadata(@PathParam("path") String path);
Future<Map<String, String>> getMetadata(@PathParam("path") String path);
/**
* Get the contents of a file

View File

@ -35,6 +35,7 @@ import java.util.concurrent.TimeoutException;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest;
import org.jclouds.http.HttpUtils;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.nirvanix.sdn.domain.UploadInfo;
import org.testng.annotations.BeforeGroups;
@ -78,19 +79,24 @@ public class SDNClientLiveTest {
Blob blob = connection.newBlob();
blob.getMetadata().setName("test.txt");
blob.setData("value");
blob.generateMD5();
byte[] md5 = blob.getMetadata().getContentMD5();
connection.upload(uploadInfo.getHost(), uploadInfo.getToken(), containerName, blob).get(30,
TimeUnit.SECONDS);
String metadataS = connection.getMetadata(containerName + "/test.txt").get(30,
Map<String, String> metadata = connection.getMetadata(containerName + "/test.txt").get(30,
TimeUnit.SECONDS);
System.err.println(metadataS);
assertEquals(metadata, ImmutableMap.of("MD5", HttpUtils.toBase64String(md5)));
String content = connection.getFile(containerName + "/test.txt").get(30, TimeUnit.SECONDS);
assertEquals(content, "value");
Map<String, String> metadata = ImmutableMap.of("chef", "sushi", "foo", "bar");
metadata = ImmutableMap.of("chef", "sushi", "foo", "bar");
connection.setMetadata(containerName + "/test.txt", metadata).get(30, TimeUnit.SECONDS);
metadata = connection.getMetadata(containerName + "/test.txt").get(30, TimeUnit.SECONDS);
assertEquals(metadata, ImmutableMap.<String, String> builder().putAll(metadata).put("MD5",
HttpUtils.toBase64String(md5)).build());
}
}

View File

@ -26,42 +26,32 @@ package org.jclouds.nirvanix.sdn;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders;
import org.jclouds.blobstore.binders.BindBlobToMultipartFormTest;
import org.jclouds.blobstore.config.BlobStoreObjectModule;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.concurrent.WithinThreadExecutorService;
import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.http.functions.ReturnStringIf200;
import org.jclouds.http.functions.ReturnVoidIf2xx;
import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.nirvanix.sdn.filters.AddSessionTokenToRequest;
import org.jclouds.nirvanix.sdn.filters.InsertUserContextIntoPath;
import org.jclouds.nirvanix.sdn.functions.ParseMetadataFromJsonResponse;
import org.jclouds.nirvanix.sdn.functions.ParseUploadInfoFromJsonResponse;
import org.jclouds.nirvanix.sdn.reference.SDNConstants;
import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.RestClientTest;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Jsr330;
import org.jclouds.util.Utils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
/**
@ -70,108 +60,136 @@ import com.google.inject.TypeLiteral;
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "sdn.SDNClient")
public class SDNClientTest {
public class SDNClientTest extends RestClientTest<SDNClient> {
private RestAnnotationProcessor<SDNClient> processor;
public void testGetStorageNode() throws SecurityException, NoSuchMethodException {
public void testGetStorageNode() throws SecurityException, NoSuchMethodException, IOException {
Method method = SDNClient.class.getMethod("getStorageNode", String.class, long.class);
GeneratedHttpRequest<?> httpMethod = processor.createRequest(method, new Object[] {
GeneratedHttpRequest<SDNClient> httpMethod = processor.createRequest(method, new Object[] {
"adriansmovies", 734859264 });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/ws/IMFS/GetStorageNode.ashx");
assertEquals(httpMethod.getEndpoint().getQuery(),
"output=json&sizeBytes=734859264&destFolderPath=adriansmovies");
assertEquals(httpMethod.getMethod(), HttpMethod.GET);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(RestAnnotationProcessor.getParserOrThrowException(method),
ParseUploadInfoFromJsonResponse.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), AddSessionTokenToRequest.class);
assertRequestLineEquals(
httpMethod,
"GET http://stub:8080/ws/IMFS/GetStorageNode.ashx?output=json&sizeBytes=734859264&destFolderPath=adriansmovies HTTP/1.1");
assertHeadersEqual(httpMethod, "");
assertEntityEquals(httpMethod, null);
assertResponseParserClassEquals(method, httpMethod, ParseUploadInfoFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpMethod);
}
public void testUpload() throws SecurityException, NoSuchMethodException, IOException {
Method method = SDNClient.class.getMethod("upload", URI.class, String.class,
String.class, Blob.class);
Method method = SDNClient.class.getMethod("upload", URI.class, String.class, String.class,
Blob.class);
Blob blob = BindBlobToMultipartFormTest.TEST_BLOB;
GeneratedHttpRequest<SDNClient> httpMethod = processor.createRequest(method,
new Object[] { URI.create("http://uploader"), "token", "adriansmovies", blob });
assertEquals(httpMethod.getEndpoint().getHost(), "uploader");
assertEquals(httpMethod.getEndpoint().getPath(), "/Upload.ashx");
assertEquals(httpMethod.getEndpoint().getQuery(),
"output=json&uploadToken=token&destFolderPath=adriansmovies");
assertEquals(httpMethod.getMethod(), HttpMethod.POST);
assertEquals(httpMethod.getHeaders().size(), 2);
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_LENGTH), Collections
.singletonList(BindBlobToMultipartFormTest.EXPECTS.length() + ""));
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList("multipart/form-data; boundary="
+ BindBlobToMultipartFormTest.BOUNDRY));
assertEquals(Utils.toStringAndClose((InputStream) httpMethod.getEntity()),
BindBlobToMultipartFormTest.EXPECTS);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnVoidIf2xx.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), AddSessionTokenToRequest.class);
GeneratedHttpRequest<SDNClient> httpMethod = processor.createRequest(method, new Object[] {
URI.create("http://uploader"), "token", "adriansmovies", blob });
assertRequestLineEquals(
httpMethod,
"POST http://uploader/Upload.ashx?output=json&uploadToken=token&destFolderPath=adriansmovies HTTP/1.1");
assertHeadersEqual(httpMethod,
"Content-Length: 131\nContent-Type: multipart/form-data; boundary=--JCLOUDS--\n");
StringBuffer expects = new StringBuffer();
expects.append("----JCLOUDS--\r\n");
expects.append("Content-Disposition: form-data; name=\"hello\"; filename=\"hello\"\r\n");
expects.append("Content-Type: text/plain\r\n\r\n");
expects.append("hello\r\n");
expects.append("----JCLOUDS----\r\n");
assertEntityEquals(httpMethod, expects.toString());
assertResponseParserClassEquals(method, httpMethod, ReturnVoidIf2xx.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpMethod);
}
public void testSetMetadata() throws SecurityException, NoSuchMethodException, IOException {
Method method = SDNClient.class.getMethod("setMetadata", String.class, Map.class);
GeneratedHttpRequest<SDNClient> httpMethod = processor
.createRequest(method, new Object[] { "adriansmovies/sushi.avi",
ImmutableMap.of("Chef", "Kawasaki") });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/ws/Metadata/SetMetadata.ashx");
assertEquals(httpMethod.getEndpoint().getQuery(),
"output=json&path=adriansmovies/sushi.avi&metadata=chef:Kawasaki");
assertEquals(httpMethod.getMethod(), HttpMethod.GET);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(httpMethod.getEntity(), null);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnVoidIf2xx.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), AddSessionTokenToRequest.class);
GeneratedHttpRequest<SDNClient> httpMethod = processor.createRequest(method, new Object[] {
"adriansmovies/sushi.avi", ImmutableMap.of("Chef", "Kawasaki") });
assertRequestLineEquals(
httpMethod,
"GET http://stub:8080/ws/Metadata/SetMetadata.ashx?output=json&path=adriansmovies/sushi.avi&metadata=chef:Kawasaki HTTP/1.1");
assertHeadersEqual(httpMethod, "");
assertEntityEquals(httpMethod, null);
assertResponseParserClassEquals(method, httpMethod, ReturnVoidIf2xx.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpMethod);
}
public void testGetMetadata() throws SecurityException, NoSuchMethodException, IOException {
Method method = SDNClient.class.getMethod("getMetadata", String.class);
GeneratedHttpRequest<SDNClient> httpMethod = processor.createRequest(method,
new Object[] { "adriansmovies/sushi.avi" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/ws/Metadata/GetMetadata.ashx");
assertEquals(httpMethod.getEndpoint().getQuery(), "output=json&path=adriansmovies/sushi.avi");
assertEquals(httpMethod.getMethod(), HttpMethod.GET);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(httpMethod.getEntity(), null);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnStringIf200.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), AddSessionTokenToRequest.class);
assertRequestLineEquals(
httpMethod,
"GET http://stub:8080/ws/Metadata/GetMetadata.ashx?output=json&path=adriansmovies/sushi.avi HTTP/1.1");
assertHeadersEqual(httpMethod, "");
assertEntityEquals(httpMethod, null);
assertResponseParserClassEquals(method, httpMethod, ParseMetadataFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpMethod);
}
public void testGetFile() throws SecurityException, NoSuchMethodException, IOException {
Method method = SDNClient.class.getMethod("getFile", String.class);
GeneratedHttpRequest<SDNClient> httpMethod = processor.createRequest(method,
new Object[] { "adriansmovies/sushi.avi" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/adriansmovies/sushi.avi");
assertEquals(httpMethod.getEndpoint().getQuery(), "output=json");
assertEquals(httpMethod.getMethod(), HttpMethod.GET);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(httpMethod.getEntity(), null);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnStringIf200.class);
assertRequestLineEquals(httpMethod,
"GET http://stub:8080/adriansmovies/sushi.avi?output=json HTTP/1.1");
assertHeadersEqual(httpMethod, "");
assertEntityEquals(httpMethod, null);
assertResponseParserClassEquals(method, httpMethod, ReturnStringIf200.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), InsertUserContextIntoPath.class);
}
@Override
protected void checkFilters(GeneratedHttpRequest<SDNClient> httpMethod) {
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), AddSessionTokenToRequest.class);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<SDNClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<SDNClient>>() {
};
}
@BeforeClass
void setupFactory() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void setupFactory() {
super.setupFactory();
}
@Override
protected Module createModule() {
return new AbstractModule() {
@Override
protected void configure() {
bind(URI.class).annotatedWith(SDN.class)
.toInstance(URI.create("http://localhost:8080"));
install(new BlobStoreObjectModule());
bind(URI.class).annotatedWith(SDN.class).toInstance(URI.create("http://stub:8080"));
bind(String.class).annotatedWith(SessionToken.class).toInstance("sessiontoken");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) {
return Logger.NULL;
@ -185,17 +203,7 @@ public class SDNClientTest {
"username");
}
@SuppressWarnings("unused")
@SessionToken
@Provides
String authTokenProvider() {
return "session-token";
}
}, new RestModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule());
processor = injector.getInstance(Key
.get(new TypeLiteral<RestAnnotationProcessor<SDNClient>>() {
}));
};
}
}