Skip to content

Commit

Permalink
fix: set storage update time in FakeStorageRpc
Browse files Browse the repository at this point in the history
  • Loading branch information
hosainnet committed Jul 19, 2020
1 parent 50e4ba4 commit a9493a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
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,8 @@
@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 +100,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 @@ -357,6 +363,7 @@ public void write(
futureContents.remove(uploadId);
if (metadata.containsKey(uploadId)) {
StorageObject storageObject = metadata.get(uploadId);
storageObject.setUpdated(now());
Long generation = storageObject.getGeneration();
if (null == generation) {
generation = Long.valueOf(0);
Expand Down Expand Up @@ -401,6 +408,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 @@ -414,6 +422,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,12 +52,9 @@ public void before() throws IOException {
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();
}
try (WriteChannel writer = localStorageService.writer(info)) {
writer.write(ByteBuffer.wrap(payloadBytes));
}
}

@After
Expand Down Expand Up @@ -105,4 +102,20 @@ 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();
}

}

0 comments on commit a9493a4

Please sign in to comment.