Factor out ByteSource payload

pull/53/head
Andrew Gaul 2015-02-13 16:00:26 -08:00
rodzic ea1a42dcd8
commit 17e9f71bb4
2 zmienionych plików z 36 dodań i 42 usunięć

Wyświetl plik

@ -16,7 +16,6 @@
package org.gaul.s3proxy; package org.gaul.s3proxy;
import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.util.Properties; import java.util.Properties;
@ -31,6 +30,7 @@ import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.ObjectMetadata;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteSource;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.inject.Module; import com.google.inject.Module;
@ -57,6 +57,8 @@ public final class S3AwsSdkTest {
"true"); "true");
} }
private static final ByteSource BYTE_SOURCE = ByteSource.wrap(new byte[1]);
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -151,8 +153,8 @@ public final class S3AwsSdkTest {
AmazonS3 client = new AmazonS3Client(awsCreds, AmazonS3 client = new AmazonS3Client(awsCreds,
new ClientConfiguration().withSignerOverride("S3SignerType")); new ClientConfiguration().withSignerOverride("S3SignerType"));
client.setEndpoint(s3Endpoint.toString()); client.setEndpoint(s3Endpoint.toString());
client.putObject(containerName, "foo", client.putObject(containerName, "foo", BYTE_SOURCE.openStream(),
new ByteArrayInputStream(new byte[0]), new ObjectMetadata()); new ObjectMetadata());
} }
@Test @Test
@ -167,8 +169,8 @@ public final class S3AwsSdkTest {
return ase.getErrorCode().equals(code); return ase.getErrorCode().equals(code);
} }
}); });
client.putObject(containerName, "foo", client.putObject(containerName, "foo", BYTE_SOURCE.openStream(),
new ByteArrayInputStream(new byte[0]), new ObjectMetadata()); new ObjectMetadata());
} }
private static String createRandomContainerName() { private static String createRandomContainerName() {

Wyświetl plik

@ -67,6 +67,8 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
public final class S3ProxyTest { public final class S3ProxyTest {
private static final ByteSource BYTE_SOURCE = ByteSource.wrap(new byte[1]);
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -177,12 +179,11 @@ public final class S3ProxyTest {
HttpClient httpClient = context.utils().http(); HttpClient httpClient = context.utils().http();
// TODO: how to interpret this? // TODO: how to interpret this?
URI uri = URI.create(s3Endpoint + "/" + containerName + "/blob"); URI uri = URI.create(s3Endpoint + "/" + containerName + "/blob");
ByteSource byteSource = ByteSource.wrap(new byte[1]); Payload payload = new ByteSourcePayload(BYTE_SOURCE);
Payload payload = new ByteSourcePayload(byteSource); payload.getContentMetadata().setContentLength(BYTE_SOURCE.size());
payload.getContentMetadata().setContentLength(byteSource.size());
httpClient.put(uri, payload); httpClient.put(uri, payload);
try (InputStream actual = httpClient.get(uri); try (InputStream actual = httpClient.get(uri);
InputStream expected = byteSource.openStream()) { InputStream expected = BYTE_SOURCE.openStream()) {
assertThat(actual).hasContentEqualTo(expected); assertThat(actual).hasContentEqualTo(expected);
} }
} }
@ -224,17 +225,16 @@ public final class S3ProxyTest {
} }
private void putBlobAndCheckIt(String blobName) throws Exception { private void putBlobAndCheckIt(String blobName) throws Exception {
ByteSource byteSource = ByteSource.wrap(new byte[42]);
Blob blob = s3BlobStore.blobBuilder(blobName) Blob blob = s3BlobStore.blobBuilder(blobName)
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob); s3BlobStore.putBlob(containerName, blob);
Blob blob2 = s3BlobStore.getBlob(containerName, blobName); Blob blob2 = s3BlobStore.getBlob(containerName, blobName);
assertThat(blob2.getMetadata().getName()).isEqualTo(blobName); assertThat(blob2.getMetadata().getName()).isEqualTo(blobName);
try (InputStream actual = blob2.getPayload().openStream(); try (InputStream actual = blob2.getPayload().openStream();
InputStream expected = byteSource.openStream()) { InputStream expected = BYTE_SOURCE.openStream()) {
assertThat(actual).hasContentEqualTo(expected); assertThat(actual).hasContentEqualTo(expected);
} }
} }
@ -261,12 +261,10 @@ public final class S3ProxyTest {
public void testBlobList() throws Exception { public void testBlobList() throws Exception {
assertThat(s3BlobStore.list(containerName)).isEmpty(); assertThat(s3BlobStore.list(containerName)).isEmpty();
// TODO: hang with zero length blobs?
ByteSource byteSource = ByteSource.wrap(new byte[1]);
ImmutableSet.Builder<String> builder = ImmutableSet.builder(); ImmutableSet.Builder<String> builder = ImmutableSet.builder();
Blob blob1 = s3BlobStore.blobBuilder("blob1") Blob blob1 = s3BlobStore.blobBuilder("blob1")
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob1); s3BlobStore.putBlob(containerName, blob1);
for (StorageMetadata metadata : s3BlobStore.list(containerName)) { for (StorageMetadata metadata : s3BlobStore.list(containerName)) {
@ -276,8 +274,8 @@ public final class S3ProxyTest {
builder = ImmutableSet.builder(); builder = ImmutableSet.builder();
Blob blob2 = s3BlobStore.blobBuilder("blob2") Blob blob2 = s3BlobStore.blobBuilder("blob2")
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob2); s3BlobStore.putBlob(containerName, blob2);
for (StorageMetadata metadata : s3BlobStore.list(containerName)) { for (StorageMetadata metadata : s3BlobStore.list(containerName)) {
@ -290,16 +288,15 @@ public final class S3ProxyTest {
public void testBlobListRecursive() throws Exception { public void testBlobListRecursive() throws Exception {
assertThat(s3BlobStore.list(containerName)).isEmpty(); assertThat(s3BlobStore.list(containerName)).isEmpty();
ByteSource byteSource = ByteSource.wrap(new byte[1]);
Blob blob1 = s3BlobStore.blobBuilder("prefix/blob1") Blob blob1 = s3BlobStore.blobBuilder("prefix/blob1")
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob1); s3BlobStore.putBlob(containerName, blob1);
Blob blob2 = s3BlobStore.blobBuilder("prefix/blob2") Blob blob2 = s3BlobStore.blobBuilder("prefix/blob2")
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob2); s3BlobStore.putBlob(containerName, blob2);
@ -321,10 +318,9 @@ public final class S3ProxyTest {
@Test @Test
public void testBlobMetadata() throws Exception { public void testBlobMetadata() throws Exception {
String blobName = "blob"; String blobName = "blob";
ByteSource byteSource = ByteSource.wrap(new byte[1]);
Blob blob1 = s3BlobStore.blobBuilder(blobName) Blob blob1 = s3BlobStore.blobBuilder(blobName)
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob1); s3BlobStore.putBlob(containerName, blob1);
@ -332,7 +328,7 @@ public final class S3ProxyTest {
blobName); blobName);
assertThat(metadata.getName()).isEqualTo(blobName); assertThat(metadata.getName()).isEqualTo(blobName);
assertThat(metadata.getContentMetadata().getContentLength()) assertThat(metadata.getContentMetadata().getContentLength())
.isEqualTo(byteSource.size()); .isEqualTo(BYTE_SOURCE.size());
assertThat(s3BlobStore.blobMetadata(containerName, assertThat(s3BlobStore.blobMetadata(containerName,
"fake-blob")).isNull(); "fake-blob")).isNull();
@ -341,10 +337,9 @@ public final class S3ProxyTest {
@Test @Test
public void testBlobRemove() throws Exception { public void testBlobRemove() throws Exception {
String blobName = "blob"; String blobName = "blob";
ByteSource byteSource = ByteSource.wrap(new byte[1]);
Blob blob = s3BlobStore.blobBuilder(blobName) Blob blob = s3BlobStore.blobBuilder(blobName)
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
s3BlobStore.putBlob(containerName, blob); s3BlobStore.putBlob(containerName, blob);
assertThat(s3BlobStore.blobExists(containerName, blobName)).isTrue(); assertThat(s3BlobStore.blobExists(containerName, blobName)).isTrue();
@ -364,10 +359,9 @@ public final class S3ProxyTest {
BlobRequestSigner signer = s3Context.getSigner(); BlobRequestSigner signer = s3Context.getSigner();
String blobName = "blob"; String blobName = "blob";
ByteSource byteSource = ByteSource.wrap(new byte[1]);
Blob blob = s3BlobStore.blobBuilder(blobName) Blob blob = s3BlobStore.blobBuilder(blobName)
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.build(); .build();
HttpRequest putRequest = signer.signPutBlob(containerName, blob, 10); HttpRequest putRequest = signer.signPutBlob(containerName, blob, 10);
HttpResponse putResponse = httpClient.invoke(putRequest); HttpResponse putResponse = httpClient.invoke(putRequest);
@ -398,7 +392,6 @@ public final class S3ProxyTest {
public void testCopyObjectPreserveMetadata() throws Exception { public void testCopyObjectPreserveMetadata() throws Exception {
String fromName = "from-name"; String fromName = "from-name";
String toName = "to-name"; String toName = "to-name";
ByteSource byteSource = ByteSource.wrap(new byte[42]);
String contentDisposition = "attachment; filename=old.jpg"; String contentDisposition = "attachment; filename=old.jpg";
String contentEncoding = "gzip"; String contentEncoding = "gzip";
String contentLanguage = "en"; String contentLanguage = "en";
@ -408,8 +401,8 @@ public final class S3ProxyTest {
"key1", "value1", "key1", "value1",
"key2", "value2"); "key2", "value2");
Blob fromBlob = s3BlobStore.blobBuilder(fromName) Blob fromBlob = s3BlobStore.blobBuilder(fromName)
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.contentDisposition(contentDisposition) .contentDisposition(contentDisposition)
.contentEncoding(contentEncoding) .contentEncoding(contentEncoding)
.contentLanguage(contentLanguage) .contentLanguage(contentLanguage)
@ -424,7 +417,7 @@ public final class S3ProxyTest {
Blob toBlob = s3BlobStore.getBlob(containerName, toName); Blob toBlob = s3BlobStore.getBlob(containerName, toName);
try (InputStream actual = toBlob.getPayload().openStream(); try (InputStream actual = toBlob.getPayload().openStream();
InputStream expected = byteSource.openStream()) { InputStream expected = BYTE_SOURCE.openStream()) {
assertThat(actual).hasContentEqualTo(expected); assertThat(actual).hasContentEqualTo(expected);
} }
ContentMetadata contentMetadata = ContentMetadata contentMetadata =
@ -446,10 +439,9 @@ public final class S3ProxyTest {
public void testCopyObjectReplaceMetadata() throws Exception { public void testCopyObjectReplaceMetadata() throws Exception {
String fromName = "from-name"; String fromName = "from-name";
String toName = "to-name"; String toName = "to-name";
ByteSource byteSource = ByteSource.wrap(new byte[42]);
Blob fromBlob = s3BlobStore.blobBuilder(fromName) Blob fromBlob = s3BlobStore.blobBuilder(fromName)
.payload(byteSource) .payload(BYTE_SOURCE)
.contentLength(byteSource.size()) .contentLength(BYTE_SOURCE.size())
.contentDisposition("attachment; filename=old.jpg") .contentDisposition("attachment; filename=old.jpg")
.contentEncoding("compress") .contentEncoding("compress")
.contentLanguage("en") .contentLanguage("en")
@ -484,7 +476,7 @@ public final class S3ProxyTest {
Blob toBlob = s3BlobStore.getBlob(containerName, toName); Blob toBlob = s3BlobStore.getBlob(containerName, toName);
try (InputStream actual = toBlob.getPayload().openStream(); try (InputStream actual = toBlob.getPayload().openStream();
InputStream expected = byteSource.openStream()) { InputStream expected = BYTE_SOURCE.openStream()) {
assertThat(actual).hasContentEqualTo(expected); assertThat(actual).hasContentEqualTo(expected);
} }
ContentMetadata toContentMetadata = ContentMetadata toContentMetadata =