|
| 1 | +package com.spectralogic.ds3client.integration; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.spectralogic.ds3client.Ds3Client; |
| 5 | +import com.spectralogic.ds3client.commands.BulkPutRequest; |
| 6 | +import com.spectralogic.ds3client.helpers.Ds3ClientHelpers; |
| 7 | +import com.spectralogic.ds3client.helpers.FileObjectGetter; |
| 8 | +import com.spectralogic.ds3client.helpers.options.WriteJobOptions; |
| 9 | +import com.spectralogic.ds3client.models.bulk.Ds3Object; |
| 10 | +import com.spectralogic.ds3client.serializer.XmlProcessingException; |
| 11 | +import com.spectralogic.ds3client.utils.ByteArraySeekableByteChannel; |
| 12 | +import com.spectralogic.ds3client.utils.ResourceUtils; |
| 13 | +import org.apache.commons.codec.digest.DigestUtils; |
| 14 | +import org.apache.commons.io.IOUtils; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URISyntaxException; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.nio.channels.SeekableByteChannel; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.security.SignatureException; |
| 27 | + |
| 28 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 29 | +import static org.hamcrest.CoreMatchers.is; |
| 30 | +import static org.junit.Assert.assertThat; |
| 31 | + |
| 32 | +public class DataIntegrity_Test { |
| 33 | + private static Ds3Client client; |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void startup() { |
| 37 | + client = Util.fromEnv(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void singleFilePut() throws IOException, URISyntaxException, XmlProcessingException, SignatureException { |
| 42 | + final String bucketName = "java_integration_test"; |
| 43 | + final String book = "beowulf.txt"; |
| 44 | + |
| 45 | + final Ds3ClientHelpers helpers = Ds3ClientHelpers.wrap(client); |
| 46 | + |
| 47 | + try { |
| 48 | + helpers.ensureBucketExists(bucketName); |
| 49 | + |
| 50 | + final File objFile = ResourceUtils.loadFileResource(Util.RESOURCE_BASE_NAME + book); |
| 51 | + final String digest = DigestUtils.sha256Hex(new FileInputStream(objFile)); |
| 52 | + final Ds3Object obj = new Ds3Object(book, objFile.length()); |
| 53 | + |
| 54 | + final Ds3ClientHelpers.Job putJob = helpers.startWriteJob(bucketName, Lists.newArrayList(obj)); |
| 55 | + putJob.transfer(new ResourceObjectPutter(Util.RESOURCE_BASE_NAME)); |
| 56 | + |
| 57 | + final Path tempDir = Files.createTempDirectory("ds3_test_"); |
| 58 | + |
| 59 | + final Ds3ClientHelpers.Job getJob = helpers.startReadAllJob(bucketName); |
| 60 | + getJob.transfer(new FileObjectGetter(tempDir)); |
| 61 | + |
| 62 | + final String secondDigest = DigestUtils.sha256Hex(Files.newInputStream(tempDir.resolve(book))); |
| 63 | + assertThat(secondDigest, is(equalTo(digest))); |
| 64 | + } |
| 65 | + finally { |
| 66 | + Util.deleteAllContents(client, bucketName); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void randomDataFile() throws IOException, SignatureException, URISyntaxException, XmlProcessingException { |
| 72 | + final String bucketName = "java_integration_test"; |
| 73 | + final String randomFileName = "random.txt"; |
| 74 | + final long seed = 12345689; |
| 75 | + final int length = 2048; |
| 76 | + |
| 77 | + sendAndVerifySingleFile(bucketName, randomFileName, seed, length); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void randomSingleByte() throws XmlProcessingException, SignatureException, IOException { |
| 82 | + final String bucketName = "java_integration_test"; |
| 83 | + final String randomFileName = "random.txt"; |
| 84 | + final long seed = 12345689; |
| 85 | + final int length = 1; |
| 86 | + |
| 87 | + sendAndVerifySingleFile(bucketName, randomFileName, seed, length); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void multiBlob() throws IOException, SignatureException, XmlProcessingException { |
| 92 | + final String bucketName = "java_integration_test"; |
| 93 | + final String randomFileName = "random.txt"; |
| 94 | + final long seed = 12345689; |
| 95 | + final int length = 2 * BulkPutRequest.MIN_UPLOAD_SIZE_IN_BYTES; |
| 96 | + |
| 97 | + sendAndVerifySingleFile(bucketName, randomFileName, seed, length); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void fullBlobPlusOneByte() throws XmlProcessingException, SignatureException, IOException { |
| 102 | + final String bucketName = "java_integration_test"; |
| 103 | + final String randomFileName = "random.txt"; |
| 104 | + final long seed = 12345; |
| 105 | + final int length = BulkPutRequest.MIN_UPLOAD_SIZE_IN_BYTES + 1; |
| 106 | + |
| 107 | + sendAndVerifySingleFile(bucketName, randomFileName, seed, length); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void threeFullBlobs() throws XmlProcessingException, SignatureException, IOException { |
| 112 | + final String bucketName = "java_integration_test"; |
| 113 | + final String randomFileName = "random.txt"; |
| 114 | + final long seed = 12345; |
| 115 | + final int length = 3 * BulkPutRequest.MIN_UPLOAD_SIZE_IN_BYTES; |
| 116 | + |
| 117 | + sendAndVerifySingleFile(bucketName, randomFileName, seed, length); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void twoFullBlobsPlusOneByte() throws XmlProcessingException, SignatureException, IOException { |
| 122 | + final String bucketName = "java_integration_test"; |
| 123 | + final String randomFileName = "random.txt"; |
| 124 | + final long seed = 12345; |
| 125 | + final int length = 2 * BulkPutRequest.MIN_UPLOAD_SIZE_IN_BYTES + 1; |
| 126 | + |
| 127 | + sendAndVerifySingleFile(bucketName, randomFileName, seed, length); |
| 128 | + } |
| 129 | + |
| 130 | + public void sendAndVerifySingleFile(final String bucketName, final String fileName, final long seed, final int length) throws IOException, SignatureException, XmlProcessingException { |
| 131 | + try { |
| 132 | + final Ds3ClientHelpers helpers = Ds3ClientHelpers.wrap(client); |
| 133 | + helpers.ensureBucketExists(bucketName); |
| 134 | + |
| 135 | + final String digest = DigestUtils.sha256Hex(new RandomDataInputStream(seed, length)); |
| 136 | + final Ds3Object obj = new Ds3Object(fileName, length); |
| 137 | + |
| 138 | + final Ds3ClientHelpers.Job putJob = helpers.startWriteJob(bucketName, Lists.newArrayList(obj), |
| 139 | + WriteJobOptions.create().withMaxUploadSize(BulkPutRequest.MIN_UPLOAD_SIZE_IN_BYTES)); |
| 140 | + putJob.transfer(new Ds3ClientHelpers.ObjectChannelBuilder() { |
| 141 | + @Override |
| 142 | + public SeekableByteChannel buildChannel(final String key) throws IOException { |
| 143 | + |
| 144 | + final byte[] randomData = IOUtils.toByteArray(new RandomDataInputStream(seed, length)); |
| 145 | + final ByteBuffer randomBuffer = ByteBuffer.wrap(randomData); |
| 146 | + |
| 147 | + final ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel(length); |
| 148 | + channel.write(randomBuffer); |
| 149 | + |
| 150 | + return channel; |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + final Path tempDir = Files.createTempDirectory("ds3_test_"); |
| 155 | + |
| 156 | + final Ds3ClientHelpers.Job getJob = helpers.startReadAllJob(bucketName); |
| 157 | + getJob.transfer(new FileObjectGetter(tempDir)); |
| 158 | + |
| 159 | + final String secondDigest = DigestUtils.sha256Hex(Files.newInputStream(tempDir.resolve(fileName))); |
| 160 | + assertThat(secondDigest, is(equalTo(digest))); |
| 161 | + } |
| 162 | + finally { |
| 163 | + Util.deleteAllContents(client, bucketName); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments