Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}