Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set storage update time in FakeStorageRpc #174

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.storage.contrib.nio.testing;

import com.google.api.client.util.DateTime;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.ServiceAccount;
import com.google.api.services.storage.model.StorageObject;
Expand All @@ -29,8 +30,10 @@
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.file.FileAlreadyExistsException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -70,6 +73,9 @@
@NotThreadSafe
class FakeStorageRpc extends StorageRpcTestBase {

private static final SimpleDateFormat RFC_3339_FORMATTER =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

// fullname -> metadata
Map<String, StorageObject> metadata = new HashMap<>();
// fullname -> contents
Expand All @@ -95,6 +101,7 @@ public StorageObject create(StorageObject object, InputStream content, Map<Optio
throws StorageException {
potentiallyThrow(options);
String key = fullname(object);
object.setUpdated(now());
metadata.put(key, object);
try {
contents.put(key, com.google.common.io.ByteStreams.toByteArray(content));
Expand Down Expand Up @@ -369,6 +376,7 @@ public StorageObject writeWithResponse(
futureContents.remove(uploadId);
if (metadata.containsKey(uploadId)) {
storageObject = metadata.get(uploadId);
storageObject.setUpdated(now());
Long generation = storageObject.getGeneration();
if (null == generation) {
generation = Long.valueOf(0);
Expand Down Expand Up @@ -414,6 +422,7 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage

rewriteRequest.target.setGeneration(generation);
rewriteRequest.target.setSize(BigInteger.valueOf(data.length));
rewriteRequest.target.setUpdated(metadata.get(sourceKey).getUpdated());

metadata.put(destKey, rewriteRequest.target);

Expand All @@ -427,6 +436,10 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage
data.length);
}

private static DateTime now() {
return DateTime.parseRfc3339(RFC_3339_FORMATTER.format(new Date()));
}

private String fullname(StorageObject so) {
return (so.getBucket() + "/" + so.getName());
}
Expand Down
Expand Up @@ -52,11 +52,8 @@ public void before() throws IOException {
BlobId id = BlobId.of(testBucket, sourceFile);
BlobInfo info = BlobInfo.newBuilder(id).build();

WriteChannel writer = localStorageService.writer(info);
try {
try (WriteChannel writer = localStorageService.writer(info)) {
writer.write(ByteBuffer.wrap(payloadBytes));
} finally {
writer.close();
}
}

Expand Down Expand Up @@ -105,4 +102,19 @@ public void testCopyIncrementsGenerations() {
assertThat(obj.getGeneration()).isEqualTo(2);
assertThat(obj.getSize()).isEqualTo(7);
}

@Test
public void testWriteNewFileSetsUpdateTime() {
Blob obj = localStorageService.get(BlobId.of(testBucket, sourceFile));

assertThat(obj.getUpdateTime()).isNotNull();
}

@Test
public void testCreateNewFileSetsUpdateTime() {
BlobInfo info = BlobInfo.newBuilder(BlobId.of(testBucket, "newFile")).build();
Blob obj = localStorageService.create(info);

assertThat(obj.getUpdateTime()).isNotNull();
}
}