diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java index ba3358fc62d5..4445d9a8c178 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java @@ -374,11 +374,21 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage String destKey = fullname(rewriteRequest.target); + // if this is a new file, set generation to 1, else increment the existing generation + long generation = 1; + if (metadata.containsKey(destKey)) { + generation = metadata.get(destKey).getGeneration() + 1; + } + checkGeneration(destKey, generationMatch); + byte[] data = contents.get(sourceKey); + + rewriteRequest.target.setGeneration(generation); + rewriteRequest.target.setSize(BigInteger.valueOf(data.length)); + metadata.put(destKey, rewriteRequest.target); - byte[] data = contents.get(sourceKey); contents.put(destKey, Arrays.copyOf(data, data.length)); return new RewriteResponse( rewriteRequest, diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java new file mode 100644 index 000000000000..6f2e5cf19438 --- /dev/null +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 2016 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.storage.contrib.nio.testing; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.WriteChannel; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Storage; +import java.io.IOException; +import java.nio.ByteBuffer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link LocalStorageHelper}. */ +@RunWith(JUnit4.class) +public class LocalStorageHelperTest { + + Storage localStorageService = null; + private final String testBucket = "bucket"; + private final String sourceFile = "testSource"; + private final String destinationFile = "testDestination"; + private final String payload = "copy me"; + + @Before + public void before() throws IOException { + localStorageService = LocalStorageHelper.getOptions().getService(); + + byte[] payloadBytes = payload.getBytes(); + BlobId id = BlobId.of(testBucket, sourceFile); + BlobInfo info = BlobInfo.newBuilder(id).build(); + + WriteChannel writer = localStorageService.writer(info); + try { + writer.write(ByteBuffer.wrap(payloadBytes)); + } finally { + writer.close(); + } + } + + @After + public void after() { + localStorageService.delete(testBucket, sourceFile); + localStorageService.delete(testBucket, destinationFile); + } + + private Storage.CopyRequest copyRequest() { + Storage.CopyRequest request = + Storage.CopyRequest.newBuilder() + .setSource(BlobId.of(testBucket, sourceFile)) + .setTarget(BlobId.of(testBucket, destinationFile)) + .build(); + + return request; + } + + @Test + public void testCopyCanBeRead() { + Storage.CopyRequest request = copyRequest(); + localStorageService.copy(request).getResult(); + Blob obj = localStorageService.get(BlobId.of(testBucket, destinationFile)); + String copiedContents = new String(obj.getContent(Blob.BlobSourceOption.generationMatch())); + + assertThat(copiedContents).isEqualTo(payload); + assertThat(obj.getGeneration()).isEqualTo(1); + assertThat(obj.getSize()).isEqualTo(7); + } + + @Test + public void testCopyIncrementsGenerations() { + Storage.CopyRequest request = copyRequest(); + + localStorageService.copy(request).getResult(); + localStorageService.copy(request).getResult(); + Blob obj = localStorageService.get(BlobId.of(testBucket, destinationFile)); + String copiedContents = new String(obj.getContent(Blob.BlobSourceOption.generationMatch())); + + assertThat(copiedContents).isEqualTo(payload); + assertThat(obj.getGeneration()).isEqualTo(2); + assertThat(obj.getSize()).isEqualTo(7); + } +}