-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from rpmoore/master
Metadata Support
- Loading branch information
Showing
23 changed files
with
515 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
ds3-sdk-integration/src/test/java/com/spectralogic/ds3client/integration/Metadata_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.spectralogic.ds3client.integration; | ||
|
||
import com.google.common.collect.ImmutableMultimap; | ||
import com.google.common.collect.Lists; | ||
import com.spectralogic.ds3client.Ds3Client; | ||
import com.spectralogic.ds3client.commands.*; | ||
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers; | ||
import com.spectralogic.ds3client.models.bulk.Ds3Object; | ||
import com.spectralogic.ds3client.networking.Metadata; | ||
import com.spectralogic.ds3client.serializer.XmlProcessingException; | ||
import com.spectralogic.ds3client.utils.ByteArraySeekableByteChannel; | ||
import org.apache.commons.io.IOUtils; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SeekableByteChannel; | ||
import java.security.SignatureException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class Metadata_Test { | ||
private static Ds3Client client; | ||
|
||
@BeforeClass | ||
public static void startup() { | ||
client = Util.fromEnv(); | ||
} | ||
|
||
@AfterClass | ||
public static void teardown() throws IOException { | ||
client.close(); | ||
} | ||
|
||
@Test | ||
public void singleMetadataField() throws IOException, SignatureException, XmlProcessingException { | ||
|
||
final Metadata metadata = processMetadataRequest("metadataBucket", ImmutableMultimap.of("name", "value")); | ||
|
||
final List<String> values = metadata.get("name"); | ||
assertThat(values, is(notNullValue())); | ||
assertFalse(values.isEmpty()); | ||
assertThat(values.size(), is(1)); | ||
assertThat(values.get(0), is("value")); | ||
|
||
} | ||
|
||
@Test | ||
public void multipleMetadataFields() throws IOException, SignatureException, XmlProcessingException { | ||
|
||
final Metadata metadata = processMetadataRequest("metadataBucket", ImmutableMultimap.of("name", "value", "key2", "value2")); | ||
|
||
final Set<String> keys = metadata.keys(); | ||
|
||
assertThat(keys.size(), is(2)); | ||
assertTrue(keys.contains("name")); | ||
assertTrue(keys.contains("key2")); | ||
|
||
final List<String> values = metadata.get("name"); | ||
assertThat(values, is(notNullValue())); | ||
assertFalse(values.isEmpty()); | ||
assertThat(values.size(), is(1)); | ||
assertThat(values.get(0), is("value")); | ||
|
||
} | ||
|
||
/* | ||
//TODO There is currently a limitation in BP where it does not handle metadata values that have the same key. | ||
@Test | ||
public void multipleMetadataFieldsForSameKey() throws XmlProcessingException, SignatureException, IOException { | ||
final Metadata metadata = processMetadataRequest("metadataBucket", ImmutableMultimap.of("name", "value", "name", "value2")); | ||
final Set<String> keys = metadata.keys(); | ||
assertThat(keys.size(), is(1)); | ||
assertTrue(keys.contains("name")); | ||
final List<String> values = metadata.get("name"); | ||
assertThat(values, is(notNullValue())); | ||
assertFalse(values.isEmpty()); | ||
assertThat(values, hasItem("value")); | ||
} | ||
@Test | ||
public void multipleMetadataFieldsForSameKeyDifferentCase() throws XmlProcessingException, SignatureException, IOException { | ||
final Metadata metadata = processMetadataRequest("metadataBucket", ImmutableMultimap.of("name", "value", "Name", "value2")); | ||
final Set<String> keys = metadata.keys(); | ||
assertThat(keys.size(), is(1)); | ||
assertTrue(keys.contains("name")); | ||
final List<String> values = metadata.get("name"); | ||
assertThat(values, is(notNullValue())); | ||
assertFalse(values.isEmpty()); | ||
assertThat(values, hasItem("value")); | ||
} | ||
*/ | ||
|
||
private static Metadata processMetadataRequest(final String bucketName, final ImmutableMultimap<String, String> metadata) throws IOException, SignatureException, XmlProcessingException { | ||
final Ds3ClientHelpers wrapper = Ds3ClientHelpers.wrap(client); | ||
wrapper.ensureBucketExists(bucketName); | ||
|
||
try { | ||
final Ds3Object obj = new Ds3Object("obj.txt", 1024); | ||
final BulkPutResponse putResponse = client.bulkPut(new BulkPutRequest(bucketName, Lists.newArrayList(obj))); | ||
final PutObjectRequest request = new PutObjectRequest(bucketName, obj.getName(), putResponse.getResult().getJobId(), 1024, 0, buildRandomChannel(1024)); | ||
|
||
for (final Map.Entry<String, String> entry : metadata.entries()) { | ||
request.withMetaData(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
final PutObjectResponse putObjResponse = client.putObject(request); | ||
|
||
assertThat(putObjResponse.getStatusCode(), is(200)); | ||
|
||
final HeadObjectResponse response = client.headObject(new HeadObjectRequest(bucketName, obj.getName())); | ||
|
||
assertThat(response.getStatusCode(), is(200)); | ||
|
||
return response.getMetadata(); | ||
|
||
} finally { | ||
Util.deleteAllContents(client, bucketName); | ||
} | ||
} | ||
|
||
private static SeekableByteChannel buildRandomChannel(int length) throws IOException { | ||
|
||
final byte[] randomData = IOUtils.toByteArray(new RandomDataInputStream(System.currentTimeMillis(), length)); | ||
final ByteBuffer randomBuffer = ByteBuffer.wrap(randomData); | ||
|
||
final ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel(length); | ||
channel.write(randomBuffer); | ||
|
||
return channel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ds3-sdk/src/main/java/com/spectralogic/ds3client/commands/HeadObjectRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.spectralogic.ds3client.commands; | ||
|
||
import com.spectralogic.ds3client.HttpVerb; | ||
|
||
public class HeadObjectRequest extends AbstractRequest { | ||
|
||
private final String bucketName; | ||
private final String objectName; | ||
|
||
public HeadObjectRequest(final String bucketName, final String objectName) { | ||
this.bucketName = bucketName; | ||
this.objectName = objectName; | ||
} | ||
|
||
@Override | ||
public String getPath() { | ||
return "/" + bucketName + "/" + objectName; | ||
} | ||
|
||
@Override | ||
public HttpVerb getVerb() { | ||
return HttpVerb.HEAD; | ||
} | ||
} |
Oops, something went wrong.